Skip to content

Commit

Permalink
Fix console requiring a restart, added EnableLogConsole winchconfig o…
Browse files Browse the repository at this point in the history
…ption
  • Loading branch information
xen-42 committed Dec 26, 2023
1 parent bd71508 commit c5b698c
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 59 deletions.
20 changes: 1 addition & 19 deletions Winch/Core/WinchCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -16,22 +15,10 @@ public class WinchCore

public static Dictionary<string, object> 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");
Expand Down Expand Up @@ -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");
}
}
}
83 changes: 58 additions & 25 deletions Winch/Logging/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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()
Expand Down Expand Up @@ -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()
Expand Down
3 changes: 2 additions & 1 deletion WinchCommon/Config/DefaultConfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@
"LogsFolder": "Logs",
"DetailedLogSources": false,
"EnableDeveloperConsole": true,
"MaxLogFiles": 10
"MaxLogFiles": 10,
"EnableLogConsole": true
}
20 changes: 7 additions & 13 deletions WinchConsole/LogSocketListener.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
2 changes: 1 addition & 1 deletion WinchConsole/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static void Main(string[] args)
}
else
{
new LogSocketListener().Init();
new LogSocketListener().Run();
}
}
}
Expand Down

0 comments on commit c5b698c

Please sign in to comment.