240 lines
6.4 KiB
C#
Raw Normal View History

2022-05-22 15:55:26 +08:00
using System;
2022-06-22 09:00:35 +08:00
using System.Collections.Generic;
2022-05-22 15:55:26 +08:00
using System.Text;
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
{
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-05-31 19:35:01 -08:00
private Thread LoggerThread;
2022-06-22 09:00:35 +08:00
private bool Stopping = false;
private bool FlushImmediately;
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;
if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); }
Name=Process.GetCurrentProcess().Id.ToString();
if (!flushImmediately)
2022-05-22 15:55:26 +08:00
{
2022-06-22 09:00:35 +08:00
LoggerThread=new Thread(() =>
2022-06-19 11:12:20 +08:00
{
2022-06-22 09:00:35 +08:00
if (!UseConsole)
2022-06-19 11:12:20 +08:00
{
2022-06-22 09:00:35 +08:00
while (LogPath==default)
{
Thread.Sleep(100);
}
if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); }
2022-06-19 11:12:20 +08:00
}
2022-06-22 09:00:35 +08:00
while (!Stopping)
{
Flush();
Thread.Sleep(1000);
}
2022-06-22 09:03:58 +08:00
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)
{
if (LogLevel>2) { return; }
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
Buffer+=msg+"\r\n";
}
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)
{
if (LogLevel>3) { return; }
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
Buffer+=msg+"\r\n";
}
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)
{
if (LogLevel>4) { return; }
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
Buffer+=msg+"\r\n";
}
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)
{
if (LogLevel>4) { return; }
lock (Buffer)
{
string msg = string.Format("[{0}][{2}] [ERR] {1}:{3}", Date(), message, Name,error.Message);
Buffer+=msg+"\r\n";
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)
{
if (LogLevel>4) { return; }
lock (Buffer)
{
2022-07-02 17:14:56 +08:00
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), "\r\n"+ex.Message, Name);
2022-05-22 15:55:26 +08:00
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)
{
if (LogLevel>1) { return; }
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
Buffer+=msg+"\r\n";
}
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-05-23 09:15:50 +08: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
Buffer+=msg+"\r\n";
}
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)
{
if (Buffer!="")
{
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-05-22 15:55:26 +08:00
Buffer="";
}
2022-05-23 09:15:50 +08:00
else
{
try
{
logWriter=new StreamWriter(LogPath, true, Encoding.UTF8);
logWriter.Write(Buffer);
logWriter.Close();
Buffer="";
}
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()
{
Stopping=true;
LoggerThread?.Join();
}
2022-05-22 15:55:26 +08:00
}
}