Skip to content

Commit

Permalink
Merge pull request #6063 from peppy/logger-timestamp-prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
smoogipoo authored Dec 5, 2023
2 parents 146a3e8 + 97d9dad commit 5252b00
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions osu.Framework/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,19 @@ public class Logger
{
private static readonly object static_sync_lock = new object();

// separate locking object for flushing so that we don't lock too long on the staticSyncLock object, since we have to
// hold this lock for the entire duration of the flush (waiting for I/O etc) before we can resume scheduling logs
// but other operations like GetLogger(), ApplyFilters() etc. can still be executed while a flush is happening.
/// <summary>
/// Separate locking object for flushing so that we don't lock too long on the staticSyncLock object, since we have to
/// hold this lock for the entire duration of the flush (waiting for I/O etc) before we can resume scheduling logs
/// but other operations like GetLogger(), ApplyFilters() etc. can still be executed while a flush is happening.
/// </summary>
private static readonly object flush_sync_lock = new object();

/// <summary>
/// Logs are stored with a consistent unix timestamp prefix per session (across all loggers) for logical grouping of
/// log files on disk.
/// </summary>
private static readonly long session_startup_timestamp = DateTimeOffset.UtcNow.ToUnixTimeSeconds();

/// <summary>
/// Whether logging is enabled. Setting this to false will disable all logging.
/// </summary>
Expand Down Expand Up @@ -60,13 +68,12 @@ public class Logger
/// </summary>
public static Storage Storage
{
private get => storage;
get => storage;
set
{
storage = value ?? throw new ArgumentNullException(nameof(value));

// clear static loggers so they are correctly purged at the new storage location.
static_loggers.Clear();
cycleLogs();
}
}

Expand All @@ -83,7 +90,7 @@ public static Storage Storage
/// <summary>
/// Gets the name of the file that this logger is logging to.
/// </summary>
public string Filename => $@"{Name}.log";
public string Filename { get; }

public int TotalLogOperations => logCount.Value;

Expand All @@ -109,6 +116,7 @@ private Logger(string name, bool checkedReserved)

Name = name;
logCount = GlobalStatistics.Get<int>(nameof(Logger), Name);
Filename = $"{session_startup_timestamp}.{Name}.log";
}

/// <summary>
Expand Down Expand Up @@ -257,10 +265,7 @@ public static Logger GetLogger(string name)
string nameLower = name.ToLowerInvariant();

if (!static_loggers.TryGetValue(nameLower, out Logger l))
{
static_loggers[nameLower] = l = Enum.TryParse(name, true, out LoggingTarget target) ? new Logger(target) : new Logger(name, true);
l.clear();
}

return l;
}
Expand Down Expand Up @@ -409,30 +414,27 @@ private void writePendingLines()
/// </summary>
public static event Action<LogEntry> NewEntry;

/// <summary>
/// Deletes log file from disk.
/// </summary>
private void clear()
private bool headerAdded;

private static void cycleLogs()
{
lock (flush_sync_lock)
try
{
scheduler.Add(() =>
DateTime logCycleCutoff = DateTime.UtcNow.AddDays(-7);
var logFiles = new DirectoryInfo(storage.GetFullPath(string.Empty)).GetFiles("*.log");

foreach (var fileInfo in logFiles)
{
try
{
Storage.Delete(Filename);
}
catch
{
// may fail if the file/directory was already cleaned up, ie. during test runs.
}
});
writer_idle.Reset();
if (fileInfo.CreationTimeUtc < logCycleCutoff)
fileInfo.Delete();
}
}
catch (Exception e)
{
Log($"Cycling logs failed ({e})");
}
}

private bool headerAdded;

private void ensureHeader()
{
if (headerAdded) return;
Expand Down

0 comments on commit 5252b00

Please sign in to comment.