Skip to content

Commit

Permalink
Extract filename independentlu of the OS
Browse files Browse the repository at this point in the history
  • Loading branch information
lontivero committed Sep 13, 2019
1 parent 4791c11 commit 071f000
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion WalletWasabi/Logging/Logger.cs
Expand Up @@ -123,7 +123,7 @@ private static void Log(LogLevel level, string message, int additionalEntrySepar
}

message = string.IsNullOrWhiteSpace(message) ? "" : message;
var category = string.IsNullOrWhiteSpace(callerFilePath) ? "" : $"{Path.GetFileNameWithoutExtension(callerFilePath)} ({callerLineNumber})";
var category = string.IsNullOrWhiteSpace(callerFilePath) ? "" : $"{ExtractFileName(callerFilePath)} ({callerLineNumber})";

var messageBuilder = new StringBuilder();
messageBuilder.Append($"{DateTime.UtcNow.ToLocalTime():yyyy-MM-dd HH:mm:ss} {level.ToString().ToUpperInvariant()}\t");
Expand Down Expand Up @@ -227,6 +227,32 @@ private static void Log(LogLevel level, string message, int additionalEntrySepar
}
}

// This method removes the path and file extension.
//
// Given Wasabi releases are currently built using Windows, the generated assymblies contain
// the hardcoded "C:\Users\User\Desktop\WalletWasabi\.......\FileName.cs" string because that
// is the real path of the file, it doesn't matter what OS was targeted.
// In Windows and Linux that string is a valid path and that means Path.GetFileNameWithoutExtension
// can extract the file name but in the case of OSX the same string is not a valid path so, it assumes
// the whole string is the file name.
private static string ExtractFileName(string callerFilePath)
{
var lastSeparatorIndex = callerFilePath.LastIndexOf("\\");
if (lastSeparatorIndex == -1)
{
lastSeparatorIndex = callerFilePath.LastIndexOf("/");
}

if (lastSeparatorIndex == -1)
{
return "Unknown"; // this should never happen but just in case.
}

lastSeparatorIndex++;
var fileNameWithoutExtension = callerFilePath.Substring(lastSeparatorIndex, callerFilePath.Length - lastSeparatorIndex - ".cs".Length);
return fileNameWithoutExtension;
}

#endregion GeneralLoggingMethods

#region ExceptionLoggingMethods
Expand Down

0 comments on commit 071f000

Please sign in to comment.