new version

This commit is contained in:
Booklordofthedings 2024-07-25 16:28:45 +02:00
parent e204e5538d
commit 772c7389a8
23 changed files with 576 additions and 430 deletions

View file

@ -4,3 +4,6 @@ Projects = {LybLog = {Path = "."}, Tests = {Path = "Tests"}}
[Workspace]
StartupProject = "Tests"
PreprocessorMacros = ["LOG_TRACE"]
[[Workspace.DistinctOptions]]
Filter = ""

View file

@ -37,6 +37,8 @@ class Program
6. Go to Properties -> Dependencies and check the box that says LybLog
7. In the file you want to use the logger add a using LybLog statement or simply say LybLog.Log.Methodname(Message). You can now use LybLog in your project
- Alternativly you can also just copy the single sourcefile into your project, since its
### Basics
- LybLog is a singleton and has somewhat resonable default settings, so for its default usage you can simply just call the basic functions and pass in your message and it will work fine for you. The basic functions provided are: Trace, Debug, Info, Warn, Error and Fatal
- Log levels or log severity are the importance of what is being logged. LybLog provides 6 common log levels, which are as follows.

92
Tests/.log Normal file
View file

@ -0,0 +1,92 @@
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:21 PM][Trace]: Tracey
[7/25/2024 3:37:21 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:22 PM][Trace]: Tracey
[7/25/2024 3:37:22 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:23 PM][Trace]: Tracey
[7/25/2024 3:37:23 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:24 PM][Trace]: Tracey
[7/25/2024 3:37:24 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:25 PM][Trace]: Tracey
[7/25/2024 3:37:25 PM][Debug]: Lets debug something
[7/25/2024 3:37:26 PM][Trace]: Tracey
[7/25/2024 3:37:26 PM][Debug]: Lets debug something
[7/25/2024 3:37:26 PM][Trace]: Tracey
[7/25/2024 3:37:26 PM][Debug]: Lets debug something

View file

@ -4,3 +4,4 @@ Dependencies = {corlib = "*", LybLog = "*"}
[Project]
Name = "Tests"
StartupObject = "Tests.Program"
ProcessorMacros = ["ENABLE_LYBLOG"]

View file

@ -1,18 +0,0 @@
/*
Feature List:
-Output to Console
-Output
-Custom Formatting
-Custom Coloring
-Output to Ide
-Output to Callbacks
-Output to files
-Cache in cache before outputting
-Write to cache when the app closes
-Write to cache on fatal
-Flush the cache manually
*/

View file

@ -1,94 +1,27 @@
namespace Tests;
using System;
using System.Threading;
using LybLog;
class Program
{
public static Monitor MainEnd = new .() ~ delete _;
public static void Main()
{
Console.WriteLine("First step of testing. All basic logging functions");
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
Console.WriteLine("Second step of testing. Logging to the ide");
Log.Settings.DoConsoleLog = false;
Log.Settings.DoIdeLog = true;
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
Console.WriteLine("Third step of testing. Logging to a callback");
Log.Settings.DoConsoleLog = false;
Log.Settings.DoIdeLog = false;
Log.Settings.DoCallbackLog = true;
Log.Settings.Callbacks.Add(new => CallbackTest);
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
Console.WriteLine("Fourth step of testing. log levels. The followign logs should only show warn or higher");
Log.Settings.DoCallbackLog = false;
Log.Settings.DoConsoleLog = true;
Log.Settings.LogLevel = .Warn;
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
Console.WriteLine("Fith step of testing. Logging to a File");
Log.Settings.DoConsoleLog = false;
Log.Settings.LogLevel = .Trace;
Log.Settings.DoFileLog = true;
Log.Settings.LogFilePath = "test.log";
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
MainEnd.Enter();
Console.WriteLine("Sixth step of testing. Cleaning file upon initialization");
Console.WriteLine("The test.log file should now be cleared");
Log.Settings.ClearLogFileOnLoad = true;
Console.ReadLine(.. scope .());
Console.WriteLine("Seventh step of testing. File logger caching");
Console.WriteLine("The file should still have no contents, since it waits for the cache to be fille");
Log.Settings.CacheSize = 3;
Log.Settings.DoConsoleLog = true;
Log.Trace("This is a trace log");
Log.Debug("This is a debug log");
Log.Info("This is a info log");
Console.WriteLine("It has been logged but no contents are in the file");
Console.ReadLine(.. scope .());
Log.Warn("This is a warn log");
Log.Error("This is a error log");
Console.WriteLine("Warn should be logged but error shouldnt due to cache sizes");
Console.WriteLine("Once fatal is logged it should be though, since fatal forces a cache empty");
Console.ReadLine(.. scope .());
Log.Fatal("This is a fatal log");
Console.ReadLine(.. scope .());
}
public static void CallbackTest(LogLevel p, String pMessage)
{
Console.WriteLine(scope $"{p.ToString(.. scope .())} This is a callback: {pMessage}");
var t = new Thread(new () => {
while(!MainEnd.TryEnter())
{
Log.Trace("Tracey");
Log.Debug("Lets debug something");
Thread.Sleep(100);
}
});
t.Start();
Console.Read();
MainEnd.Exit();
t.Join();
}
}

