From a3e79e83b455e14b759ece3075266490e414c352 Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 21 Apr 2024 23:31:47 -0700 Subject: [PATCH] FSM x) Add whatFile for fast cached lookup of filenames from a path x) Debug renames x) Add default OnChanged handler that does nothing x) Cleanups to the FSM stuff --- imm/FSM.cs | 55 +++++++++++++++++++++++++------------- imm/Imm.cs | 71 +++++++++++++++++++++++++------------------------- logging/Log.cs | 30 +++++++++++++++++++++ 3 files changed, 103 insertions(+), 53 deletions(-) diff --git a/imm/FSM.cs b/imm/FSM.cs index 8806d46..4fdfabd 100644 --- a/imm/FSM.cs +++ b/imm/FSM.cs @@ -1,6 +1,8 @@  using System; +using System.Runtime.CompilerServices; +using Optional; @@ -13,25 +15,25 @@ public record class Context : imm.Recorded } -public record class State( CTX Context ) : imm.Recorded> - where T : State +public record class State( CTX Context ) : imm.Recorded + where TSUB : State where CTX : Context { - virtual public (CTX, T) onEnter(CTX ctx, State oldState) + virtual public (CTX, TSUB) onEnter(CTX ctx, State oldState) { - return (ctx, (T)this); + return (ctx, (TSUB)this); } - virtual public (CTX, T) onExit(CTX ctx, State newState) + virtual public (CTX, TSUB) onExit(CTX ctx, State newState) { - return (ctx, (T)this); + return (ctx, (TSUB)this); } } -public record class FSM : imm.Recorded> - where T : FSM +public record class FSM : imm.Recorded + where TSUB : FSM where ST : State where CTX : Context { @@ -44,37 +46,54 @@ public record class FSM : imm.Recorded> State = stStart; } - public FSM Transition(ST newState) + public TSUB Transition(ST newState, string reason, + [CallerMemberName] string memberName = "", + [CallerFilePath] string filePath = "", + [CallerLineNumber] int lineNumber = 0, + [CallerArgumentExpression("fn")] + string expression = default + ) { + log.debug( $"Trans from {State.GetType().Name} to {newState.GetType().Name} for {reason}" ); + var origState = State; - var (newOldCTX, oldState) = State.onExit(Context, newState); + var (newCtx, oldState) = State.onExit(Context, newState); - var (newCTX, storeState) = newState.onEnter(newOldCTX, oldState); + var (newCTX, storeState) = newState.onEnter(newCtx, oldState); - var newFSM = this.Process( this with + var newFSM = this.Process( (v) => (this as TSUB) with { Context = newCTX, State = storeState, - }, $"Trans: {origState.GetType().Name} to {newState.GetType().Name}" ); + }, $"{reason}" ); return newFSM; } - public FSM Process( Func fn, string reason ) + /* + public TSUB ( Func fn, string reason, + [CallerMemberName] string member = "", + [CallerFilePath] string file = "", + [CallerLineNumber] int line = 0, + [CallerArgumentExpression("fn")] + string expression = default + ) { var newState = fn( State ); - if( object.ReferenceEquals( newState, State ) ) return this; + if( object.ReferenceEquals( newState, State ) ) return (TSUB)this; - FSM newFSM = this.Process( this with + TSUB newFSM = this.Process( this with { Context = Context, State = newState, - }, $"Processing: {newState.GetType().Name} for {reason}" ); + }, $"Processing: {newState.GetType().Name} for {reason}", + member, file, line, expression ); - return newFSM; + return (TSUB)newFSM; } + */ } diff --git a/imm/Imm.cs b/imm/Imm.cs index 63d4d82..82b2e35 100644 --- a/imm/Imm.cs +++ b/imm/Imm.cs @@ -14,23 +14,23 @@ static public class Util { //This can handle both Timed and Recorded static public T Process( ref T obj, Func fn, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] - string expression = default ) + string dbgExp = default ) where T : Recorded { - obj = obj.Process( fn, reason, memberName, filePath, lineNumber, expression ); + obj = obj.Process( fn, reason, dbgName, dbgPath, lineNumber, dbgExp ); return obj; } static public T LightProcess( ref T obj, Func fn, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] - string expression = default ) + string dbgExp = default ) where T : Versioned { obj = obj.Process( fn, reason ); @@ -61,7 +61,7 @@ public record class Versioned public MetaData Meta => MetaStorage; - public ChangeDelegate OnChange; + public ChangeDelegate OnChange = (x, y) => {}; public T Process( Func fn, string reason = "" ) { @@ -88,7 +88,7 @@ public record class Recorded : Versioned public string Expression { get; internal set; } = ""; public string MemberName { get; internal set; } = ""; public string FilePath { get; internal set; } = ""; - public int LineNumber { get; internal set; } = -1; + public int LineNumber { get; internal set; } = -1; public MetaData() { } } @@ -105,29 +105,29 @@ public record class Recorded : Versioned virtual public T Record( string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0 ) { - return Process( t => t, reason, memberName, filePath, lineNumber ); + return Process( t => t, reason, dbgName, dbgPath, lineNumber ); } virtual public T Process( T next, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("next")] - string expression = default ) + string dbgExp = default ) { - return Process( ( old ) => next, reason, memberName, filePath, lineNumber, expression ); + return Process( ( old ) => next, reason, dbgName, dbgPath, lineNumber, dbgExp ); } virtual public T Process( Func fn, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("fn")] - string expression = default ) + string dbgExp = default ) { var orig = ( T )this; @@ -142,8 +142,8 @@ public record class Recorded : Versioned Reason = reason, ZZOld = orig, - MemberName = memberName, - FilePath = filePath, + MemberName = dbgName, + FilePath = dbgPath, LineNumber = lineNumber, } }; @@ -178,30 +178,31 @@ public record class Timed : Recorded override public T Record( string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0 ) { - return Process( t => t, reason, memberName, filePath, lineNumber ); + return Process( t => t, reason, dbgName, dbgPath, lineNumber ); } override public T Process( T next, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", [CallerLineNumber] int lineNumber = 0, [CallerArgumentExpression("next")] - string expression = default ) + string dbgExp = default ) { - return Process( ( old ) => next, reason, memberName, filePath, lineNumber, expression ); + return Process( ( old ) => next, reason, dbgName, dbgPath, lineNumber, dbgExp ); } override public T Process( Func fn, string reason = "", - [CallerMemberName] string memberName = "", - [CallerFilePath] string filePath = "", - [CallerLineNumber] int lineNumber = 0, + [CallerMemberName] string dbgName = "", + [CallerFilePath] string dbgPath = "", + [CallerLineNumber] int dbgLine = 0, [CallerArgumentExpression("fn")] - string expression = default ) + string dbgExp = default + ) { var orig = ( T )this; @@ -216,9 +217,9 @@ public record class Timed : Recorded Reason = reason, //Recorded - MemberName = memberName, - FilePath = filePath, - LineNumber = lineNumber, + MemberName = dbgName, + FilePath = dbgPath, + LineNumber = dbgLine, ZZOld = orig, //Timed diff --git a/logging/Log.cs b/logging/Log.cs index bb9503a..d835e2d 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -9,6 +9,7 @@ using System.Reflection; using System.Collections.Concurrent; using System.Linq; using System.Threading; +using System.Security.Cryptography.X509Certificates; //using System.Threading.Tasks; static public class log @@ -99,6 +100,35 @@ static public class log public delegate void Log_delegate( LogEvent evt ); + static ImmutableDictionary s_files = ImmutableDictionary.Empty; + + static public string whatFile(string path) + { + var file = ""; + + var pathHash = path.GetHashCode(); + if (s_files.TryGetValue(pathHash, out var autoCat)) + { + file = autoCat; + } + else + { + var pathPieces = path.Split('\\'); + + if (pathPieces.Length < 2) + { + pathPieces = path.Split('/'); + } + + var lastDir = pathPieces[pathPieces.Length - 1]; + + ImmutableInterlocked.AddOrUpdate(ref s_files, pathHash, lastDir, (key, value) => { return lastDir; }); + + file = lastDir; + } + + return file; + }