-
Notifications
You must be signed in to change notification settings - Fork 186
Expand file tree
/
Copy pathLogBuffer.cs
More file actions
184 lines (147 loc) · 4.3 KB
/
LogBuffer.cs
File metadata and controls
184 lines (147 loc) · 4.3 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
using System.Buffers;
using ColumnizerLib;
using NLog;
namespace LogExpert.Core.Classes.Log;
public class LogBuffer
{
#region Fields
private SpinLock _contentLock = new(enableThreadOwnerTracking: false);
private static readonly Logger _logger = LogManager.GetCurrentClassLogger();
#if DEBUG
private readonly List<long> _filePositions; // file position for every line
#endif
private LogLine[] _lineArray;
private int _lineArrayLength; // capacity of the rented array
private int MAX_LINES = 500;
#endregion
#region cTor
// Don't use a primary constructor here: field initializers (like MAX_LINES) run before primary constructor parameters are assigned,
// so MAX_LINES would always be set to its default value before the constructor body can assign it. Use a regular constructor instead.
public LogBuffer (ILogFileInfo fileInfo, int maxLines)
{
FileInfo = fileInfo;
MAX_LINES = maxLines;
_lineArray = ArrayPool<LogLine>.Shared.Rent(maxLines);
_lineArrayLength = _lineArray.Length;
#if DEBUG
_filePositions = new(MAX_LINES);
#endif
}
#endregion
#region Properties
public long StartPos { set; get; }
public long Size
{
set
{
field = value;
#if DEBUG
if (_filePositions.Count > 0)
{
if (field < _filePositions[^1] - StartPos)
{
_logger.Error("LogBuffer overall Size must be greater than last line file position!");
}
}
#endif
}
get;
}
public int EndLine => StartLine + LineCount;
public int StartLine { set; get; }
public int LineCount { get; private set; }
public bool IsDisposed { get; private set; }
public ILogFileInfo FileInfo { get; set; }
public int DroppedLinesCount { get; set; }
public int PrevBuffersDroppedLinesSum { get; set; }
#endregion
#region Public methods
public void AddLine (LogLine lineMemory, long filePos)
{
if (LineCount < _lineArrayLength)
{
_lineArray[LineCount] = lineMemory;
LineCount++;
}
#if DEBUG
else
{
_logger.Error("AddLine overflow: LineCount={0} >= _lineArrayLength={1}", LineCount, _lineArrayLength);
}
#endif
#if DEBUG
_filePositions.Add(filePos);
#endif
IsDisposed = false;
}
public void ClearLines ()
{
Array.Clear(_lineArray, 0, LineCount);
LineCount = 0;
}
/// <summary>
/// Prepares the buffer for reuse from the pool.
/// </summary>
public void Reinitialise (ILogFileInfo fileInfo, int maxLines)
{
FileInfo = fileInfo;
MAX_LINES = maxLines;
StartLine = 0;
StartPos = 0;
Size = 0;
LineCount = 0;
DroppedLinesCount = 0;
PrevBuffersDroppedLinesSum = 0;
IsDisposed = false;
_lineArray = ArrayPool<LogLine>.Shared.Rent(maxLines);
_lineArrayLength = _lineArray.Length;
#if DEBUG
_filePositions.Clear();
DisposeCount = 0;
#endif
}
public void DisposeContent ()
{
if (_lineArray != null)
{
Array.Clear(_lineArray, 0, LineCount);
ArrayPool<LogLine>.Shared.Return(_lineArray);
_lineArray = null;
LineCount = 0;
}
IsDisposed = true;
#if DEBUG
DisposeCount++;
#endif
}
public LogLine? GetLineMemoryOfBlock (int num)
{
return num < LineCount && num >= 0
? _lineArray[num]
: null;
}
/// <summary>
/// Acquires the content lock. The caller MUST call <see cref="ReleaseContentLock"/> in a finally block.
/// </summary>
public void AcquireContentLock (ref bool lockTaken)
{
_contentLock.Enter(ref lockTaken);
}
/// <summary>
/// Releases the content lock previously acquired via <see cref="AcquireContentLock"/>.
/// </summary>
public void ReleaseContentLock ()
{
_contentLock.Exit(useMemoryBarrier: false);
}
#endregion
#if DEBUG
public long DisposeCount { get; private set; }
public long GetFilePosForLineOfBlock (int line)
{
return line >= 0 && line < _filePositions.Count
? _filePositions[line]
: -1;
}
#endif
}