using VRage.Utils;
namespace PluginLoader;
public static class LogFile
{
private const string fileName = "loader.log";
private static StreamWriter writer;
public static void Init(string mainPath)
{
var file = Path.Combine(mainPath, fileName);
try
{
writer = File.CreateText(file);
}
catch
{
writer = null;
}
}
///
/// Writes the specifed text to the log file.
/// WARNING: Not thread safe!
///
public static void WriteLine(string text, bool gameLog = true)
{
try
{
writer?.WriteLine($"{DateTime.UtcNow:O} {text}");
if (gameLog)
WriteGameLog(text);
writer?.Flush();
}
catch
{
Dispose();
}
}
///
/// Writes the specifed text to the game log file.
/// This function is thread safe.
///
public static void WriteGameLog(string text)
{
MyLog.Default.WriteLine($"[PluginLoader] {text}");
}
public static void WriteTrace(string text, bool gameLog = true)
{
#if DEBUG
writer?.WriteLine($"{DateTime.UtcNow:O} {text}");
if (gameLog)
WriteGameLog($"[PluginLoader] {text}");
writer?.Flush();
#endif
}
public static void Dispose()
{
if (writer == null)
return;
try
{
writer.Flush();
writer.Close();
}
catch
{
}
writer = null;
}
}