-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathLog.cs
More file actions
113 lines (98 loc) · 2.99 KB
/
Log.cs
File metadata and controls
113 lines (98 loc) · 2.99 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
using System;
using System.Diagnostics;
using System.IO;
namespace LiveSplit.VAS
{
public static class Log
{
private const string PREFIX = "[VAS] ";
private const string STANDARD_FORMAT = "{0} {1}";
private static TextWriter _TextWriter = new StringWriter();
public static bool VerboseEnabled { get; set; } = true;
public static bool WriteToFileEnabled { get; set; } = true;
public static event EventHandler<string> LogUpdated;
static Log()
{
try
{
if (!EventLog.SourceExists("VideoAutoSplit"))
EventLog.CreateEventSource("VideoAutoSplit", "Application");
}
catch { }
try
{
var listener = new EventLogTraceListener("VideoAutoSplit");
listener.Filter = new EventTypeFilter(SourceLevels.Warning);
Trace.Listeners.Add(listener);
}
catch { }
}
public static string ReadAll() => _TextWriter.ToString();
private static void Write(string message)
{
try
{
var str = "[" + DateTime.Now.ToString("hh:mm:ss.fff") + "] " + message;
_TextWriter.WriteLine(str);
LogUpdated?.Invoke(null, str);
if (WriteToFileEnabled) WriteToFile(str);
}
catch { }
}
private static void WriteToFile(string message)
{
try
{
using (var fs = new FileStream(@"VASErrorLog.txt", FileMode.Append, FileAccess.Write))
using (var sw = new StreamWriter(fs))
{
sw.WriteLineAsync(message);
}
}
catch { }
}
public static void Verbose(string message)
{
if (VerboseEnabled) Info(message);
}
public static void Info(string message)
{
try
{
Trace.TraceInformation(STANDARD_FORMAT, PREFIX, message);
Write(message);
}
catch { }
}
public static void Warning(string message)
{
try
{
Trace.TraceWarning(STANDARD_FORMAT, PREFIX, message);
Write(message);
}
catch { }
}
public static void Error(Exception ex, string description)
{
try
{
Trace.TraceError(STANDARD_FORMAT, PREFIX, description);
Write(description);
Trace.TraceError("{0}\n\n{1}", ex.Message, ex.StackTrace);
Write(ex.Message);
Write(ex.StackTrace);
}
catch { }
}
public static void Flush()
{
try
{
_TextWriter.Flush();
_TextWriter = new StringWriter();
}
catch { }
}
}
}