Transform logging system into library

This commit is contained in:
ChronosX88 2020-11-09 12:32:31 +04:00
parent 18568159a2
commit 9c5619bee9
Signed by: ChronosXYZ
GPG Key ID: 085A69A82C8C511A
16 changed files with 58 additions and 171 deletions

3
.gitignore vendored
View File

@ -3,4 +3,5 @@ bin
obj obj
/.plugins /.plugins
/src/ThirdPartyPlugins /src/ThirdPartyPlugins
.idea .idea
*.sln

View File

@ -1,6 +1,6 @@
using Zirconium.Core.Plugins; using Zirconium.Core.Plugins;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Core.Logging; using Log4Sharp;
using WebSocketSharp.Server; using WebSocketSharp.Server;
using Zirconium.Core.Database; using Zirconium.Core.Database;

View File

@ -21,14 +21,14 @@ namespace Zirconium.Core
protected override void OnClose(CloseEventArgs e) protected override void OnClose(CloseEventArgs e)
{ {
_app.SessionManager.DeleteSession(ID); _app.SessionManager.DeleteSession(ID);
Logging.Log.Info($"Connection {ID} was closed (reason: {e.Reason})"); Log.Info($"Connection {ID} was closed (reason: {e.Reason})");
// TODO implement closing connection // TODO implement closing connection
} }
protected override void OnError(ErrorEventArgs e) protected override void OnError(ErrorEventArgs e)
{ {
Logging.Log.Error($"Error occurred: {e.Exception}"); Log.Error($"Error occurred: {e.Exception}");
} }
protected override void OnMessage(MessageEventArgs e) protected override void OnMessage(MessageEventArgs e)
@ -74,7 +74,7 @@ namespace Zirconium.Core
session.ClientAddress = ip; session.ClientAddress = ip;
session.ConnectionHandler = this; session.ConnectionHandler = this;
_app.SessionManager.AddSession(this.ID, session); _app.SessionManager.AddSession(this.ID, session);
Logging.Log.Info($"Connection {this.ID} was created"); Log.Info($"Connection {this.ID} was created");
} }
public void SendMessage(string message) public void SendMessage(string message)

View File

@ -1,5 +1,5 @@
using MongoDB.Driver; using MongoDB.Driver;
using Zirconium.Core.Logging; using Log4Sharp;
namespace Zirconium.Core.Database namespace Zirconium.Core.Database
{ {

View File

@ -1,50 +0,0 @@
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);
}
public static void Debug(string message) {
_defaultLogger.Debug(message);
}
}
}

View File

@ -1,104 +0,0 @@
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 Debug(string message) {
if (_isVerbose) {
writeLog(LogType.Debug, 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.FullName}", 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;
}
case LogType.Debug: {
tagFormatter = new Formatter("DEBUG", Color.LightGray);
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,
Debug
}
}
}

View File

