Files
RAGECOOP-V/Core/Logger.cs

177 lines
4.9 KiB
C#
Raw Normal View History

2022-10-15 17:06:19 +08:00
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
using System.Diagnostics;
2022-06-22 09:00:35 +08:00
using System.IO;
using System.Threading;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
2022-05-22 15:55:26 +08:00
{
public enum LogLevel
{
Trace = 0,
Debug = 1,
Info = 2,
Warning = 3,
Error = 4
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
public class Logger : MarshalByRefObject, IDisposable
2022-05-22 15:55:26 +08:00
{
public class LogLine
{
internal LogLine() { }
public DateTime TimeStamp;
public LogLevel LogLevel;
public string Message;
}
2022-05-23 09:15:50 +08:00
/// <summary>
/// 0:Trace, 1:Debug, 2:Info, 3:Warning, 4:Error
/// </summary>
2022-05-22 15:55:26 +08:00
public int LogLevel = 0;
2022-07-01 14:39:43 +08:00
/// <summary>
/// Name of this logger
/// </summary>
public string Name = "Logger";
public readonly string DateTimeFormat = "HH:mm:ss";
2022-07-01 14:39:43 +08:00
/// <summary>
/// Whether to use UTC time for timestamping the log
2022-07-01 14:39:43 +08:00
/// </summary>
public readonly bool UseUtc = false;
public List<StreamWriter> Writers = new List<StreamWriter> { new StreamWriter(Console.OpenStandardOutput()) };
public int FlushInterval = 1000;
public event FlushDelegate OnFlush;
public bool FlushImmediately = false;
public delegate void FlushDelegate(LogLine line, string fomatted);
2022-05-31 19:35:01 -08:00
2022-09-08 12:41:56 -07:00
private readonly Thread LoggerThread;
2022-06-22 09:00:35 +08:00
private bool Stopping = false;
private readonly ConcurrentQueue<LogLine> _queuedLines = new ConcurrentQueue<LogLine>();
internal Logger()
2022-05-22 15:55:26 +08:00
{
2022-09-08 12:41:56 -07:00
Name = Process.GetCurrentProcess().Id.ToString();
if (!FlushImmediately)
2022-05-22 15:55:26 +08:00
{
2022-09-08 12:41:56 -07:00
LoggerThread = new Thread(() =>
{
while (!Stopping)
{
Flush();
Thread.Sleep(1000);
}
Flush();
});
2022-06-22 09:00:35 +08:00
LoggerThread.Start();
}
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
/// <param name="message"></param>
2022-05-22 15:55:26 +08:00
public void Info(string message)
{
Enqueue(2, message);
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
/// <param name="message"></param>
2022-05-22 15:55:26 +08:00
public void Warning(string message)
{
Enqueue(3, message);
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
/// <param name="message"></param>
2022-05-22 15:55:26 +08:00
public void Error(string message)
{
Enqueue(4, message);
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
2022-07-02 17:14:56 +08:00
/// <param name="message"></param>
/// <param name="error"></param>
public void Error(string message, Exception error)
{
Enqueue(4, $"{message}:\n {error}");
2022-07-02 17:14:56 +08:00
}
/// <summary>
///
/// </summary>
2022-07-01 14:39:43 +08:00
/// <param name="ex"></param>
2022-05-22 15:55:26 +08:00
public void Error(Exception ex)
{
Enqueue(4, ex.ToString());
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
/// <param name="message"></param>
2022-05-22 15:55:26 +08:00
public void Debug(string message)
{
Enqueue(1, message);
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
/// <param name="message"></param>
2022-05-22 15:55:26 +08:00
public void Trace(string message)
{
Enqueue(0, message);
}
public void Enqueue(int level, string message)
{
if (level < LogLevel) { return; }
_queuedLines.Enqueue(new LogLine()
2022-05-22 15:55:26 +08:00
{
Message = message,
TimeStamp = UseUtc ? DateTime.UtcNow : DateTime.Now,
LogLevel = (LogLevel)level
});
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
2022-05-22 15:55:26 +08:00
}
2022-10-15 17:06:19 +08:00
private string Format(LogLine line)
2022-05-22 15:55:26 +08:00
{
return string.Format("[{0}][{2}] [{3}] {1}", line.TimeStamp.ToString(DateTimeFormat), line.Message, Name, line.LogLevel.ToString());
2022-05-22 15:55:26 +08:00
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
2022-05-22 15:55:26 +08:00
public void Flush()
{
lock (_queuedLines)
2022-05-22 15:55:26 +08:00
{
try
2022-05-22 15:55:26 +08:00
{
while (_queuedLines.TryDequeue(out var line))
2022-05-23 09:15:50 +08:00
{
var formatted = Format(line);
Writers.ForEach(x => { x.WriteLine(formatted); x.Flush(); });
OnFlush?.Invoke(line, formatted);
2022-05-23 09:15:50 +08:00
}
2022-05-22 15:55:26 +08:00
}
catch { }
2022-05-22 15:55:26 +08:00
}
}
2022-07-01 14:39:43 +08:00
/// <summary>
/// Stop backdround thread and flush all pending messages.
/// </summary>
2022-06-01 19:05:45 +08:00
public void Dispose()
{
2022-09-08 12:41:56 -07:00
Stopping = true;
2022-06-01 19:05:45 +08:00
LoggerThread?.Join();
}
2022-05-22 15:55:26 +08:00
}
}