View file

@ -1,6 +0,0 @@
[12/19/2022 10:46:54 AM]:[TRACE]:This is a trace log
[12/19/2022 10:46:54 AM]:[DEBUG3]:This is a debug log
[12/19/2022 10:46:54 AM]:[INFO]:This is a info log
[12/19/2022 10:46:57 AM]:[WARN]:This is a warn log
[12/19/2022 10:46:57 AM]:[ERROR]:This is a error log
[12/19/2022 10:46:57 AM]:[FATAL]:This is a fatal log

View file

@ -1,14 +0,0 @@
namespace LybLog;
/*
This file creates a global acessor for the logger class,
Allowing the user to do Log.Warn and not having to do LybLog.Log.Warn
*/
public static
{
public static LybLog Log
{
get { return LybLog.Log; }
private set; //Its a singleton so we dont rly want people to set it
}
}

View file

@ -1,29 +1,482 @@
/*
Lyblog
A threadsafe logging library by Booklordofthedings
This library uses locks to provide a threadsafe way to log stuff
It has a build in static field called Log that can be used for logging,
or you can instantiate your own LybLog object and use that for logging.
Different instances are handled differently from each other and as such can also
have different settings.
Settings themselves are stored in the settting field
and mostly just do what they say.
Logging can be done with either the Log function
or their corresponding LogLevel function.
If you add a LYBLOG_[LogLevel] to the preprocessor you can also exclude specific log levels entirely
at compile time, to speed things up.
There are also channels available that you can log to and activate or not activate.
The channels are an optional input into the logging functions, and you can log to multiple ones by seperating them with a '/'
*/
namespace LybLog;
using System;
using System.IO;
using System.Threading;
using System.Collections;
static
{
public static LybLog Log = new .() ~ delete _;
}
class LybLog
{
private static LybLog _Object ~ delete _; //Singleton instance goes here
public LybLogSettings Settings {get; private set;} = new .() ~ delete _; //Storing the settings in its own class makes the base cleaner
private List<String> _Cache = new .() ~ FlushAndDeleteContainerAndItems!(_); //Allows file writes to be done in batches
private Monitor _lock = new .() ~ delete _;
//Singleton logic
public static LybLog Log
private LogSetting _settings = new .() ~ delete _;
public LogSetting Settings
{
get
{
if(_Object == null)
_Object = new .();
return _Object;
_lock.Enter();
defer _lock.Exit();
return _settings;
}
set
{
_lock.Enter();
_settings = value;
_lock.Exit();
}
private set {}
}
private this() {} //Private constructor so no one else can create this object
private String _buffer = new .(Settings.BufferSize) ~ delete _;
public String Buffer
{
get
{
_lock.Enter();
defer _lock.Exit();
return _buffer;
}
}
[Inline]
public void Log(StringView pMessage, LogLevel pLogLevel, StringView pChannel = "Default")
{
using(_lock.Enter())
{
if(Settings.LogLevel.Underlying > pLogLevel.Underlying)
return;
bool correctChannel = false;
for(var i in pChannel.Split('/'))
if(Settings.EnabledChannels.ContainsAlt<StringView>(i))
correctChannel = true;
if(!correctChannel)
return;
if(Settings.DoConsoleLog)
{
if(Settings.DoConsoleColors)
{
switch(pLogLevel)
{
case .Trace:
Console.ForegroundColor = Settings.TraceColor;
case .Debug:
Console.ForegroundColor = Settings.DebugColor;
case .Info:
Console.ForegroundColor = Settings.InfoColor;
case .Warning:
Console.ForegroundColor = Settings.WarningColor;
case .Fatal:
Console.ForegroundColor = Settings.FatalColor;
}
}
Console.WriteLine(Settings.MessageFormatter.Invoke(.. scope .(), pMessage, pLogLevel));
if(Settings.DoConsoleColors)
Console.ResetColor();
}
if(Settings.DoFileLog)
{
Buffer.Append(scope $"{Settings.MessageFormatter.Invoke(.. scope .(), pMessage, pLogLevel)}\n");
if(Buffer.Length > Settings.BufferSize)
{
StringView outPath = Settings.FilePath;
if(Settings.DoCustomFileLog)
outPath = Settings.CustomFileCallback.Invoke(.. scope .());
File.WriteAllText(outPath, Buffer)
.IgnoreError();
Buffer.Clear();
}
}
if(Settings.DoCallbackLog)
{
Settings.[Friend]_lock.Enter();
for(var i in Settings.[Friend]_callbacks)
i.value.Invoke(pMessage, pLogLevel, pChannel);
Settings.[Friend]_lock.Exit();
}
}
}
#if LYBLOG_FATAL || LYBLOG_WARNING || LYBLOG_INFO || LYBLOG_DEBUG || LYBLOG_TRACE
[SkipCall]
#endif
public void Trace(StringView pMessage, StringView pChannel = "Default") => Log(pMessage, .Trace, pChannel);
#if LYBLOG_FATAL || LYBLOG_WARNING || LYBLOG_INFO || LYBLOG_DEBUG
[SkipCall]
#endif
public void Debug(StringView pMessage, StringView pChannel = "Default") => Log(pMessage, .Debug, pChannel);
#if LYBLOG_FATAL || LYBLOG_WARNING || LYBLOG_INFO
[SkipCall]
#endif
public void Info(StringView pMessage, StringView pChannel = "Default") => Log(pMessage, .Info, pChannel);
#if LYBLOG_FATAL || LYBLOG_WARNING
[SkipCall]
#endif
public void Warning(StringView pMessage, StringView pChannel = "Default") => Log(pMessage, .Warning, pChannel);
#if LYBLOG_FATAL
[SkipCall]
#endif
public void Fatal(StringView pMessage, StringView pChannel = "Default") => Log(pMessage, .Fatal, pChannel);
}
class LogSetting
{
private Monitor _lock = new .() ~ delete _;
private uint64 _callbackId = 0;
private Dictionary<uint64, delegate void(StringView, LogLevel, StringView)> _callbacks = new .(10) ~ DeleteDictionaryAndValues!(_);
public uint64 AddCallback(delegate void(StringView, LogLevel, StringView) pCallback)
{
_lock.Enter();
defer _lock.Exit();
_callbacks.Add(_callbackId,pCallback);
_callbackId++;
return _callbackId-1;
}
public Result<void> RemoveCallback(uint64 pId)
{
_lock.Enter();
defer _lock.Exit();
if(_callbacks.ContainsKey(pId))
return .Err;
delete _callbacks.GetAndRemove(pId).Value.value;
return .Ok;
}
private delegate void(String, StringView, LogLevel) _messageFormatter = (new (output, message, logLevel) => {
output.Append(scope $"[{DateTime.Now.ToLocalTime()}][{logLevel.ToString(.. scope .())}]: {scope String(message)..Replace("\n", "\n\t")}");
}) ~ delete _;
public delegate void(String, StringView, LogLevel) MessageFormatter
{
get
{
_lock.Enter();
defer _lock.Exit();
return _messageFormatter;
}
set
{
_lock.Enter();
delete _messageFormatter;
_messageFormatter = value;
_lock.Exit();
}
}
//Wether we should log into any custom callbacks
private bool _doCallbackLog = false;
public bool DoCallbackLog
{
get
{
_lock.Enter();
defer _lock.Exit();
return _doCallbackLog;
}
set
{
_lock.Enter();
_doCallbackLog = value;
_lock.Exit();
}
}
//Wether we should log into any custom callbacks
private bool _doCustomFileLog = false;
public bool DoCustomFileLog
{
get
{
_lock.Enter();
defer _lock.Exit();
return _doCustomFileLog;
}
set
{
_lock.Enter();
_doCustomFileLog = value;
_lock.Exit();
}
}
private delegate void(String) _customFileCallback ~ delete _;
public delegate void(String) CustomFileCallback
{
get
{
_lock.Enter();
defer _lock.Exit();
return _customFileCallback;
}
set
{
_lock.Enter();
delete _customFileCallback;
_customFileCallback = value;
_lock.Exit();
}
}
//Wether we should change the color of the console if we log to it
private bool _doConsoleColors = true;
public bool DoConsoleColors
{
get
{
_lock.Enter();
defer _lock.Exit();
return _doConsoleColors;
}
set
{
_lock.Enter();
_doConsoleColors = value;
_lock.Exit();
}
}
private ConsoleColor _traceColor = .Gray;
public ConsoleColor TraceColor
{
get
{
_lock.Enter();
defer _lock.Exit();
return _traceColor;
}
set
{
_lock.Enter();
_traceColor = value;
_lock.Exit();
}
}
private ConsoleColor _debugColor = .Yellow;
public ConsoleColor DebugColor
{
get
{
_lock.Enter();
defer _lock.Exit();
return _debugColor;
}
set
{
_lock.Enter();
_debugColor = value;
_lock.Exit();
}
}
private ConsoleColor _infoColor = .Blue;
public ConsoleColor InfoColor
{
get
{
_lock.Enter();
defer _lock.Exit();
return _infoColor;
}
set
{
_lock.Enter();
_infoColor = value;
_lock.Exit();
}
}
private ConsoleColor _warningColor = .DarkYellow;
public ConsoleColor WarningColor
{
get
{
_lock.Enter();
defer _lock.Exit();
return _warningColor;
}
set
{
_lock.Enter();
_warningColor = value;
_lock.Exit();
}
}
private ConsoleColor _fatalColor = .Red;
public ConsoleColor FatalColor
{
get
{
_lock.Enter();
defer _lock.Exit();
return _fatalColor;
}
set
{
_lock.Enter();
_fatalColor = value;
_lock.Exit();
}
}
//Wether we should log into a specific file
private bool _doFileLog = false;
public bool DoFileLog
{
get
{
_lock.Enter();
defer _lock.Exit();
return _doFileLog;
}
set
{
_lock.Enter();
_doFileLog = value;
_lock.Exit();
}
}
//Wether we should log to the default console
private bool _doConsoleLog = true;
public bool DoConsoleLog
{
get
{
_lock.Enter();
defer _lock.Exit();
return _doConsoleLog;
}
set
{
_lock.Enter();
_doConsoleLog = value;
_lock.Exit();
}
}
private String _filePath = new .(".log") ~ delete _;
public StringView FilePath
{
get
{
_lock.Enter();
defer _lock.Exit();
return _filePath;
}
set
{
_lock.Enter();
delete _filePath;
_filePath = new .(value);
_lock.Exit();
}
}
//How large the stored logs can be, before we force write them to disk
private uint32 _bufferSize = 4096;
public uint32 BufferSize
{
get
{
_lock.Enter();
defer _lock.Exit();
return _bufferSize;
}
set
{
_lock.Enter();
_bufferSize = value;
_lock.Exit();
}
}
//Logs send in these channels wont be processed
private HashSet<String> _enabledChannels = new .(10) {new .("Default")}~ DeleteContainerAndItems!(_);
public HashSet<String> EnabledChannels
{
get
{
_lock.Enter();
defer _lock.Exit();
return _enabledChannels;
}
}
//Logs below this level wont be processed
private LogLevel _logLevel = .Info;
public LogLevel LogLevel
{
get
{
_lock.Enter();
defer _lock.Exit();
return _logLevel;
}
set
{
_lock.Enter();
_logLevel = value;
_lock.Exit();
}
}
}
enum LogLevel
{
Trace,
Debug,
Info,
Warning,
Fatal
}

