Files
RAGECOOP-V/RageCoop.Core/Logger.cs

239 lines
6.4 KiB
C#
Raw Permalink Normal View History

2022-05-22 15:55:26 +08:00
using System;
using System.Diagnostics;
2022-06-22 09:00:35 +08:00
using System.IO;
2022-09-08 12:41:56 -07:00
using System.Text;
2022-06-22 09:00:35 +08:00
using System.Threading;
2022-05-22 15:55:26 +08:00
namespace RageCoop.Core
2022-05-22 15:55:26 +08:00
{
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
2022-06-22 09:00:35 +08:00
public class Logger : IDisposable
2022-05-22 15:55:26 +08:00
{
2022-06-22 09:00:35 +08:00
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>
2022-06-22 09:00:35 +08:00
public string Name { get; set; }
2022-07-01 14:39:43 +08:00
/// <summary>
/// Path to log file.
/// </summary>
2022-05-31 19:35:01 -08:00
public string LogPath;
2022-07-01 14:39:43 +08:00
/// <summary>
/// Whether to flush messages to console instead of log file
/// </summary>
2022-05-31 19:35:01 -08:00
public bool UseConsole = false;
2022-06-22 09:00:35 +08:00
private StreamWriter logWriter;
2022-05-31 19:35:01 -08:00
2022-06-22 09:00:35 +08:00
private string Buffer = "";
2022-09-08 12:41:56 -07:00
private readonly Thread LoggerThread;
2022-06-22 09:00:35 +08:00
private bool Stopping = false;
2022-09-08 12:41:56 -07:00
private readonly bool FlushImmediately;
2022-06-22 09:00:35 +08:00
2022-07-01 14:39:43 +08:00
internal Logger(bool flushImmediately = false, bool overwrite = true)
2022-05-22 15:55:26 +08:00
{
2022-06-22 09:00:35 +08:00
FlushImmediately = flushImmediately;
2022-09-08 12:41:56 -07:00
if (File.Exists(LogPath) && overwrite) { File.Delete(LogPath); }
Name = Process.GetCurrentProcess().Id.ToString();
2022-06-22 09:00:35 +08:00
if (!flushImmediately)
2022-05-22 15:55:26 +08:00
{
2022-09-08 12:41:56 -07:00
LoggerThread = new Thread(() =>
{
if (!UseConsole)
{
while (LogPath == default)
{
Thread.Sleep(100);
}
if (File.Exists(LogPath) && overwrite) { File.Delete(LogPath); }
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 2) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-06-22 09:00:35 +08:00
string msg = string.Format("[{0}][{2}] [INF] {1}", Date(), message, Name);
2022-05-22 15:55:26 +08:00
2022-09-08 12:41:56 -07:00
Buffer += msg + "\r\n";
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 3) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-06-22 09:00:35 +08:00
string msg = string.Format("[{0}][{2}] [WRN] {1}", Date(), message, Name);
2022-05-22 15:55:26 +08:00
2022-09-08 12:41:56 -07:00
Buffer += msg + "\r\n";
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 4) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-06-22 09:00:35 +08:00
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), message, Name);
2022-05-22 15:55:26 +08:00
2022-09-08 12:41:56 -07:00
Buffer += msg + "\r\n";
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 4) { return; }
2022-07-02 17:14:56 +08:00
lock (Buffer)
{
2022-09-08 12:41:56 -07:00
string msg = string.Format("[{0}][{2}] [ERR] {1}:{3}", Date(), message, Name, error.Message);
Buffer += msg + "\r\n";
2022-07-02 17:14:56 +08:00
Trace(error.ToString());
}
if (FlushImmediately)
{
Flush();
}
}
/// <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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 4) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-09-08 12:41:56 -07:00
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), "\r\n" + ex.Message, Name);
Buffer += msg + "\r\n";
2022-07-02 17:14:56 +08:00
Trace(ex.ToString());
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 1) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-06-22 09:00:35 +08:00
string msg = string.Format("[{0}][{2}] [DBG] {1}", Date(), message, Name);
2022-05-22 15:55:26 +08:00
2022-09-08 12:41:56 -07:00
Buffer += msg + "\r\n";
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
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)
{
2022-09-08 12:41:56 -07:00
if (LogLevel > 0) { return; }
2022-05-22 15:55:26 +08:00
lock (Buffer)
{
2022-06-22 09:00:35 +08:00
string msg = string.Format("[{0}][{2}] [TRC] {1}", Date(), message, Name);
2022-05-22 15:55:26 +08:00
2022-09-08 12:41:56 -07:00
Buffer += msg + "\r\n";
2022-05-22 15:55:26 +08:00
}
2022-06-22 09:00:35 +08:00
if (FlushImmediately)
{
Flush();
}
2022-05-22 15:55:26 +08:00
}
private string Date()
{
return DateTime.Now.ToString();
}
2022-07-01 14:39:43 +08:00
/// <summary>
///
/// </summary>
2022-05-22 15:55:26 +08:00
public void Flush()
{
lock (Buffer)
{
2022-09-08 12:41:56 -07:00
if (Buffer != "")
2022-05-22 15:55:26 +08:00
{
2022-05-23 09:15:50 +08:00
if (UseConsole)
2022-05-22 15:55:26 +08:00
{
2022-05-23 09:15:50 +08:00
Console.Write(Buffer);
2022-09-08 12:41:56 -07:00
Buffer = "";
2022-05-22 15:55:26 +08:00
}
2022-05-23 09:15:50 +08:00
else
{
try
{
2022-09-08 12:41:56 -07:00
logWriter = new StreamWriter(LogPath, true, Encoding.UTF8);
2022-05-23 09:15:50 +08:00
logWriter.Write(Buffer);
logWriter.Close();
2022-09-08 12:41:56 -07:00
Buffer = "";
2022-05-23 09:15:50 +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
}
}