2016-11-15 17:54:13 +00:00
|
|
|
|
using System;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.IO;
|
|
|
|
|
using System.Linq;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
using System.Text;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
using System.Threading;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
using System.Threading.Tasks;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
|
2016-11-15 17:54:13 +00:00
|
|
|
|
namespace Galactic_Colors_Control_Common
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
public class Logger
|
|
|
|
|
{
|
2016-11-12 11:33:10 +00:00
|
|
|
|
public enum logType { dev, debug, info, warm, error, fatal }
|
|
|
|
|
public enum logConsole { normal, show, hide }
|
|
|
|
|
|
2016-10-09 12:27:08 +00:00
|
|
|
|
public struct Log
|
|
|
|
|
{
|
|
|
|
|
public string text;
|
|
|
|
|
public logType type;
|
2016-11-12 11:33:10 +00:00
|
|
|
|
public logConsole console;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
|
2016-11-12 11:33:10 +00:00
|
|
|
|
public Log(string p1, logType p2, logConsole p3 = logConsole.normal)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
text = p1;
|
|
|
|
|
type = p2;
|
2016-11-12 11:33:10 +00:00
|
|
|
|
console = p3;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 17:54:13 +00:00
|
|
|
|
private List<Log> toWriteLogs = new List<Log>();
|
|
|
|
|
private string logPath;
|
|
|
|
|
private ConsoleColor[] logBackColor;
|
|
|
|
|
private ConsoleColor[] logForeColor;
|
|
|
|
|
private Thread Updater;
|
2016-11-16 14:20:55 +00:00
|
|
|
|
private logType logLevel = logType.info;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
private bool _run = true;
|
2016-11-16 14:20:55 +00:00
|
|
|
|
public bool run { get { return _run; } }
|
|
|
|
|
private static bool _debug = false;
|
|
|
|
|
private static bool _dev = false;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Create log file and start logger thread
|
|
|
|
|
/// </summary>
|
2016-11-15 17:54:13 +00:00
|
|
|
|
/// <param name="LogPath">Absolute path to logs directory</param>
|
2016-11-16 14:20:55 +00:00
|
|
|
|
public void Initialise(string LogPath, ConsoleColor[] backColor, ConsoleColor[] foreColor, logType LogLevel, bool debug, bool dev)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
logPath = LogPath;
|
|
|
|
|
logBackColor = backColor;
|
|
|
|
|
logForeColor = foreColor;
|
|
|
|
|
logLevel = LogLevel;
|
2016-11-16 14:20:55 +00:00
|
|
|
|
_debug = debug;
|
|
|
|
|
_dev = dev;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
if (!Directory.Exists(logPath))
|
2016-11-12 11:33:10 +00:00
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
Directory.CreateDirectory(logPath);
|
2016-11-12 11:33:10 +00:00
|
|
|
|
Write("Log Directory Created", logType.info);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
2016-10-10 11:41:14 +00:00
|
|
|
|
//Sort old logs
|
2016-11-15 17:54:13 +00:00
|
|
|
|
string[] files = Directory.GetFiles(logPath);
|
2016-11-12 11:33:10 +00:00
|
|
|
|
foreach (string file in files)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
if (Path.GetExtension(file) == ".log")
|
|
|
|
|
{
|
|
|
|
|
string name = Path.GetFileName(file);
|
|
|
|
|
name = name.Substring(0, Math.Min(name.Length, 10));
|
|
|
|
|
if (name.Length == 10)
|
|
|
|
|
{
|
|
|
|
|
if (name != DateTime.UtcNow.ToString("yyyy-MM-dd", CultureInfo.InvariantCulture))
|
|
|
|
|
{
|
|
|
|
|
int y;
|
|
|
|
|
int m;
|
|
|
|
|
int d;
|
2016-11-12 11:33:10 +00:00
|
|
|
|
if (int.TryParse(new string(name.Take(4).ToArray()), out y) && int.TryParse(new string(name.Skip(5).Take(2).ToArray()), out m) && int.TryParse(new string(name.Skip(8).Take(2).ToArray()), out d))
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
if (!Directory.Exists(logPath + "/" + y + "/" + m + "/" + d))
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
Directory.CreateDirectory(logPath + "/" + y + "/" + m + "/" + d);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
2016-11-15 17:54:13 +00:00
|
|
|
|
File.Move(file, logPath + "/" + y + "/" + m + "/" + d + "/" + Path.GetFileName(file));
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
int i = 0;
|
2016-11-15 17:54:13 +00:00
|
|
|
|
while (File.Exists(logPath + "/" + DateTime.UtcNow.ToString("yyyy-MM-dd-", CultureInfo.InvariantCulture) + i + ".log")) { i++; }
|
|
|
|
|
logPath = logPath + "/" + DateTime.UtcNow.ToString("yyyy-MM-dd-", CultureInfo.InvariantCulture) + i + ".log";
|
2016-10-09 12:27:08 +00:00
|
|
|
|
Write("Log path:" + logPath, logType.debug);
|
|
|
|
|
Updater = new Thread(new ThreadStart(UpdaterLoop));
|
|
|
|
|
Updater.Start();
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-15 17:54:13 +00:00
|
|
|
|
public void Join()
|
|
|
|
|
{
|
|
|
|
|
_run = false;
|
|
|
|
|
Updater.Join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void ChangeLevel(logType level)
|
|
|
|
|
{
|
|
|
|
|
logLevel = level;
|
|
|
|
|
Write("Change to " + logLevel.ToString(), logType.info, logConsole.show);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add log to log pile
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="text">Log text</param>
|
|
|
|
|
/// <param name="type">Log status</param>
|
2016-11-12 11:33:10 +00:00
|
|
|
|
/// <param name="console">Server display modifier</param>
|
2016-11-15 17:54:13 +00:00
|
|
|
|
public void Write(string text, logType type, logConsole console = logConsole.normal)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-12 11:33:10 +00:00
|
|
|
|
Write(new Log(text, type, console));
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Add log to log pile
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <param name="log">Log struct</param>
|
2016-11-15 17:54:13 +00:00
|
|
|
|
private void Write(Log log)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-16 14:20:55 +00:00
|
|
|
|
if (_debug || _dev)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
2016-11-12 11:33:10 +00:00
|
|
|
|
//Add Source Method
|
|
|
|
|
log.text = "[" + new StackTrace().GetFrame(2).GetMethod().Name + "]: " + log.text;
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
2016-11-12 11:33:10 +00:00
|
|
|
|
toWriteLogs.Add(log);
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-10 11:41:14 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Write log pile to logfile and console
|
|
|
|
|
/// </summary>
|
2016-11-15 17:54:13 +00:00
|
|
|
|
public void UpdaterLoop()
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
while (_run || toWriteLogs.Count > 0)
|
|
|
|
|
{
|
2016-11-12 11:33:10 +00:00
|
|
|
|
while (toWriteLogs.Count > 0)
|
2016-10-09 12:27:08 +00:00
|
|
|
|
{
|
|
|
|
|
Log log = toWriteLogs[0];
|
2016-11-15 17:54:13 +00:00
|
|
|
|
if (log.type >= logLevel)
|
2016-11-12 11:33:10 +00:00
|
|
|
|
{
|
|
|
|
|
File.AppendAllText(logPath, DateTime.UtcNow.ToString("[yyyy-MM-dd]", CultureInfo.InvariantCulture) + " [" + log.type.ToString().ToUpper() + "]: " + log.text + Environment.NewLine);
|
|
|
|
|
if (log.console != logConsole.hide)
|
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
ConsoleWrite(log);
|
2016-11-12 11:33:10 +00:00
|
|
|
|
}
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
2016-11-12 11:33:10 +00:00
|
|
|
|
else
|
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
if (log.console == logConsole.show)
|
2016-11-12 11:33:10 +00:00
|
|
|
|
{
|
2016-11-15 17:54:13 +00:00
|
|
|
|
ConsoleWrite(log);
|
2016-11-12 11:33:10 +00:00
|
|
|
|
}
|
2016-11-15 17:54:13 +00:00
|
|
|
|
}
|
2016-10-09 12:27:08 +00:00
|
|
|
|
toWriteLogs.Remove(log);
|
|
|
|
|
}
|
|
|
|
|
Thread.Sleep(200);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-11-15 17:54:13 +00:00
|
|
|
|
|
|
|
|
|
private void ConsoleWrite(Log log)
|
|
|
|
|
{
|
|
|
|
|
Console.BackgroundColor = logBackColor[(int)log.type];
|
|
|
|
|
Console.ForegroundColor = logForeColor[(int)log.type];
|
|
|
|
|
Console.Write("\b");
|
|
|
|
|
Console.WriteLine(DateTime.UtcNow.ToString("[yyyy-MM-dd]", CultureInfo.InvariantCulture) + ": " + log.text);
|
|
|
|
|
Common.ConsoleResetColor();
|
|
|
|
|
Console.Write(">");
|
|
|
|
|
}
|
2016-10-09 12:27:08 +00:00
|
|
|
|
}
|
2016-11-15 17:54:13 +00:00
|
|
|
|
}
|