Skip to content

Commit

Permalink
feat: improve our logger to write our logs to specific file as well a…
Browse files Browse the repository at this point in the history
…s propagate to bepinex
  • Loading branch information
nwesterhausen committed Oct 30, 2024
1 parent 3a27595 commit 6647822
Showing 1 changed file with 60 additions and 7 deletions.
67 changes: 60 additions & 7 deletions DiscordConnector/Logger.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
namespace DiscordConnector;
using System.IO;
using System.Threading.Tasks;

namespace DiscordConnector;
class VDCLogger
{
private static BepInEx.Logging.ManualLogSource _logger;
private static string _logFilePath;
private bool _logDebugMessages = false;
private static readonly string LOG_NAME = "vdc.log";

public VDCLogger(BepInEx.Logging.ManualLogSource logger)
{
_logger = logger;
_logFilePath = System.IO.Path.Combine(BepInEx.Paths.ConfigPath, PluginInfo.PLUGIN_ID, LOG_NAME);
InitializeLogFile();
_logger.LogInfo("Logger initialized.");
}

Expand All @@ -15,32 +22,78 @@ internal void SetLogLevel(bool logDebugMessages)
_logDebugMessages = logDebugMessages;
}

public void LogDebug(string message)
private void InitializeLogFile()
{
if (File.Exists(_logFilePath))
{
string oldLogFilePath = $"{_logFilePath}.old";
File.Move(_logFilePath, oldLogFilePath);
_logger.LogInfo($"Existing log file moved to {oldLogFilePath}");
}
}

/// <summary>
/// Write to a log for just this plugin. This will be in "BepInEx/config/plugin-id/vdc.log".
/// </summary>
/// <param name="severity">The severity to include, e.g. "WARN" or "DEBUG"</param>
/// <param name="message">The message to log</param>
/// <returns>
/// This will attempt to write to the log file. If it fails, it will log an error to the BepInEx logger.
///
/// Nothing is returned from this method.
/// </returns>
private async Task LogToFileAsync(string severity, string message)
{
_logger.LogDebug(message);
try
{
using (StreamWriter writer = new StreamWriter(_logFilePath, true))
{
await writer.WriteLineAsync($"{System.DateTime.Now} [{severity}]: {message}");
}
}
catch (System.Exception ex)
{
_logger.LogError($"Error writing to log file: {ex.Message}");
}
}


public async Task LogDebugAsync(string message)
{
await LogToFileAsync("DEBUG", message);
if (_logDebugMessages)
{
_logger.LogInfo(message);
}
}

public void LogInfo(string message)
public async Task LogInfoAsync(string message)
{
await LogToFileAsync("INFO", message);
_logger.LogInfo(message);
}

public void LogWarning(string message)
public async Task LogWarningAsync(string message)
{
await LogToFileAsync("WARNING", message);
_logger.LogWarning(message);
}

public void LogError(string message)
public async Task LogErrorAsync(string message)
{
await LogToFileAsync("ERROR", message);
_logger.LogError(message);
}

public void LogFatal(string message)
public async Task LogFatalAsync(string message)
{
await LogToFileAsync("FATAL", message);
_logger.LogFatal(message);
}

public void LogDebug(string message) => LogDebugAsync(message).GetAwaiter().GetResult();
public void LogInfo(string message) => LogInfoAsync(message).GetAwaiter().GetResult();
public void LogWarning(string message) => LogWarningAsync(message).GetAwaiter().GetResult();
public void LogError(string message) => LogErrorAsync(message).GetAwaiter().GetResult();
public void LogFatal(string message) => LogFatalAsync(message).GetAwaiter().GetResult();
}

0 comments on commit 6647822

Please sign in to comment.