From 0fce863a81f2e594171b4a787bdf69ce04acf907 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 26 May 2024 22:27:45 -0700 Subject: [PATCH] Refactor logs for structured logging and add new Value struct: - Refactor various logs to enable automagic structured logging - Add `Value` struct for creating values with debug expressions - Update `LogEvent` constructor to include an expression field - Modify log methods to pass debug expressions for messages - Implement new methods for logging values and objects with debug expressions --- logging/Log.cs | 143 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 113 insertions(+), 30 deletions(-) diff --git a/logging/Log.cs b/logging/Log.cs index 682b1ef..ed1eee4 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -11,13 +11,77 @@ using System.Linq; using System.Threading; using System.Security.Cryptography.X509Certificates; using lib.Net; +using System.Dynamic; //using System.Threading.Tasks; #nullable enable +/* + +T O D O : +x) Refactor various logs in order to do automagic structured logging +x) + + +D O N E: + + +N O T D O I N G : + + +*/ + +public record struct Value( T _val = default, string _exp = "" ) +{ + + static public Value Get( T v, + [CallerArgumentExpression("fn")] + string dbgExp = "" + ) + { + return new( v, dbgExp ); + } +} + +public struct SourceLoc +{ + readonly string _reason = ""; + readonly string _dbgName = ""; + readonly string _dbgPath = ""; + readonly int _dbgLine = -1; + + public SourceLoc( string reason, string dbgName, string dbgPath, int dbgLine ) + { + _reason = reason; + _dbgName = dbgName; + _dbgPath = dbgPath; + _dbgLine = dbgLine; + } + + static public SourceLoc Record( + string reason = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", + [CallerLineNumber] int dbgLine = 0 + ) + { + return new SourceLoc( reason, dbgName, dbgPath, dbgLine ); + } + + +} + + static public class log { + static public Value Value( T val, + [CallerArgumentExpression("val")] + string dbgExp = "" + ) + { + return new( val, dbgExp ); + } [Flags] public enum LogType @@ -55,6 +119,7 @@ static public class log public string Path; public int Line; public string Member; + public string Exp; public string Cat; public object? Obj; @@ -64,7 +129,7 @@ static public class log static ImmutableDictionary s_shortname = ImmutableDictionary.Empty; - public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, object? obj ) + public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, string exp, object? obj ) { //Cache the automatic category names @@ -100,12 +165,13 @@ static public class log Line = line; Member = member; Cat = cat; + Exp = exp; Obj = obj; } } - static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", string exp = "" ) { - var logEvent = new LogEvent( logType, msg, path, line, member, cat, obj ); + var logEvent = new LogEvent( logType, msg, path, line, member, cat, exp, obj ); return logEvent; } @@ -199,52 +265,69 @@ static public class log #endregion // Util #region Forwards - static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Fatal, path, line, member, cat, obj ); + logBase( msg, LogType.Fatal, path, line, member, cat, dbgExp, obj ); } - static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Error, path, line, member, cat, obj ); + logBase( msg, LogType.Error, path, line, member, cat, dbgExp, obj ); } - static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Warn, path, line, member, cat, obj ); + logBase( msg, LogType.Warn, path, line, member, cat, dbgExp, obj ); } - static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.High, path, line, member, cat, obj ); + logBase( msg, LogType.High, path, line, member, cat, dbgExp, obj ); } static public void info( string msg, string cat = "", object? obj = null, - [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Info, path, line, member, cat, obj ); + logBase( msg, LogType.Info, path, line, member, cat, dbgExp, obj ); } - static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Debug, path, line, member, cat, obj ); + logBase( msg, LogType.Debug, path, line, member, cat, dbgExp, obj ); } - static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("msg")] string dbgExp = "" ) { - logBase( msg, LogType.Trace, path, line, member, cat, obj ); + logBase( msg, LogType.Trace, path, line, member, cat, dbgExp, obj ); } + + + + + static public void inf_valueo( string msg, Value v1, string cat = "" ) + { + } + + static public void info( string msg, A a, string cat = "", [CallerArgumentExpression("a")] string dbgExpA = "" ) + { + } + + static public void info( string msg, object a, object b, string cat = "", [CallerArgumentExpression("a")] string dbgExpA = "", [CallerArgumentExpression("a")] string dbgExpB = "" ) + { + } + + #endregion #region Helpers - static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "", [CallerArgumentExpression("obj")] string dbgExpObj = "" ) { var list = refl.GetAllProperties( obj.GetType() ); lock( s_lock ) { - var evt = new LogEvent( type, header, path, line, member, cat, obj ); + var evt = new LogEvent( type, header, path, line, member, cat, dbgExpObj, obj ); { s_events.Enqueue( evt ); @@ -255,11 +338,11 @@ static public class log { var v = pi.GetValue( obj ); - logBase( $"{prefix}{pi.Name} = {v}", type, path, line, member, cat ); + logBase( $"{prefix}{pi.Name} = {v}", type, path, line, member, dbgExpObj, cat ); } catch( Exception ex ) { - logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, "log" ); + logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, path, line, member, cat, dbgExpObj, obj); } } @@ -284,16 +367,16 @@ static public class log static object s_lock = new object(); - static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null ) + static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", string exp = "", object? obj = null ) { - LogEvent evt = new LogEvent( type, msg, path, line, member, cat, obj ); + LogEvent evt = new LogEvent( type, msg, path, line, member, cat, exp, obj ); return evt; } - static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null ) + static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", string exp = "", object? obj = null ) { - var evt = logCreateEvent( msg, type, path, line, member, cat ); + var evt = logCreateEvent( msg, type, path, line, member, cat, exp ); s_events.Enqueue( evt ); } @@ -365,10 +448,10 @@ static public class log { var time = DateTime.Now; // Header for this run - var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", null ); - var beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", null ); - var endLine = new LogEvent( LogType.Raw, $"End E N D ******************************************************************************************************************", "", 0, "", "lib.time", null ); - var timeLine = new LogEvent( LogType.Raw, $"D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", null ); + var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", "", null ); + var beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", "", null ); + var endLine = new LogEvent( LogType.Raw, $"End E N D ******************************************************************************************************************", "", 0, "", "lib.time", "", null ); + var timeLine = new LogEvent( LogType.Raw, $"D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", "", null ); //writeToAll( endLine ); @@ -572,7 +655,7 @@ static public class log { s_lastDisplaySeconds = curSeconds; - var minuteEvt = new LogEvent( LogType.Raw, $"T I M E ==> {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")} : {evt.Time.ToShortDateString()}", "", 0, "", "lib.time", null ); + var minuteEvt = new LogEvent( LogType.Raw, $"T I M E ==> {evt.Time.Hour.ToString("00")}:{evt.Time.Minute.ToString("00")}:{evt.Time.Second.ToString("00")}.{evt.Time.Millisecond.ToString("000")} : {evt.Time.ToShortDateString()}", "", 0, "", "lib.time", "", null ); minuteEvt.Time = evt.Time; writeSpecialEvent( minuteEvt ); }