From 193067bf5cdb8c73c671f4bb642a7a3724745fff Mon Sep 17 00:00:00 2001 From: Marc Hernandez Date: Sun, 26 May 2024 19:49:45 -0700 Subject: [PATCH] Fix nullability issues in Log.cs and Imm.cs - Updated Log.cs to handle nullable object types - Modified LogEvent class to accept nullable object types - Adjusted methods in log class to use nullable object types where necessary - Initialized s_thread variable with Thread.CurrentThread - Updated MemorySourceText and MemoryRefResolver classes in Script.cs - Implemented overrides for SourceText properties and methods in MemorySourceText class - Refactored MemoryRefResolver class to handle nullability correctly --- imm/Imm.cs | 2 +- logging/Log.cs | 72 ++++++++++++++++++++++++++------------------------ scr/Script.cs | 71 +++++++++++++++++++++++++++---------------------- 3 files changed, 78 insertions(+), 67 deletions(-) diff --git a/imm/Imm.cs b/imm/Imm.cs index 23ca6d4..89c5bbf 100644 --- a/imm/Imm.cs +++ b/imm/Imm.cs @@ -307,7 +307,7 @@ public record class Timed : Recorded, imm.Imm } [DebuggerBrowsable(DebuggerBrowsableState.Never)] - new public MetaData Meta => MetaStorage as MetaData; + new public MetaData Meta => MetaStorage as MetaData ?? new MetaData(); public TimeSpan Since => Meta.TouchedAt - Meta.Old?.Meta.TouchedAt ?? TimeSpan.MaxValue; diff --git a/logging/Log.cs b/logging/Log.cs index 0357503..682b1ef 100644 --- a/logging/Log.cs +++ b/logging/Log.cs @@ -13,6 +13,8 @@ using System.Security.Cryptography.X509Certificates; using lib.Net; //using System.Threading.Tasks; +#nullable enable + static public class log { @@ -55,14 +57,14 @@ static public class log public string Member; public string Cat; - public object Obj; + public object? Obj; static ImmutableDictionary s_shortname = ImmutableDictionary.Empty; - public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, object obj ) + public LogEvent( LogType logType, string msg, string path, int line, string member, string cat, object? obj ) { //Cache the automatic category names @@ -101,7 +103,7 @@ static public class log Obj = obj; } } - static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static LogEvent CreateLogEvent( LogType logType, string msg, string cat, object? obj, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { var logEvent = new LogEvent( logType, msg, path, line, member, cat, obj ); @@ -135,7 +137,7 @@ static public class log static internal ConcurrentQueue s_events = new ConcurrentQueue(); - static private Thread s_thread; + static private Thread s_thread = Thread.CurrentThread; /* static public Log log @@ -197,38 +199,38 @@ static public class log #endregion // Util #region Forwards - static public void fatal( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void fatal( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Fatal, path, line, member, cat, obj ); } - static public void error( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void error( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Error, path, line, member, cat, obj ); } - static public void warn( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void warn( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Warn, path, line, member, cat, obj ); } - static public void high( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void high( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.High, path, line, member, cat, obj ); } - static public void info( string msg, string cat = "", object obj = null, + static public void info( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Info, path, line, member, cat, obj ); } - static public void debug( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void debug( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Debug, path, line, member, cat, obj ); } - static public void trace( string msg, string cat = "", object obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) + static public void trace( string msg, string cat = "", object? obj = null, [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" ) { logBase( msg, LogType.Trace, path, line, member, cat, obj ); } @@ -266,9 +268,9 @@ static public class log } //This might seem a little odd, but the intent is that usually you wont need to set notExpectedValue. - static public void expected( T value, string falseString, string trueString = "", T notExpectedValue = default( T ) ) + static public void expected( T value, string falseString, string trueString = "", T? notExpectedValue = default( T ) ) { - if( !value.Equals( notExpectedValue ) ) + if( !object.Equals( value, notExpectedValue ) ) { log.debug( $"Properly got {value}{trueString}" ); } @@ -282,14 +284,14 @@ static public class log static object s_lock = new object(); - static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object obj = null ) + static public LogEvent logCreateEvent( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null ) { LogEvent evt = new LogEvent( type, msg, path, line, member, cat, obj ); return evt; } - static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object obj = null ) + static public void logBase( string msg, LogType type = LogType.Debug, string path = "", int line = -1, string member = "", string cat = "unk", object? obj = null ) { var evt = logCreateEvent( msg, type, path, line, member, cat ); @@ -299,10 +301,14 @@ static public class log static Endpoints s_endpoints = Endpoints.Console; static int s_catWidth = 14; - static int s_timeWidth = 3; static void startup( string filename, Endpoints endpoints ) { + if( string.IsNullOrWhiteSpace( filename ) ) + { + return; + } + s_startTime = DateTime.Now; s_cwd = Directory.GetCurrentDirectory(); @@ -317,12 +323,9 @@ static public class log s_thread.Name = $"Logging"; s_thread.Start(); - //TODO: Fix this so itll work without a directory. - Directory.CreateDirectory( Path.GetDirectoryName( filename ) ); + var dir = Path.GetDirectoryName( filename ); - string dir = Path.GetDirectoryName( filename ); - - if( dir.Length > 0 ) + if( dir?.Length > 0 ) { Directory.CreateDirectory( dir ); } @@ -363,9 +366,9 @@ static public class log var time = DateTime.Now; // Header for this run var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", null ); - var beginLine = new LogEvent( LogType.Raw, $"B E G I N ************************************************************************************************************************", "", 0, "", "lib.time", null ); - var endLine = new LogEvent( LogType.Raw, $"E N D ************************************************************************************************************************", "", 0, "", "lib.time", null ); - var timeLine = new LogEvent( LogType.Raw, $" D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", null ); + var beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", null ); + var endLine = new LogEvent( LogType.Raw, $"End E N D ******************************************************************************************************************", "", 0, "", "lib.time", null ); + var timeLine = new LogEvent( LogType.Raw, $"D A T E {time.Year}/{time.Month.ToString("00")}/{time.Day.ToString("00")} T I M E {time.Hour.ToString("00")}:{time.Minute.ToString("00")}:{time.Second.ToString("00")}.{time.Millisecond.ToString("000")}{time.Microsecond.ToString("000")}", "", 0, "", "lib.time", null ); //writeToAll( endLine ); @@ -392,6 +395,7 @@ static public class log } + public static void stop() { if( !s_running ) return; @@ -403,11 +407,11 @@ static public class log Thread.Sleep( 0 ); } - s_writer.Close(); - s_stream.Close(); + s_writer?.Close(); + s_stream?.Close(); - s_errorWriter.Close(); - s_errorStream.Close(); + s_errorWriter?.Close(); + s_errorStream?.Close(); } @@ -595,7 +599,7 @@ static public class log if( ( s_endpoints & Endpoints.File ) == Endpoints.File ) { var line = msgFile( evt ); - s_writer.WriteLine( line ); + s_writer?.WriteLine( line ); } if( ( s_endpoints & Endpoints.Console ) == Endpoints.Console ) @@ -609,7 +613,7 @@ static public class log //Debug.WriteLine( finalLine ); - s_writer.Flush(); + s_writer?.Flush(); foreach( Log_delegate cb in s_delegates ) { @@ -651,11 +655,11 @@ static public class log } - private static Stream s_stream; - private static StreamWriter s_writer; + private static Stream? s_stream; + private static StreamWriter? s_writer; - private static Stream s_errorStream; - private static StreamWriter s_errorWriter; + private static Stream? s_errorStream; + private static StreamWriter? s_errorWriter; private static ArrayList s_delegates = new ArrayList(); diff --git a/scr/Script.cs b/scr/Script.cs index fe24105..706cb5e 100644 --- a/scr/Script.cs +++ b/scr/Script.cs @@ -18,9 +18,24 @@ using System.Reflection; using System.Text; using System.Threading; +public class MemorySourceText : SourceText +{ + public override char this[int position] => ' '; + + public override Encoding? Encoding => Encoding.UTF8; + + public override int Length => 0; + + public override void CopyTo( int sourceIndex, char[] destination, int destinationIndex, int count ) + { + + } +} + + public class MemoryRefResolver : SourceReferenceResolver { - public override bool Equals(object other) + public override bool Equals(object? other) { return false; } @@ -30,25 +45,17 @@ public class MemoryRefResolver : SourceReferenceResolver return 0; } - public override string NormalizePath(string path, string baseFilePath) - { - return path; - } + private MemoryStream _fakeMS = new MemoryStream(); - public override Stream OpenRead(string resolvedPath) - { - return null; - } + public override string? NormalizePath( string? path, string? baseFilePath ) => path; - public override SourceText ReadText(string resolvedPath) - { - return null; - } + public override Stream OpenRead( string resolvedPath ) => _fakeMS; - public override string ResolveReference(string path, string baseFilePath) - { - return null; - } + private MemorySourceText _fakeMST = new(); + + public override SourceText ReadText( string resolvedPath ) => _fakeMST; + + public override string? ResolveReference( string? path, string? baseFilePath ) => path; } @@ -114,7 +121,7 @@ public static class scr return safeStr.ToString(); } - static public FileSystemWatcher s_watcher; + static public FileSystemWatcher s_watcher = new(); static public Action s_fnAss = (ass) => { log.warn( $"Need to replace s_fnAss with custom function" ); }; @@ -144,41 +151,41 @@ public static class scr } - static void OnCreated( object sender, FileSystemEventArgs e ) + static void OnCreated( object sender, FileSystemEventArgs fse ) { - log.debug( $"{e.Name} got {e.ChangeType}" ); + log.debug( $"{fse.Name} got {fse.ChangeType}" ); - if( e.Name.EndsWith( ".cs" ) ) + if( fse.Name?.EndsWith( ".cs" ) ?? false ) { - Process( e.FullPath ); + Process( fse.FullPath ); } } - static void OnDeleted( object sender, FileSystemEventArgs e ) + static void OnDeleted( object sender, FileSystemEventArgs fse ) { - log.debug( $"{e.Name} got {e.ChangeType}" ); + log.debug( $"{fse.Name} got {fse.ChangeType}" ); } - static void OnRenamed( object sender, FileSystemEventArgs e ) + static void OnRenamed( object sender, FileSystemEventArgs fse ) { - log.debug( $"{e.Name} got {e.ChangeType}" ); + log.debug( $"{fse.Name} got {fse.ChangeType}" ); - if( e.Name.EndsWith(".cs") ) + if( fse.Name?.EndsWith(".cs") ?? false ) { while( true ) { try { - Process( e.FullPath ); + Process( fse.FullPath ); return; } catch( System.IO.IOException ex ) { - + log.debug( $"Ignoring {ex.Message}" ); } catch( Exception ex ) { - log.error( $"Got ex {ex.GetType().Name} trying to process {e.FullPath}" ); + log.error( $"Got ex {ex.GetType().Name} trying to process {fse.FullPath}" ); log.error( $"-> {ex.Message}" ); return; } @@ -316,10 +323,10 @@ public static class scr { if( t.GetGenericArguments().Length == 0 ) { - return t.FullName.Replace( '+', '.' ); + return t.FullName?.Replace( '+', '.' ) ?? ""; } var genArgs = t.GetGenericArguments(); - var typeDef = t.FullName; + var typeDef = t.FullName ?? ""; var indexOfTick = typeDef.IndexOf("`");