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.Reflection;
/*
TODO:
x)
*/
namespace lib
{
@ -83,47 +90,62 @@ namespace lib
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 )
{
Type[] types = new Type[0];
object[] parms = new object[0];
cfg = CreateTemplate( filename, t );
}
//types[ 0 ] = typeof( string );
//parms[ 0 ] = filename;
return cfg;
}
ConstructorInfo cons = t?.GetConstructor(types);
private static Config CreateTemplate( string filename, Type t )
{
Type[] types = new Type[0];
object[] parms = new object[0];
try
{
cfg = (Config)cons?.Invoke( parms );
}
catch( Exception e )
{
log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
}
//types[ 0 ] = typeof( string );
//parms[ 0 ] = filename;
Config cfg = null;
//cfg.SetFilename( filename );
ConstructorInfo cons = t?.GetConstructor(types);
if( s_cfg.writeOutTemplateFiles )
{
var templateFile = $"templates/{filename}";
try
{
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;
@ -146,7 +168,7 @@ namespace lib
}
#endregion
private string m_filename = "";
private string m_filename = "{unknown}";
public Config()
{

View File

@ -276,6 +276,7 @@ static public class log
writeToAll( evt );
}
// TODO PERF Replace this with a semaphore/mutex
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 )
{
try
@ -329,6 +361,7 @@ static public class log
{
char sym = getSymbol( evt.LogType );
var truncatedCat = evt.Cat.Substring( 0, Math.Min( 8, evt.Cat.Length ) );
string finalLine = string.Format( "{0,-8}{1}| {2}", truncatedCat, sym, evt.Msg );
@ -338,7 +371,10 @@ static public class log
s_writer.WriteLine( finalLine );
Console.WriteLine( finalLine );
setConsoleColor( evt );
Console.WriteLine( finalLine );
Console.ResetColor();
Debug.WriteLine( finalLine );

View File

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