2018-04-24 18:01:27 -04:00
|
|
|
|
using SharedLibraryCore;
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
2017-05-27 19:29:20 -04:00
|
|
|
|
using System.IO;
|
|
|
|
|
|
2018-04-08 14:48:40 -04:00
|
|
|
|
namespace IW4MAdmin.Application
|
2017-05-27 19:29:20 -04:00
|
|
|
|
{
|
2018-04-08 02:44:42 -04:00
|
|
|
|
class Logger : SharedLibraryCore.Interfaces.ILogger
|
2017-05-27 19:29:20 -04:00
|
|
|
|
{
|
|
|
|
|
enum LogType
|
2018-05-05 16:36:26 -04:00
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Verbose,
|
|
|
|
|
Info,
|
|
|
|
|
Debug,
|
|
|
|
|
Warning,
|
2017-11-13 16:58:23 -05:00
|
|
|
|
Error,
|
|
|
|
|
Assert
|
2017-05-27 19:29:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2018-08-03 22:11:58 -04:00
|
|
|
|
readonly string FileName;
|
|
|
|
|
readonly object ThreadLock;
|
2017-05-27 19:29:20 -04:00
|
|
|
|
|
|
|
|
|
public Logger(string fn)
|
|
|
|
|
{
|
|
|
|
|
FileName = fn;
|
2017-06-01 13:42:28 -04:00
|
|
|
|
ThreadLock = new object();
|
2017-05-28 21:07:33 -04:00
|
|
|
|
if (File.Exists(fn))
|
|
|
|
|
File.Delete(fn);
|
2017-05-27 19:29:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void Write(string msg, LogType type)
|
|
|
|
|
{
|
2018-05-05 16:36:26 -04:00
|
|
|
|
string stringType = type.ToString();
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
stringType = Utilities.CurrentLocalization.LocalizationIndex[$"GLOBAL_{type.ToString().ToUpper()}"];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception) { }
|
2018-04-24 18:01:27 -04:00
|
|
|
|
|
2018-08-07 14:43:09 -04:00
|
|
|
|
string LogLine = $"[{DateTime.Now.ToString("MM.dd.yyy HH:mm:ss.fff")}] - {stringType}: {msg}";
|
2017-06-01 13:42:28 -04:00
|
|
|
|
lock (ThreadLock)
|
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#if DEBUG
|
|
|
|
|
// lets keep it simple and dispose of everything quickly as logging wont be that much (relatively)
|
2017-05-30 17:23:31 -04:00
|
|
|
|
|
2017-05-27 19:29:20 -04:00
|
|
|
|
Console.WriteLine(LogLine);
|
2017-05-28 16:47:21 -04:00
|
|
|
|
File.AppendAllText(FileName, LogLine + Environment.NewLine);
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#else
|
2017-06-12 13:50:00 -04:00
|
|
|
|
if (type == LogType.Error || type == LogType.Verbose)
|
|
|
|
|
Console.WriteLine(LogLine);
|
2017-06-01 13:42:28 -04:00
|
|
|
|
//if (type != LogType.Debug)
|
2018-04-05 00:38:45 -04:00
|
|
|
|
File.AppendAllText(FileName, $"{LogLine}{Environment.NewLine}");
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#endif
|
2017-06-05 22:49:26 -04:00
|
|
|
|
}
|
2017-05-27 19:29:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteVerbose(string msg)
|
|
|
|
|
{
|
|
|
|
|
Write(msg, LogType.Verbose);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteDebug(string msg)
|
|
|
|
|
{
|
|
|
|
|
Write(msg, LogType.Debug);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteError(string msg)
|
|
|
|
|
{
|
|
|
|
|
Write(msg, LogType.Error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteInfo(string msg)
|
|
|
|
|
{
|
|
|
|
|
Write(msg, LogType.Info);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void WriteWarning(string msg)
|
|
|
|
|
{
|
|
|
|
|
Write(msg, LogType.Warning);
|
|
|
|
|
}
|
2017-11-13 16:58:23 -05:00
|
|
|
|
|
|
|
|
|
public void WriteAssert(bool condition, string msg)
|
|
|
|
|
{
|
|
|
|
|
if (!condition)
|
|
|
|
|
Write(msg, LogType.Assert);
|
|
|
|
|
}
|
2017-05-27 19:29:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|