Skip to content

Commit

Permalink
Test
Browse files Browse the repository at this point in the history
  • Loading branch information
DartPower committed Dec 19, 2019
1 parent be4d8e5 commit 5db84f3
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 132 deletions.
260 changes: 130 additions & 130 deletions Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Entry> 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<Entry> 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<Entry>();
thread = null;
isRunning = false;
}
public Logger(string id, Targets targets)
{
this.id = id;
this.targets = targets;
lockObject = new object();
entries = new Queue<Entry>();
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); }
}
}
6 changes: 4 additions & 2 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 5db84f3

Please sign in to comment.