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 )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
Log.error( "Caught exception {0}", e );
|
Log.error( $"Exception while creating config {t.ToString()}, Msg {e.Message}" );
|
||||||
}
|
}
|
||||||
|
|
||||||
cfg.SetFilename( filename );
|
cfg.SetFilename( filename );
|
||||||
|
|||||||
2
Conn.cs
2
Conn.cs
@ -81,7 +81,7 @@ public class Conn
|
|||||||
}
|
}
|
||||||
catch( Exception e )
|
catch( Exception e )
|
||||||
{
|
{
|
||||||
lib.Log.warn( "Exception sending obj {0} of {1}", obj, e );
|
lib.Log.warn( $"Exception sending obj {obj} of {e}" );
|
||||||
throw;
|
throw;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
187
Log.cs
187
Log.cs
@ -2,16 +2,77 @@ using System;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Diagnostics;
|
using System.Diagnostics;
|
||||||
using System.Collections;
|
using System.Collections;
|
||||||
|
using System.Runtime.CompilerServices;
|
||||||
//using System.Threading.Tasks;
|
//using System.Threading.Tasks;
|
||||||
|
|
||||||
namespace lib
|
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
|
public class Log : TraceListener
|
||||||
{
|
{
|
||||||
static public void create( String filename )
|
static public void create( string filename )
|
||||||
{
|
{
|
||||||
s_log = new Log( filename );
|
s_log = new Log( filename );
|
||||||
}
|
}
|
||||||
@ -20,7 +81,9 @@ namespace lib
|
|||||||
{
|
{
|
||||||
string msg = "==============================================================================\nLogfile shutdown at " + DateTime.Now.ToString();
|
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();
|
s_log.stop();
|
||||||
|
|
||||||
@ -28,7 +91,8 @@ namespace lib
|
|||||||
}
|
}
|
||||||
|
|
||||||
static private Log s_log;
|
static private Log s_log;
|
||||||
|
|
||||||
|
/*
|
||||||
static public Log log
|
static public Log log
|
||||||
{
|
{
|
||||||
get
|
get
|
||||||
@ -36,14 +100,51 @@ namespace lib
|
|||||||
return s_log;
|
return s_log;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
// Forwards.
|
// Forwards.
|
||||||
static public void error( String msg, params object[] args ) { lock( s_log ) { log.error_i( msg, args ); } }
|
static public void fatal( string msg, string cat = "unknown", object obj = null )
|
||||||
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 ); } }
|
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.
|
//TODO: Fix this so itll work without a directory.
|
||||||
Directory.CreateDirectory( Path.GetDirectoryName( filename ) );
|
Directory.CreateDirectory( Path.GetDirectoryName( filename ) );
|
||||||
@ -58,7 +159,9 @@ namespace lib
|
|||||||
|
|
||||||
string msg = "\n==============================================================================\nLogfile " + filename + " startup at " + DateTime.Now.ToString();
|
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 )
|
public override void Write( string msg )
|
||||||
@ -86,12 +189,13 @@ namespace lib
|
|||||||
m_delegates.Add( cb );
|
m_delegates.Add( cb );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
private void writeFileAndLine( StackTrace st )
|
private void writeFileAndLine( StackTrace st )
|
||||||
{
|
{
|
||||||
StackFrame frame = st.GetFrame( 1 );
|
StackFrame frame = st.GetFrame( 1 );
|
||||||
|
|
||||||
String srcFile = frame.GetFileName();
|
string srcFile = frame.GetFileName();
|
||||||
String srcLine = frame.GetFileLineNumber().ToString();
|
string srcLine = frame.GetFileLineNumber().Tostring();
|
||||||
|
|
||||||
Console.WriteLine( "{0} ({1}):", srcFile, srcLine );
|
Console.WriteLine( "{0} ({1}):", srcFile, srcLine );
|
||||||
}
|
}
|
||||||
@ -102,8 +206,8 @@ namespace lib
|
|||||||
{
|
{
|
||||||
StackFrame frame = st.GetFrame( i );
|
StackFrame frame = st.GetFrame( i );
|
||||||
|
|
||||||
String srcFile = frame.GetFileName();
|
string srcFile = frame.GetFileName();
|
||||||
String srcLine = frame.GetFileLineNumber().ToString();
|
string srcLine = frame.GetFileLineNumber().Tostring();
|
||||||
|
|
||||||
if( srcFile != null )
|
if( srcFile != null )
|
||||||
{
|
{
|
||||||
@ -111,28 +215,38 @@ namespace lib
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
private char getSymbol( String type )
|
private char getSymbol( LogType type )
|
||||||
{
|
{
|
||||||
if( type == "info" )
|
switch( type )
|
||||||
return ' ';
|
{
|
||||||
if( type == "warn" )
|
case LogType.Trace:
|
||||||
|
return '.';
|
||||||
|
case LogType.Debug:
|
||||||
return '-';
|
return '-';
|
||||||
if( type == "error" )
|
case LogType.Info:
|
||||||
|
return ' ';
|
||||||
|
case LogType.Warn:
|
||||||
|
return '+';
|
||||||
|
case LogType.Error:
|
||||||
return '*';
|
return '*';
|
||||||
|
case LogType.Fatal:
|
||||||
return '?';
|
return '*';
|
||||||
|
default: return '?';
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeToAll( String type, String cat, String msg )
|
private void writeToAll( LogEvent evt )
|
||||||
{
|
{
|
||||||
try
|
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.WriteLine( finalMsg );
|
||||||
//Console.Out.Write( finalMsg );
|
//Console.Out.Write( finalMsg );
|
||||||
@ -154,7 +268,7 @@ namespace lib
|
|||||||
{
|
{
|
||||||
//lock( cb )
|
//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( () => {
|
//var t = Task.Run( () => {
|
||||||
StackTrace st = new StackTrace( true );
|
StackTrace st = new StackTrace( true );
|
||||||
|
|
||||||
writeStack( st );
|
writeStack( st );
|
||||||
|
|
||||||
String msgPrint = msg;
|
string msgPrint = msg;
|
||||||
|
|
||||||
if( args.Length > 0 )
|
if( args.Length > 0 )
|
||||||
{
|
{
|
||||||
msgPrint = String.Format( msg, args );
|
msgPrint = string.Format( msg, args );
|
||||||
}
|
}
|
||||||
|
|
||||||
writeToAll( "error", "log", msgPrint );
|
writeToAll( "error", "log", msgPrint );
|
||||||
//} );
|
//} );
|
||||||
}
|
}
|
||||||
|
private void warn_i( string msg, object obj )
|
||||||
private void warn_i( String msg, params object[] args )
|
|
||||||
{
|
{
|
||||||
//var t = Task.Run( () => {
|
//var t = Task.Run( () => {
|
||||||
StackTrace st = new StackTrace( true );
|
StackTrace st = new StackTrace( true );
|
||||||
|
|
||||||
writeStack( st );
|
writeStack( st );
|
||||||
|
|
||||||
String msgPrint = msg;
|
string msgPrint = msg;
|
||||||
|
|
||||||
if( args.Length > 0 )
|
if( args.Length > 0 )
|
||||||
{
|
{
|
||||||
msgPrint = String.Format( msg, args );
|
msgPrint = string.Format( msg, args );
|
||||||
}
|
}
|
||||||
|
|
||||||
writeToAll( "warn", "log", msgPrint );
|
writeToAll( "warn", "log", msgPrint );
|
||||||
//});
|
//});
|
||||||
}
|
}
|
||||||
|
private void info_i( string msg, object obj )
|
||||||
private void info_i( String msg, params object[] args )
|
|
||||||
{
|
{
|
||||||
//var t = Task.Run( () => {
|
//var t = Task.Run( () => {
|
||||||
StackTrace st = new StackTrace( true );
|
StackTrace st = new StackTrace( true );
|
||||||
|
|
||||||
String msgPrint = msg;
|
string msgPrint = msg;
|
||||||
|
|
||||||
if( args.Length > 0 )
|
if( args.Length > 0 )
|
||||||
{
|
{
|
||||||
msgPrint = String.Format( msg, args );
|
msgPrint = string.Format( msg, args );
|
||||||
}
|
}
|
||||||
|
|
||||||
writeToAll( "info", "log", msgPrint );
|
writeToAll( "info", "log", msgPrint );
|
||||||
//} );
|
//} );
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
||||||
private Stream m_stream;
|
private Stream m_stream;
|
||||||
private StreamWriter m_writer;
|
private StreamWriter m_writer;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user