This commit is contained in:
Marc Hernandez 2022-02-26 21:30:00 -08:00
parent e7f284052e
commit 115ab629dc
3 changed files with 87 additions and 29 deletions

View File

@ -3,6 +3,13 @@ using System.IO;
using System.Xml; using System.Xml;
using System.Reflection; using System.Reflection;
/*
TODO:
x)
*/
namespace lib namespace lib
{ {
@ -83,47 +90,62 @@ namespace lib
try try
{ {
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read); if( File.Exists( filename ) )
{
FileStream fs = new FileStream(filename, FileMode.Open, FileAccess.Read);
XmlFormatter2 formatter = new XmlFormatter2(); XmlFormatter2 formatter = new XmlFormatter2();
cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) ); cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) );
cfg.SetFilename( filename ); cfg.SetFilename( filename );
}
else
{
cfg = CreateTemplate( filename, t );
}
} }
catch( IOException ) catch( IOException )
{ {
Type[] types = new Type[0]; cfg = CreateTemplate( filename, t );
object[] parms = new object[0]; }
//types[ 0 ] = typeof( string ); return cfg;
//parms[ 0 ] = filename; }
ConstructorInfo cons = t?.GetConstructor(types); private static Config CreateTemplate( string filename, Type t )
{
Type[] types = new Type[0];
object[] parms = new object[0];
try //types[ 0 ] = typeof( string );
{ //parms[ 0 ] = filename;
cfg = (Config)cons?.Invoke( parms ); Config cfg = null;
}
catch( Exception e )
{
log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
}
//cfg.SetFilename( filename ); ConstructorInfo cons = t?.GetConstructor(types);
if( s_cfg.writeOutTemplateFiles ) try
{ {
var templateFile = $"templates/{filename}"; cfg = (Config)cons?.Invoke( parms );
}
catch( Exception e )
{
log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
}
var dirName = Path.GetDirectoryName(templateFile); //cfg.SetFilename( filename );
lib.Util.checkAndAddDirectory( dirName ); if( s_cfg.writeOutTemplateFiles )
{
var templateFile = $"templates/{filename}";
log.info( $"Writing out template config of type {t?.Name} in {templateFile}" ); var dirName = Path.GetDirectoryName(templateFile);
Config.save( cfg, templateFile ); lib.Util.checkAndAddDirectory( dirName );
}
log.info( $"Writing out template config of type {t?.Name} in {templateFile}" );
Config.save( cfg, templateFile );
} }
return cfg; return cfg;
@ -146,7 +168,7 @@ namespace lib
} }
#endregion #endregion
private string m_filename = ""; private string m_filename = "{unknown}";
public Config() public Config()
{ {

View File

@ -276,6 +276,7 @@ static public class log
writeToAll( evt ); writeToAll( evt );
} }
// TODO PERF Replace this with a semaphore/mutex
Thread.Sleep( 0 ); Thread.Sleep( 0 );
} }
} }
@ -320,6 +321,37 @@ static public class log
} }
} }
private static void setConsoleColor( log.LogEvent evt )
{
switch( evt.LogType )
{
case log.LogType.Trace:
Console.ForegroundColor = ConsoleColor.DarkGray;
break;
case log.LogType.Debug:
Console.ForegroundColor = ConsoleColor.Gray;
break;
case log.LogType.Info:
Console.ForegroundColor = ConsoleColor.DarkGreen;
break;
case log.LogType.High:
Console.ForegroundColor = ConsoleColor.Cyan;
break;
case log.LogType.Warn:
Console.ForegroundColor = ConsoleColor.Yellow;
break;
case log.LogType.Error:
Console.ForegroundColor = ConsoleColor.DarkRed;
Console.BackgroundColor = ConsoleColor.DarkGray;
break;
case log.LogType.Fatal:
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.DarkGray;
break;
}
}
static private void writeToAll( LogEvent evt ) static private void writeToAll( LogEvent evt )
{ {
try try
@ -329,6 +361,7 @@ static public class log
{ {
char sym = getSymbol( evt.LogType ); char sym = getSymbol( evt.LogType );
var truncatedCat = evt.Cat.Substring( 0, Math.Min( 8, evt.Cat.Length ) ); var truncatedCat = evt.Cat.Substring( 0, Math.Min( 8, evt.Cat.Length ) );
string finalLine = string.Format( "{0,-8}{1}| {2}", truncatedCat, sym, evt.Msg ); string finalLine = string.Format( "{0,-8}{1}| {2}", truncatedCat, sym, evt.Msg );
@ -338,7 +371,10 @@ static public class log
s_writer.WriteLine( finalLine ); s_writer.WriteLine( finalLine );
Console.WriteLine( finalLine ); setConsoleColor( evt );
Console.WriteLine( finalLine );
Console.ResetColor();
Debug.WriteLine( finalLine ); Debug.WriteLine( finalLine );

View File

@ -58,7 +58,7 @@ namespace res
//For serialization //For serialization
public Ref() public Ref()
: :
base( "<unknown>" ) base( "{unknown}" )
{ {
} }