Add flushImmediately to logger
This commit is contained in:
@ -1,12 +1,13 @@
|
||||
using System;
|
||||
using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
|
||||
namespace RageCoop.Core
|
||||
{
|
||||
|
||||
public class Logger : IDisposable
|
||||
{
|
||||
|
||||
@ -14,15 +15,22 @@ namespace RageCoop.Core
|
||||
/// 0:Trace, 1:Debug, 2:Info, 3:Warning, 4:Error
|
||||
/// </summary>
|
||||
public int LogLevel = 0;
|
||||
public string Name { get; set; }
|
||||
public string LogPath;
|
||||
public bool UseConsole = false;
|
||||
private static StreamWriter logWriter;
|
||||
private StreamWriter logWriter;
|
||||
|
||||
private string Buffer = "";
|
||||
private Thread LoggerThread;
|
||||
private bool Stopping = false;
|
||||
private bool FlushImmediately;
|
||||
|
||||
public Logger(bool overwrite=true)
|
||||
public Logger(bool flushImmediately = false, bool overwrite = true)
|
||||
{
|
||||
FlushImmediately = flushImmediately;
|
||||
if (File.Exists(LogPath)&&overwrite) { File.Delete(LogPath); }
|
||||
Name=Process.GetCurrentProcess().Id.ToString();
|
||||
if (!flushImmediately)
|
||||
{
|
||||
LoggerThread=new Thread(() =>
|
||||
{
|
||||
@ -42,16 +50,21 @@ namespace RageCoop.Core
|
||||
});
|
||||
LoggerThread.Start();
|
||||
}
|
||||
}
|
||||
|
||||
public void Info(string message)
|
||||
{
|
||||
if (LogLevel>2) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [INF] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [INF] {1}", Date(), message, Name);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void Warning(string message)
|
||||
@ -59,11 +72,15 @@ namespace RageCoop.Core
|
||||
if (LogLevel>3) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [WRN] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [WRN] {1}", Date(), message, Name);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
@ -71,21 +88,29 @@ namespace RageCoop.Core
|
||||
if (LogLevel>4) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), message, Name);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
public void Error(Exception ex)
|
||||
{
|
||||
if (LogLevel>4) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(),"\r\n"+ex.ToString(), Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [ERR] {1}", Date(), "\r\n"+ex.ToString(), Name);
|
||||
// msg += string.Format("\r\n[{0}][{2}] [ERR] {1}", Date(), "\r\n"+ex.StackTrace, Process.GetCurrentProcess().Id);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void Debug(string message)
|
||||
@ -94,10 +119,14 @@ namespace RageCoop.Core
|
||||
if (LogLevel>1) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [DBG] {1}", Date(), message,Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [DBG] {1}", Date(), message, Name);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
|
||||
public void Trace(string message)
|
||||
@ -105,10 +134,14 @@ namespace RageCoop.Core
|
||||
if (LogLevel>0) { return; }
|
||||
lock (Buffer)
|
||||
{
|
||||
string msg = string.Format("[{0}][{2}] [TRC] {1}", Date(), message, Process.GetCurrentProcess().Id);
|
||||
string msg = string.Format("[{0}][{2}] [TRC] {1}", Date(), message, Name);
|
||||
|
||||
Buffer+=msg+"\r\n";
|
||||
}
|
||||
if (FlushImmediately)
|
||||
{
|
||||
Flush();
|
||||
}
|
||||
}
|
||||
|
||||
private string Date()
|
||||
|
Reference in New Issue
Block a user