Skip to content

Instantly share code, notes, and snippets.

@thatcosmonaut
Last active August 2, 2023 00:58
Show Gist options
  • Save thatcosmonaut/cdf4a2efb88a9b4186e9c335a0a0ef3a to your computer and use it in GitHub Desktop.
Save thatcosmonaut/cdf4a2efb88a9b4186e9c335a0a0ef3a to your computer and use it in GitHub Desktop.
Samurai Gunn 2 Logger System
using System.IO;
namespace SamuraiGunn2
{
public static class Logger
{
private static StreamWriter streamWriter;
public static void SetLogOutputFile(string filePath)
{
if (streamWriter != null)
{
streamWriter.Close();
}
streamWriter = new StreamWriter(filePath);
}
public static void WriteLine(string message)
{
if (streamWriter != null)
{
streamWriter.WriteLine(message);
streamWriter.Flush();
}
System.Console.WriteLine(message);
}
public static void Finish()
{
if (streamWriter != null)
{
streamWriter.Close();
streamWriter = null;
}
}
}
}
/* SOMEWHERE NEAR THE TOP OF MAIN... */
if (!System.IO.Directory.Exists(Title.UserDataDirectory))
{
System.IO.Directory.CreateDirectory(Title.UserDataDirectory);
}
Logger.SetLogOutputFile(System.IO.Path.Combine(Title.UserDataDirectory, "log.txt"));
// configure logging
MoonWorks.Logger.LogInfo = Logger.WriteLine;
MoonWorks.Logger.LogWarn = Logger.WriteLine;
MoonWorks.Logger.LogError = Logger.WriteLine;
using System.IO;
namespace SamuraiGunn2
{
public static class Title
{
public static string UserDataDirectory = $"{Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.LocalApplicationData), "SamuraiGunn2MoonWorks")}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment