-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLog.cs
240 lines (218 loc) · 7.62 KB
/
Log.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
//===----------------------------------------------------------------------===//
//
// Violet Styler
//
//===----------------------------------------------------------------------===//
//
// Copyright (C) 2021. violet-team. All Rights Reserved.
//
//===----------------------------------------------------------------------===//
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Xml.Serialization;
namespace violet_styler
{
/// <summary>
/// Contains all instance information.
/// </summary>
public class InstanceMonitor
{
public static Dictionary<string, object> Instances = new Dictionary<string, object>();
}
/// <summary>
/// Class to make lazy instance easier to implement.
/// </summary>
/// <typeparam name="T"></typeparam>
public class ILazy<T>
where T : new()
{
private static readonly Lazy<T> instance = new Lazy<T>(() =>
{
T instance = new T();
InstanceMonitor.Instances.Add(instance.GetType().Name.ToLower(), instance);
return instance;
});
public static T Instance => instance.Value;
public static bool IsValueCreated => instance.IsValueCreated;
}
public class Logs : ILazy<Logs>
{
/// <summary>
/// Serialize an object.
/// </summary>
/// <param name="toSerialize"></param>
/// <returns></returns>
public static string SerializeObject(object toSerialize)
{
try
{
return JsonConvert.SerializeObject(toSerialize, Formatting.Indented, new JsonSerializerSettings
{
ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});
}
catch
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(toSerialize.GetType());
using (StringWriter textWriter = new StringWriter())
{
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
}
catch
{
return toSerialize.ToString();
}
}
}
public delegate void NotifyEvent(object sender, EventArgs e);
event EventHandler LogCollectionChange;
event EventHandler LogErrorCollectionChange;
event EventHandler LogWarningCollectionChange;
object event_lock = new object();
/// <summary>
/// Attach your own notify event.
/// </summary>
/// <param name="notify_event"></param>
public void AddLogNotify(NotifyEvent notify_event)
{
LogCollectionChange += new EventHandler(notify_event);
}
public void ClearLogNotify()
{
LogCollectionChange = null;
}
/// <summary>
/// Attach your own notify event.
/// </summary>
/// <param name="notify_event"></param>
public void AddLogErrorNotify(NotifyEvent notify_event)
{
LogErrorCollectionChange += new EventHandler(notify_event);
}
/// <summary>
/// Attach your own notify event.
/// </summary>
/// <param name="notify_event"></param>
public void AddLogWarningNotify(NotifyEvent notify_event)
{
LogWarningCollectionChange += new EventHandler(notify_event);
}
/// <summary>
/// Push some message to log.
/// </summary>
/// <param name="str"></param>
public void Push(string str)
{
write_log(DateTime.Now, str);
lock (event_lock) LogCollectionChange?.Invoke(Tuple.Create(DateTime.Now, str, false), null);
}
/// <summary>
/// Push some object to log.
/// </summary>
/// <param name="obj"></param>
public void Push(object obj)
{
write_log(DateTime.Now, obj.ToString());
write_log(DateTime.Now, SerializeObject(obj));
lock (event_lock)
{
LogCollectionChange?.Invoke(Tuple.Create(DateTime.Now, obj.ToString(), false), null);
LogCollectionChange?.Invoke(Tuple.Create(DateTime.Now, SerializeObject(obj), true), null);
}
}
/// <summary>
/// Push some message to log.
/// </summary>
/// <param name="str"></param>
public void PushError(string str)
{
write_error_log(DateTime.Now, str);
lock (event_lock) LogErrorCollectionChange?.Invoke(Tuple.Create(DateTime.Now, str, false), null);
}
/// <summary>
/// Push some object to log.
/// </summary>
/// <param name="obj"></param>
public void PushError(object obj)
{
write_error_log(DateTime.Now, obj.ToString());
write_error_log(DateTime.Now, SerializeObject(obj));
lock (event_lock)
{
LogErrorCollectionChange?.Invoke(Tuple.Create(DateTime.Now, obj.ToString(), false), null);
LogErrorCollectionChange?.Invoke(Tuple.Create(DateTime.Now, SerializeObject(obj), true), null);
}
}
/// <summary>
/// Push some message to log.
/// </summary>
/// <param name="str"></param>
public void PushWarning(string str)
{
write_warning_log(DateTime.Now, str);
lock (event_lock) LogWarningCollectionChange?.Invoke(Tuple.Create(DateTime.Now, str, false), null);
}
/// <summary>
/// Push some object to log.
/// </summary>
/// <param name="obj"></param>
public void PushWarning(object obj)
{
write_warning_log(DateTime.Now, obj.ToString());
write_warning_log(DateTime.Now, SerializeObject(obj));
lock (event_lock)
{
LogWarningCollectionChange?.Invoke(Tuple.Create(DateTime.Now, obj.ToString(), false), null);
LogWarningCollectionChange?.Invoke(Tuple.Create(DateTime.Now, SerializeObject(obj), true), null);
}
}
public void PushException(Exception e)
{
PushError($"Message: {e.Message}\n" + e.StackTrace);
if (e.InnerException != null)
PushException(e.InnerException);
}
public void Panic()
{
Environment.Exit(1);
}
object log_lock = new object();
private void write_log(DateTime dt, string message)
{
CultureInfo en = new CultureInfo("en-US");
lock (log_lock)
{
File.AppendAllText("log.txt", $"[{dt.ToString(en)}] {message}\r\n");
}
}
private void write_error_log(DateTime dt, string message)
{
CultureInfo en = new CultureInfo("en-US");
lock (log_lock)
{
File.AppendAllText("log.txt", $"[{dt.ToString(en)}] [Error] {message}\r\n");
}
}
private void write_warning_log(DateTime dt, string message)
{
CultureInfo en = new CultureInfo("en-US");
lock (log_lock)
{
File.AppendAllText("log.txt", $"[{dt.ToString(en)}] [Warning] {message}\r\n");
}
}
}
}