diff --git a/Globals.cs b/Globals.cs new file mode 100644 index 0000000..459ec00 --- /dev/null +++ b/Globals.cs @@ -0,0 +1,6 @@ + + +global using ImmArray = System.Collections.Immutable.ImmutableArray; +global using ImmDict = System.Collections.Immutable.ImmutableDictionary; +global using ImmSet = System.Collections.Immutable.ImmutableHashSet; +global using II = System.Collections.Immutable.ImmutableInterlocked; diff --git a/logging/GC.cs b/logging/GC.cs index a8558a8..80304e4 100644 --- a/logging/GC.cs +++ b/logging/GC.cs @@ -108,11 +108,11 @@ public class LogGC ); stacklist = ImmutableHashSet.Create( "{TEST_ITEM}" ); - log.logEndpointForCategory( "Method/MemoryAllocatedForJitCode", log.Endpoints.File ); - log.logEndpointForCategory( "TypeLoad/Stop", log.Endpoints.File ); - log.logEndpointForCategory( "GC/BulkRootStaticVar", log.Endpoints.File ); - log.logEndpointForCategory( "GC/BulkNode", log.Endpoints.File ); - log.logEndpointForCategory( "GC/BulkRootStaticVar", log.Endpoints.File ); + log.endpointForCat( "Method/MemoryAllocatedForJitCode", log.Endpoints.File ); + log.endpointForCat( "TypeLoad/Stop", log.Endpoints.File ); + log.endpointForCat( "GC/BulkRootStaticVar", log.Endpoints.File ); + log.endpointForCat( "GC/BulkNode", log.Endpoints.File ); + log.endpointForCat( "GC/BulkRootStaticVar", log.Endpoints.File ); } diff --git a/logging/Log.cs b/logging/Log.cs index 2f6960d..9ff5db2 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -21,6 +21,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax; /* T O D O : +x) Hook the C# prints from glue. +x) Fix x) Refactor various logs in order to do automagic structured logging ref: https://learn.microsoft.com/en-us/dotnet/csharp/whats-new/tutorials/interpolated-string-handler @@ -33,8 +35,14 @@ N O T D O I N G : */ -public record struct Value( T _val = default, string _exp = "" ) +public record struct Value( T _val, string _exp = "" ) { + public static T Default = default; + + public static implicit operator T( Value v ) + { + return v._val; + } static public Value Get( U v, [CallerArgumentExpression("fn")] @@ -79,6 +87,9 @@ static public class log //static + #region CLR Logging + + static log() { log.high( $"Starting tracers" ); @@ -140,6 +151,7 @@ static public class log Tracing.TraceLogMonitor.Run(); } + #endregion // CLR Logging static public Value Value( T val, [CallerArgumentExpression("val")] @@ -246,15 +258,11 @@ static public class log return logEvent; } - - public delegate void Log_delegate( LogEvent evt ); - static ImmutableDictionary s_logEPforCat = ImmutableDictionary.Empty; - - static public void logEndpointForCategory( string cat, Endpoints ep ) + static public void endpointForCat( string cat, Endpoints ep ) { ImmutableInterlocked.AddOrUpdate( ref s_logEPforCat, cat, ep, (k, v) => ep ); } @@ -277,21 +285,10 @@ static public class log stop(); } - static internal ConcurrentQueue s_events = new ConcurrentQueue(); static private Thread s_thread = Thread.CurrentThread; - /* - static public Log log - { - get - { - return s_log; - } - } - */ - static string s_cwd = Directory.GetCurrentDirectory(); static int s_cwdLength = s_cwd.Length; static ImmutableDictionary s_files = ImmutableDictionary.Empty; @@ -368,11 +365,13 @@ static public class log logBase( msg, LogType.Fatal, path, line, member, cat, dbgExp, obj ); } + [StackTraceHidden] 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, dbgExp, obj ); } + [StackTraceHidden] 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, dbgExp, obj ); @@ -417,7 +416,6 @@ static public class log #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 = "", [CallerArgumentExpression("obj")] string dbgExpObj = "" ) { @@ -464,6 +462,20 @@ static public class log static object s_lock = new object(); + static object s_lockTypeCallback = new object(); + static ImmutableDictionary> s_callbacks = ImmutableDictionary>.Empty; + + [StackTraceHidden] + static public void addDirectCallback( LogType logType, Action callback ) + { + //ImmutableInterlocked.Add( ref s_callbacks, logType, callback ); + var added = II.TryAdd( ref s_callbacks, logType, callback ); + if( !added ) + { + log.warn( $"Failed to add callback for {logType}" ); + } + } + 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 ) { @@ -471,12 +483,26 @@ static public class log return evt; } - + [StackTraceHidden] 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, exp ); - s_events.Enqueue( evt ); + s_callbacks.TryGetValue( type, out var callback ); + + if( callback != null ) + { + lock( s_lockTypeCallback ) + { + callback( evt ); + } + } + else + { + s_events.Enqueue( evt ); + } + + } @@ -795,7 +821,7 @@ static public class log #region Write - var finalEndpoints = s_logEPforCat.TryGetValue( evt.Cat, out var ep ) ? ep | s_endpoints : s_endpoints; + var finalEndpoints = s_logEPforCat.TryGetValue( evt.Cat, out var ep ) ? ep & s_endpoints : s_endpoints; if( ( finalEndpoints & Endpoints.File ) == Endpoints.File ) { diff --git a/scr/Script.cs b/scr/Script.cs index 706cb5e..d8a7908 100644 --- a/scr/Script.cs +++ b/scr/Script.cs @@ -230,31 +230,30 @@ public static class scr var memRef = new MemoryRefResolver(); - using (var ms = new MemoryStream()) - using (var pdb = new MemoryStream()) + using MemoryStream ms = new (); + using MemoryStream pdb = new (); + + var result = CompileAndEmit(assemblyName, new[] { syntaxTree }, ms, pdb, platform); + + if (!result.Success) { - var result = CompileAndEmit(assemblyName, new[] { syntaxTree }, ms, pdb, platform); - - if (!result.Success) + if (onFailure == null) { - if (onFailure == null) - { - } - else - { - LogDiags( uniquePath, result.Diagnostics.Length, result.Diagnostics ); - - onFailure( result.Diagnostics ); - } } else { - ms.Seek(0, SeekOrigin.Begin); - var assembly = Assembly.Load(ms.ToArray(), pdb.ToArray()); + LogDiags( uniquePath, result.Diagnostics.Length, result.Diagnostics ); - onSuccess( assembly ); + onFailure( result.Diagnostics ); } } + else + { + ms.Seek(0, SeekOrigin.Begin); + var assembly = Assembly.Load(ms.ToArray(), pdb.ToArray()); + + onSuccess( assembly ); + } } public static void LogDiags( string uniquePath, int count, IEnumerable diags ) @@ -268,7 +267,7 @@ public static class scr private static EmitResult CompileAndEmit(string assemblyName, SyntaxTree[] syntaxTrees, MemoryStream ms, MemoryStream pdb, Platform platform) { - var memRef = new MemoryRefResolver(); + MemoryRefResolver memRef = new(); // @@@@ TODO :: Config release / debug CSharpCompilation compilation = CSharpCompilation.Create(