Implement logging system

This commit is contained in:
ChronosX88 2020-06-26 15:41:09 +04:00
parent 1cba8c39e8
commit 0e8bb5d1b7
3 changed files with 145 additions and 5 deletions

View File

@ -0,0 +1,46 @@
using System;
namespace Zirconium.Core.Logging
{
public class Log
{
private static Logger _defaultLogger = new Logger("", true, false, true);
private static object loggerMutex = new Object();
public static Logger Initialize(string name, bool verbose, bool systemLog) {
var logger = new Logger(name, verbose, systemLog, false);
if (!_defaultLogger.isInitialized) {
lock (loggerMutex) {
if (!_defaultLogger.isInitialized) {
var defLogger = new Logger(name, verbose, systemLog, true);
_defaultLogger = defLogger;
_defaultLogger.isInitialized = true;
}
}
}
return logger;
}
public static void Close() {
lock (loggerMutex) {
_defaultLogger.Close();
}
}
public static void Error(string message) {
_defaultLogger.Error(message);
}
public static void Warning(string message) {
_defaultLogger.Warning(message);
}
public static void Fatal(string message) {
_defaultLogger.Fatal(message);
}
public static void Info(string message) {
_defaultLogger.Info(message);
}
}
}

View File

@ -0,0 +1,93 @@
using System.Diagnostics;
using System;
using System.Drawing;
using Colorful;
namespace Zirconium.Core.Logging
{
public class Logger
{
private bool _isVerbose;
private bool _isDefaultLogger;
internal bool isInitialized;
internal Logger(string name, bool verbose, bool systemLog, bool isDefaultLogger) {
this._isVerbose = verbose;
this._isDefaultLogger = isDefaultLogger;
}
public void Close() {}
public void Error(string message) {
writeLog(LogType.Error, message);
}
public void Warning(string message) {
if (_isVerbose) {
writeLog(LogType.Warning, message);
}
}
public void Info(string message) {
if (_isVerbose) {
writeLog(LogType.Info, message);
}
}
public void Fatal(string message) {
writeLog(LogType.Fatal, message);
System.Environment.Exit(1);
}
private void writeLog(LogType logType, string message) {
var st = new StackTrace();
StackFrame stFrame;
if (_isDefaultLogger) {
stFrame = st.GetFrame(3);
} else {
stFrame = st.GetFrame(2);
}
var dateTimeFormatter = new Formatter($"{DateTime.Now}", Color.White);
var frameFormatter = new Formatter($"{stFrame.GetMethod().ReflectedType.Name}", Color.DeepPink);
string outputMessage = "[{0}] [{1}] | [{2}]: " + message;
Formatter tagFormatter = null;
switch (logType) {
case LogType.Error: {
tagFormatter = new Formatter("ERROR", Color.Red);
break;
}
case LogType.Fatal: {
tagFormatter = new Formatter("FATAL", Color.DarkRed);
break;
}
case LogType.Info: {
tagFormatter = new Formatter("INFO", Color.Aqua);
break;
}
case LogType.Warning: {
tagFormatter = new Formatter("WARNING", Color.Yellow);
break;
}
default: {
tagFormatter = new Formatter("UNKNOWN", Color.Green);
break;
}
}
var formatters = new Formatter[] {
dateTimeFormatter,
frameFormatter,
tagFormatter
};
Colorful.Console.WriteLineFormatted(outputMessage, Color.Gray, formatters);
}
internal enum LogType {
Error,
Fatal,
Info,
Warning
}
}
}

View File

@ -4,10 +4,11 @@
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="websocketsharp.core" Version="1.0.0"/>
<PackageReference Include="YamlDotNet" Version="8.1.2"/>
<PackageReference Include="JWT" Version="7.2.1"/>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3"/>
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0"/>
<PackageReference Include="Colorful.Console" Version="1.2.10" />
<PackageReference Include="websocketsharp.core" Version="1.0.0" />
<PackageReference Include="YamlDotNet" Version="8.1.2" />
<PackageReference Include="JWT" Version="7.2.1" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" />
</ItemGroup>
</Project>