forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.cpp
More file actions
193 lines (172 loc) · 5.1 KB
/
Copy pathFileReader.cpp
File metadata and controls
193 lines (172 loc) · 5.1 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
// (C) Copyright Gert-Jan de Vos and Jan Wilmans 2013.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <cassert>
#include <filesystem>
#include "CobaltFusion/stringbuilder.h"
#include "Win32/Win32Lib.h"
#include "DebugViewppLib/FileIO.h"
#include "DebugViewppLib/FileReader.h"
#include "DebugViewppLib/LineBuffer.h"
#include "DebugViewppLib/Line.h"
namespace fusion {
namespace debugviewpp {
namespace fs = std::filesystem;
FileReader::FileReader(Timer& timer, ILineBuffer& linebuffer, FileType::type filetype, const std::wstring& filename, bool keeptailing) :
LogSource(timer, SourceType::File, linebuffer),
m_filename(Str(filename).str()),
m_name(Str(fs::path(filename).filename().string()).str()),
m_fileType(filetype),
m_handle(FindFirstChangeNotification(fs::path(m_filename).parent_path().wstring().c_str(), 0, FILE_NOTIFY_CHANGE_SIZE)), //todo: maybe adding FILE_NOTIFY_CHANGE_LAST_WRITE could have benefits, not sure what though.
m_ifstream(m_filename, std::ios::in),
m_filenameOnly(std::filesystem::path(m_filename).filename().string()),
m_initialized(false),
m_keeptailing(keeptailing),
m_thread([this] { PollThread(); })
{
SetDescription(filename);
}
FileReader::~FileReader() = default;
void FileReader::Abort()
{
LogSource::Abort();
if (m_thread.joinable())
{
m_thread.join();
}
}
bool FileReader::GetAutoNewLine() const
{
return true;
}
void FileReader::PollThread()
{
// FILE_NOTIFY_CHANGE_SIZE is broken on windows vista and above in that it does not
// trigger this notification until a) a cache timeout of N? (unspecified on MSDN, but > 30 seconds) or b) someone queries the status of the file
// both are not useful when tailing a file.
// references:
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-writefile
// https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-findfirstchangenotificationa
// MSDN: 'For operating systems that use extensive caching, detection occurs only when the cache is sufficiently flushed.'
// affect: timestamps for lines read from logfiles have > 500ms accuracy and unittests are slow.
uintmax_t filesize = fs::file_size(fs::path(m_filename));
ReadUntilEof();
while (!AtEnd())
{
if (Win32::WaitForSingleObject(m_handle.get(), 500))
{
// we got a FILE_NOTIFY_CHANGE_SIZE event
ReadUntilEof();
FindNextChangeNotification(m_handle.get());
}
else
{
// timeout occurred, poll the filesize
uintmax_t newFilesize = fs::file_size(fs::path(m_filename));
if (filesize != newFilesize)
{
filesize = newFilesize;
ReadUntilEof();
}
}
}
}
void FileReader::Initialize()
{
//if (m_initialized)
// return;
//m_initialized = true;
//if (m_ifstream.is_open())
//{
// ReadUntilEof();
//}
}
boost::signals2::connection FileReader::SubscribeToUpdate(UpdateSignal::slot_type slot)
{
return m_update.connect(slot);
}
HANDLE FileReader::GetHandle() const
{
return INVALID_HANDLE_VALUE; // m_handle.get();
}
void FileReader::Notify()
{
//assert(m_handle);
//ReadUntilEof();
//if (!m_keeptailing)
//{
// Abort();
// return;
//}
//FindNextChangeNotification(m_handle.get());
}
void FileReader::ReadUntilEof()
{
std::string line;
int count = 0;
while (std::getline(m_ifstream, line))
{
m_line += line;
if ((++count % 5000) == 0)
{
m_update();
}
if (m_ifstream.eof())
{
// the line ended without a newline character
}
else
{
SafeAddLine(m_line);
m_line.clear();
}
}
m_update();
if (m_ifstream.eof())
{
m_ifstream.clear(); // clear EOF condition
// resync to end of file, even if the file shrunk
auto lastReadPosition = m_ifstream.tellg();
m_ifstream.seekg(0, std::ifstream::end);
auto length = m_ifstream.tellg();
if (length > lastReadPosition)
{
m_ifstream.seekg(lastReadPosition);
}
else if (length != lastReadPosition)
{
m_line.clear();
Add(stringbuilder() << "file shrank, resynced at offset " << length);
}
}
else
{
// Some error other then EOF occured
Add("Stopped tailing " + m_filename);
LogSource::Abort();
}
m_update();
}
void FileReader::SafeAddLine(const std::string& line)
{
try
{
AddLine(line);
}
catch (std::exception& e)
{
Add(stringbuilder() << "Error parsing line: " << e.what());
}
}
void FileReader::AddLine(const std::string& line)
{
// AnyFileReader and BinaryFileReader override this method
Add(line);
}
void FileReader::PreProcess(Line& line) const
{
line.processName = m_filenameOnly;
}
} // namespace debugviewpp
} // namespace fusion