sharplib/cfg/Config.cs
Marc Hernandez 9aeac39704 Title: Refactored logging system and updated Config.cs
x) Updated `Config.cs` to include a new instance of `XmlFormatter2Cfg` with specified data structure and POD.
x) Renamed the `destroy()` method in `Log.cs` to `shutdown()`.
x) Modified the startup process in `Log.cs` to check if it's already running, providing an info log if true.
x) Changed the encoding for StreamWriter in `startup()` method to UTF8 and added buffer size.
x) Commented out error stream related lines in Log.cs as they are not being used currently.
x) Added exception handling when processing message headers in log events.
x) Added checks for null or whitespace strings before writing them into files or console. Trimmed any null characters from lines before writing them into files.
x) Removed unnecessary white spaces and commented code blocks.
2024-08-04 01:30:23 -07:00

184 lines
3.1 KiB
C#

using System;
using System.IO;
using System.Xml;
using System.Reflection;
/*
TODO:
x)
*/
namespace lib
{
public class DescAttribute : Attribute
{
public string Desc { get; private set; }
public DescAttribute( string desc )
{
Desc = desc;
}
}
[Serializable]
public class ConfigCfg : Config
{
public readonly bool writeOutTemplateFiles = true;
}
public interface ConfigBase
{
}
public record class ConfigRec : ConfigBase
{
}
public class Config : ConfigBase
{
//private int _test = 0;
private static lib.XmlFormatter2Cfg s_templateCfg = new() {
datastructure = Datastructure.Tree,
POD = POD.Elements,
};
private static ConfigCfg s_cfg = new();
static public void startup( string filename )
{
res.Mgr.register( load );
res.Mgr.registerSub( typeof( ConfigBase ) );
s_cfg = load<ConfigCfg>( filename );
}
#region SaveLoad
static public T res_load<T>( string filename ) where T : Config
{
return load<T>( filename );
}
static public Config load( string filename )
{
return load( filename, null );
}
static public T load<T>( string filename ) where T : Config
{
return (T)load( filename, typeof( T ) );
}
static public Config load( string filename, Type t )
{
Config cfg = null;
try
{
if( File.Exists( filename ) )
{
FileStream fs = new FileStream( filename, FileMode.Open, FileAccess.Read );
XmlFormatter2 formatter = new XmlFormatter2();
cfg = (Config)( t != null ? formatter.DeserializeKnownType( fs, t ) : formatter.Deserialize( fs ) );
cfg.SetFilename( filename );
}
else
{
cfg = CreateTemplate( filename, t );
}
}
catch( IOException )
{
cfg = CreateTemplate( filename, t );
}
return cfg;
}
private static Config CreateTemplate( string filename, Type t )
{
Type[] types = new Type[0];
object[] parms = new object[0];
//types[ 0 ] = typeof( string );
//parms[ 0 ] = filename;
Config cfg = null;
ConstructorInfo cons = t?.GetConstructor( types );
try
{
cfg = (Config)cons?.Invoke( parms );
}
catch( Exception e )
{
log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
}
//cfg.SetFilename( filename );
if( s_cfg.writeOutTemplateFiles )
{
var templateFile = $"templates/{filename}";
var dirName = Path.GetDirectoryName( templateFile );
Util.checkAndAddDirectory( dirName );
log.info( $"Writing out template config of type {t?.Name} in {templateFile}" );
save( cfg, templateFile );
}
return cfg;
}
static public void save( Config cfg )
{
save( cfg, cfg._filename );
}
static public void save( Config cfg, String filename )
{
FileStream fs = new( filename, FileMode.Create, FileAccess.Write );
XmlFormatter2 formatter = new();
formatter.Serialize( fs, cfg );
fs.Close();
}
#endregion
private string _filename = "{unknown}";
public Config()
{
}
public Config( string filename )
{
_filename = filename;
}
public String Filename { get { return _filename; } }
protected void SetFilename( String filename ) { _filename = filename; }
}
}