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
This commit is contained in:
Marc Hernandez 2024-05-26 19:49:45 -07:00
parent 4e59ae0331
commit 193067bf5c
3 changed files with 78 additions and 67 deletions

View File

@ -307,7 +307,7 @@ public record class Timed<T> : Recorded<T>, imm.Imm
} }
[DebuggerBrowsable(DebuggerBrowsableState.Never)] [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; public TimeSpan Since => Meta.TouchedAt - Meta.Old?.Meta.TouchedAt ?? TimeSpan.MaxValue;

View File

@ -13,6 +13,8 @@ using System.Security.Cryptography.X509Certificates;
using lib.Net; using lib.Net;
//using System.Threading.Tasks; //using System.Threading.Tasks;
#nullable enable
static public class log static public class log
{ {
@ -55,14 +57,14 @@ static public class log
public string Member; public string Member;
public string Cat; public string Cat;
public object Obj; public object? Obj;
static ImmutableDictionary<int, string> s_shortname = ImmutableDictionary<int, string>.Empty; static ImmutableDictionary<int, string> s_shortname = ImmutableDictionary<int, string>.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 //Cache the automatic category names
@ -101,7 +103,7 @@ static public class log
Obj = obj; 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 ); var logEvent = new LogEvent( logType, msg, path, line, member, cat, obj );
@ -135,7 +137,7 @@ static public class log
static internal ConcurrentQueue<LogEvent> s_events = new ConcurrentQueue<LogEvent>(); static internal ConcurrentQueue<LogEvent> s_events = new ConcurrentQueue<LogEvent>();
static private Thread s_thread; static private Thread s_thread = Thread.CurrentThread;
/* /*
static public Log log static public Log log
@ -197,38 +199,38 @@ static public class log
#endregion // Util #endregion // Util
#region Forwards #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 ); 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 ); 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 ); 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 ); 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 = "" ) [CallerFilePath] string path = "", [CallerLineNumber] int line = -1, [CallerMemberName] string member = "" )
{ {
logBase( msg, LogType.Info, path, line, member, cat, obj ); 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 ); 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 ); 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. //This might seem a little odd, but the intent is that usually you wont need to set notExpectedValue.
static public void expected<T>( T value, string falseString, string trueString = "", T notExpectedValue = default( T ) ) static public void expected<T>( 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}" ); log.debug( $"Properly got {value}{trueString}" );
} }
@ -282,14 +284,14 @@ static public class log
static object s_lock = new object(); 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 ); LogEvent evt = new LogEvent( type, msg, path, line, member, cat, obj );
return evt; 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 ); var evt = logCreateEvent( msg, type, path, line, member, cat );
@ -299,10 +301,14 @@ static public class log
static Endpoints s_endpoints = Endpoints.Console; static Endpoints s_endpoints = Endpoints.Console;
static int s_catWidth = 14; static int s_catWidth = 14;
static int s_timeWidth = 3;
static void startup( string filename, Endpoints endpoints ) static void startup( string filename, Endpoints endpoints )
{ {
if( string.IsNullOrWhiteSpace( filename ) )
{
return;
}
s_startTime = DateTime.Now; s_startTime = DateTime.Now;
s_cwd = Directory.GetCurrentDirectory(); s_cwd = Directory.GetCurrentDirectory();
@ -317,12 +323,9 @@ static public class log
s_thread.Name = $"Logging"; s_thread.Name = $"Logging";
s_thread.Start(); s_thread.Start();
//TODO: Fix this so itll work without a directory. var dir = Path.GetDirectoryName( filename );
Directory.CreateDirectory( Path.GetDirectoryName( filename ) );
string dir = Path.GetDirectoryName( filename ); if( dir?.Length > 0 )
if( dir.Length > 0 )
{ {
Directory.CreateDirectory( dir ); Directory.CreateDirectory( dir );
} }
@ -363,8 +366,8 @@ static public class log
var time = DateTime.Now; var time = DateTime.Now;
// Header for this run // Header for this run
var blankLine = new LogEvent( LogType.Raw, $"", "", 0, "", "lib.time", null ); 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 beginLine = new LogEvent( LogType.Raw, $"Begin B E G I N ******************************************************************************************************************", "", 0, "", "lib.time", null );
var endLine = new LogEvent( LogType.Raw, $"E N D ************************************************************************************************************************", "", 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 ); 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 );
@ -392,6 +395,7 @@ static public class log
} }
public static void stop() public static void stop()
{ {
if( !s_running ) return; if( !s_running ) return;
@ -403,11 +407,11 @@ static public class log
Thread.Sleep( 0 ); Thread.Sleep( 0 );
} }
s_writer.Close(); s_writer?.Close();
s_stream.Close(); s_stream?.Close();
s_errorWriter.Close(); s_errorWriter?.Close();
s_errorStream.Close(); s_errorStream?.Close();
} }
@ -595,7 +599,7 @@ static public class log
if( ( s_endpoints & Endpoints.File ) == Endpoints.File ) if( ( s_endpoints & Endpoints.File ) == Endpoints.File )
{ {
var line = msgFile( evt ); var line = msgFile( evt );
s_writer.WriteLine( line ); s_writer?.WriteLine( line );
} }
if( ( s_endpoints & Endpoints.Console ) == Endpoints.Console ) if( ( s_endpoints & Endpoints.Console ) == Endpoints.Console )
@ -609,7 +613,7 @@ static public class log
//Debug.WriteLine( finalLine ); //Debug.WriteLine( finalLine );
s_writer.Flush(); s_writer?.Flush();
foreach( Log_delegate cb in s_delegates ) foreach( Log_delegate cb in s_delegates )
{ {
@ -651,11 +655,11 @@ static public class log
} }
private static Stream s_stream; private static Stream? s_stream;
private static StreamWriter s_writer; private static StreamWriter? s_writer;
private static Stream s_errorStream; private static Stream? s_errorStream;
private static StreamWriter s_errorWriter; private static StreamWriter? s_errorWriter;
private static ArrayList s_delegates = new ArrayList(); private static ArrayList s_delegates = new ArrayList();

View File

@ -18,9 +18,24 @@ using System.Reflection;
using System.Text; using System.Text;
using System.Threading; 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 class MemoryRefResolver : SourceReferenceResolver
{ {
public override bool Equals(object other) public override bool Equals(object? other)
{ {
return false; return false;
} }
@ -30,25 +45,17 @@ public class MemoryRefResolver : SourceReferenceResolver
return 0; return 0;
} }
public override string NormalizePath(string path, string baseFilePath) private MemoryStream _fakeMS = new MemoryStream();
{
return path;
}
public override Stream OpenRead(string resolvedPath) public override string? NormalizePath( string? path, string? baseFilePath ) => path;
{
return null;
}
public override SourceText ReadText(string resolvedPath) public override Stream OpenRead( string resolvedPath ) => _fakeMS;
{
return null;
}
public override string ResolveReference(string path, string baseFilePath) private MemorySourceText _fakeMST = new();
{
return null; 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(); return safeStr.ToString();
} }
static public FileSystemWatcher s_watcher; static public FileSystemWatcher s_watcher = new();
static public Action<Assembly> s_fnAss = (ass) => { static public Action<Assembly> s_fnAss = (ass) => {
log.warn( $"Need to replace s_fnAss with custom function" ); 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 ) while( true )
{ {
try try
{ {
Process( e.FullPath ); Process( fse.FullPath );
return; return;
} }
catch( System.IO.IOException ex ) catch( System.IO.IOException ex )
{ {
log.debug( $"Ignoring {ex.Message}" );
} }
catch( Exception ex ) 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}" ); log.error( $"-> {ex.Message}" );
return; return;
} }
@ -316,10 +323,10 @@ public static class scr
{ {
if( t.GetGenericArguments().Length == 0 ) if( t.GetGenericArguments().Length == 0 )
{ {
return t.FullName.Replace( '+', '.' ); return t.FullName?.Replace( '+', '.' ) ?? "";
} }
var genArgs = t.GetGenericArguments(); var genArgs = t.GetGenericArguments();
var typeDef = t.FullName; var typeDef = t.FullName ?? "";
var indexOfTick = typeDef.IndexOf("`"); var indexOfTick = typeDef.IndexOf("`");