View file

@ -1,13 +0,0 @@
namespace LybLog;
using System;
using System.Collections;
extension LybLog
{
private mixin FlushAndDeleteContainerAndItems(List<String> pContainer)
{
Flush();
DeleteContainerAndItems!(pContainer);
}
}

View file

@ -1,29 +0,0 @@
namespace LybLog;
using System;
using System.Diagnostics;
extension LybLog
{
private void Log(LogLevel pLL, String pMessage)
{
var fMessage = Settings.DefaultFormater.Format(pLL, pMessage, .. scope String());
if(Settings.DoConsoleLog)
Console.WriteLine(fMessage);
if(Settings.DoIdeLog)
Debug.WriteLine(fMessage);
if(Settings.DoCallbackLog && Settings.Callbacks.HasListeners)
Settings.Callbacks(pLL, pMessage);
if(Settings.DoFileLog)
{
_Cache.Add(new String(fMessage));
if(_Cache.Count > Settings.CacheSize)
Flush();
}
Console.ForegroundColor = .White;
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_DEBUG && !LOG_TRACE
[SkipCall]
#endif
///Variable values needed for debugging
public void Debug(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Debug.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Debug;
Log(.Debug, pMessage);
}
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_ERROR && !LOG_WARN && !LOG_INFO && !LOG_DEBUG && !LOG_TRACE
[SkipCall]
#endif
///An error in the execution of a specific part of the code, program can still continue
public void Error(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Trace.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Error;
Log(.Error, pMessage);
}
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_FATAL && !LOG_ERROR && !LOG_WARN && !LOG_INFO && !LOG_DEBUG && !LOG_TRACE
[SkipCall]
#endif
///Unable to continue executing
public void Fatal(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Fatal.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Fatal;
Log(.Fatal, pMessage);
}
}
}

