Skip to content

Commit 324066b

Browse files
committed
Added initialization method, remove duplicate code
1 parent d4aa75c commit 324066b

File tree

1 file changed

+38
-34
lines changed

1 file changed

+38
-34
lines changed

src/LogExpert/Classes/Log/LogfileReader.cs

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -58,73 +58,77 @@ public class LogfileReader : IAutoLogLineColumnizerCallback
5858
#endregion
5959

6060
#region cTor
61-
6261
public LogfileReader(string fileName, EncodingOptions encodingOptions, bool multiFile, int bufferCount, int linesPerBuffer, MultiFileOptions multiFileOptions)
6362
{
6463
if (fileName == null)
6564
{
66-
return;
65+
throw new ArgumentNullException(nameof(fileName));
6766
}
6867

6968
_fileName = fileName;
70-
EncodingOptions = encodingOptions;
71-
IsMultiFile = multiFile;
7269
_MAX_BUFFERS = bufferCount;
7370
_MAX_LINES_PER_BUFFER = linesPerBuffer;
7471
_multiFileOptions = multiFileOptions;
7572
_logLineFx = GetLogLineInternal;
76-
InitLruBuffers();
77-
78-
if (multiFile)
79-
{
80-
ILogFileInfo info = GetLogFileInfo(fileName);
81-
RolloverFilenameHandler rolloverHandler = new(info, _multiFileOptions);
82-
LinkedList<string> nameList = rolloverHandler.GetNameList();
83-
84-
ILogFileInfo fileInfo = null;
85-
foreach (string name in nameList)
86-
{
87-
fileInfo = AddFile(name);
88-
}
89-
90-
_watchedILogFileInfo = fileInfo; // last added file in the list is the watched file
91-
}
92-
else
93-
{
94-
_watchedILogFileInfo = AddFile(fileName);
95-
}
73+
IsMultiFile = multiFile;
9674

97-
StartGCThread();
75+
InitializeReader(encodingOptions, null);
9876
}
9977

10078
public LogfileReader(string[] fileNames, EncodingOptions encodingOptions, int bufferCount, int linesPerBuffer, MultiFileOptions multiFileOptions)
10179
{
10280
if (fileNames == null || fileNames.Length < 1)
10381
{
104-
return;
82+
throw new ArgumentNullException(nameof(fileNames));
10583
}
10684

107-
EncodingOptions = encodingOptions;
108-
IsMultiFile = true;
85+
_fileName = fileNames[0];
10986
_MAX_BUFFERS = bufferCount;
11087
_MAX_LINES_PER_BUFFER = linesPerBuffer;
11188
_multiFileOptions = multiFileOptions;
11289
_logLineFx = GetLogLineInternal;
90+
IsMultiFile = true;
91+
92+
InitializeReader(encodingOptions, fileNames);
93+
}
11394

95+
private void InitializeReader(EncodingOptions encodingOptions, string[]? additionalFiles)
96+
{
97+
EncodingOptions = encodingOptions;
11498
InitLruBuffers();
11599

116-
ILogFileInfo fileInfo = null;
117-
foreach (string name in fileNames)
100+
if (additionalFiles != null)
118101
{
119-
fileInfo = AddFile(name);
102+
// Handle array constructor case
103+
ILogFileInfo fileInfo = null;
104+
foreach (string name in additionalFiles)
105+
{
106+
fileInfo = AddFile(name);
107+
}
108+
_watchedILogFileInfo = fileInfo;
120109
}
110+
else if (IsMultiFile)
111+
{
112+
// Handle single file multiFile case
113+
ILogFileInfo info = GetLogFileInfo(_fileName);
114+
RolloverFilenameHandler rolloverHandler = new(info, _multiFileOptions);
115+
LinkedList<string> nameList = rolloverHandler.GetNameList();
121116

122-
_watchedILogFileInfo = fileInfo;
123-
_fileName = fileInfo.FullName;
117+
ILogFileInfo fileInfo = null;
118+
foreach (string name in nameList)
119+
{
120+
fileInfo = AddFile(name);
121+
}
122+
_watchedILogFileInfo = fileInfo; // last added file in the list is the watched file
123+
}
124+
else
125+
{
126+
// Handle single file case
127+
_watchedILogFileInfo = AddFile(_fileName);
128+
}
124129

125130
StartGCThread();
126131
}
127-
128132
#endregion
129133

130134
#region Delegates

0 commit comments

Comments
 (0)