#nullable disable using System; using System.IO; namespace StardewModdingAPI.Framework.Logging { /// Manages reading and writing to log file. internal class LogFileManager : IDisposable { /********* ** Fields *********/ /// The underlying stream writer. private readonly StreamWriter Stream; /********* ** Accessors *********/ /// The full path to the log file being written. public string Path { get; } /********* ** Public methods *********/ /// Construct an instance. /// The log file to write. public LogFileManager(string path) { this.Path = path; // create log directory if needed string logDir = System.IO.Path.GetDirectoryName(path); if (logDir == null) throw new ArgumentException($"The log path '{path}' is not valid."); Directory.CreateDirectory(logDir); // open log file stream this.Stream = new StreamWriter(path, append: false) { AutoFlush = true }; } /// Write a message to the log. /// The message to log. public void WriteLine(string message) { // always use Windows-style line endings for convenience // (Linux/macOS editors are fine with them, Windows editors often require them) this.Stream.Write(message + "\r\n"); } /// Release all resources. public void Dispose() { this.Stream.Dispose(); } } }