Reworked logging.

No more cross-logging, using Trace for file logging.
This commit is contained in:
alterNERDtive 2020-12-29 19:08:14 +01:00
parent 6186d24096
commit 4c75dbd236
10 changed files with 144 additions and 15 deletions

View file

@ -23,7 +23,30 @@ namespace EliteAttack
/*================\
| plugin contexts |
\================*/
private static void Context_Log(dynamic vaProxy)
{
string message = vaProxy.GetText("~message");
string level = vaProxy.GetText("~level");
if (level == null)
{
Log.Log(message);
}
else
{
try
{
Log.Log(message, (LogLevel)Enum.Parse(typeof(LogLevel), level.ToUpper()));
}
catch (ArgumentNullException) { throw; }
catch (ArgumentException)
{
Log.Error($"Invalid log level '{level}'.");
}
}
}
private static void Context_Startup(dynamic vaProxy)
{
Log.Notice("Starting up …");
@ -59,12 +82,13 @@ namespace EliteAttack
{
switch (context)
{
// plugin methods
case "startup":
Context_Startup(vaProxy);
break;
// plugin settings
// NYI
// log
case "log.log":
Context_Log(vaProxy);
break;
// invalid
default:
Log.Error($"Invalid plugin context '{vaProxy.Context}'.");

View file

@ -205,6 +205,29 @@ namespace RatAttack
}
}
private static void Context_Log(dynamic vaProxy)
{
string message = vaProxy.GetText("~message");
string level = vaProxy.GetText("~level");
if (level == null)
{
Log.Log(message);
}
else
{
try
{
Log.Log(message, (LogLevel)Enum.Parse(typeof(LogLevel), level.ToUpper()));
}
catch (ArgumentNullException) { throw; }
catch (ArgumentException)
{
Log.Error($"Invalid log level '{level}'.");
}
}
}
private static void Context_Startup(dynamic vaProxy)
{
Log.Notice("Starting up …");
@ -262,6 +285,10 @@ namespace RatAttack
case "edsm.getnearestcmdr":
Context_EDSM_GetNearestCMDR(vaProxy);
break;
// log
case "log.log":
Context_Log(vaProxy);
break;
// invalid
default:
Log.Error($"Invalid plugin context '{vaProxy.Context}'.");

View file

@ -81,6 +81,29 @@ namespace SpanshAttack
vaProxy.SetInt("~exitCode", p.ExitCode);
}
private static void Context_Log(dynamic vaProxy)
{
string message = vaProxy.GetText("~message");
string level = vaProxy.GetText("~level");
if (level == null)
{
Log.Log(message);
}
else
{
try
{
Log.Log(message, (LogLevel)Enum.Parse(typeof(LogLevel), level.ToUpper()));
}
catch (ArgumentNullException) { throw; }
catch (ArgumentException)
{
Log.Error($"Invalid log level '{level}'.");
}
}
}
private static void Context_Spansh_Nearestsystem(dynamic vaProxy)
{
int x = vaProxy.GetInt("~x") ?? throw new ArgumentNullException("~x");
@ -222,6 +245,10 @@ namespace SpanshAttack
case "edts.getcoordinates":
Context_EDTS_GetCoordinates(vaProxy);
break;
// log
case "log.log":
Context_Log(vaProxy);
break;
// Spansh
case "spansh.systemexists":
Context_Spansh_SytemExists(vaProxy);

View file

@ -274,19 +274,18 @@ namespace alterNERDtive
private static void Context_Log(dynamic vaProxy)
{
string sender = vaProxy.GetText("~sender");
string message = vaProxy.GetText("~message");
string level = vaProxy.GetText("~level");
if (level == null)
{
Log.Log(sender, message);
Log.Log(message);
}
else
{
try
{
Log.Log(sender, message, (LogLevel)Enum.Parse(typeof(LogLevel), level.ToUpper()));
Log.Log(message, (LogLevel)Enum.Parse(typeof(LogLevel), level.ToUpper()));
}
catch (ArgumentNullException) { throw; }
catch (ArgumentException)

View file

@ -476,6 +476,11 @@ namespace alterNERDtive.util
private readonly dynamic VA;
private readonly string ID;
private static readonly string LogFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "alterNERDtive-logs");
private static readonly TextWriterTraceListener LogListener = new TextWriterTraceListener(Path.Combine(LogFolder, "VoiceAttack.log"), "error");
private static readonly TextWriterTraceListener DebugListener = new TextWriterTraceListener(Path.Combine(LogFolder, "VoiceAttack-debug.log"), "debug");
private readonly TraceSource TraceSource;
private static readonly string[] LogColour = { "red", "yellow", "green", "blue", "gray" };
public LogLevel? CurrentLogLevel
{
@ -495,20 +500,67 @@ namespace alterNERDtive.util
}
private static LogLevel? currentLogLevel;
public VoiceAttackLog(dynamic vaProxy, string id) => (VA, ID) = (vaProxy, id);
static VoiceAttackLog()
{
Directory.CreateDirectory(LogFolder);
string path = Path.Combine(LogFolder, $"VoiceAttack");
foreach (string name in new String[] { "", "-debug" })
{
File.Delete($"{path}{name}-5.log");
for (int i = 4; i > 0; i--)
{
if (File.Exists($"{path}{name}-{i}.log"))
{
File.Move($"{path}{name}-{i}.log", $"{path}{name}-{i + 1}.log");
}
}
File.Move($"{path}{name}.log", $"{path}{name}-1.log");
}
File.Delete(Path.Combine(LogFolder, "VoiceAttack.log"));
File.Delete(Path.Combine(LogFolder, "VoiceAttack-debug.log"));
DebugListener.TraceOutputOptions = LogListener.TraceOutputOptions = TraceOptions.DateTime;
LogListener.Filter = new EventTypeFilter(SourceLevels.Information);
DebugListener.Filter = new EventTypeFilter(SourceLevels.All);
}
public VoiceAttackLog(dynamic vaProxy, string id)
{
VA = vaProxy;
ID = id;
TraceSource = new TraceSource(ID);
TraceSource.Listeners.Add(LogListener);
TraceSource.Listeners.Add(DebugListener);
TraceSource.Switch.Level = SourceLevels.All;
}
public void Log(string message, LogLevel level = LogLevel.INFO)
{
Log(ID, message, level);
}
public void Log(string sender, string message, LogLevel level = LogLevel.INFO)
{
_ = sender ?? throw new ArgumentNullException("sender");
_ = message ?? throw new ArgumentNullException("message");
if (level <= CurrentLogLevel)
VA.WriteToLog($"{level} | {sender}: {message}", LogColour[(int)level]);
{
VA.WriteToLog($"{level} | {ID}: {message}", LogColour[(int)level]);
}
switch(level)
{
case LogLevel.ERROR:
TraceSource.TraceEvent(TraceEventType.Error, 0, message);
break;
case LogLevel.WARN:
TraceSource.TraceEvent(TraceEventType.Warning, 1, message);
break;
case LogLevel.NOTICE:
case LogLevel.INFO:
TraceSource.TraceEvent(TraceEventType.Information, 2, message);
break;
case LogLevel.DEBUG:
TraceSource.TraceEvent(TraceEventType.Verbose, 3, message);
break;
}
TraceSource.Flush();
}
public void Error(string message) => Log(message, LogLevel.ERROR);

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.