View file

@ -1,18 +0,0 @@
namespace LybLog;
using System;
using System.IO;
extension LybLog
{
///Will write all cached messages to the logfile if logfile is active
public void Flush()
{
for(var i in _Cache)
{
File.WriteAllText(Settings.LogFilePath, scope $"{i}{Environment.NewLine}",true).IgnoreError();
}
ClearAndDeleteItems(_Cache);
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_INFO && !LOG_DEBUG && !LOG_TRACE
[SkipCall]
#endif
///General step information, such as a system being loaded
public void Info(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Info.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Info;
Log(.Info, pMessage);
}
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_TRACE
[SkipCall]
#endif
///Step by step information about the program
public void Trace(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Trace.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Trace;
Log(.Trace, pMessage);
}
}
}

View file

@ -1,19 +0,0 @@
namespace LybLog;
using System;
extension LybLog
{
#if !LOG_WARN && !LOG_INFO && !LOG_DEBUG && !LOG_TRACE
[SkipCall]
#endif
///Unexpected things, that may be problems down the line but could be fixed by the program
public void Warn(String pMessage)
{
if(Settings.LogLevel.Underlying <= LogLevel.Warn.Underlying)
{ //Check wether we should even do this
Console.ForegroundColor = Settings.Colors.Warn;
Log(.Warn, pMessage);
}
}
}

