-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.cs
More file actions
74 lines (62 loc) · 2.08 KB
/
Log.cs
File metadata and controls
74 lines (62 loc) · 2.08 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System;
using System.IO;
using System.Threading;
namespace maple
{
static class Log
{
public static string LogPath { get; private set; }
public static int ImportantEvents { get; private set; } = 0;
public static int DebugEvents { get; private set; } = 0;
private static ReaderWriterLock rwLock = new ReaderWriterLock();
private const int WriterLockTimeout = 500;
public static void InitializeLogger()
{
LogPath = Settings.MapleDirectory + "\\log.txt";
File.CreateText(LogPath).Close();
Write("New logging session started at " + DateTime.Now.ToString("HH:mm:ss"), "logger");
#if DEBUG
Write("Maple is running under a debugger, additional events may be logged", "logger");
#endif
}
public static void DisableLogging()
{
File.Delete(LogPath);
}
public static void Write(string text, string speaker, bool important = false)
{
if (!Settings.Properties.EnableLogging) return;
string template = "[{0}]: {1}\n";
if (important)
{
template = "!!! " + template;
ImportantEvents++;
}
try
{
rwLock.AcquireWriterLock(WriterLockTimeout);
File.AppendAllText(LogPath, String.Format(template, speaker, text));
}
finally
{
rwLock.ReleaseWriterLock();
}
}
public static void WriteDebug(string text, string speaker)
{
if (!Settings.Properties.EnableLogging) return;
#if DEBUG
try
{
rwLock.AcquireWriterLock(WriterLockTimeout);
File.AppendAllText(LogPath, String.Format("DEBUG [{0}]: {1}\n", speaker, text));
DebugEvents++;
}
finally
{
rwLock.ReleaseWriterLock();
}
#endif
}
}
}