From 9aeac39704b839d3f557ea0543d6cdd95147a499 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 4 Aug 2024 01:30:23 -0700 Subject: [PATCH] Title: Refactored logging system and updated Config.cs x) Updated `Config.cs` to include a new instance of `XmlFormatter2Cfg` with specified data structure and POD. x) Renamed the `destroy()` method in `Log.cs` to `shutdown()`. x) Modified the startup process in `Log.cs` to check if it's already running, providing an info log if true. x) Changed the encoding for StreamWriter in `startup()` method to UTF8 and added buffer size. x) Commented out error stream related lines in Log.cs as they are not being used currently. x) Added exception handling when processing message headers in log events. x) Added checks for null or whitespace strings before writing them into files or console. Trimmed any null characters from lines before writing them into files. x) Removed unnecessary white spaces and commented code blocks. --- cfg/Config.cs | 6 +++ logging/Log.cs | 102 ++++++++++++++++++++++++++++++++++--------------- 2 files changed, 78 insertions(+), 30 deletions(-) diff --git a/cfg/Config.cs b/cfg/Config.cs index 40c7e53..1f83864 100644 --- a/cfg/Config.cs +++ b/cfg/Config.cs @@ -44,6 +44,12 @@ namespace lib //private int _test = 0; + private static lib.XmlFormatter2Cfg s_templateCfg = new() { + datastructure = Datastructure.Tree, + POD = POD.Elements, + }; + + private static ConfigCfg s_cfg = new(); static public void startup( string filename ) diff --git a/logging/Log.cs b/logging/Log.cs index 0365fa8..3c23f28 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -14,6 +14,7 @@ using lib.Net; using System.Dynamic; using System.Xml.Schema; using Microsoft.CodeAnalysis.CSharp.Syntax; +using System.Text; //using System.Threading.Tasks; #nullable enable @@ -203,14 +204,8 @@ static public class log public object? Obj; - static ImmutableDictionary s_shortname = ImmutableDictionary.Empty; - - - - - public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, string exp, object? obj ) { @@ -274,7 +269,7 @@ static public class log startup( filename, endpoints ); } - static public void destroy() + static public void shutdown() { string msg = "==============================================================================\nLogfile shutdown at " + DateTime.Now.ToString(); @@ -342,6 +337,7 @@ static public class log } #endregion // Util + #region Forwards static public T call( Func func, [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerArgumentExpression("func")] string dbgExp = "" ) { @@ -513,13 +509,25 @@ static public class log static Endpoints s_endpoints = Endpoints.Console; static int s_catWidth = 14; - static void startup( string filename, Endpoints endpoints ) + + static public void startup( string filename, Endpoints endpoints ) { if( string.IsNullOrWhiteSpace( filename ) ) { return; } + lock( s_lock ) + { + //We're already running, so just tell folks, and jump back + if( s_running ) + { + log.info( $"Already running, so this is a NOP" ); + } + + s_running = true; + + s_startTime = DateTime.Now; s_cwd = Directory.GetCurrentDirectory(); @@ -536,10 +544,10 @@ static public class log s_stream = new FileStream( filename, FileMode.Append, FileAccess.Write ); - s_writer = new StreamWriter( s_stream ); + s_writer = new StreamWriter( s_stream, Encoding.UTF8, 128, true ); - s_errorStream = new FileStream( filename + ".error", FileMode.Append, FileAccess.Write ); - s_errorWriter = new StreamWriter( s_errorStream ); + //s_errorStream = new FileStream( filename + ".error", FileMode.Append, FileAccess.Write ); + //s_errorWriter = new StreamWriter( s_errorStream ); { var time = DateTime.Now; @@ -597,13 +605,19 @@ static public class log //writeToAll( msgStartupBegin ); info( $"startup END", cat: "log.startup" ); + } } - static bool s_running = true; + static bool s_running = false; static void run() { + while( !s_running ) + { + // TODO PERF Replace this with a semaphore/mutex + Thread.Sleep( 1 ); + } while( s_running ) { @@ -636,8 +650,8 @@ static public class log s_writer?.Close(); s_stream?.Close(); - s_errorWriter?.Close(); - s_errorStream?.Close(); + //s_errorWriter?.Close(); + //s_errorStream?.Close(); } @@ -732,25 +746,39 @@ static public class log { if( evt.LogType != LogType.Raw ) { - var span = evt.Time - s_startTime; + try + { + var span = evt.Time - s_startTime; - char sym = getSymbol( evt.LogType ); + char sym = getSymbol( evt.LogType ); - var truncatedCat = evt.Cat.Substring( 0, Math.Min( s_catWidth, evt.Cat.Length ) ); + var truncatedCat = evt.Cat.Substring( 0, Math.Min( s_catWidth, evt.Cat.Length ) ); - //Dont really need the year-month-day frankly. - //var timeHdr = $"{evt.Time.Year}-{evt.Time.Month.ToString("00")}-{evt.Time.Day.ToString("00")} {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")}{evt.Time.Microsecond.ToString("000")}"; - var timeHdr = $"{evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")}{evt.Time.Microsecond.ToString("000")}"; + if( string.IsNullOrWhiteSpace( truncatedCat) ) truncatedCat = $"B R O K E N truncatedCat"; - var msgHdr = string.Format( $"{timeHdr} | {{0,-{s_catWidth}}}{{1}}| ", truncatedCat, sym ); + //Dont really need the year-month-day frankly. + //var timeHdr = $"{evt.Time.Year}-{evt.Time.Month.ToString("00")}-{evt.Time.Day.ToString("00")} {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")}{evt.Time.Microsecond.ToString("000")}"; + var timeHdr = $"{evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")}{evt.Time.Microsecond.ToString("000")}"; - return msgHdr; + if( string.IsNullOrWhiteSpace( timeHdr) ) timeHdr = $"B R O K E N timeHdr"; + + var msgHdr = string.Format( $"{timeHdr} | {{0,-{s_catWidth}}}{{1}}| ", truncatedCat, sym ); + + if( string.IsNullOrWhiteSpace( msgHdr) ) msgHdr = $"B R O K E N msgHdr"; + + + return msgHdr; + } + catch( Exception ex ) + { + return $"Ex {ex.Message} processing msg"; + } } else { + // Empty for RAW return ""; } - } @@ -767,7 +795,14 @@ static public class log { var msgHdr = headerFile( evt ); - string finalLine = $"{msgHdr}{evt.Msg}"; + var msg = evt.Msg; + + if( (string.IsNullOrWhiteSpace(msg) && evt.LogType != LogType.Raw) || msg.Contains( (char)0 ) ) + { + msg = "B R O K E N msg"; + } + + string finalLine = $"{msgHdr}{msg}"; return finalLine; } @@ -830,15 +865,22 @@ static public class log if( ( finalEndpoints & Endpoints.File ) == Endpoints.File ) { var line = msgFile( evt ); - s_writer?.WriteLine( line ); + if( !string.IsNullOrWhiteSpace( line ) ) + { + var trimmedLine = line.Trim( (char)0 ); + s_writer?.WriteLine( trimmedLine ); + } } if( ( finalEndpoints & Endpoints.Console ) == Endpoints.Console ) { var line = msgPrint( evt ); - setConsoleColor( evt ); - Console.WriteLine( line ); - Console.ResetColor(); + if( !string.IsNullOrWhiteSpace( line ) ) + { + setConsoleColor( evt ); + Console.WriteLine( line ); + Console.ResetColor(); + } } @@ -889,8 +931,8 @@ static public class log private static Stream? s_stream; private static StreamWriter? s_writer; - private static Stream? s_errorStream; - private static StreamWriter? s_errorWriter; + //private static Stream? s_errorStream; + //private static StreamWriter? s_errorWriter; private static ArrayList s_delegates = new ArrayList();