-
Notifications
You must be signed in to change notification settings - Fork 0
/
Logger.cs
289 lines (288 loc) · 12.2 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
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
using System.IO.Compression;
public class Logger {
private static string barFull = "█", barEmpty = " ";
private static string? logfilename;
private static StreamWriter? logstream;
/// <summary>
/// Function to initialize the file logging
/// (<paramref name="log"/>)
/// </summary>
/// <param name="log">If the logger has to log to file</param>
public static void InitializeLogging(bool log) {
if(!log) return;
logfilename = LongTimeString() + ".log";
IEnumerable<string> logfiles = Directory.EnumerateFiles("./", "*.log");
foreach(string item in logfiles) {
Compress(item);
}
logstream = File.CreateText(logfilename);
}
/// <summary>
/// Function to reopen the log stream
/// </summary>
public static void ReinitializeLogging() {
if(logfilename != null) logstream = new StreamWriter(logfilename, append: true);
}
/// <summary>
/// Function to close the log stream
/// </summary>
public static void TerminateLogging() {
if(logstream != null) {
logstream.Close();
}
}
/// <summary>
/// Function to output a success message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Success(string message) {
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Green;
Console.Write("(Success) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLine(time + "(Success) " + message);
}
/// <summary>
/// Function to output an info message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Info(string message) {
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Cyan;
Console.Write("(Info) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
}
/// <summary>
/// Function to output a warning message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Warning(string message) {
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Yellow;
Console.Write("(Warning) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLine(time + "(Warning) " + message);
}
/// <summary>
/// Function to output an error message
/// (<paramref name="message"/>)
/// </summary>
/// <param name="message">The message to output</param>
public static void Error(string message) {
string time = TimeString();
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(time);
Console.ForegroundColor = ConsoleColor.Red;
Console.Write("(Error) ");
Console.ForegroundColor = ConsoleColor.White;
Console.WriteLine(message);
Console.ResetColor();
if(logstream != null) logstream.WriteLine(time + "(Error) " + message);
}
/// <summary>
/// Function to clear the last console line
///(<paramref name="line"/>)
/// </summary>
/// <param name="line">The line to remove, default 1</param>
public static void RemoveLine(Int16 line = 1) {
Int32 currentLineCursor = Console.CursorTop;
Console.SetCursorPosition(0, currentLineCursor - line);
for(Int32 i = 0; i < Console.WindowWidth; i++)
Console.Write(" ");
Console.SetCursorPosition(0, currentLineCursor - line);
}
/// <summary>
/// Function to get the string time
/// </summary>
/// <returns>The string time, hh:mm:ss.msmsms</returns>
public static string TimeString() {
int hour = DateTime.Now.Hour, minute = DateTime.Now.Minute,
second = DateTime.Now.Second, millisecond = DateTime.Now.Millisecond;
return "[" + (hour < 10 ? "0" : "") + hour.ToString() + ":" + (minute < 10 ? "0" : "") + minute.ToString() + ":" +
(second < 10 ? "0" : "") + second.ToString() + "." +
(millisecond < 100 ? (millisecond < 10 ? "00" : "0") : "") + millisecond.ToString() + "] ";
}
/// <summary>
/// Function to get the long string time
/// </summary>
/// <returns>The long string time, YYYY-MM-DD_hh.mm.ss.msmsms</returns>
public static string LongTimeString() {
int year = DateTime.Now.Year, month = DateTime.Now.Month, day = DateTime.Now.Day,
hour = DateTime.Now.Hour, minute = DateTime.Now.Minute,
second = DateTime.Now.Second, millisecond = DateTime.Now.Millisecond;
return year.ToString() + "-" + (month < 10 ? "0" : "") + month.ToString() + "-" +
(day < 10 ? "0" : "") + day.ToString() + "_" + (hour < 10 ? "0" : "") + hour.ToString() + "." +
(minute < 10 ? "0" : "") + minute.ToString() + "." + (second < 10 ? "0" : "") + second.ToString() + "." +
(millisecond < 100 ? (millisecond < 10 ? "00" : "0") : "") + millisecond.ToString();
}
/// <summary>
/// Function to print a progress bar string
/// (<paramref name="currentSize"/>, <paramref name="totalSize"/>, <paramref name="currentElements"/>, <paramref name="totalElements"/>)
/// </summary>
/// <param name="currentSize">The current size</param>
/// <param name="totalSize">The total size</param>
/// <param name="currentElements">The current number of elements</param>
/// <param name="totalElements">The total number of elements</param>
public static void ProgressBar(UInt64 currentSize, UInt64 totalSize, Int32 currentElements, Int32 totalElements) {
string bar = "[";
Int16 percent = (Int16)((float)currentSize / totalSize * 100);
for(Int16 i = 1; i <= percent; i++) {
bar += barFull;
}
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write(bar);
bar = "";
for(Int16 i = (Int16)(percent + 1); i <= 100; i++) {
bar += barEmpty;
}
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.Write(bar);
bar = "] " + percent.ToString() + "% (" + HumanReadableSize(currentSize) + "/" + HumanReadableSize(totalSize) + ") (" +
currentElements + "/" + totalElements + ")";
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(bar);
Console.ResetColor();
}
/// <summary>
/// Function to print a progress bar string, showing only item number progress
/// (<paramref name="currentElements"/>, <paramref name="totalElements"/>)
/// </summary>
/// <param name="currentElements">The current number of elements</param>
/// <param name="totalElements">The total number of elements</param>
public static void ProgressBarItemsOnly(Int32 currentElements, Int32 totalElements) {
string bar = "[";
Int16 percent = (Int16)((float)currentElements / totalElements * 100);
for(Int16 i = 1; i <= percent; i++) {
bar += barFull;
}
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.Write(bar);
bar = "";
for(Int16 i = (Int16)(percent + 1); i <= 100; i++) {
bar += barEmpty;
}
Console.BackgroundColor = ConsoleColor.DarkGray;
Console.Write(bar);
bar = "] " + percent.ToString() + "% (" + currentElements + "/" + totalElements + ")";
Console.ResetColor();
Console.ForegroundColor = ConsoleColor.DarkYellow;
Console.WriteLine(bar);
Console.ResetColor();
}
/// <summary>
/// Function to print a message of the file that is being copied
/// (<paramref name="reason"/>, <paramref name="file"/>)
/// </summary>
/// <param name="reason">The reason</param>
/// <param name="file">The name of the file</param>
public static void InfoReason(Reason reason, string file, UInt64? size = null) {
string line = "";
switch(reason) {
case Reason.CopyNotThere:
line = "Copying because not there: ";
break;
case Reason.CopyDifferentSize:
line = "Copying because different size: ";
break;
case Reason.CopyDifferentContent:
line = "Copying because different content: ";
break;
case Reason.Remove:
line = "Removing: ";
break;
}
Info(line + file + " (" + ((size != null) ? HumanReadableSize((UInt64)size) : "folder") + ")");
}
/// <summary>
/// Function to print a message of the file that has been copied
/// (<paramref name="reason"/>, <paramref name="file"/>)
/// </summary>
/// <param name="reason">The reason</param>
/// <param name="file">The name of the file</param>
public static void SuccessReason(Reason reason, string file, UInt64? size = null) {
string line = "";
switch(reason) {
case Reason.CopyNotThere:
line = "Copied because not there: ";
break;
case Reason.CopyDifferentSize:
line = "Copied because different size: ";
break;
case Reason.CopyDifferentContent:
line = "Copied because different content: ";
break;
case Reason.Remove:
line = "Removed: ";
break;
}
Success(line + file + " (" + ((size != null) ? HumanReadableSize((UInt64)size) : "folder") + ")");
}
/// <summary>
/// Function to convert a file size into a readable format
/// (<paramref name="size"/>)
/// </summary>
/// <param name="size">The size in bytes</param>
/// <returns>A string with size and unit</returns>
public static string HumanReadableSize(UInt64 size) {
UInt16 unit = 1024;
// Bytes
if(size < unit) return size.ToString() + "B";
// KiBytes
UInt64 KiBytes = (UInt64)Math.Floor((float)size / unit);
UInt16 Bytes = (UInt16)(size % unit);
if(KiBytes < unit) return KiBytes.ToString() + "KiB&" + Bytes.ToString() + "B";
// MiBytes
UInt32 MiBytes = (UInt32)Math.Floor((float)KiBytes / unit);
KiBytes %= unit;
if(MiBytes < unit) return MiBytes.ToString() + "MiB&" + KiBytes.ToString() + "KiB";
UInt16 GiBytes = (UInt16)Math.Floor((float)MiBytes / unit);
MiBytes %= unit;
return GiBytes.ToString() + "GiB&" + MiBytes.ToString() + "MiB";
}
/// <summary>
/// Function to compress a file to .gz
/// (<paramref name="filename"/>)
/// </summary>
/// <param name="filename">The name of the file to compress</param>
public static void Compress(string filename) {
FileInfo fi = new FileInfo(filename);
// Get the stream of the source file.
using (FileStream inFile = fi.OpenRead()) {
// Prevent compressing hidden and
// already compressed files.
if((File.GetAttributes(fi.FullName) & FileAttributes.Hidden) != FileAttributes.Hidden) {
// Create the compressed file.
using (FileStream outFile = File.Create(fi.FullName + ".gz")) {
using (GZipStream Compress = new GZipStream(outFile, CompressionMode.Compress)) {
// Copy the source file into
// the compression stream.
inFile.CopyTo(Compress);
}
}
}
}
try {
File.Delete(filename);
Info("Compressed " + filename);
}
catch(Exception e) {
Warning("Could not compress " + filename + ", error: " + e);
}
}
}