forked from LogExperts/LogExpert
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSquareBracketColumnizer.cs
More file actions
319 lines (270 loc) · 9.77 KB
/
Copy pathSquareBracketColumnizer.cs
File metadata and controls
319 lines (270 loc) · 9.77 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
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
using System.Globalization;
using System.Text.RegularExpressions;
using static LogExpert.Core.Classes.Columnizer.TimeFormatDeterminer;
namespace LogExpert.Core.Classes.Columnizer;
public class SquareBracketColumnizer : ILogLineColumnizer, IColumnizerPriority
{
#region ILogLineColumnizer implementation
private int _timeOffset;
private readonly TimeFormatDeterminer _timeFormatDeterminer = new();
// TODO: need preparing this columnizer with sample log lines before use it.
private int _columnCount = 5;
private bool _isTimeExists;
public SquareBracketColumnizer ()
{
}
public SquareBracketColumnizer (int columnCount, bool isTimeExists) : this()
{
// Add message column
_columnCount = columnCount + 1;
_isTimeExists = isTimeExists;
if (_isTimeExists)
{
// Time and date
_columnCount += 2;
}
}
public bool IsTimeshiftImplemented ()
{
return true;
}
public void SetTimeOffset (int msecOffset)
{
_timeOffset = msecOffset;
}
public int GetTimeOffset ()
{
return _timeOffset;
}
public DateTime GetTimestamp (ILogLineColumnizerCallback callback, ILogLine line)
{
IColumnizedLogLine cols = SplitLine(callback, line);
if (cols == null || cols.ColumnValues == null || cols.ColumnValues.Length < 2)
{
return DateTime.MinValue;
}
if (cols.ColumnValues[0].FullValue.Length == 0 || cols.ColumnValues[1].FullValue.Length == 0)
{
return DateTime.MinValue;
}
FormatInfo formatInfo = _timeFormatDeterminer.DetermineDateTimeFormatInfo(line.FullLine);
if (formatInfo == null)
{
return DateTime.MinValue;
}
try
{
var dateTime = DateTime.ParseExact(
cols.ColumnValues[0].FullValue + " " + cols.ColumnValues[1].FullValue, formatInfo.DateTimeFormat,
formatInfo.CultureInfo);
return dateTime;
}
catch (Exception)
{
return DateTime.MinValue;
}
}
public void PushValue (ILogLineColumnizerCallback callback, int column, string value, string oldValue)
{
if (column == 1)
{
try
{
FormatInfo formatInfo = _timeFormatDeterminer.DetermineTimeFormatInfo(oldValue);
if (formatInfo == null)
{
return;
}
var newDateTime = DateTime.ParseExact(value, formatInfo.TimeFormat, formatInfo.CultureInfo);
var oldDateTime = DateTime.ParseExact(oldValue, formatInfo.TimeFormat, formatInfo.CultureInfo);
var mSecsOld = oldDateTime.Ticks / TimeSpan.TicksPerMillisecond;
var mSecsNew = newDateTime.Ticks / TimeSpan.TicksPerMillisecond;
_timeOffset = (int)(mSecsNew - mSecsOld);
}
catch (FormatException)
{
}
}
}
public string GetName ()
{
return "Square Bracket Columnizer";
}
public string GetDescription ()
{
return "Splits every line into n fields: Date, Time and the rest of the log message";
}
public int GetColumnCount ()
{
return _columnCount;
}
public string[] GetColumnNames ()
{
var columnNames = new List<string>(GetColumnCount());
if (_isTimeExists)
{
columnNames.Add("Date");
columnNames.Add("Time");
}
// TODO: Make this configurable.
if (GetColumnCount() > 3)
{
columnNames.Add("Level");
}
if (GetColumnCount() > 4)
{
columnNames.Add("Source");
}
// Last column is the message
columnNames.Add("Message");
var i = 1;
while (columnNames.Count < GetColumnCount())
{
columnNames.Insert(columnNames.Count - 1, $"Source{i++}");
}
return columnNames.ToArray();
}
public IColumnizedLogLine SplitLine (ILogLineColumnizerCallback callback, ILogLine line)
{
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
// 03.01.2008 14:48:00.066 <rest of line>
ColumnizedLogLine clogLine = new()
{
LogLine = line
};
var columns = new Column[]
{
new() {FullValue = "", Parent = clogLine},
new() {FullValue = "", Parent = clogLine},
new() {FullValue = "", Parent = clogLine},
};
var temp = line.FullLine;
if (temp.Length < 3)
{
columns[2].FullValue = temp;
return clogLine;
}
FormatInfo formatInfo = _timeFormatDeterminer.DetermineDateTimeFormatInfo(line.FullLine);
if (formatInfo == null)
{
columns[2].FullValue = temp;
SquareSplit(ref columns, temp, 0, 0, 0, clogLine);
}
else
{
var endPos = formatInfo.DateTimeFormat.Length;
var timeLen = formatInfo.TimeFormat.Length;
var dateLen = formatInfo.DateFormat.Length;
try
{
if (_timeOffset != 0)
{
var dateTime = DateTime.ParseExact(temp[..endPos], formatInfo.DateTimeFormat,
formatInfo.CultureInfo);
dateTime = dateTime.Add(new TimeSpan(0, 0, 0, 0, _timeOffset));
var newDate = dateTime.ToString(formatInfo.DateTimeFormat, formatInfo.CultureInfo);
SquareSplit(ref columns, newDate, dateLen, timeLen, endPos, clogLine);
}
else
{
SquareSplit(ref columns, temp, dateLen, timeLen, endPos, clogLine);
}
}
catch (Exception)
{
columns[0].FullValue = "n/a";
columns[1].FullValue = "n/a";
columns[2].FullValue = temp;
}
}
clogLine.ColumnValues = columns.Select(a => a as IColumn).ToArray();
return clogLine;
}
void SquareSplit (ref Column[] columns, string line, int dateLen, int timeLen, int dateTimeEndPos, ColumnizedLogLine clogLine)
{
List<Column> columnList = [];
var restColumn = _columnCount;
if (_isTimeExists)
{
columnList.Add(new Column { FullValue = line[..dateLen], Parent = clogLine });
columnList.Add(new Column { FullValue = line.Substring(dateLen + 1, timeLen), Parent = clogLine });
restColumn -= 2;
}
var nextPos = dateTimeEndPos;
var rest = line;
for (var i = 0; i < restColumn; i++)
{
rest = rest[nextPos..];
//var fullValue = rest.Substring(0, rest.IndexOf(']')).TrimStart(new char[] {' '}).TrimEnd(new char[] { ' ' });
var trimmed = rest.TrimStart([' ']);
if (string.IsNullOrEmpty(trimmed) || trimmed[0] != '[' || rest.IndexOf(']', StringComparison.Ordinal) < 0 || i == restColumn - 1)
{
columnList.Add(new Column { FullValue = rest, Parent = clogLine });
break;
}
nextPos = rest.IndexOf(']', StringComparison.Ordinal) + 1;
var fullValue = rest[..nextPos];
columnList.Add(new Column { FullValue = fullValue, Parent = clogLine });
}
while (columnList.Count < _columnCount)
{
columnList.Insert(columnList.Count - 1, new Column { FullValue = "", Parent = clogLine });
}
columns = columnList.ToArray();
}
public Priority GetPriority (string fileName, IEnumerable<ILogLine> samples)
{
Priority result = Priority.NotSupport;
TimeFormatDeterminer timeDeterminer = new();
var timeStampExistsCount = 0;
var bracketsExistsCount = 0;
var maxBracketNumbers = 1;
foreach (ILogLine logline in samples)
{
var line = logline?.FullLine;
if (string.IsNullOrEmpty(line))
{
continue;
}
var bracketNumbers = 1;
if (null != timeDeterminer.DetermineDateTimeFormatInfo(line))
{
timeStampExistsCount++;
}
else
{
timeStampExistsCount--;
}
var noSpaceLine = line.Replace(" ", string.Empty, StringComparison.Ordinal);
if (noSpaceLine.Contains('[', StringComparison.Ordinal) && noSpaceLine.Contains(']', StringComparison.Ordinal)
&& noSpaceLine.IndexOf('[', StringComparison.Ordinal) < noSpaceLine.IndexOf(']', StringComparison.Ordinal))
{
bracketNumbers += Regex.Matches(noSpaceLine, @"\]\[").Count;
bracketsExistsCount++;
}
else
{
bracketsExistsCount--;
}
maxBracketNumbers = Math.Max(bracketNumbers, maxBracketNumbers);
}
// Add message
_columnCount = maxBracketNumbers + 1;
_isTimeExists = timeStampExistsCount > 0;
if (_isTimeExists)
{
_columnCount += 2;
}
if (maxBracketNumbers > 1)
{
result = Priority.WellSupport;
if (bracketsExistsCount > 0)
{
result = Priority.PerfectlySupport;
}
}
return result;
}
#endregion
}