Added logger

This commit is contained in:
Simon Lübeß
2020-11-05 22:20:09 +01:00
parent bf796a2a25
commit 0cf3f5f572
12 changed files with 321 additions and 8 deletions
+1 -7
View File
@@ -1,11 +1,5 @@
FileVersion = 1
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}}
Projects = {Sandbox = {Path = "Sandbox"}, GlitchyEngine = {Path = "GlitchyEngine"}, GlitchLog = {Path = "GlitchLog"}}
[Workspace]
StartupProject = "Sandbox"
[Configs.Test.Win32]
ConfigSelections = {corlib = {Config = "Debug"}}
[Configs.Test.Win64]
ConfigSelections = {corlib = {Config = "Debug"}}
+4
View File
@@ -0,0 +1,4 @@
FileVersion = 1
[Project]
Name = "GlitchLog"
+123
View File
@@ -0,0 +1,123 @@
using System;
using System.Diagnostics;
using internal GlitchLog;
namespace GlitchLog
{
public abstract class Logger
{
protected LogLevel _logLevel;
public LogLevel Level
{
get => _logLevel;
set => _logLevel = value;
}
public abstract String Name {get; set;}
public abstract void Trace(StringView format, params Object[] args);
public abstract void Info(StringView format, params Object[] args);
public abstract void Warning(StringView format, params Object[] args);
public abstract void Error(StringView format, params Object[] args);
public abstract void Critical(StringView format, params Object[] args);
public abstract void Log(LogLevel level, StringView format, params Object[] args);
}
public class DebugLogger : Logger
{
// {l} = log level (first parameter)
// {t} = current date time (second parameter)
// {n} = logger name (third parameter)
// {m} = message
private String _name;
public override String Name
{
get => _name;
set => _name = value;
}
public this()
{
Runtime.Assert(Debug.IsDebuggerPresent, "The DebugLogger requires a debugger to be present.");
}
#if GL_NOLOG || GL_NOTRACE
[SkipCall]
#endif
[Inline]
public override void Trace(StringView format, params Object[] args)
{
InternalLog(.Trace, format, params args);
}
#if GL_NOLOG || GL_NOINFO
[SkipCall]
#endif
[Inline]
public override void Info(StringView format, params Object[] args)
{
InternalLog(.Info, format, params args);
}
#if GL_NOLOG || GL_NOWARNING
[SkipCall]
#endif
[Inline]
public override void Warning(StringView format, params Object[] args)
{
InternalLog(.Warning, format, params args);
}
#if GL_NOLOG || GL_NOERROR
[SkipCall]
#endif
[Inline]
public override void Error(StringView format, params Object[] args)
{
InternalLog(.Error, format, params args);
}
#if GL_NOLOG || GL_NOCRITICAL
[SkipCall]
#endif
[Inline]
public override void Critical(StringView format, params Object[] args)
{
InternalLog(.Critical, format, params args);
}
#if GL_NOLOG
[SkipCall]
#endif
[Inline]
public override void Log(LogLevel level, StringView format, params Object[] args)
{
InternalLog(level, format, params args);
}
private void InternalLog(LogLevel level, StringView format, params Object[] args)
{
if(_logLevel > level)
return;
String msg = scope String(4096);
if(GlitchLog._preMsgFormat.Ptr != null)
msg.AppendF(GlitchLog._preMsgFormat, level.UpperString, DateTime.Now, _name);
msg.AppendF(format, params args);
if(GlitchLog._postMsgFormat.Ptr != null)
msg.AppendF(GlitchLog._postMsgFormat, level.UpperString, DateTime.Now, _name);
msg.Append('\n');
Debug.[Friend]Write(msg.Ptr, msg.Length);
}
}
}
+43
View File
@@ -0,0 +1,43 @@
using System;
using System.IO;
using System.Text;
namespace GlitchLog
{
public class FileLogger : ILogger
{
StreamWriter _sw ~ delete _;
[AllowAppend]
public this(String fileName)
{
var sw = append StreamWriter();
_sw = sw;
_sw.Create(fileName);
}
public void Trace(StringView format, params Object[] args)
{
_sw.Write("TRACE: ");
_sw.WriteLine(format, params args);
}
public void Info(StringView format, params Object[] args)
{
_sw.Write("INFO: ");
_sw.WriteLine(format, params args);
}
public void Warning(StringView format, params Object[] args)
{
_sw.Write("WARNING: ");
_sw.WriteLine(format, params args);
}
public void Error(StringView format, params Object[] args)
{
_sw.Write("ERROR: ");
_sw.WriteLine(format, params args);
}
}
}
+65
View File
@@ -0,0 +1,65 @@
using System;
using System.Text;
using System.Diagnostics;
namespace GlitchLog
{
public static class GlitchLog
{
private static String _format;
internal static String _preparedFormat ~ delete _;
internal static StringView _preMsgFormat;
internal static StringView _postMsgFormat;
public static String Format
{
get => _format;
set
{
_format = value;
PrepareFormat();
}
}
static void PrepareFormat()
{
delete _preparedFormat;
// Length = 0 -> no format -> only print msg
if(_format.Length == 0)
{
_preMsgFormat.Ptr = null;
_postMsgFormat.Ptr = null;
}
_preparedFormat = new .(_format);
// Todo: this is utter garbage
_preparedFormat.Replace("{l", "{0");
_preparedFormat.Replace("{t", "{1");
_preparedFormat.Replace("{n", "{2");
int index = _preparedFormat.IndexOf("{m}");
// {m} not in format-string -> format is pre msg
if(index == -1)
{
_preMsgFormat = _preparedFormat;
_postMsgFormat.Ptr = null;
}
else
{
if(index == 0)
_preMsgFormat.Ptr = null;
else
_preMsgFormat = .(_preparedFormat, 0, index);
if(index == _preparedFormat.Length - 3)
_postMsgFormat.Ptr = null;
else
_postMsgFormat = .(_preparedFormat, index + 3, _preparedFormat.Length - index - 3);
}
}
}
}
+12
View File
@@ -0,0 +1,12 @@
using System;
namespace GlitchLog
{
public interface ILogger
{
void Trace(StringView format, params Object[] args);
void Info(StringView format, params Object[] args);
void Warning(StringView format, params Object[] args);
void Error(StringView format, params Object[] args);
}
}
+24
View File
@@ -0,0 +1,24 @@
using System;
namespace GlitchLog
{
/**
* Defines the severity level of a log message.
* @remarks also used to determine which levels will be logged.
*/
public enum LogLevel
{
case Trace = 0;
case Debug;
case Info;
case Warning;
case Error;
case Critical;
case Off;
public static readonly StringView[?] UpperStringViews = .("TRACE", "DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL", "OFF");
[Inline]
public StringView UpperString => UpperStringViews[this.Underlying];
}
}
+1
View File
@@ -1,4 +1,5 @@
FileVersion = 1
Dependencies = {GlitchLog = "*", corlib = "*"}
[Project]
Name = "GlitchyEngine"
+29
View File
@@ -0,0 +1,29 @@
using System;
using GlitchLog;
namespace GlitchyEngine
{
public class Log
{
static Logger _engineLogger ~ delete _;
static Logger _clientLogger ~ delete _;
[Inline]
public static Logger EngineLogger => _engineLogger;
[Inline]
public static Logger ClientLogger => _clientLogger;
static this()
{
GlitchLog.Format = "[{t:HH:mm:ss.fff}] ({n})|{l}: {m}";
_engineLogger = new DebugLogger();
_engineLogger.Name = "ENGINE";
_engineLogger.Level = .Trace;
_clientLogger = new DebugLogger();
_clientLogger.Name = "APP";
_clientLogger.Level = .Trace;
}
}
}
+14
View File
@@ -1,4 +1,6 @@
using System;
using GlitchLog;
using System.Diagnostics;
namespace GlitchyEngine
{
@@ -9,12 +11,24 @@ namespace GlitchyEngine
public static int Main(String[] args)
{
Log.EngineLogger.Info("Initializing Application...");
Stopwatch initWatch = .StartNew();
var app = CreateApplication();
initWatch.Stop();
Log.EngineLogger.Info("Application initialized ({}ms).", initWatch.ElapsedMilliseconds);
app.Run();
Log.EngineLogger.Info("Uninitializing Application...");
delete app;
Log.EngineLogger.Info("Application uninitialized.");
return 0;
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
FileVersion = 1
Dependencies = {corlib = "*", GlitchyEngine = "*"}
Dependencies = {GlitchyEngine = "*", corlib = "*"}
[Project]
Name = "Sandbox"
+4
View File
@@ -1,5 +1,7 @@
using System;
using GlitchyEngine;
using System.Diagnostics;
using GlitchLog;
namespace Sandbox
{
@@ -8,6 +10,8 @@ namespace Sandbox
[Export, LinkName("CreateApplication")]
public static Application CreateApplication()
{
return new SandboxApp();
}
}