From 5db84f3219bc0bb3cc341d77712badbc67ec6843 Mon Sep 17 00:00:00 2001 From: DartPower Date: Fri, 20 Dec 2019 00:55:05 +0300 Subject: [PATCH] Test --- Logger.cs | 260 ++++++++++++++++++++++++++--------------------------- Program.cs | 6 +- 2 files changed, 134 insertions(+), 132 deletions(-) diff --git a/Logger.cs b/Logger.cs index 7283ed6..2212839 100644 --- a/Logger.cs +++ b/Logger.cs @@ -5,169 +5,169 @@ namespace SharpMCL { -public sealed class Logger -{ - [Flags] - public enum Targets - { - Console, - File - } - - public enum Level - { - Debug, - Info, - Warn, - Error, - Fatal - } - - private class Entry + public class Logger { - private DateTime dateTime; - private Level level; - private int threadId; - private string message; - - public Entry(DateTime dateTime, Level level, int threadId, string message) + [Flags] + public enum Targets { - this.dateTime = dateTime; - this.level = level; - this.threadId = threadId; - this.message = message.Replace("\t", "|").Replace("\r", "|").Replace("\n", "|"); + Console, + File } - public DateTime DateTime { get { return dateTime; } } - public Level Level { get { return level; } } - public int ThreadId { get { return threadId; } } - public string Message { get { return message; } } - - public void WriteToConsole() + public enum Level { - var dateTime2 = dateTime.ToString("yyyy.MM.dd HH:mm:ss.fff"); - var line = string.Format("{0}\t{1}\t{2}\t{3}", dateTime2, level, threadId, message); - Console.WriteLine(line); + Debug, + Info, + Warn, + Error, + Fatal } - public void WriteToFile(string loggerId) + private class Entry { - var date = dateTime.ToString("yyyyMMdd"); - var logFileName = string.Format(string.IsNullOrEmpty(loggerId) ? "{0}.log" : "{0}_{1}.log", date, loggerId); - using (var streamWriter = new StreamWriter(logFileName, true)) + private DateTime dateTime; + private Level level; + private int threadId; + private string message; + + public Entry(DateTime dateTime, Level level, int threadId, string message) { - var time = dateTime.ToString("HH:mm:ss.fff"); - var line = string.Format("{0}\t{1}\t{2}\t{3}", time, level, threadId, message); - streamWriter.WriteLine(line); + this.dateTime = dateTime; + this.level = level; + this.threadId = threadId; + this.message = message.Replace("\t", "|").Replace("\r", "|").Replace("\n", "|"); } - } - } - public const Logger Null = null; + public DateTime DateTime { get { return dateTime; } } + public Level Level { get { return level; } } + public int ThreadId { get { return threadId; } } + public string Message { get { return message; } } - private string id; - private Targets targets; - private object lockObject; - private Queue entries; - private Thread thread; - private bool isRunning; + public void WriteToConsole() + { + var dateTime2 = dateTime.ToString("yyyy.MM.dd HH:mm:ss.fff"); + var line = string.Format("{0}\t{1}\t{2}\t{3}", dateTime2, level, threadId, message); + Console.WriteLine(line); + } - private void Run() - { - while (true) - { - lock (lockObject) + public void WriteToFile(string loggerId) { - if (entries.Count == 0) + var date = dateTime.ToString("yyyyMMdd"); + var logFileName = string.Format(string.IsNullOrEmpty(loggerId) ? "{0}.log" : "{0}_{1}.log", date, loggerId); + using (var streamWriter = new StreamWriter(logFileName, true)) { - if (!isRunning) - break; + var time = dateTime.ToString("HH:mm:ss.fff"); + var line = string.Format("{0}\t{1}\t{2}\t{3}", time, level, threadId, message); + streamWriter.WriteLine(line); } - else + } + } + + public const Logger Null = null; + + private string id; + private Targets targets; + private object lockObject; + private Queue entries; + private Thread thread; + private bool isRunning; + + private void Run() + { + while (true) + { + lock (lockObject) { - try + if (entries.Count == 0) { - var entry = entries.Dequeue(); - if (targets.HasFlag(Targets.Console)) - entry.WriteToConsole(); - if (targets.HasFlag(Targets.File)) - entry.WriteToFile(id); + if (!isRunning) + break; } - catch (Exception exception) + else { - Console.WriteLine(exception); - isRunning = false; + try + { + var entry = entries.Dequeue(); + if (targets.HasFlag(Targets.Console)) + entry.WriteToConsole(); + if (targets.HasFlag(Targets.File)) + entry.WriteToFile(id); + } + catch (Exception exception) + { + Console.WriteLine(exception); + isRunning = false; + } } } + Thread.Sleep(10); } - Thread.Sleep(10); } - } - public Logger(string id, Targets targets) - { - this.id = id; - this.targets = targets; - lockObject = new object(); - entries = new Queue(); - thread = null; - isRunning = false; - } + public Logger(string id, Targets targets) + { + this.id = id; + this.targets = targets; + lockObject = new object(); + entries = new Queue(); + thread = null; + isRunning = false; + } - public void Start() - { - lock (lockObject) + public void Start() { - if (isRunning) - return; - isRunning = true; + lock (lockObject) + { + if (isRunning) + return; + isRunning = true; + } + thread = new Thread(Run); + thread.Start(); } - thread = new Thread(Run); - thread.Start(); - } - public void Stop() - { - lock (lockObject) + public void Stop() { - if (!isRunning) - return; - isRunning = false; + lock (lockObject) + { + if (!isRunning) + return; + isRunning = false; + } + thread.Join(); + thread = null; } - thread.Join(); - thread = null; - } - public void Log(Level level, string message) - { - lock (lockObject) + public void Log(Level level, string message) { - if (!isRunning) - return; - var timeStamp = DateTime.Now; - var threadId = Thread.CurrentThread.ManagedThreadId; - var entry = new Entry(timeStamp, level, threadId, message); - entries.Enqueue(entry); + lock (lockObject) + { + if (!isRunning) + return; + var timeStamp = DateTime.Now; + var threadId = Thread.CurrentThread.ManagedThreadId; + var entry = new Entry(timeStamp, level, threadId, message); + entries.Enqueue(entry); + } } + public void Debug(string message) { Log(Level.Debug, message); } + public void Info(string message) { Log(Level.Info, message); } + public void Warn(string message) { Log(Level.Warn, message); } + public void Error(string message) { Log(Level.Error, message); } + public void Fatal(string message) { Log(Level.Fatal, message); } + + public void Log(Level level, string format, params object[] args) { Log(level, string.Format(format, args)); } + public void Debug(string format, params object[] args) { Log(Level.Debug, format, args); } + public void Info(string format, params object[] args) { Log(Level.Info, format, args); } + public void Warn(string format, params object[] args) { Log(Level.Warn, format, args); } + public void Error(string format, params object[] args) { Log(Level.Error, format, args); } + public void Fatal(string format, params object[] args) { Log(Level.Fatal, format, args); } + + public void Log(Level level, object obj) { Log(level, obj.ToString()); } + public void Debug(object obj) { Log(Level.Debug, obj); } + public void Info(object obj) { Log(Level.Info, obj); } + public void Warn(object obj) { Log(Level.Warn, obj); } + public void Error(object obj) { Log(Level.Error, obj); } + public void Fatal(object obj) { Log(Level.Fatal, obj); } } - public void Debug(string message) { Log(Level.Debug, message); } - public void Info(string message) { Log(Level.Info, message); } - public void Warn(string message) { Log(Level.Warn, message); } - public void Error(string message) { Log(Level.Error, message); } - public void Fatal(string message) { Log(Level.Fatal, message); } - - public void Log(Level level, string format, params object[] args) { Log(level, string.Format(format, args)); } - public void Debug(string format, params object[] args) { Log(Level.Debug, format, args); } - public void Info(string format, params object[] args) { Log(Level.Info, format, args); } - public void Warn(string format, params object[] args) { Log(Level.Warn, format, args); } - public void Error(string format, params object[] args) { Log(Level.Error, format, args); } - public void Fatal(string format, params object[] args) { Log(Level.Fatal, format, args); } - - public void Log(Level level, object obj) { Log(level, obj.ToString()); } - public void Debug(object obj) { Log(Level.Debug, obj); } - public void Info(object obj) { Log(Level.Info, obj); } - public void Warn(object obj) { Log(Level.Warn, obj); } - public void Error(object obj) { Log(Level.Error, obj); } - public void Fatal(object obj) { Log(Level.Fatal, obj); } -} } \ No newline at end of file diff --git a/Program.cs b/Program.cs index 099964d..c572b68 100644 --- a/Program.cs +++ b/Program.cs @@ -19,12 +19,14 @@ static class Program static public Form CreatePackForm1; static public Form KonamiForm1; - static public Logger logger; - [STAThread] static void Main(string[] args) { Marshal.PrelinkAll(typeof(Program)); + Trace.AutoFlush = true; + Trace.Listeners.Clear(); + Trace.Listeners.Add(new ConsoleTraceListener()); + Trace.Listeners.Add(new TextWriterTraceListener(System.IO.Path.GetFileNameWithoutExtension(typeof(Program).Assembly.GetName().Name) + ".log")); Process currentProcess = Process.GetCurrentProcess(); currentProcess.PriorityClass = ProcessPriorityClass.High; Console.BackgroundColor = ConsoleColor.Black;