Skip to content

Commit

Permalink
Merge pull request #607 from ow-mods/dev
Browse files Browse the repository at this point in the history
Updated file logging
  • Loading branch information
misternebula authored Jan 13, 2025
2 parents f43dba8 + 952b07e commit 3b1f165
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 7 deletions.
6 changes: 5 additions & 1 deletion src/OWML.Common/Interfaces/IOwmlConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace OWML.Common
using System;

namespace OWML.Common
{
public interface IOwmlConfig
{
Expand All @@ -25,5 +27,7 @@ public interface IOwmlConfig
bool IncrementalGC { get; set; }

int SocketPort { get; set; }

DateTime LoadTime { get; set; }
}
}
6 changes: 5 additions & 1 deletion src/OWML.Common/OwmlConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using System;
using System.IO;
using Newtonsoft.Json;

namespace OWML.Common
Expand All @@ -17,6 +18,9 @@ public class OwmlConfig : IOwmlConfig
[JsonProperty("incrementalGC")]
public bool IncrementalGC { get; set; }

[JsonProperty("loadTime")]
public DateTime LoadTime { get; set; }

[JsonIgnore]
public bool IsSpaced => Directory.Exists(Path.Combine(GamePath, "Outer Wilds_Data"));

Expand Down
2 changes: 1 addition & 1 deletion src/OWML.Launcher/OWML.Manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"author": "Alek",
"name": "OWML",
"uniqueName": "Alek.OWML",
"version": "2.14.0",
"version": "2.14.1",
"minGameVersion": "1.1.15.1018",
"maxGameVersion": "1.1.15.1018"
}
13 changes: 13 additions & 0 deletions src/OWML.Launcher/Program.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.IO;
using OWML.Abstractions;
using OWML.Common;
using OWML.GameFinder;
Expand All @@ -25,6 +26,7 @@ public static Container CreateContainer(string[] args)
var hasConsolePort = argumentHelper.HasArgument(Constants.ConsolePortArgument);
SaveConsolePort(owmlConfig, hasConsolePort, argumentHelper);
SaveOwmlPath(owmlConfig);
SaveCurrentLogPath(owmlConfig);
var owmlManifest = GetOwmlManifest();
var consoleWriter = CreateConsoleWriter(owmlConfig, owmlManifest, hasConsolePort);

Expand Down Expand Up @@ -79,6 +81,17 @@ private static void SaveOwmlPath(IOwmlConfig owmlConfig)
JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, owmlConfig);
}

private static void SaveCurrentLogPath(IOwmlConfig owmlConfig)
{
if (File.Exists($"{owmlConfig.LogsPath}/latest.txt"))
{
File.Delete($"{owmlConfig.LogsPath}/latest.txt");
}

owmlConfig.LoadTime = DateTime.Now;
JsonHelper.SaveJsonObject(Constants.OwmlConfigFileName, owmlConfig);
}

private static IModManifest GetOwmlManifest() =>
JsonHelper.LoadJsonObject<ModManifest>(Constants.OwmlManifestFileName);

Expand Down
20 changes: 17 additions & 3 deletions src/OWML.Logging/ModLogger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,19 @@ public class ModLogger : IModLogger
{
private readonly IModManifest _manifest;
private static string _logFileName;
private static string _latestFileName;

public ModLogger(IOwmlConfig config, IModManifest manifest)
{
_manifest = manifest;
_logFileName = $"{config.LogsPath}/OWML.Log.{DateTime.Now:dd-MM-yyyy-HH.mm.ss}.txt";
_logFileName = $"{config.LogsPath}/OWML.Log.{config.LoadTime:yyyy-MM-ddTHH.mm.ss}.txt";

if (!Directory.Exists(config.LogsPath))
{
Directory.CreateDirectory(config.LogsPath);
}

_latestFileName = $"{config.LogsPath}/latest.txt";
}

[Obsolete("Use ModHelper.Console.WriteLine with messageType = Debug instead.")]
Expand All @@ -29,7 +32,18 @@ public void Log(string s) =>
public void Log(params object[] objects) =>
Log(string.Join(" ", objects.Select(o => o.ToString()).ToArray()));

private static void LogInternal(string message) =>
File.AppendAllText(_logFileName, $"{DateTime.Now}: {message}{Environment.NewLine}");
private static void LogInternal(string message)
{
var text = $"{DateTime.Now}: {message}{Environment.NewLine}";
try
{
File.AppendAllText(_logFileName, text);
File.AppendAllText(_latestFileName, text);
}
catch
{
// ignored
}
}
}
}
2 changes: 1 addition & 1 deletion src/OWML.ModHelper.Events/HarmonyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ private Harmony CreateInstance()
if (_owmlConfig.DebugMode)
{
_console.WriteLine("Enabling Harmony debug mode.", MessageType.Debug);
FileLog.logPath = $"{_owmlConfig.LogsPath}/Harmony.Log.{DateTime.Now:dd-MM-yyyy-HH.mm.ss}.txt";
FileLog.logPath = $"{_owmlConfig.LogsPath}/Harmony.Log.{_owmlConfig.LoadTime:yyyy-MM-ddTHH.mm.ss}.txt";
HarmonyFileLog.Enabled = true;
}
harmony = new Harmony(_manifest.UniqueName);
Expand Down

0 comments on commit 3b1f165

Please sign in to comment.