@ -2,7 +2,7 @@
using System.Threading; using System.Threading;
using CommandLine; using CommandLine;
using Nett; using Nett;
using Zirconium.Core.Logging; using Log4Sharp;
namespace Zirconium.Core namespace Zirconium.Core
{ {
@ -17,6 +17,7 @@ namespace Zirconium.Core
static void Main(string[] args) static void Main(string[] args)
{ {
Log.SetLogLevel(LogLevel.Debug);
string configPath = null; string configPath = null;
Parser.Default.ParseArguments<RunOptions>(args) Parser.Default.ParseArguments<RunOptions>(args)
.WithParsed<RunOptions>(o => .WithParsed<RunOptions>(o =>

View File

@ -2,11 +2,10 @@ using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Runtime.Loader;
using System.Threading; using System.Threading;
using Log4Sharp;
using McMaster.NETCore.Plugins; using McMaster.NETCore.Plugins;
using MongoDB.Driver; using MongoDB.Driver;
using Zirconium.Core.Logging;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Core.Plugins.IPC; using Zirconium.Core.Plugins.IPC;
@ -34,7 +33,7 @@ namespace Zirconium.Core.Plugins
var loaders = new List<PluginLoader>(); var loaders = new List<PluginLoader>();
if (folderPath == "") if (folderPath == "")
{ {
Logging.Log.Warning("Plugins folder path is not specified!"); Log.Warning("Plugins folder path is not specified!");
return; return;
} }
@ -61,8 +60,8 @@ namespace Zirconium.Core.Plugins
var pluginDll = Path.Combine(_currentPluginFolderPath, pluginName, pluginName + ".dll"); var pluginDll = Path.Combine(_currentPluginFolderPath, pluginName, pluginName + ".dll");
if (File.Exists(pluginDll)) if (File.Exists(pluginDll))
{ {
Logging.Log.Debug("Found plugin " + pluginName); Log.Debug("Found plugin " + pluginName);
Logging.Log.Debug("Try to initialize plugin " + pluginName); Log.Debug("Trying to initialize plugin " + pluginName);
loader = PluginLoader.CreateFromAssemblyFile( loader = PluginLoader.CreateFromAssemblyFile(
pluginDll, pluginDll,
sharedTypes: new[] { sharedTypes: new[] {
@ -95,7 +94,7 @@ namespace Zirconium.Core.Plugins
{ {
// This assumes the implementation of IPlugin has a parameterless constructor // This assumes the implementation of IPlugin has a parameterless constructor
plugin = (IPluginAPI)Activator.CreateInstance(pluginType); plugin = (IPluginAPI)Activator.CreateInstance(pluginType);
Logging.Log.Debug($"Created plugin instance '{plugin.GetPluginUniqueName()}'."); Log.Info($"Plugin '{plugin.GetPluginUniqueName()}' initialized successfully");
plugin.PreInitialize(this); plugin.PreInitialize(this);
plugin.Initialize(_pluginHostAPI); plugin.Initialize(_pluginHostAPI);
} }

View File

@ -4,7 +4,7 @@ using System.Collections.Generic;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Utils; using Zirconium.Utils;
using Zirconium.Core.Logging; using Log4Sharp;
using Newtonsoft.Json; using Newtonsoft.Json;
using System.Security.Cryptography; using System.Security.Cryptography;

View File

@ -6,6 +6,7 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Colorful.Console" Version="1.2.10" /> <PackageReference Include="Colorful.Console" Version="1.2.10" />
<PackageReference Include="CommandLineParser" Version="2.8.0" /> <PackageReference Include="CommandLineParser" Version="2.8.0" />
<PackageReference Include="Log4Sharp" Version="1.0.1" />
<PackageReference Include="websocketsharp.core" Version="1.0.0" /> <PackageReference Include="websocketsharp.core" Version="1.0.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" /> <PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" />

View File

@ -1,6 +1,6 @@
using System; using System;
using Newtonsoft.Json; using Newtonsoft.Json;
using Zirconium.Core.Logging; using Log4Sharp;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;

View File

@ -4,7 +4,7 @@ using Newtonsoft.Json;
using Zirconium.Core.Models; using Zirconium.Core.Models;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
using Zirconium.Utils; using Zirconium.Utils;
using Zirconium.Core.Logging; using Log4Sharp;
namespace BasicChat namespace BasicChat
{ {

View File

@ -0,0 +1,7 @@
namespace MessageStorage
{
public class Message
{
}
}

View File

@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../Zirconium/Zirconium.csproj">
<ExcludeAssets>runtime</ExcludeAssets>
</ProjectReference>
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,18 @@
using System;
using Zirconium.Core.Plugins.Interfaces;
namespace MessageStorage
{
public class Plugin : IPluginAPI
{
public void Initialize(IPluginHostAPI pluginHost)
{
throw new NotImplementedException();
}
public string GetPluginUniqueName()
{
return "MessageStoragePlugin";
}
}
}

View File

@ -3,7 +3,7 @@ using System.Dynamic;
using MongoDB.Bson; using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes; using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver; using MongoDB.Driver;
using Zirconium.Core.Logging; using Log4Sharp;
using Zirconium.Core.Plugins.Interfaces; using Zirconium.Core.Plugins.Interfaces;
namespace TestMongoDB namespace TestMongoDB