Modified Logger
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
using System;
|
||||
using Firebird2D.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
@@ -6,51 +7,72 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace Firebird2D.Logger
|
||||
{
|
||||
public enum LogType
|
||||
{
|
||||
Info,
|
||||
Warning,
|
||||
Error
|
||||
}
|
||||
|
||||
public static class GameLogger
|
||||
public class GameLogger : ILogger
|
||||
{
|
||||
private static readonly string LogFilePath = "Logs/Log.txt";
|
||||
private string LogFilePath = "Logs/";
|
||||
|
||||
public static bool logActive { get { return _logActive; } set { _logActive = value; if (value == false) Log("Logger", LogType.Info, "Log Deactivated"); } }
|
||||
public bool LogActive { get { return _logActive; } set { _logActive = value; if (value == false) Log("Logger", LogType.Info, "Log Deactivated"); } }
|
||||
|
||||
private static bool _logActive = true;
|
||||
private bool _logActive = true;
|
||||
|
||||
public static void Initalize()
|
||||
private FileStream? fileStream;
|
||||
|
||||
private readonly object _fileLock = new();
|
||||
|
||||
private static readonly List<GameLogger> instances = [];
|
||||
|
||||
public int InstanceNumber { get; private set; }
|
||||
|
||||
private GameLogger(int instanceNumber, string instanceName)
|
||||
{
|
||||
LogFilePath += $"Log_{instanceName}.txt";
|
||||
InstanceNumber = instanceNumber;
|
||||
Initalize();
|
||||
}
|
||||
|
||||
public static ILogger GetNewInstance(string instanceName)
|
||||
{
|
||||
GameLogger instance = new(instances.Count, instanceName);
|
||||
instances.Add(instance);
|
||||
return instance;
|
||||
}
|
||||
|
||||
public static ILogger GetInstance(int instanceNumber)
|
||||
{
|
||||
return instances[instanceNumber];
|
||||
}
|
||||
|
||||
private void Initalize()
|
||||
{
|
||||
if (!Directory.Exists("Logs"))
|
||||
{
|
||||
Directory.CreateDirectory("Logs");
|
||||
|
||||
using (FileStream fileStream = File.Open(LogFilePath, FileMode.OpenOrCreate | FileMode.Append))
|
||||
{
|
||||
AddText(fileStream, "===================" + System.DateTime.Today.ToString("dd,MM,yy") + "===================");
|
||||
}
|
||||
}
|
||||
|
||||
fileStream = File.Open(LogFilePath, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.Read);
|
||||
|
||||
lock (_fileLock)
|
||||
{
|
||||
AddText(fileStream, "===================" + DateTime.Now.ToString("dd.MM.yy HH:mm:ss") + "===================" + Environment.NewLine);
|
||||
}
|
||||
}
|
||||
|
||||
public static void Log(string loggingInstance, LogType type, string message)
|
||||
public void Log(string loggingInstance, LogType type, string message)
|
||||
{
|
||||
if (_logActive)
|
||||
{
|
||||
using (FileStream fileStream = File.Open(LogFilePath, FileMode.OpenOrCreate | FileMode.Append))
|
||||
if (_logActive && fileStream != null)
|
||||
{
|
||||
lock (_fileLock)
|
||||
{
|
||||
AddText(fileStream, type.ToString("g") + ": " + loggingInstance + " -> " + message);
|
||||
fileStream.Close();
|
||||
AddText(fileStream, $"{DateTime.Now:HH:mm:ss} {type}: {loggingInstance} -> {message}{Environment.NewLine}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void AddText(FileStream fs, string value)
|
||||
{
|
||||
byte[] info = new UTF8Encoding(true).GetBytes(value);
|
||||
fs.Write(info, 0, info.Length);
|
||||
using StreamWriter writer = new(fs, Encoding.UTF8, 1024, leaveOpen: true);
|
||||
writer.Write(value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user