-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cs
77 lines (50 loc) · 1.53 KB
/
Logging.cs
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
using System;
using System.IO;
using System.Text;
using System.Collections.Generic;
using Sandbox.ModAPI;
internal class Logger {
private static Dictionary<String, Logger> INSTANCE_MAP = new Dictionary<string, Logger>();
private readonly StringBuilder cache = new StringBuilder();
private readonly TextWriter textWriter;
private Logger(string logFile) {
try {
textWriter = MyAPIGateway.Utilities.WriteFileInLocalStorage(logFile, typeof(Logger));
} catch {
}
}
public static Logger getLogger(String fileName) {
if (MyAPIGateway.Utilities == null)
return null;
Logger logger = null;
if (INSTANCE_MAP.TryGetValue(fileName, out logger))
return logger;
logger = new Logger(fileName + ".log");
INSTANCE_MAP.Add(fileName, logger);
return logger;
}
public void WriteLine(string text) {
try {
if (cache.Length > 0)
textWriter.WriteLine(cache);
cache.Clear();
cache.Append(DateTime.Now.ToString("[HH:mm:ss] "));
textWriter.WriteLine(cache.Append(text));
textWriter.Flush();
cache.Clear();
} catch {
}
}
public void Write(string text) {
cache.Append(text);
}
internal void Close() {
try {
if (cache.Length > 0)
textWriter.WriteLine(cache);
textWriter.Flush();
textWriter.Close();
} catch {
}
}
}