Make log window actually work

Fix formatting on log output, so it can be properly displayed in the Console Window
This commit is contained in:
2023-08-02 19:06:06 +02:00
parent e25fc82994
commit 7bd7297e69
8 changed files with 280 additions and 256 deletions

View File

@@ -34,6 +34,7 @@ namespace WolfLauncher.core
private DirectoryInfo dataDir = new DirectoryInfo("launcherdata");
private DirectoryInfo mcDir = new DirectoryInfo("launcherdata/minecraft");
private MinecraftPath mcPath;
private FileInfo log4jFile = new FileInfo("launcherdata/log-config.xml");
// Monitoring running instance
private Instance runningInstance;
@@ -48,6 +49,9 @@ namespace WolfLauncher.core
if (!mcDir.Exists)
mcDir.Create();
if (!log4jFile.Exists)
File.WriteAllText(log4jFile.FullName, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n<Configuration status=\"WARN\">\r\n <Appenders>\r\n <Console name=\"SysOut\" target=\"SYSTEM_OUT\">\r\n <PatternLayout pattern=\"[%d{HH:mm:ss}] [%t/%level]: %msg{nolookups}%n\" />\r\n </Console>\r\n <RollingRandomAccessFile name=\"File\" fileName=\"logs/latest.log\" filePattern=\"logs/%d{yyyy-MM-dd}-%i.log.gz\">\r\n <PatternLayout pattern=\"[%d{HH:mm:ss}] [%t/%level]: %msg{nolookups}%n\" />\r\n <Policies>\r\n <TimeBasedTriggeringPolicy />\r\n <OnStartupTriggeringPolicy />\r\n </Policies>\r\n </RollingRandomAccessFile>\r\n </Appenders>\r\n <Loggers>\r\n <Root level=\"info\">\r\n <filters>\r\n <MarkerFilter marker=\"NETWORK_PACKETS\" onMatch=\"DENY\" onMismatch=\"NEUTRAL\" />\r\n </filters>\r\n <AppenderRef ref=\"SysOut\"/>\r\n <AppenderRef ref=\"File\"/>\r\n </Root>\r\n </Loggers>\r\n</Configuration>");
// Setup library and minecraft paths
mcPath = new MinecraftPath(mcDir.ToString());
cmLauncher = new CMLauncher(mcPath);
@@ -198,6 +202,16 @@ namespace WolfLauncher.core
process.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
process.EnableRaisingEvents = true;
// Replace log4j config, so that we can actually read it to the console
var arg = process.StartInfo.Arguments;
if (arg.IndexOf("-Dlog4j.configurationFile=") != 0)
{
var rep = arg.Substring(arg.IndexOf("-Dlog4j.configurationFile="));
rep = rep.Substring(0, rep.IndexOf(" "));
arg = arg.Replace(rep, "-Dlog4j.configurationFile=" + log4jFile.FullName);
process.StartInfo.Arguments = arg;
}
// Launch the game
process.Start();
process.BeginErrorReadLine();

View File

@@ -1,8 +1,10 @@
using System;
using System.Collections.Concurrent;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;
using WolfLauncher.gui;
using WolfLauncher.model;
/**
@@ -26,7 +28,7 @@ namespace WolfLauncher.core
private Timer tmr;
// Variables for the active Console window if any
public RichTextBox logWindow { get; set; }
public ConsoleControl.ConsoleControl logWindow { get; set; }
public Instance loggingInstance { get; set; }
public bool canUpdateLog { get; set; }
@@ -58,14 +60,27 @@ namespace WolfLauncher.core
while (logQueue.TryDequeue(out string msg))
{
sb.AppendLine(msg);
// Console window is open, so we update the log window
if (logWindow != null && !logWindow.IsDisposed && canUpdateLog)
{
Color clr = Color.White;
if (msg.ToString().Contains("INFO"))
clr = Color.Cyan;
if (msg.ToString().Contains("WARN"))
clr = Color.Yellow;
if (msg.ToString().Contains("ERROR"))
clr = Color.Red;
logWindow.WriteOutput(msg + Environment.NewLine, clr);
}
}
// Write to log file for persistance
File.AppendAllText(logFile.FullName, sb.ToString());
// Console window is open, so we update the log window
if (logWindow != null && !logWindow.IsDisposed && canUpdateLog)
logWindow.AppendText(sb.ToString());
}
/**
@@ -126,15 +141,30 @@ namespace WolfLauncher.core
if (loggingInstance != null && ins != null && ins == loggingInstance)
{
// Clear old log from window if any
logWindow.Clear();
logWindow.ClearOutput();
// Check if persisted log exists
if (!logFile.Exists)
return;
// Load log from file into window
string log = File.ReadAllText(logFile.FullName, Encoding.UTF8);
logWindow.Text = log;
string[] log = File.ReadAllLines(logFile.FullName, Encoding.UTF8);
foreach (var s in log)
{
Color clr = Color.White;
if (s.Contains("INFO"))
clr = Color.Cyan;
if (s.Contains("WARN"))
clr = Color.Yellow;
if (s.Contains("ERROR"))
clr = Color.Red;
logWindow.WriteOutput(s + Environment.NewLine, clr);
}
}
}
}