Skip to content

Commit

Permalink
Merge branch 'master' into stop-clock-update
Browse files Browse the repository at this point in the history
  • Loading branch information
peppy authored Nov 13, 2017
2 parents 81a3551 + 3e85d28 commit 44fa87f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
28 changes: 21 additions & 7 deletions osu.Framework/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,27 @@ public class Logger
/// </summary>
public static string VersionIdentifier = @"unknown";

private static Storage storage;

/// <summary>
/// The storage to place logs inside.
/// </summary>
public static Storage Storage;
public static Storage Storage
{
private get
{
return storage;
}

set
{
if (value == null) throw new ArgumentNullException(nameof(value));

storage = value;
lock (flush_sync_lock)
backgroundScheduler.Enabled = true;
}
}

/// <summary>
/// Add a plain-text phrase which should always be filtered from logs. The filtered phrase will be replaced with asterisks (*).
Expand Down Expand Up @@ -318,9 +335,6 @@ public void Add(string message = @"", LogLevel level = LogLevel.Verbose)

backgroundScheduler.Add(delegate
{
if (Storage == null)
return;

try
{
using (var stream = Storage.GetStream(Filename, FileAccess.Write, FileMode.Append))
Expand All @@ -346,7 +360,7 @@ public void Add(string message = @"", LogLevel level = LogLevel.Verbose)
private void clear()
{
lock (flush_sync_lock)
backgroundScheduler.Add(() => Storage?.Delete(Filename));
backgroundScheduler.Add(() => Storage.Delete(Filename));
addHeader();
}

Expand All @@ -361,7 +375,7 @@ private void addHeader()

private static readonly List<string> filters = new List<string>();
private static readonly Dictionary<string, Logger> static_loggers = new Dictionary<string, Logger>();
private static ThreadedScheduler backgroundScheduler = new ThreadedScheduler(@"Logger");
private static ThreadedScheduler backgroundScheduler = new ThreadedScheduler(@"Logger") { Enabled = false };

/// <summary>
/// Pause execution until all logger writes have completed and file handles have been closed.
Expand All @@ -371,7 +385,7 @@ public static void Flush()
lock (flush_sync_lock)
{
backgroundScheduler.Dispose();
backgroundScheduler = new ThreadedScheduler(@"Logger");
backgroundScheduler = new ThreadedScheduler(@"Logger") { Enabled = storage != null };
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion osu.Framework/Threading/Scheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -327,13 +327,18 @@ public class ThreadedScheduler : Scheduler
private bool isDisposed;
private readonly Thread workerThread;

/// <summary>
/// Whether scheduled tasks should be run. Disabling temporarily pauses all execution.
/// </summary>
public bool Enabled = true;

public ThreadedScheduler(string threadName = null, int runInterval = 50)
{
workerThread = new Thread(() =>
{
while (!isDisposed)
{
Update();
if (Enabled) Update();
Thread.Sleep(runInterval);
}
})
Expand Down

0 comments on commit 44fa87f

Please sign in to comment.