Make logging more like all other logging frameworks
This commit is contained in:
parent
1bd126d578
commit
880b23db4c
@ -122,7 +122,7 @@ public class Config
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
Log.error( "Caught exception {0}", e );
|
||||
Log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
|
||||
}
|
||||
|
||||
cfg.SetFilename( filename );
|
||||
|
||||
2
Conn.cs
2
Conn.cs
@ -81,7 +81,7 @@ public class Conn
|
||||
}
|
||||
catch( Exception e )
|
||||
{
|
||||
lib.Log.warn( "Exception sending obj {0} of {1}", obj, e );
|
||||
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
185
Log.cs
185
Log.cs
@ -2,16 +2,77 @@ using System;
|
||||
using System.IO;
|
||||
using System.Diagnostics;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
//using System.Threading.Tasks;
|
||||
|
||||
namespace lib
|
||||
{
|
||||
|
||||
public delegate void Log_delegate( String type, String cat, String msg );
|
||||
|
||||
|
||||
[Flags]
|
||||
public enum LogTypeNew
|
||||
{
|
||||
Invalid = 0,
|
||||
|
||||
// Frequency
|
||||
FrequencyBase = 1,
|
||||
FrequencyBits = 2,
|
||||
FrequencyMask = ( ( 1 << FrequencyBits ) - 1 ) << FrequencyBase,
|
||||
|
||||
Detail = 0b01 << FrequencyBase,
|
||||
Normal = 0b10 << FrequencyBase,
|
||||
Overview = 0b11 << FrequencyBase,
|
||||
|
||||
// Type
|
||||
TypeBase = FrequencyBase + FrequencyBits,
|
||||
TypeBits = 3,
|
||||
TypeMask = ( ( 1 << TypeBits ) - 1 ) << TypeBase,
|
||||
|
||||
Startup = 0b001 << TypeBase,
|
||||
Running = 0b010 << TypeBase,
|
||||
Shutdown= 0b011 << TypeBase,
|
||||
Error = 0b101 << TypeBase,
|
||||
|
||||
}
|
||||
|
||||
|
||||
[Flags]
|
||||
public enum LogType
|
||||
{
|
||||
Invalid = 0,
|
||||
Trace = 1,
|
||||
Debug = 2,
|
||||
Info = 3,
|
||||
Warn = 4,
|
||||
Error = 5,
|
||||
Fatal = 6,
|
||||
}
|
||||
|
||||
public struct LogEvent
|
||||
{
|
||||
public DateTime Time;
|
||||
public LogType LogType;
|
||||
public string Cat;
|
||||
public string Msg;
|
||||
public object Obj;
|
||||
|
||||
public LogEvent( LogType logType, string cat, string msg, object obj )
|
||||
{
|
||||
Time = DateTime.Now;
|
||||
LogType = logType;
|
||||
Cat = cat;
|
||||
Msg = msg;
|
||||
Obj = obj;
|
||||
}
|
||||
}
|
||||
|
||||
public delegate void Log_delegate( LogEvent evt );
|
||||
|
||||
|
||||
public class Log : TraceListener
|
||||
{
|
||||
static public void create( String filename )
|
||||
static public void create( string filename )
|
||||
{
|
||||
s_log = new Log( filename );
|
||||
}
|
||||
@ -20,7 +81,9 @@ namespace lib
|
||||
{
|
||||
string msg = "==============================================================================\nLogfile shutdown at " + DateTime.Now.ToString();
|
||||
|
||||
s_log.writeToAll( "info", "log", msg );
|
||||
var evt = new LogEvent( LogType.Info, "System", msg, null );
|
||||
|
||||
s_log.writeToAll( evt );
|
||||
|
||||
s_log.stop();
|
||||
|
||||
@ -29,6 +92,7 @@ namespace lib
|
||||
|
||||
static private Log s_log;
|
||||
|
||||
/*
|
||||
static public Log log
|
||||
{
|
||||
get
|
||||
@ -36,14 +100,51 @@ namespace lib
|
||||
return s_log;
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
// Forwards.
|
||||
static public void error( String msg, params object[] args ) { lock( s_log ) { log.error_i( msg, args ); } }
|
||||
static public void warn( String msg, params object[] args ) { lock( s_log ) { log.warn_i( msg, args ); } }
|
||||
static public void info( String msg, params object[] args ) { lock( s_log ) { log.info_i( msg, args ); } }
|
||||
static public void fatal( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log(msg, LogType.Fatal, cat, obj);
|
||||
}
|
||||
|
||||
static public void error( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log(msg, LogType.Error, cat, obj);
|
||||
}
|
||||
|
||||
static public void warn( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log( msg, LogType.Warn, cat, obj );
|
||||
}
|
||||
|
||||
static public void info( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log(msg, LogType.Info, cat, obj);
|
||||
}
|
||||
|
||||
static public void debug( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log(msg, LogType.Debug, cat, obj);
|
||||
}
|
||||
|
||||
static public void trace( string msg, string cat = "unknown", object obj = null )
|
||||
{
|
||||
log(msg, LogType.Trace, cat, obj);
|
||||
}
|
||||
|
||||
static public void log( string msg, LogType type = LogType.Debug, string cat = "unknown", object obj = null )
|
||||
{
|
||||
lock(s_log)
|
||||
{
|
||||
var evt = new LogEvent( type, cat, msg, obj );
|
||||
|
||||
s_log.writeToAll( evt );
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private Log( String filename )
|
||||
private Log( string filename )
|
||||
{
|
||||
//TODO: Fix this so itll work without a directory.
|
||||
Directory.CreateDirectory( Path.GetDirectoryName( filename ) );
|
||||
@ -58,7 +159,9 @@ namespace lib
|
||||
|
||||
string msg = "\n==============================================================================\nLogfile " + filename + " startup at " + DateTime.Now.ToString();
|
||||
|
||||
writeToAll( "info", "log", msg );
|
||||
var evt = new LogEvent( LogType.Info, "System", msg, null );
|
||||
|
||||
writeToAll( evt );
|
||||
}
|
||||
|
||||
public override void Write( string msg )
|
||||
@ -86,12 +189,13 @@ namespace lib
|
||||
m_delegates.Add( cb );
|
||||
}
|
||||
|
||||
/*
|
||||
private void writeFileAndLine( StackTrace st )
|
||||
{
|
||||
StackFrame frame = st.GetFrame( 1 );
|
||||
|
||||
String srcFile = frame.GetFileName();
|
||||
String srcLine = frame.GetFileLineNumber().ToString();
|
||||
string srcFile = frame.GetFileName();
|
||||
string srcLine = frame.GetFileLineNumber().Tostring();
|
||||
|
||||
Console.WriteLine( "{0} ({1}):", srcFile, srcLine );
|
||||
}
|
||||
@ -102,8 +206,8 @@ namespace lib
|
||||
{
|
||||
StackFrame frame = st.GetFrame( i );
|
||||
|
||||
String srcFile = frame.GetFileName();
|
||||
String srcLine = frame.GetFileLineNumber().ToString();
|
||||
string srcFile = frame.GetFileName();
|
||||
string srcLine = frame.GetFileLineNumber().Tostring();
|
||||
|
||||
if( srcFile != null )
|
||||
{
|
||||
@ -111,28 +215,38 @@ namespace lib
|
||||
}
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
private char getSymbol( String type )
|
||||
private char getSymbol( LogType type )
|
||||
{
|
||||
if( type == "info" )
|
||||
return ' ';
|
||||
if( type == "warn" )
|
||||
switch( type )
|
||||
{
|
||||
case LogType.Trace:
|
||||
return '.';
|
||||
case LogType.Debug:
|
||||
return '-';
|
||||
if( type == "error" )
|
||||
case LogType.Info:
|
||||
return ' ';
|
||||
case LogType.Warn:
|
||||
return '+';
|
||||
case LogType.Error:
|
||||
return '*';
|
||||
|
||||
return '?';
|
||||
case LogType.Fatal:
|
||||
return '*';
|
||||
default: return '?';
|
||||
}
|
||||
}
|
||||
|
||||
private void writeToAll( String type, String cat, String msg )
|
||||
private void writeToAll( LogEvent evt )
|
||||
{
|
||||
try
|
||||
{
|
||||
lock( this )
|
||||
// _SHOULDNT_ need this since we lock at the top.
|
||||
//lock( this )
|
||||
{
|
||||
char sym = getSymbol( type );
|
||||
char sym = getSymbol( evt.LogType );
|
||||
|
||||
String finalMsg = String.Format( "{0,-10}{1}| {2}", type, sym, msg );
|
||||
string finalMsg = string.Format( "{0,-6}{1}| {2}", evt.Cat, sym, evt.Msg );
|
||||
|
||||
//Console.WriteLine( finalMsg );
|
||||
//Console.Out.Write( finalMsg );
|
||||
@ -154,7 +268,7 @@ namespace lib
|
||||
{
|
||||
//lock( cb )
|
||||
{
|
||||
cb( type, cat, msg );
|
||||
cb( evt );
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -166,58 +280,57 @@ namespace lib
|
||||
}
|
||||
}
|
||||
|
||||
private void error_i( String msg, params object[] args )
|
||||
/*
|
||||
private void error_i( string msg, object obj )
|
||||
{
|
||||
//var t = Task.Run( () => {
|
||||
StackTrace st = new StackTrace( true );
|
||||
|
||||
writeStack( st );
|
||||
|
||||
String msgPrint = msg;
|
||||
string msgPrint = msg;
|
||||
|
||||
if( args.Length > 0 )
|
||||
{
|
||||
msgPrint = String.Format( msg, args );
|
||||
msgPrint = string.Format( msg, args );
|
||||
}
|
||||
|
||||
writeToAll( "error", "log", msgPrint );
|
||||
//} );
|
||||
}
|
||||
|
||||
private void warn_i( String msg, params object[] args )
|
||||
private void warn_i( string msg, object obj )
|
||||
{
|
||||
//var t = Task.Run( () => {
|
||||
StackTrace st = new StackTrace( true );
|
||||
|
||||
writeStack( st );
|
||||
|
||||
String msgPrint = msg;
|
||||
string msgPrint = msg;
|
||||
|
||||
if( args.Length > 0 )
|
||||
{
|
||||
msgPrint = String.Format( msg, args );
|
||||
msgPrint = string.Format( msg, args );
|
||||
}
|
||||
|
||||
writeToAll( "warn", "log", msgPrint );
|
||||
//});
|
||||
}
|
||||
|
||||
private void info_i( String msg, params object[] args )
|
||||
private void info_i( string msg, object obj )
|
||||
{
|
||||
//var t = Task.Run( () => {
|
||||
StackTrace st = new StackTrace( true );
|
||||
|
||||
String msgPrint = msg;
|
||||
string msgPrint = msg;
|
||||
|
||||
if( args.Length > 0 )
|
||||
{
|
||||
msgPrint = String.Format( msg, args );
|
||||
msgPrint = string.Format( msg, args );
|
||||
}
|
||||
|
||||
writeToAll( "info", "log", msgPrint );
|
||||
//} );
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
private Stream m_stream;
|
||||
private StreamWriter m_writer;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user