-
Notifications
You must be signed in to change notification settings - Fork 8
/
Logger.cs
104 lines (86 loc) · 2.84 KB
/
Logger.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
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
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace BagOfTricks {
public class Logger {
private String _path;
private bool _removeHtmlTags = true;
public bool RemoveHtmlTags { get => _removeHtmlTags; set => _removeHtmlTags = value; }
private bool _useTimeStamp = true;
public bool UseTimeStamp { get => _useTimeStamp; set => _useTimeStamp = value; }
public Logger() : this(Storage.bagOfTicksLogFile) {
}
public Logger(String fileName, String fileExtension = ".log") {
_path = Path.Combine(Storage.modEntryPath, (fileName + fileExtension));
Clear();
}
public void Log(string str) {
if (_removeHtmlTags) {
str = Common.RemoveHtmlTags(str);
}
if (UseTimeStamp) {
ToFile(TimeStamp() + " " + str);
}
else {
ToFile(str);
}
}
private static string TimeStamp() {
return "[" + DateTime.Now.ToString("yyyy-MM-dd_HH:mm:ss.ff") + "]";
}
private void ToFile(string s) {
try {
using (StreamWriter stream = File.AppendText(_path)) {
stream.WriteLine(s);
}
}
catch (Exception e) {
Main.modLogger.Log(e.ToString());
}
}
public void Clear() {
if (File.Exists(_path)) {
try {
File.Delete(_path);
using (File.Create(_path)) {
}
}
catch (Exception e) {
Main.modLogger.Log(e.ToString());
}
}
}
}
public class HtmlLogger : Logger {
public HtmlLogger() : this(Storage.bagOfTicksLogFile) {
}
public HtmlLogger(String fileName) : base(fileName, ".html") {
this.RemoveHtmlTags = false;
this.UseTimeStamp = false;
}
public new void Log(string str) {
str = Common.UnityRichTextToHtml(str);
base.Log(str);
}
}
public static class LoggerUtils {
public static void InitBagOfTrickLogger() {
if (Main.botLoggerLog == null) {
Main.botLoggerLog = new Logger();
}
}
public static void InitBattleLogDefaultLogger() {
if (Main.battleLoggerLog == null) {
Main.battleLoggerLog = new Logger(Storage.battleLogFile);
}
}
public static void InitBattleLogHtmlLogger() {
if (Main.battleLoggerHtml == null) {
Main.battleLoggerHtml = new HtmlLogger(Storage.battleLogFile);
}
}
}
}