2018-04-24 18:01:27 -04:00
|
|
|
|
using SharedLibraryCore;
|
2019-05-09 21:00:00 -04:00
|
|
|
|
using SharedLibraryCore.Interfaces;
|
2018-04-24 18:01:27 -04:00
|
|
|
|
using System;
|
2019-06-24 12:01:34 -04:00
|
|
|
|
using System.Diagnostics;
|
2017-05-27 19:29:20 -04:00
|
|
|
|
using System.IO;
|
2018-10-06 12:47:14 -04:00
|
|
|
|
using System.Threading;
|
2019-07-24 20:15:07 -04:00
|
|
|
|
using System.Threading.Tasks;
|
2017-05-27 19:29:20 -04:00
|
|
|
|
|
2018-04-08 14:48:40 -04:00
|
|
|
|
namespace IW4MAdmin.Application
|
2017-05-27 19:29:20 -04:00
|
|
|
|
{
|
2019-05-09 21:00:00 -04:00
|
|
|
|
class Logger : 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;
|
2018-10-06 12:47:14 -04:00
|
|
|
|
readonly SemaphoreSlim OnLogWriting;
|
2018-10-07 22:34:30 -04:00
|
|
|
|
static readonly short MAX_LOG_FILES = 10;
|
2017-05-27 19:29:20 -04:00
|
|
|
|
|
|
|
|
|
public Logger(string fn)
|
|
|
|
|
{
|
2018-10-10 20:22:08 -04:00
|
|
|
|
FileName = Path.Join(Utilities.OperatingDirectory, "Log", $"{fn}.log");
|
2018-10-07 22:34:30 -04:00
|
|
|
|
OnLogWriting = new SemaphoreSlim(1, 1);
|
|
|
|
|
RotateLogs();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// rotates logs when log is initialized
|
|
|
|
|
/// </summary>
|
|
|
|
|
private void RotateLogs()
|
|
|
|
|
{
|
|
|
|
|
string maxLog = FileName + MAX_LOG_FILES;
|
|
|
|
|
|
|
|
|
|
if (File.Exists(maxLog))
|
|
|
|
|
{
|
|
|
|
|
File.Delete(maxLog);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (int i = MAX_LOG_FILES - 1; i >= 0; i--)
|
|
|
|
|
{
|
|
|
|
|
string logToMove = i == 0 ? FileName : FileName + i;
|
|
|
|
|
string movedLogName = FileName + (i + 1);
|
|
|
|
|
|
|
|
|
|
if (File.Exists(logToMove))
|
|
|
|
|
{
|
|
|
|
|
File.Move(logToMove, movedLogName);
|
|
|
|
|
}
|
|
|
|
|
}
|
2017-05-27 19:29:20 -04:00
|
|
|
|
}
|
|
|
|
|
|
2019-07-24 20:15:07 -04:00
|
|
|
|
async Task Write(string msg, LogType type)
|
2017-05-27 19:29:20 -04:00
|
|
|
|
{
|
2019-07-24 20:15:07 -04:00
|
|
|
|
await OnLogWriting.WaitAsync();
|
2018-10-06 12:47:14 -04:00
|
|
|
|
|
2018-05-05 16:36:26 -04:00
|
|
|
|
string stringType = type.ToString();
|
2019-08-04 21:38:55 -04:00
|
|
|
|
msg = msg.StripColors();
|
2018-05-05 16:36:26 -04:00
|
|
|
|
|
|
|
|
|
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}";
|
2018-10-07 22:34:30 -04:00
|
|
|
|
try
|
|
|
|
|
{
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#if DEBUG
|
2018-10-07 22:34:30 -04:00
|
|
|
|
// lets keep it simple and dispose of everything quickly as logging wont be that much (relatively)
|
2019-08-04 21:38:55 -04:00
|
|
|
|
Console.WriteLine(LogLine);
|
2019-07-24 20:15:07 -04:00
|
|
|
|
await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}");
|
|
|
|
|
//Debug.WriteLine(msg);
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#else
|
2017-06-12 13:50:00 -04:00
|
|
|
|
if (type == LogType.Error || type == LogType.Verbose)
|
2019-06-24 12:01:34 -04:00
|
|
|
|
{
|
2019-08-04 21:38:55 -04:00
|
|
|
|
Console.WriteLine(LogLine);
|
2019-06-24 12:01:34 -04:00
|
|
|
|
}
|
2019-07-24 20:15:07 -04:00
|
|
|
|
await File.AppendAllTextAsync(FileName, $"{LogLine}{Environment.NewLine}");
|
2017-05-27 19:29:20 -04:00
|
|
|
|
#endif
|
2018-10-07 22:34:30 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
catch (Exception ex)
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Well.. It looks like your machine can't event write to the log file. That's something else...");
|
|
|
|
|
Console.WriteLine(ex.GetExceptionInfo());
|
|
|
|
|
}
|
2018-10-06 12:47:14 -04:00
|
|
|
|
|
|
|
|
|
OnLogWriting.Release(1);
|
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
|
|
|
|
}
|
|
|
|
|
}
|