summaryrefslogtreecommitdiff
path: root/src/StardewModdingAPI/Framework/Logging/LogFileManager.cs
blob: 1f6ade1d963a3b5b73d8a63b231e626edbe3958e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
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();
        }
    }
}