diff --git a/src/Zirconium/Core/Logging/Log.cs b/src/Zirconium/Core/Logging/Log.cs new file mode 100644 index 0000000..7d0c801 --- /dev/null +++ b/src/Zirconium/Core/Logging/Log.cs @@ -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); + } + } +} \ No newline at end of file diff --git a/src/Zirconium/Core/Logging/Logger.cs b/src/Zirconium/Core/Logging/Logger.cs new file mode 100644 index 0000000..1405fb9 --- /dev/null +++ b/src/Zirconium/Core/Logging/Logger.cs @@ -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 + } + } +} \ No newline at end of file diff --git a/src/Zirconium/Zirconium.csproj b/src/Zirconium/Zirconium.csproj index 7ba4307..2984bd2 100644 --- a/src/Zirconium/Zirconium.csproj +++ b/src/Zirconium/Zirconium.csproj @@ -4,10 +4,11 @@ netcoreapp3.1 - - - - - + + + + + + \ No newline at end of file