summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-02-11 01:15:56 -0500
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-02-11 01:15:56 -0500
commit46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f (patch)
tree905b16cb83e0d47d7ae89b4ffcfaa2a02630cb6d /src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
parent3e91af6b06deec6aa2dca80945c82af528094c52 (diff)
downloadSMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.tar.gz
SMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.tar.bz2
SMAPI-46b7d7a4001e209d7201ed5cad38cad2f1ad2a7f.zip
redirect the game's debug messages into trace logs (#233)
The game writes debug messages directly to the console, which shows up for SMAPI users. This commit redirects direct console messages to a monitor.
Diffstat (limited to 'src/StardewModdingAPI/Framework/Logging/LogFileManager.cs')
-rw-r--r--src/StardewModdingAPI/Framework/Logging/LogFileManager.cs48
1 files changed, 48 insertions, 0 deletions
diff --git a/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs b/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
new file mode 100644
index 00000000..1f6ade1d
--- /dev/null
+++ b/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
@@ -0,0 +1,48 @@
+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;
+
+
+ /*********
+ ** Public methods
+ *********/
+ /// <summary>Construct an instance.</summary>
+ /// <param name="path">The log file to write.</param>
+ public LogFileManager(string path)
+ {
+ // create log directory if needed
+ string logDir = 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();
+ }
+ }
+}