mirror of
https://github.com/cadmium-im/zirconium-sharp.git
synced 2024-12-23 09:21:52 +00:00
Transform logging system into library
This commit is contained in:
parent
18568159a2
commit
9c5619bee9
3
.gitignore
vendored
3
.gitignore
vendored
@ -3,4 +3,5 @@ bin
|
||||
obj
|
||||
/.plugins
|
||||
/src/ThirdPartyPlugins
|
||||
.idea
|
||||
.idea
|
||||
*.sln
|
@ -1,6 +1,6 @@
|
||||
using Zirconium.Core.Plugins;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
using WebSocketSharp.Server;
|
||||
using Zirconium.Core.Database;
|
||||
|
||||
|
@ -21,14 +21,14 @@ namespace Zirconium.Core
|
||||
protected override void OnClose(CloseEventArgs e)
|
||||
{
|
||||
_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
|
||||
|
||||
}
|
||||
|
||||
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)
|
||||
@ -74,7 +74,7 @@ namespace Zirconium.Core
|
||||
session.ClientAddress = ip;
|
||||
session.ConnectionHandler = this;
|
||||
_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)
|
||||
|
@ -1,5 +1,5 @@
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
|
||||
namespace Zirconium.Core.Database
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
}
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
using System.Threading;
|
||||
using CommandLine;
|
||||
using Nett;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
|
||||
namespace Zirconium.Core
|
||||
{
|
||||
@ -17,6 +17,7 @@ namespace Zirconium.Core
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
Log.SetLogLevel(LogLevel.Debug);
|
||||
string configPath = null;
|
||||
Parser.Default.ParseArguments<RunOptions>(args)
|
||||
.WithParsed<RunOptions>(o =>
|
||||
|
@ -2,11 +2,10 @@ using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.Loader;
|
||||
using System.Threading;
|
||||
using Log4Sharp;
|
||||
using McMaster.NETCore.Plugins;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Logging;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
using Zirconium.Core.Plugins.IPC;
|
||||
@ -34,7 +33,7 @@ namespace Zirconium.Core.Plugins
|
||||
var loaders = new List<PluginLoader>();
|
||||
if (folderPath == "")
|
||||
{
|
||||
Logging.Log.Warning("Plugins folder path is not specified!");
|
||||
Log.Warning("Plugins folder path is not specified!");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -61,8 +60,8 @@ namespace Zirconium.Core.Plugins
|
||||
var pluginDll = Path.Combine(_currentPluginFolderPath, pluginName, pluginName + ".dll");
|
||||
if (File.Exists(pluginDll))
|
||||
{
|
||||
Logging.Log.Debug("Found plugin " + pluginName);
|
||||
Logging.Log.Debug("Try to initialize plugin " + pluginName);
|
||||
Log.Debug("Found plugin " + pluginName);
|
||||
Log.Debug("Trying to initialize plugin " + pluginName);
|
||||
loader = PluginLoader.CreateFromAssemblyFile(
|
||||
pluginDll,
|
||||
sharedTypes: new[] {
|
||||
@ -95,7 +94,7 @@ namespace Zirconium.Core.Plugins
|
||||
{
|
||||
// This assumes the implementation of IPlugin has a parameterless constructor
|
||||
plugin = (IPluginAPI)Activator.CreateInstance(pluginType);
|
||||
Logging.Log.Debug($"Created plugin instance '{plugin.GetPluginUniqueName()}'.");
|
||||
Log.Info($"Plugin '{plugin.GetPluginUniqueName()}' initialized successfully");
|
||||
plugin.PreInitialize(this);
|
||||
plugin.Initialize(_pluginHostAPI);
|
||||
}
|
||||
|
@ -4,7 +4,7 @@ using System.Collections.Generic;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
using Zirconium.Utils;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
using Newtonsoft.Json;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
|
@ -6,6 +6,7 @@
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Colorful.Console" Version="1.2.10" />
|
||||
<PackageReference Include="CommandLineParser" Version="2.8.0" />
|
||||
<PackageReference Include="Log4Sharp" Version="1.0.1" />
|
||||
<PackageReference Include="websocketsharp.core" Version="1.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
|
||||
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.3.0" />
|
||||
|
@ -1,6 +1,6 @@
|
||||
using System;
|
||||
using Newtonsoft.Json;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
|
||||
|
@ -4,7 +4,7 @@ using Newtonsoft.Json;
|
||||
using Zirconium.Core.Models;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
using Zirconium.Utils;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
|
||||
namespace BasicChat
|
||||
{
|
||||
|
7
src/ZirconiumPlugins/MessageStorage/Message.cs
Normal file
7
src/ZirconiumPlugins/MessageStorage/Message.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace MessageStorage
|
||||
{
|
||||
public class Message
|
||||
{
|
||||
|
||||
}
|
||||
}
|
14
src/ZirconiumPlugins/MessageStorage/MessageStorage.csproj
Normal file
14
src/ZirconiumPlugins/MessageStorage/MessageStorage.csproj
Normal 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>
|
18
src/ZirconiumPlugins/MessageStorage/Plugin.cs
Normal file
18
src/ZirconiumPlugins/MessageStorage/Plugin.cs
Normal 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";
|
||||
}
|
||||
}
|
||||
}
|
@ -3,7 +3,7 @@ using System.Dynamic;
|
||||
using MongoDB.Bson;
|
||||
using MongoDB.Bson.Serialization.Attributes;
|
||||
using MongoDB.Driver;
|
||||
using Zirconium.Core.Logging;
|
||||
using Log4Sharp;
|
||||
using Zirconium.Core.Plugins.Interfaces;
|
||||
|
||||
namespace TestMongoDB
|
||||
|
Loading…
Reference in New Issue
Block a user