This repository has been archived by the owner on Sep 16, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
322 lines (303 loc) · 13.5 KB
/
Program.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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
using System;
using System.IO;
namespace TimestampLogger
{
public class MainClass
{
public static void Main(string[] args)
{
Settings settings = Settings.Default;
if (ParseArguments(settings, args))
{// Display Help
using (var help_stream = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream("TimestampLogger.help.txt"))
{
using (var help_reader = new StreamReader(help_stream))
{
while (!help_reader.EndOfStream)
Console.WriteLine(help_reader.ReadLine());
}
}
return;
}
if (settings.PrefixDistance < settings.DateFormat.Length)
settings.PrefixDistance = settings.DateFormat.Length > 255 ? (byte)255 : (byte)settings.DateFormat.Length;
if (!Console.IsInputRedirected)
{
settings.PrintToConsole = false;
Console.Error.WriteLine("Console input is not redirected.");
Console.Error.WriteLine("Printing to Console is disabled to prevent double-typed chars.");
}
// TextWriter array init
TextWriter[] writers = new TextWriter[settings.OutputCount];
if (writers.Length == 0)
{
Console.Error.WriteLine("Nowhere to output.");
return;
}
// Output to Console
if (settings.PrintToConsole)
AddWriter(writers, Console.Out);
// Output File
if (!string.IsNullOrEmpty(settings.OutputFile))
{
var writer = new StreamWriter(File.Open(settings.OutputFile, FileMode.Create, FileAccess.Write, FileShare.Read))
{
AutoFlush = true
};
AddWriter(writers, writer);
}
// Output Directory
if (!string.IsNullOrEmpty(settings.OutputDirectory))
{
var now = settings.DateUTC ? DateTime.UtcNow : DateTime.Now;
string path = string.Format(settings.OutputDirectory, now.Day, now.Month, now.DayOfYear, now.Year.ToString("D4"), (now.Year % 100).ToString("D2"));
var writer = new StreamWriter(File.Open(path, FileMode.Create, FileAccess.Write, FileShare.Read))
{
AutoFlush = true
};
AddWriter(writers, writer);
}
// Processing
bool prevCharNewLine = true;
int c;
while ((c = Console.Read()) != -1)
{
if (settings.LineBreak != Settings.LineBreakType.Original) // New Line Tricks
{
if (prevCharNewLine)
{
prevCharNewLine = false;
switch (settings.LineBreak)
{
case Settings.LineBreakType.r:
Write(writers, '\r');
break;
case Settings.LineBreakType.n:
Write(writers, '\n');
break;
case Settings.LineBreakType.rn:
Write(writers, '\r');
Write(writers, '\n');
break;
case Settings.LineBreakType.nr:
Write(writers, '\n');
Write(writers, '\r');
break;
}
WritePrefix(writers, settings);
if (c == '\n' || c == '\r')
continue;
}
else
{
if (c == '\n' || c == '\r')
{
prevCharNewLine = true;
continue;
}
}
}
else
{
if (c == '\n' || c == '\r')
prevCharNewLine = true;
else if (prevCharNewLine)
{
WritePrefix(writers, settings);
prevCharNewLine = false;
}
}
Write(writers, (char)c);
}
// Dispose StreamWriters (files) but not other (Console) TextWriters
for (int writer_i = 0; writer_i < writers.Length; writer_i++)
if (writers[writer_i] is StreamWriter)
writers[writer_i].Dispose();
}
/// <summary>
/// Add writer to first empty (<see cref="null"/>) item in array.
/// </summary>
/// <param name="writers">Array of writers into which we will be adding.</param>
/// <param name="writer">Writer to add.</param>
public static void AddWriter(TextWriter[] writers, TextWriter writer)
{
for (int i = 0; i < writers.Length; i++)
{
if (writers[i] == null)
{
writers[i] = writer;
return;
}
}
}
public static void WritePrefix(TextWriter[] writers, Settings settings)
{
DateTime now = settings.DateUTC ? DateTime.UtcNow : DateTime.Now;
string prefix = now.ToString(settings.DateFormat);
switch (settings.Brackets)
{
default:
case Settings.BracketsType.None:
break;
case Settings.BracketsType.Round:
prefix = "(" + prefix + ")";
break;
case Settings.BracketsType.Square:
prefix = "[" + prefix + "]";
break;
case Settings.BracketsType.Curly:
prefix = "{" + prefix + "}";
break;
case Settings.BracketsType.Angle:
prefix = "<" + prefix + ">";
break;
}
for (int char_i = 0; char_i < prefix.Length; char_i++)
for (int i = 0; i < writers.Length; i++)
writers[i].Write(prefix[char_i]);
for (int index_from_left = prefix.Length; index_from_left < settings.PrefixDistance; index_from_left++)
for (int i = 0; i < writers.Length; i++)
writers[i].Write(' ');
}
public static void Write(TextWriter[] writers, char c)
{
for (int i = 0; i < writers.Length; i++)
writers[i].Write(c);
}
public static bool ParseArguments(Settings settings, string[] arguments)
{
for (int arg_i = 0; arg_i < arguments.Length; arg_i++)
{
string arg = arguments[arg_i];
if (string.IsNullOrEmpty(arg))
continue;
if (arg[0] == '-')
{
if (arg.Length < 2)
{
Console.Error.WriteLine("Unknown argument '{0}'. Too short.", arg);
continue;
}
if (arg[1] == '-') // --
{
if (arg.Length < 2)
{
Console.Error.WriteLine("Unknown argument '{0}'. Too short.", arg);
continue;
}
int equal_index = arg.IndexOf('=', 2);
string key = equal_index == -1 ? arg.Substring(2) : arg.Substring(2, equal_index - 2);
string val = equal_index == -1 || equal_index == arg.Length - 1 ? null : arg.Substring(equal_index + 1);
switch (key)
{
case "help":
return true;
case "output":
case "output-file":
settings.OutputFile = val;
break;
case "output-dir":
case "output-directory":
settings.OutputDirectory = val;
break;
case "format":
settings.DateFormat = val;
break;
case "utc":
case "utc-date":
settings.DateUTC = true;
break;
case "brackets":
{
switch (val.ToLowerInvariant())
{
case "none":
case "empty":
settings.Brackets = Settings.BracketsType.None;
break;
case "round":
settings.Brackets = Settings.BracketsType.Round;
break;
case "square":
settings.Brackets = Settings.BracketsType.Square;
break;
case "curly":
settings.Brackets = Settings.BracketsType.Curly;
break;
case "angle":
settings.Brackets = Settings.BracketsType.Angle;
break;
default:
Console.Error.WriteLine("Unknown brackets type '{0}'.", val);
break;
}
}
break;
case "prefix":
{
if (byte.TryParse(val, out byte prefix_b))
settings.PrefixDistance = prefix_b;
else
Console.Error.WriteLine("Unsupported prefix distance '{0}'. Cannot be >255.", val);
}
break;
case "line-break":
{
switch (val.ToLowerInvariant())
{
case "r":
settings.LineBreak = Settings.LineBreakType.r;
break;
case "n":
settings.LineBreak = Settings.LineBreakType.n;
break;
case "rn":
settings.LineBreak = Settings.LineBreakType.rn;
break;
case "nr":
settings.LineBreak = Settings.LineBreakType.nr;
break;
default:
Console.Error.WriteLine("Unknown line-break type '{0}'.", val);
break;
}
}
break;
case "no-print":
settings.PrintToConsole = false;
break;
default:
Console.Error.WriteLine("Unknown argument key '{0}'.", key);
break;
}
}
else // -
{
for (int arg_char_i = 1; arg_char_i < arg.Length; arg_char_i++)
{
char arg_char = arg[arg_char_i];
switch (arg_char)
{
case 'u':// utc-date
settings.DateUTC = true;
break;
case 'n':// no-print
settings.PrintToConsole = false;
break;
default:
Console.Error.WriteLine("Unknown argument char '{0}'.", arg_char);
break;
}
}
}
}
else
{
Console.Error.WriteLine("Unknown argument '{0}'. Missing minus char.", arg);
continue;
}
}
return false;
}
}
}