View file

@ -1,8 +0,0 @@
namespace LybLog;
using System;
interface ILoggingFormatter
{
public void Format(LogLevel pLogLevel, String pMessage, String pFormatedString);
}

View file

@ -1,13 +0,0 @@
namespace LybLog;
using System;
class LogColors
{
public ConsoleColor Trace {get; set;} = .Cyan;
public ConsoleColor Debug {get; set;} = .Blue;
public ConsoleColor Info {get; set;} = .Gray;
public ConsoleColor Warn {get; set;} = .Yellow;
public ConsoleColor Error {get; set;} = .Red;
public ConsoleColor Fatal {get; set;} = .DarkRed;
}

View file

@ -1,11 +0,0 @@
namespace LybLog;
public enum LogLevel
{
Trace = 0,
Debug = 1,
Info = 2,
Warn = 3,
Error = 4,
Fatal = 5
}

View file

@ -1,69 +0,0 @@
namespace LybLog;
using System;
using System.IO;
class LybLogSettings
{
//General Settings
///Current log level, only logs of higher priority than this one will be logged
public LogLevel LogLevel {get; set;} = .Trace;
///Event callback for the log message
public ref Event<delegate void(LogLevel, String)> Callbacks {get; set;} ~ _.Dispose();
private String _LogFilePath = new String(".log") ~ delete _;
///Path in relation to the executing path where the logfile will be save
public String LogFilePath {
get
{
return _LogFilePath;
}
set
{
delete _LogFilePath;
_LogFilePath = new String(value);
}
}
///How many messages should be written to the cache before it dumps into the logfile
public uint32 CacheSize {get; set;} = 1;
///What colors to use for the console output
public LogColors Colors {get; set;} = new .() ~ delete _;
public ILoggingFormatter DefaultFormater {get; set;} = new LoggingFormatter() ~ delete _;
//Toggle Settings
///Should the logger output to the console
public bool DoConsoleLog {get; set; } = true;
///Should the event callbacks be called
public bool DoCallbackLog {get; set; } = true;
///Should the logger output to the ide console
public bool DoIdeLog {get; set; } = false;
///Should the logger write to the logfile
public bool DoFileLog {get; set; } = false;
private bool _ClearLogFileOnLoad = false;
///Should the logfile be cleared when the application first writes to it
public bool ClearLogFileOnLoad
{
get
{
return _ClearLogFileOnLoad;
}
set
{
if(value == true)
{ //Clear the logfile
var res = File.WriteAllText(LogFilePath, "");
}
_ClearLogFileOnLoad = value;
}
}
}

View file

@ -1,25 +0,0 @@
namespace LybLog;
using System;
class LoggingFormatter : ILoggingFormatter
{
void ILoggingFormatter.Format(LogLevel pLogLevel, System.String pMessage, System.String pFormatedString)
{
switch(pLogLevel)
{
case .Trace:
pFormatedString.Append(scope $"[{DateTime.Now}]:[TRACE]:{pMessage}");
case .Debug:
pFormatedString.Append(scope $"[{DateTime.Now}]:[DEBUG3]:{pMessage}");
case .Info:
pFormatedString.Append(scope $"[{DateTime.Now}]:[INFO]:{pMessage}");
case .Warn:
pFormatedString.Append(scope $"[{DateTime.Now}]:[WARN]:{pMessage}");
case .Error:
pFormatedString.Append(scope $"[{DateTime.Now}]:[ERROR]:{pMessage}");
case .Fatal:
pFormatedString.Append(scope $"[{DateTime.Now}]:[FATAL]:{pMessage}");
}
}
}