diff options
author | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-14 11:44:02 -0400 |
---|---|---|
committer | Jesse Plamondon-Willard <github@jplamondonw.com> | 2017-10-14 11:44:02 -0400 |
commit | 79118316065a01322d8ea12a14589ec016794c32 (patch) | |
tree | 7a26668a66ea0630a2b9367ac820fe7a6d99ac77 /src/SMAPI/Framework/Logging/LogFileManager.cs | |
parent | af1a2bde8219c5d4b8660b13702725626a4a5647 (diff) | |
parent | 8aec1eff99858716afe7b8604b512181f78c214f (diff) | |
download | SMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.gz SMAPI-79118316065a01322d8ea12a14589ec016794c32.tar.bz2 SMAPI-79118316065a01322d8ea12a14589ec016794c32.zip |
Merge branch 'develop' into stable
Diffstat (limited to 'src/SMAPI/Framework/Logging/LogFileManager.cs')
-rw-r--r-- | src/SMAPI/Framework/Logging/LogFileManager.cs | 57 |
1 files changed, 57 insertions, 0 deletions
diff --git a/src/SMAPI/Framework/Logging/LogFileManager.cs b/src/SMAPI/Framework/Logging/LogFileManager.cs new file mode 100644 index 00000000..8cfe0527 --- /dev/null +++ b/src/SMAPI/Framework/Logging/LogFileManager.cs @@ -0,0 +1,57 @@ +using System; +using System.IO; + +namespace StardewModdingAPI.Framework.Logging +{ + /// <summary>Manages reading and writing to log file.</summary> + internal class LogFileManager : IDisposable + { + /********* + ** Properties + *********/ + /// <summary>The underlying stream writer.</summary> + private readonly StreamWriter Stream; + + + /********* + ** Accessors + *********/ + /// <summary>The full path to the log file being written.</summary> + public string Path { get; } + + + /********* + ** Public methods + *********/ + /// <summary>Construct an instance.</summary> + /// <param name="path">The log file to write.</param> + 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 }; + } + + /// <summary>Write a message to the log.</summary> + /// <param name="message">The message to log.</param> + public void WriteLine(string message) + { + // always use Windows-style line endings for convenience + // (Linux/Mac editors are fine with them, Windows editors often require them) + this.Stream.Write(message + "\r\n"); + } + + /// <summary>Release all resources.</summary> + public void Dispose() + { + this.Stream.Dispose(); + } + } +} |