Add global aliases for Immutable collections
x) Refactor logging endpoints to use more concise method names x) Implement implicit conversion for Value struct x) Add direct callback functionality to log events
This commit is contained in:
parent
bf7e47e532
commit
066d3bc7f9
6
Globals.cs
Normal file
6
Globals.cs
Normal file
@ -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;
|
||||||
@ -108,11 +108,11 @@ public class LogGC
|
|||||||
);
|
);
|
||||||
stacklist = ImmutableHashSet.Create( "{TEST_ITEM}" );
|
stacklist = ImmutableHashSet.Create( "{TEST_ITEM}" );
|
||||||
|
|
||||||
log.logEndpointForCategory( "Method/MemoryAllocatedForJitCode", log.Endpoints.File );
|
log.endpointForCat( "Method/MemoryAllocatedForJitCode", log.Endpoints.File );
|
||||||
log.logEndpointForCategory( "TypeLoad/Stop", log.Endpoints.File );
|
log.endpointForCat( "TypeLoad/Stop", log.Endpoints.File );
|
||||||
log.logEndpointForCategory( "GC/BulkRootStaticVar", log.Endpoints.File );
|
log.endpointForCat( "GC/BulkRootStaticVar", log.Endpoints.File );
|
||||||
log.logEndpointForCategory( "GC/BulkNode", log.Endpoints.File );
|
log.endpointForCat( "GC/BulkNode", log.Endpoints.File );
|
||||||
log.logEndpointForCategory( "GC/BulkRootStaticVar", log.Endpoints.File );
|
log.endpointForCat( "GC/BulkRootStaticVar", log.Endpoints.File );
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -21,6 +21,8 @@ using Microsoft.CodeAnalysis.CSharp.Syntax;
|
|||||||
/*
|
/*
|
||||||
|
|
||||||
T O D O :
|
T O D O :
|
||||||
|
x) Hook the C# prints from glue.
|
||||||
|
x) Fix
|
||||||
x) Refactor various logs in order to do automagic structured logging
|
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
|
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>( T _val = default, string _exp = "" )
|
public record struct Value<T>( T _val, string _exp = "" )
|
||||||
{
|
{
|
||||||
|
public static T Default = default;
|
||||||
|
|
||||||
|
public static implicit operator T( Value<T> v )
|
||||||
|
{
|
||||||
|
return v._val;
|
||||||
|
}
|
||||||
|
|
||||||
static public Value<U> Get<U>( U v,
|
static public Value<U> Get<U>( U v,
|
||||||
[CallerArgumentExpression("fn")]
|
[CallerArgumentExpression("fn")]
|
||||||
@ -79,6 +87,9 @@ static public class log
|
|||||||
|
|
||||||
//static
|
//static
|
||||||
|
|
||||||
|
#region CLR Logging
|
||||||
|
|
||||||
|
|
||||||
static log()
|
static log()
|
||||||
{
|
{
|
||||||
log.high( $"Starting tracers" );
|
log.high( $"Starting tracers" );
|
||||||
@ -140,6 +151,7 @@ static public class log
|
|||||||
Tracing.TraceLogMonitor.Run();
|
Tracing.TraceLogMonitor.Run();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion // CLR Logging
|
||||||
|
|
||||||
static public Value<T> Value<T>( T val,
|
static public Value<T> Value<T>( T val,
|
||||||
[CallerArgumentExpression("val")]
|
[CallerArgumentExpression("val")]
|
||||||
@ -246,15 +258,11 @@ static public class log
|
|||||||
return logEvent;
|
return logEvent;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public delegate void Log_delegate( LogEvent evt );
|
public delegate void Log_delegate( LogEvent evt );
|
||||||
|
|
||||||
|
|
||||||
static ImmutableDictionary<string, Endpoints> s_logEPforCat = ImmutableDictionary<string, Endpoints>.Empty;
|
static ImmutableDictionary<string, Endpoints> s_logEPforCat = ImmutableDictionary<string, Endpoints>.Empty;
|
||||||
|
|
||||||
|
static public void endpointForCat( string cat, Endpoints ep )
|
||||||
static public void logEndpointForCategory( string cat, Endpoints ep )
|
|
||||||
{
|
{
|
||||||
ImmutableInterlocked.AddOrUpdate( ref s_logEPforCat, cat, ep, (k, v) => ep );
|
ImmutableInterlocked.AddOrUpdate( ref s_logEPforCat, cat, ep, (k, v) => ep );
|
||||||
}
|
}
|
||||||
@ -277,21 +285,10 @@ static public class log
|
|||||||
stop();
|
stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static internal ConcurrentQueue<LogEvent> s_events = new ConcurrentQueue<LogEvent>();
|
static internal ConcurrentQueue<LogEvent> s_events = new ConcurrentQueue<LogEvent>();
|
||||||
|
|
||||||
static private Thread s_thread = Thread.CurrentThread;
|
static private Thread s_thread = Thread.CurrentThread;
|
||||||
|
|
||||||
/*
|
|
||||||
static public Log log
|
|
||||||
{
|
|
||||||
get
|
|
||||||
{
|
|
||||||
return s_log;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
static string s_cwd = Directory.GetCurrentDirectory();
|
static string s_cwd = Directory.GetCurrentDirectory();
|
||||||
static int s_cwdLength = s_cwd.Length;
|
static int s_cwdLength = s_cwd.Length;
|
||||||
static ImmutableDictionary<int, string> s_files = ImmutableDictionary<int, string>.Empty;
|
static ImmutableDictionary<int, string> s_files = ImmutableDictionary<int, string>.Empty;
|
||||||
@ -368,11 +365,13 @@ static public class log
|
|||||||
logBase( msg, LogType.Fatal, path, line, member, cat, dbgExp, obj );
|
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 = "" )
|
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 );
|
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 = "" )
|
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 );
|
logBase( msg, LogType.Warn, path, line, member, cat, dbgExp, obj );
|
||||||
@ -417,7 +416,6 @@ static public class log
|
|||||||
|
|
||||||
#endregion
|
#endregion
|
||||||
|
|
||||||
|
|
||||||
#region Helpers
|
#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 = "" )
|
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_lock = new object();
|
||||||
|
static object s_lockTypeCallback = new object();
|
||||||
|
static ImmutableDictionary<LogType, Action<LogEvent>> s_callbacks = ImmutableDictionary<LogType, Action<LogEvent>>.Empty;
|
||||||
|
|
||||||
|
[StackTraceHidden]
|
||||||
|
static public void addDirectCallback( LogType logType, Action<LogEvent> 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 )
|
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,15 +483,29 @@ static public class log
|
|||||||
return evt;
|
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 )
|
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 );
|
var evt = logCreateEvent( msg, type, path, line, member, cat, exp );
|
||||||
|
|
||||||
|
s_callbacks.TryGetValue( type, out var callback );
|
||||||
|
|
||||||
|
if( callback != null )
|
||||||
|
{
|
||||||
|
lock( s_lockTypeCallback )
|
||||||
|
{
|
||||||
|
callback( evt );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
s_events.Enqueue( evt );
|
s_events.Enqueue( evt );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static Endpoints s_endpoints = Endpoints.Console;
|
static Endpoints s_endpoints = Endpoints.Console;
|
||||||
static int s_catWidth = 14;
|
static int s_catWidth = 14;
|
||||||
|
|
||||||
@ -795,7 +821,7 @@ static public class log
|
|||||||
|
|
||||||
#region Write
|
#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 )
|
if( ( finalEndpoints & Endpoints.File ) == Endpoints.File )
|
||||||
{
|
{
|
||||||
|
|||||||
@ -230,9 +230,9 @@ public static class scr
|
|||||||
var memRef = new MemoryRefResolver();
|
var memRef = new MemoryRefResolver();
|
||||||
|
|
||||||
|
|
||||||
using (var ms = new MemoryStream())
|
using MemoryStream ms = new ();
|
||||||
using (var pdb = new MemoryStream())
|
using MemoryStream pdb = new ();
|
||||||
{
|
|
||||||
var result = CompileAndEmit(assemblyName, new[] { syntaxTree }, ms, pdb, platform);
|
var result = CompileAndEmit(assemblyName, new[] { syntaxTree }, ms, pdb, platform);
|
||||||
|
|
||||||
if (!result.Success)
|
if (!result.Success)
|
||||||
@ -255,7 +255,6 @@ public static class scr
|
|||||||
onSuccess( assembly );
|
onSuccess( assembly );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static void LogDiags( string uniquePath, int count, IEnumerable<Diagnostic> diags )
|
public static void LogDiags( string uniquePath, int count, IEnumerable<Diagnostic> diags )
|
||||||
{
|
{
|
||||||
@ -268,7 +267,7 @@ public static class scr
|
|||||||
|
|
||||||
private static EmitResult CompileAndEmit(string assemblyName, SyntaxTree[] syntaxTrees, MemoryStream ms, MemoryStream pdb, Platform platform)
|
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
|
// @@@@ TODO :: Config release / debug
|
||||||
CSharpCompilation compilation = CSharpCompilation.Create(
|
CSharpCompilation compilation = CSharpCompilation.Create(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user