-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathLogger.cs
More file actions
163 lines (137 loc) · 4.66 KB
/
Logger.cs
File metadata and controls
163 lines (137 loc) · 4.66 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
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
using System;
using System.Diagnostics;
using System.IO;
namespace FFBitrateViewer
{
public enum LogLevel
{
DEBUG = 0,
INFO = 1,
WARNING = 2,
ERROR = 3
}
public class Logger
{
private string FS;
private StreamWriter? Stream;
private readonly bool AddTimestamp;
private readonly bool Append;
private readonly bool AutoFlush;
private bool IsOpened = false;
private bool IsDisabled = false;
public LogLevel MinLevel { get; private set; }
public string FileName { get { return Path.GetFileName(FS); } }
public Logger(LogLevel minLogLevel, string fs, bool append = false, bool timestamp = false, bool autoflush = false)
{
MinLevel = minLogLevel;
FS = fs;
Append = append;
AddTimestamp = timestamp;
AutoFlush = autoflush;
}
public void Disable()
{
IsDisabled = true;
}
public bool Open(string? fs = null)
{
if (IsDisabled) return false;
if (!string.IsNullOrEmpty(fs) && !string.Equals(fs, FS, StringComparison.InvariantCultureIgnoreCase))
{
FS = fs;
if (IsOpened) Close();
}
if (!IsOpened && !string.IsNullOrEmpty(FS))
{
Stream = new StreamWriter(FS, Append);
IsOpened = true;
}
return IsOpened;
}
public void Log(LogLevel level, string line)
{
if (IsDisabled || string.IsNullOrEmpty(line)) return;
if (level < MinLevel) return;
string logPrefix = "";
string debugPrefix = "";
switch (level)
{
case LogLevel.DEBUG:
logPrefix = "DEBUG: ";
debugPrefix = logPrefix;
break;
case LogLevel.INFO:
debugPrefix = "INFO: ";
break;
case LogLevel.WARNING:
logPrefix = "WARNING: ";
debugPrefix = logPrefix;
break;
case LogLevel.ERROR:
logPrefix = "ERROR: ";
debugPrefix = logPrefix;
break;
}
Debug.WriteLine(debugPrefix + line);
Log(logPrefix + line);
}
//public void Log(List<string> line, char separator = '\t', bool? addTimestamp = null)
//{
// Log(string.Join(separator.ToString(), line), addTimestamp);
//}
public void Log(string line, bool? addTimestamp = null)
{
if (!Open()) return;
if ((addTimestamp ?? AddTimestamp) == true)
{
DateTime now = DateTime.Now;
Stream?.WriteLine($"{now:yyyy-MM-dd HH:mm:ss}\t" + line);
}
else
{
Stream?.WriteLine(line);
}
if (AutoFlush) Stream?.Flush();
}
//public void LogCSV(DataDictionary pairs, int frame, string? exclude = null, char separator = '\t')
//{
// if (!Open()) return;
// if (frame == 0)
// {
// string header = "frame";
// foreach (string s in pairs.Keys)
// {
// if (string.IsNullOrEmpty(exclude) || !exclude.Equals(s))
// {
// if (header != "") header += separator;
// header += s;
// }
// }
// Log(header);
// }
// string line = "" + frame;
// foreach (object? o in pairs.Values)
// {
// string? s = o?.ToString();
// if (string.IsNullOrEmpty(exclude) || !exclude.Equals(s))
// {
// if (line != "") line += separator;
// line += s;
// }
// }
// Log(line);
//}
//public void LogCSV(List<DataDictionary> data, string? frameNoKey = null, char separator = '\t')
//{
// for(int frame = 0; frame < data.Count; ++frame) LogCSV(data[frame], frame, frameNoKey, separator);
//}
public void Close()
{
if (!IsOpened || Stream == null) return;
IsOpened = false;
Stream.Flush();
Stream.Close();
Stream.Dispose();
}
}
}