Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract filename independently of the OS #2266

Merged
merged 2 commits into from Sep 13, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 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,27 @@ 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("\\");
molnard marked this conversation as resolved.
Show resolved Hide resolved
if (lastSeparatorIndex == -1)
{
lastSeparatorIndex = callerFilePath.LastIndexOf("/");
}

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

#endregion GeneralLoggingMethods

#region ExceptionLoggingMethods
Expand Down