diff --git a/SharpLib.csproj b/SharpLib.csproj index 9ff0706..50e9c31 100644 --- a/SharpLib.csproj +++ b/SharpLib.csproj @@ -11,15 +11,14 @@ true true 14.0 + Copyright 2003..2025 Marc Hernandez + A base set of - - - $(NoWarn);SYSLIB0050;CS8981; CS8632 - + @@ -30,16 +29,13 @@ - - - - - + + diff --git a/logging/Log.cs b/logging/Log.cs index 00929b4..3ecf422 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -72,6 +72,9 @@ public struct SourceLoc public string Log => ToLogString(); + public string File => _dbgFile; + public string Method => _dbgName; + static public SourceLoc Record( string reason = "", [CallerMemberName] string dbgName = "", @@ -199,12 +202,12 @@ static public class log public string Exp; public string Cat; - public object? Obj; + public SourceLoc? Loc; static ImmutableDictionary s_shortname = ImmutableDictionary.Empty; - public LogEvent( LogType logType, string msg, string dbgPath, int dbgLine, string dbgMethod, string cat, string exp, object? obj ) + public LogEvent( LogType logType, string msg, string dbgPath, int dbgLine, string dbgMethod, string cat, string exp, SourceLoc? loc ) { //Cache the automatic category names @@ -241,13 +244,13 @@ static public class log Member = dbgMethod; Cat = cat; Exp = exp; - Obj = obj; + Loc = loc; } } - static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", string exp = "" ) + static LogEvent CreateLogEvent( LogType logType, string msg, string cat, SourceLoc? loc, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", string exp = "" ) { - var logEvent = new LogEvent( logType, msg, dbgPath, dbgLine, dbgMethod, cat, exp, obj ); + var logEvent = new LogEvent( logType, msg, dbgPath, dbgLine, dbgMethod, cat, exp, loc ); return logEvent; } @@ -286,7 +289,7 @@ static public class log => new SourceLoc( "", dbgName, dbgPath, dbgLine ); - static public string whatFile( string path ) + static public string whatFile( string path, bool incExtension = false ) { var file = ""; @@ -304,11 +307,20 @@ static public class log pathPieces = path.Split( '/' ); } - var lastDir = pathPieces[pathPieces.Length - 1]; + var lastPathPiece = pathPieces[pathPieces.Length - 1]; - ImmutableInterlocked.AddOrUpdate( ref s_files, pathHash, lastDir, ( key, value ) => { return lastDir; } ); + ImmutableInterlocked.AddOrUpdate( ref s_files, pathHash, lastPathPiece, ( key, value ) => { return lastPathPiece; } ); - file = lastDir; + if( !incExtension ) + { + var periodIndex = lastPathPiece.IndexOf( '.' ); + if( periodIndex > 0 ) + { + lastPathPiece = lastPathPiece.Substring( 0, periodIndex ); + } + } + + file = lastPathPiece; } return file; @@ -341,7 +353,7 @@ static public class log } /* - static public void info( string msg, string cat = "", object? obj = null, + static public void info( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) */ @@ -383,6 +395,7 @@ static public class log return v; } + [StackTraceHidden] static public void call( Action func, [CallerMemberName] string dbgName = "", [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerArgumentExpression( "func" )] string dbgExp = "" ) { log.info( $"{dbgExp}", "", null, dbgPath, dbgLine, dbgName, dbgExp ); @@ -390,57 +403,84 @@ static public class log log.info( $"| Done", "", null, dbgPath, dbgLine, dbgName, dbgExp ); } - static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) - { - logBase( msg, LogType.Fatal, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); - } - [StackTraceHidden] - static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) - { - logBase( $"{dbgMethod}: {msg}", LogType.Error, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); - } - - [StackTraceHidden] - static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) - { - logBase( msg, LogType.Warn, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); - } - - static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) - { - logBase( msg, LogType.High, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); - } - - static public void info( string msg, string cat = "", object? obj = null, + static public void info( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) { - logBase( msg, LogType.Info, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); + logBase( msg, LogType.Info, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); } - static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + [StackTraceHidden] + static public void debug( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) { - logBase( msg, LogType.Debug, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); + logBase( msg, LogType.Debug, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); } - static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + [StackTraceHidden] + static public void trace( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) { - logBase( msg, LogType.Trace, dbgPath, dbgLine, dbgMethod, cat, dbgExp, obj ); + logBase( msg, LogType.Trace, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + } + + [StackTraceHidden] + static public void warn( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + logBase( msg, LogType.Warn, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + } + + [StackTraceHidden] + static public void high( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + logBase( msg, LogType.High, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + } + + [StackTraceHidden] + static public void error( string msg, string cat = "", SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + logBase( $"{dbgMethod}: {msg}", LogType.Error, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); } + [StackTraceHidden] + // VERY SPECIALIZED ONLY USE IF YOU KNOW WHAT YOU ARE DOING + static public void row_internal( string msg, string cat = "", SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + logBase( msg, LogType.Raw, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + } + [StackTraceHidden] + static public void exception( Exception ex, string msg, string cat = "", SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + + logBase( $"*******************************************************************************", + LogType.Raw, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + logBase( $"{dbgMethod}: {msg} | Caught At:", LogType.Error, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + logBase( $"{dbgPath}({dbgLine}): {dbgMethod}: ---------------", + LogType.Raw, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + LogStackTrace( ex ); + } + + + + [StackTraceHidden] + static public void fatal( string msg, string cat = "",SourceLoc? loc = null, [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "msg" )] string dbgExp = "" ) + { + logBase( msg, LogType.Fatal, dbgPath, dbgLine, dbgMethod, cat, dbgExp, loc ); + } + +//new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", "", null ) + #endregion #region Helpers - static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "obj" )] string dbgExpObj = "" ) + static public void logProps( object obj, string header, LogType type = LogType.Debug, string cat = "", string prefix = "", [CallerFilePath] string dbgPath = "", [CallerLineNumber] int dbgLine = -1, [CallerMemberName] string dbgMethod = "", [CallerArgumentExpression( "obj" )] string dbgExpObj = "", SourceLoc? loc = null ) { var list = refl.GetAllProperties( obj.GetType() ); lock( s_lock ) { - var evt = new LogEvent( type, header, dbgPath, dbgLine, dbgMethod, cat, dbgExpObj, obj ); + var evt = new LogEvent( type, header, dbgPath, dbgLine, dbgMethod, cat, dbgExpObj, loc ); { // Use Add instead of Enqueue s_events.Add( evt ); @@ -455,7 +495,7 @@ static public class log } catch( Exception ex ) { - logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, dbgPath, dbgLine, dbgMethod, cat, dbgExpObj, obj ); + logBase( $"Exception processing {pi.Name} {ex.Message}", LogType.Error, dbgPath, dbgLine, dbgMethod, cat, dbgExpObj, loc ); } } @@ -475,6 +515,26 @@ static public class log log.warn( $"Got {notExpectedValue} instead of {value}{falseString}" ); } } + + static void LogStackTrace( Exception ex, string prefixMsg = "" ) + { + var stackTrace = new StackTrace( ex, true ); + var formattedTrace = "Stack Trace:\n"; + + if( ex.InnerException != null ) + { + var prefix = $"{prefixMsg}Inner->"; + LogStackTrace( ex.InnerException, prefix ); + } + + log.error( $"{prefixMsg} Exception: {ex.Message}" ); + + foreach( var fr in stackTrace.GetFrames() ) + { + logBase( $"{fr.Log}", LogType.Raw ); + } + } + #endregion @@ -494,14 +554,14 @@ static public class log } - static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string dbgPath = "", int dbgLine = -1, string dbgMethod = "", string cat = "unk", string exp = "", object? obj = null ) + static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string dbgPath = "", int dbgLine = -1, string dbgMethod = "", string cat = "unk", string exp = "",SourceLoc? loc = null ) { - LogEvent evt = new LogEvent( type, msg, dbgPath, dbgLine, dbgMethod, cat, exp, obj ); + LogEvent evt = new LogEvent( type, msg, dbgPath, dbgLine, dbgMethod, cat, exp, loc ); return evt; } [StackTraceHidden] - static public void logBase( string msg, LogType type = LogType.Debug, string dbgPath = "", int dbgLine = -1, string dbgMethod = "", string cat = "unk", string exp = "", object? obj = null ) + static public void logBase( string msg, LogType type = LogType.Debug, string dbgPath = "", int dbgLine = -1, string dbgMethod = "", string cat = "unk", string exp = "",SourceLoc? loc = null ) { var evt = logCreateEvent( msg, type, dbgPath, dbgLine, dbgMethod, cat, exp ); diff --git a/task/Task.cs b/task/Task.cs new file mode 100644 index 0000000..57c936f --- /dev/null +++ b/task/Task.cs @@ -0,0 +1,16 @@ + +using System.Threading.Tasks; +using System.Threading; +using System.Diagnostics; + + +namespace lib; + + +static public class Task +{ + + + + +} \ No newline at end of file diff --git a/util/Debug.cs b/util/Debug.cs new file mode 100644 index 0000000..416fa7b --- /dev/null +++ b/util/Debug.cs @@ -0,0 +1,28 @@ + + + + + +using System.Diagnostics; + + + + +static public class DebugEx +{ + + + extension(StackFrame sf) + { + public string Method => sf.GetMethod().Name; + public string File => sf.GetFileName(); + public int Line => sf.GetFileLineNumber(); + public int Column => sf.GetFileColumnNumber(); + + public string Log => $"{sf.File}({sf.Line}, {sf.Column}): {sf.Method}"; + } + + + + +} \ No newline at end of file diff --git a/util/RandomEx.cs b/util/RandomEx.cs new file mode 100644 index 0000000..ea01cd1 --- /dev/null +++ b/util/RandomEx.cs @@ -0,0 +1,19 @@ + + + + +public static class RandomEx +{ + +/* +// NextBytes work fine + public static void FillBytes( this Random rnd, ref byte[] buffer ) + { + rnd.NextBytes() + + for( int i = 0; i < buffer.Length; ++i ) + { + } + +*/ +} \ No newline at end of file