From c5b698cf1f641dbe1d43463cb7554aa82f2db4dd Mon Sep 17 00:00:00 2001 From: Nick Date: Tue, 26 Dec 2023 15:14:01 -0500 Subject: [PATCH] Fix console requiring a restart, added EnableLogConsole winchconfig option --- Winch/Core/WinchCore.cs | 20 +------ Winch/Logging/Logger.cs | 83 +++++++++++++++++++-------- WinchCommon/Config/DefaultConfig.json | 3 +- WinchConsole/LogSocketListener.cs | 20 +++---- WinchConsole/Program.cs | 2 +- 5 files changed, 69 insertions(+), 59 deletions(-) diff --git a/Winch/Core/WinchCore.cs b/Winch/Core/WinchCore.cs index bd069c1c..51687df2 100644 --- a/Winch/Core/WinchCore.cs +++ b/Winch/Core/WinchCore.cs @@ -2,7 +2,6 @@ using Newtonsoft.Json; using System; using System.Collections.Generic; -using System.Diagnostics; using System.IO; using System.Reflection; using Winch.Logging; @@ -16,22 +15,10 @@ public class WinchCore public static Dictionary WinchModConfig = new(); - public static string WinchInstallLocation = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); + public static string WinchInstallLocation => Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); public static void Main() { - Log.Info("Starting console"); - try - { - Process.Start(Path.Combine(WinchInstallLocation, "WinchConsole.exe")); - } - catch (Exception e) - { - Log.Error($"Could not start console : {e}"); - } - - AppDomain.CurrentDomain.ProcessExit += new EventHandler(OnProcessExit); - try { string metaPath = Path.Combine(WinchInstallLocation, "mod_meta.json"); @@ -77,10 +64,5 @@ public static void Main() Log.Debug("Harmony Patching complete."); } - - public static void OnProcessExit(object sender, EventArgs e) - { - Log.Debug("DREDGE application exited"); - } } } diff --git a/Winch/Logging/Logger.cs b/Winch/Logging/Logger.cs index 30713d9d..3a6512b7 100644 --- a/Winch/Logging/Logger.cs +++ b/Winch/Logging/Logger.cs @@ -2,8 +2,11 @@ using System.Collections.Generic; using System.Diagnostics; using System.IO; +using System.Net.Sockets; +using System.Net; using System.Text.RegularExpressions; using Winch.Config; +using Winch.Core; namespace Winch.Logging { @@ -12,28 +15,54 @@ public class Logger private LogFile? _log; private LogFile? _latestLog; - private bool _writeLogs; + private bool _writeLogsToFile; + private bool _writeLogsToConsole; private LogLevel? _minLogLevel; private LogSocket? _logSocket; + public string LogConsoleExe => Path.Combine(WinchCore.WinchInstallLocation, "WinchConsole.exe"); + public Logger() { - _writeLogs = WinchConfig.GetProperty("WriteLogsToFile", true); - if (!_writeLogs) - return; + _writeLogsToFile = WinchConfig.GetProperty("WriteLogsToFile", true); + if (_writeLogsToFile) + { + _minLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), WinchConfig.GetProperty("LogLevel", "DEBUG")); + _log = new LogFile(); + _latestLog = new LogFile("latest.log"); + CleanupLogs(); + } - _minLogLevel = (LogLevel)Enum.Parse(typeof(LogLevel), WinchConfig.GetProperty("LogLevel", "DEBUG")); - _log = new LogFile(); - _latestLog = new LogFile("latest.log"); + _writeLogsToConsole = WinchConfig.GetProperty("EnableLogConsole", true); - var portStr = WinchConfig.GetProperty("LogPort", string.Empty); - if (!string.IsNullOrEmpty(portStr) && int.TryParse(portStr, out var port)) + if (_writeLogsToConsole) { - _logSocket = new LogSocket(this, port); + // Find an avialable port for the logs + var listener = new TcpListener(IPAddress.Loopback, 0); + listener.Start(); + var port = ((IPEndPoint)listener.LocalEndpoint).Port; + listener.Stop(); + + // Console exe will get the port from the WinchConfig file + WinchConfig.SetProperty("LogPort", $"{port}"); + + Info($"Writing logs to port {port}"); + + try + { + Info($"Starting console at path {LogConsoleExe}"); + Process.Start(LogConsoleExe); + + _logSocket = new LogSocket(this, port); + } + catch (Exception e) + { + Error($"Could not start console : {e}"); + } } - CleanupLogs(); + Info($"Writing logs to file: {_writeLogsToFile}. Writing logs to console: {_writeLogsToConsole}."); } private static void CleanupLogs() @@ -66,21 +95,25 @@ private void Log(LogLevel level, string message) private void Log(LogLevel level, string message, string source) { - _logSocket?.WriteToSocket(new LogMessage() + if (level < _minLogLevel) + return; + + if (_writeLogsToConsole) { - Level = level, - Message = message, - Source = source - }); - - if (!_writeLogs) - return; - if (level < _minLogLevel) - return; - - string logMessage = $"[{GetLogTimestamp()}] [{source}] [{level}] : {message}"; - _log?.Write(logMessage); - _latestLog?.Write(logMessage); + _logSocket?.WriteToSocket(new LogMessage() + { + Level = level, + Message = message, + Source = source + }); + } + + if (_writeLogsToFile) + { + string logMessage = $"[{GetLogTimestamp()}] [{source}] [{level}] : {message}"; + _log?.Write(logMessage); + _latestLog?.Write(logMessage); + } } private string GetLogTimestamp() diff --git a/WinchCommon/Config/DefaultConfig.json b/WinchCommon/Config/DefaultConfig.json index f56e1971..7a732993 100644 --- a/WinchCommon/Config/DefaultConfig.json +++ b/WinchCommon/Config/DefaultConfig.json @@ -4,5 +4,6 @@ "LogsFolder": "Logs", "DetailedLogSources": false, "EnableDeveloperConsole": true, - "MaxLogFiles": 10 + "MaxLogFiles": 10, + "EnableLogConsole": true } \ No newline at end of file diff --git a/WinchConsole/LogSocketListener.cs b/WinchConsole/LogSocketListener.cs index cd8495b9..f072b9d2 100644 --- a/WinchConsole/LogSocketListener.cs +++ b/WinchConsole/LogSocketListener.cs @@ -22,21 +22,15 @@ public LogSocketListener() } - public void Init() + public void Run() { - var listener = new TcpListener(IPAddress.Loopback, 0); - listener.Start(); - _port = ((IPEndPoint)listener.LocalEndpoint).Port; - WriteByType(LogLevel.INFO, $"Found available port {_port}"); - listener.Stop(); - - WinchConfig.SetProperty("LogPort", $"{_port}"); - - SetupSocketListener(); - } + var portStr = WinchConfig.GetProperty("LogPort", string.Empty); + + if (string.IsNullOrEmpty(portStr) || !int.TryParse(portStr, out _port)) + { + return; + } - private void SetupSocketListener() - { WriteByType(LogLevel.INFO, $"Setting up socket listener {_port}"); try { diff --git a/WinchConsole/Program.cs b/WinchConsole/Program.cs index 24f4ca74..f5b1b7d8 100644 --- a/WinchConsole/Program.cs +++ b/WinchConsole/Program.cs @@ -24,7 +24,7 @@ static void Main(string[] args) } else { - new LogSocketListener().Init(); + new LogSocketListener().Run(); } } }