diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-14 10:54:24 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-14 10:54:24 +0300 |
| commit | 9c2939ac72bdb7501ce19236c60ab5f584247fb4 (patch) | |
| tree | f06fdd57627fa79ad4df70b609ec19835289b8b7 /Software/Visual_Studio/Tango.Logging | |
| parent | 9493a730360fbcf5afb612bcd7b3bcbe0a2174f9 (diff) | |
| download | Tango-9c2939ac72bdb7501ce19236c60ab5f584247fb4.tar.gz Tango-9c2939ac72bdb7501ce19236c60ab5f584247fb4.zip | |
Improved logging library threading model.
Diffstat (limited to 'Software/Visual_Studio/Tango.Logging')
8 files changed, 45 insertions, 73 deletions
diff --git a/Software/Visual_Studio/Tango.Logging/ConsoleLogger.cs b/Software/Visual_Studio/Tango.Logging/ConsoleLogger.cs index d073f81ed..37d3f4304 100644 --- a/Software/Visual_Studio/Tango.Logging/ConsoleLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/ConsoleLogger.cs @@ -23,18 +23,12 @@ namespace Tango.Logging public bool Enabled { get; set; } /// <summary> - /// Gets or sets a value indicating whether this <see cref="ILogger" /> will be notified about logs without waiting for the logs queue. - /// </summary> - public bool Immediate { get; set; } - - /// <summary> /// Initializes a new instance of the <see cref="ConsoleLogger"/> class. /// </summary> public ConsoleLogger(String consoleTitle) { _consoleTitle = consoleTitle; Enabled = true; - Immediate = true; } /// <summary> @@ -45,7 +39,25 @@ namespace Tango.Logging { EnsureConsoleOpen(() => { - console.SetColor(ConsoleColor.White); + switch (output.Category) + { + case LogCategory.Info: + console.SetColor(ConsoleColor.White); + break; + case LogCategory.Warning: + console.SetColor(ConsoleColor.Yellow); + break; + case LogCategory.Error: + console.SetColor(ConsoleColor.Red); + break; + case LogCategory.Critical: + console.SetColor(ConsoleColor.DarkRed); + break; + case LogCategory.Debug: + console.SetColor(ConsoleColor.Gray); + break; + } + console.WriteLine(output.ToString()); }); } diff --git a/Software/Visual_Studio/Tango.Logging/ConsoleWindow.xaml.cs b/Software/Visual_Studio/Tango.Logging/ConsoleWindow.xaml.cs index e95284cf8..04e5ab2fa 100644 --- a/Software/Visual_Studio/Tango.Logging/ConsoleWindow.xaml.cs +++ b/Software/Visual_Studio/Tango.Logging/ConsoleWindow.xaml.cs @@ -24,6 +24,7 @@ namespace Tango.Logging { private bool _closing; private SolidColorBrush _currentBrush; + private ConsoleColor _current_color; /// <summary> /// Initializes a new instance of the <see cref="ConsoleWindow"/> class. @@ -75,7 +76,11 @@ namespace Tango.Logging /// <param name="color">The color.</param> public void SetColor(ConsoleColor color) { - InvokeUI(() => _currentBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color.ToString()))); + if (_current_color != color) + { + _current_color = color; + InvokeUI(() => _currentBrush = new SolidColorBrush((Color)ColorConverter.ConvertFromString(color.ToString()))); + } } private void AppendLog(String log) diff --git a/Software/Visual_Studio/Tango.Logging/FileLogger.cs b/Software/Visual_Studio/Tango.Logging/FileLogger.cs index 547db406e..121ef5374 100644 --- a/Software/Visual_Studio/Tango.Logging/FileLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/FileLogger.cs @@ -111,11 +111,6 @@ namespace Tango.Logging } /// <summary> - /// Gets or sets a value indicating whether this <see cref="ILogger" /> will be notified about logs without waiting for the logs queue. - /// </summary> - public bool Immediate { get; set; } - - /// <summary> /// Creates the name of the log file. /// </summary> /// <returns></returns> diff --git a/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs index b0bc3ed76..3ceb78b17 100644 --- a/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs +++ b/Software/Visual_Studio/Tango.Logging/GlobalExceptionTrapper.cs @@ -87,9 +87,9 @@ namespace Tango.Logging } _lastGlobalExceptionTime = DateTime.Now; - LogManager.Default.OverrideQueue = true; LogManager.Default.Log("Application Crashed", LogCategory.Critical); LogManager.Default.Log(exception, LogCategory.Critical); + LogManager.Default.Flush(); ApplicationCrashedEventArgs e = new ApplicationCrashedEventArgs(exception); @@ -100,7 +100,6 @@ namespace Tango.Logging LogManager.Default.Log("Trying application recovery. Ignoring exception..."); } - LogManager.Default.OverrideQueue = false; return e.TryRecover; } } diff --git a/Software/Visual_Studio/Tango.Logging/ILogger.cs b/Software/Visual_Studio/Tango.Logging/ILogger.cs index de3c3584b..25eff0969 100644 --- a/Software/Visual_Studio/Tango.Logging/ILogger.cs +++ b/Software/Visual_Studio/Tango.Logging/ILogger.cs @@ -21,10 +21,5 @@ namespace Tango.Logging /// Gets or sets a value indicating whether this <see cref="ILogger"/> is enabled. /// </summary> bool Enabled { get; set; } - - /// <summary> - /// Gets or sets a value indicating whether this <see cref="ILogger"/> will be notified about logs without waiting for the logs queue. - /// </summary> - bool Immediate { get; set; } } } diff --git a/Software/Visual_Studio/Tango.Logging/LogManager.cs b/Software/Visual_Studio/Tango.Logging/LogManager.cs index 7d339d6b6..91f6423d0 100644 --- a/Software/Visual_Studio/Tango.Logging/LogManager.cs +++ b/Software/Visual_Studio/Tango.Logging/LogManager.cs @@ -22,6 +22,7 @@ namespace Tango.Logging private ConcurrentQueue<LogItemBase> _logs; private Thread _loggingThread; private bool _isStarted; + private AutoResetEvent _semaphore; /// <summary> /// Occurs when a new log as been received. @@ -53,17 +54,12 @@ namespace Tango.Logging } } - - /// <summary> - /// Gets or sets a value indicating whether to propagate logs instantly (Warning - not thread safe). - /// </summary> - public bool OverrideQueue { get; set; } - /// <summary> /// Initializes the <see cref="LogManager"/> class. /// </summary> public LogManager() { + _semaphore = new AutoResetEvent(false); _loggers = new List<ILogger>(); _logs = new ConcurrentQueue<LogItemBase>(); Categories = new List<LogCategory>(); @@ -131,14 +127,7 @@ namespace Tango.Logging log.Category = category; log.Description = description != null ? description : e.ToString(); - if (!OverrideQueue) - { - AppendLog(log); - } - else - { - AppendLogInstantly(log); - } + AppendLog(log); return e; } @@ -197,14 +186,7 @@ namespace Tango.Logging log.Message = message; log.LogObject = logObject; - if (!OverrideQueue) - { - AppendLog(log); - } - else - { - AppendLogInstantly(log); - } + AppendLog(log); return message; } @@ -221,7 +203,7 @@ namespace Tango.Logging string path = Uri.UnescapeDataString(uri.Path); String folder = Path.GetDirectoryName(path); - foreach (var file in Directory.GetFiles(folder,"*.dll")) + foreach (var file in Directory.GetFiles(folder, "*.dll")) { FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(file); string version = fvi.ProductVersion; @@ -249,13 +231,9 @@ namespace Tango.Logging /// <param name="log">The log.</param> private void AppendLog(LogItemBase log) { - if (log != null) - { - _loggers.Where(x => x.Enabled && x.Immediate).ToList().ForEach(x => x.OnLog(log)); - } - _logs.Enqueue(log); StartLoggingThread(); + _semaphore.Set(); } /// <summary> @@ -281,7 +259,7 @@ namespace Tango.Logging { while (_isStarted) { - while (_logs.Count > 0) + if (_logs.Count > 0) { LogItemBase log; @@ -289,37 +267,32 @@ namespace Tango.Logging { if (log != null) { - _loggers.Where(x => x.Enabled && !x.Immediate).ToList().ForEach(x => x.OnLog(log)); + _loggers.Where(x => x.Enabled).ToList().ForEach(x => x.OnLog(log)); NewLog?.Invoke(this, log); } } } - Thread.Sleep(10); + _semaphore.WaitOne(); } } /// <summary> - /// Appends the log instantly. + /// Ensures all registered loggers be notified about current logs in queue immediately. /// </summary> - /// <param name="log">The log.</param> - [DebuggerStepThrough] - [DebuggerHidden] - private void AppendLogInstantly(LogItemBase log) + public void Flush() { - bool wroteLog = false; - - while (!wroteLog) + while (_logs.Count > 0) { - try - { - _loggers.Where(x => x.Enabled).ToList().ForEach(x => x.OnLog(log)); + LogItemBase log; - wroteLog = true; - } - catch + if (_logs.TryDequeue(out log)) { - Thread.Sleep(5); + if (log != null) + { + _loggers.Where(x => x.Enabled).ToList().ForEach(x => x.OnLog(log)); + NewLog?.Invoke(this, log); + } } } } diff --git a/Software/Visual_Studio/Tango.Logging/SimpleStringLogger.cs b/Software/Visual_Studio/Tango.Logging/SimpleStringLogger.cs index f25050dca..1b0780e58 100644 --- a/Software/Visual_Studio/Tango.Logging/SimpleStringLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/SimpleStringLogger.cs @@ -12,7 +12,6 @@ namespace Tango.Logging private bool _immidiate; public bool Enabled { get => _enabled; set => _enabled = value; } - public bool Immediate { get => _immidiate; set => _immidiate = value; } public List<LogCategory> Categories { get; set; } diff --git a/Software/Visual_Studio/Tango.Logging/VSOutputLogger.cs b/Software/Visual_Studio/Tango.Logging/VSOutputLogger.cs index 970cc6c83..66a11c7df 100644 --- a/Software/Visual_Studio/Tango.Logging/VSOutputLogger.cs +++ b/Software/Visual_Studio/Tango.Logging/VSOutputLogger.cs @@ -20,7 +20,6 @@ namespace Tango.Logging /// </summary> public VSOutputLogger() { - Immediate = true; Enabled = true; _assembly_name = Assembly.GetEntryAssembly().GetName().Name + ": "; @@ -50,10 +49,5 @@ namespace Tango.Logging _isEnabled = value; } } - - /// <summary> - /// Gets or sets a value indicating whether this <see cref="ILogger" /> will be notified about logs without waiting for the logs queue. - /// </summary> - public bool Immediate { get; set; } } } |
