Add filtering and timing

This commit is contained in:
Marc Hernandez 2026-02-05 18:27:56 -08:00
parent 6b1e193c90
commit 3bf3220beb
3 changed files with 124 additions and 1 deletions

View File

@ -12,7 +12,9 @@
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<LangVersion>14.0</LangVersion>
<Copyright>Copyright 2003..2025 Marc Hernandez</Copyright>
<Description>A base set of </Description>
<Description>A base set of functionality</Description>
<SatelliteResourceLanguages>en</SatelliteResourceLanguages>
<!--
I Want to turn this on, but cant yet. Implementing nullability 1 file at a gime
<Nullable>enable</Nullable>

View File

@ -529,11 +529,29 @@ static public class log
#endregion
public delegate bool FilterFn( LogEvent evt );
static object s_lock = new object();
static object s_lockTypeCallback = new object();
static ImmutableDictionary<LogType, Action<LogEvent>> s_callbacks = ImmutableDictionary<LogType, Action<LogEvent>>.Empty;
//static ConcurrentDictionary<string, List<FilterFn>> s_filters = new();
static public void addFilter( string name, FilterFn filter )
{
/*
s_filters.AddOrUpdate( name,
( k ) => new List<FilterFn> { filter },
( k, v ) => {
v.Add( filter );
return v;
} );
*/
}
[StackTraceHidden]
static public void addDirectCallback( LogType logType, Action<LogEvent> callback )
{
@ -681,6 +699,27 @@ static public class log
// automatically finish when s_events.CompleteAdding() is called from another thread.
foreach( var evt in s_events.GetConsumingEnumerable() )
{
/*
// Apply filters
var cat = evt.Cat;
var blocked = false;
if( s_filters.TryGetValue( cat, out var filterList ) )
{
foreach( var filter in filterList )
{
if( !filter( evt ) )
{
blocked = true;
break;
}
}
if( blocked )
{
continue; // Skip this event
}
}
//*/
writeToAll( evt );
}
}
@ -945,6 +984,7 @@ static public class log
{
var trimmedLine = line.Trim( (char)0 );
s_writer?.WriteLine( trimmedLine );
s_writer?.Flush();
}
}

81
util/Exec.cs Normal file
View File

@ -0,0 +1,81 @@
using System.Diagnostics;
namespace lib;
public readonly ref struct Defer
{
private readonly Action _onDispose;
public Defer( Action onDispose )
{
_onDispose = onDispose;
}
public void Dispose()
{
_onDispose?.Invoke();
}
public static Defer Do( Action action ) => new Defer( action );
}
public readonly ref struct Timed
{
private readonly long _startTimestamp;
private readonly Action<TimeSpan> _onDispose;
public Timed( Action<TimeSpan> onDispose )
{
_onDispose = onDispose;
_startTimestamp = Stopwatch.GetTimestamp();
}
public void Dispose()
{
// Calculate elapsed ticks
long endTimestamp = Stopwatch.GetTimestamp();
long elapsedTicks = endTimestamp - _startTimestamp;
// Convert to TimeSpan based on system timer frequency
// (Standard formula for high-resolution timing)
double seconds = (double)elapsedTicks / Stopwatch.Frequency;
var elapsed = TimeSpan.FromSeconds( seconds );
_onDispose?.Invoke( elapsed );
}
// Optional: Allow peeking at the time without stopping
public TimeSpan Elapsed
{
get
{
long current = Stopwatch.GetTimestamp();
return TimeSpan.FromSeconds( (double)( current - _startTimestamp ) / Stopwatch.Frequency );
}
}
public static Timed Do( Action<TimeSpan> action ) => new Timed( action );
}
static public class Exec
{
static public void RunIf( bool condition, Action func )
{
if( condition )
func();
}
static public T RunIf<T>( bool condition, Func<T> func, T ret = default! )
{
if( condition )
return func();
return ret;
}
}