forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLogSources.cpp
More file actions
532 lines (471 loc) · 15.6 KB
/
Copy pathLogSources.cpp
File metadata and controls
532 lines (471 loc) · 15.6 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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
// (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 <iostream>
#include <chrono>
#include <boost/algorithm/string.hpp>
#include "CobaltFusion/stringbuilder.h"
#include "CobaltFusion/thread.h"
#include "CobaltFusion/fusionassert.h"
#include "Win32/Win32Lib.h"
#include "Win32/Utilities.h"
#include "DebugViewppLib/LogSources.h"
#include "DebugViewppLib/ProcessReader.h"
#include "DebugViewppLib/PipeReader.h"
#include "DebugViewppLib/FileReader.h"
#include "DebugViewppLib/BinaryFileReader.h"
#include "DebugViewppLib/AnyFileReader.h"
#include "DebugViewppLib/DBWinReader.h"
#include "DebugViewppLib/KernelReader.h"
#include "DebugViewppLib/DbgviewReader.h"
#include "DebugViewppLib/SocketReader.h"
#include "DebugViewppLib/TestSource.h"
#include "DebugViewppLib/ProcessInfo.h"
#include "DebugViewppLib/Conversions.h"
#include "DebugViewppLib/LineBuffer.h"
#include "DebugViewppLib/VectorLineBuffer.h"
#include "DebugViewppLib/Loopback.h"
// class Logsources has a vector<LogSource> and start a thread for LogSources::Listen()
// - Listen() executes every LogSource::GetHandle() in m_sources and calls Notify() for any signaled handle.
// - LogSource::Notify reads input en writes to linebuffer (passed at construction)
//
namespace fusion {
namespace debugviewpp {
using namespace std::chrono_literals;
LogSources::LogSources(IExecutor& executor, bool startListening) :
m_updateEvent(CreateEvent(nullptr, 0, 0, nullptr)),
m_linebuffer(64 * 1024),
m_loopback(std::make_unique<Loopback>(m_timer, m_linebuffer)),
m_executor(executor),
m_throttledUpdate(m_executor, 25, [&] { m_update(); })
{
m_processMonitor.ConnectProcessEnded([this](DWORD pid, HANDLE handle) { OnProcessEnded(pid, handle); });
if (startListening)
{
m_listenThread.CallAsync([this] { Listen(); });
}
}
LogSources::~LogSources()
{
if (m_executor.IsExecutorThread())
{ // this is true when the GuiExector is used
Abort();
}
else
{
m_executor.Call([&] { Abort(); });
}
}
void LogSources::AddMessage(const std::string& message)
{
m_loopback->AddInternal(message);
m_throttledUpdate();
}
void LogSources::AddMessage(DWORD pid, const std::string& processName, const std::string& message)
{
m_loopback->Add(pid, processName, message);
m_throttledUpdate();
}
void LogSources::UpdateSettings(const std::unique_ptr<LogSource>& pSource)
{
pSource->SetAutoNewLine(GetAutoNewLine());
}
void LogSources::Add(std::unique_ptr<LogSource> pSource)
{
{
std::lock_guard<std::mutex> lock(m_sourcesSchedule_mutex);
m_sourcesScheduleToAdd.emplace_back(std::move(pSource));
}
Win32::SetEvent(m_updateEvent);
}
void LogSources::InternalRemove(LogSource* pLogSource)
{
auto description = pLogSource->GetDescription();
EraseElements(m_sources, {pLogSource});
AddMessage(stringbuilder() << "Source '" << description << "' was removed.");
}
void LogSources::Remove(LogSource* pLogSource)
{
m_sourcesScheduledToRemove.push_back(pLogSource);
Win32::SetEvent(m_updateEvent);
}
void LogSources::RemoveSources(std::function<bool(LogSource*)> predicate)
{
std::lock_guard<std::mutex> lock(m_sourcesSchedule_mutex);
for (auto& pSource : m_sources)
{
if (predicate(pSource.get()))
{
m_sourcesScheduledToRemove.push_back(pSource.get());
}
}
Win32::SetEvent(m_updateEvent);
}
void LogSources::CallSources(std::function<void(LogSource*)> predicate)
{
std::lock_guard<std::mutex> lock(m_sourcesSchedule_mutex);
for (auto& pSource : m_sources)
{
predicate(pSource.get());
}
}
void LogSources::CallSources(std::function<void(const LogSource*)> predicate) const
{
std::lock_guard<std::mutex> lock(m_sourcesSchedule_mutex);
for (const auto& pSource : m_sources)
{
predicate(pSource.get());
}
}
void LogSources::SetAutoNewLine(bool value)
{
m_autoNewLine = value;
for (auto& pSource : m_sources)
{
pSource->SetAutoNewLine(value);
}
}
bool LogSources::GetAutoNewLine() const
{
return m_autoNewLine;
}
void LogSources::SetProcessPrefix(bool value)
{
m_processPrefix = value;
}
bool LogSources::GetProcessPrefix() const
{
return m_processPrefix;
}
void LogSources::Abort()
{
m_processMonitor.Abort();
m_update.disconnect_all_slots();
m_end = true;
CallSources([](LogSource* logsource) { logsource->Abort(); });
RemoveSources([](LogSource* /*unused*/) { return true; });
Win32::SetEvent(m_updateEvent);
m_listenThread.Synchronize();
}
void LogSources::ResetTimer()
{
m_timer.Reset();
}
boost::signals2::connection LogSources::SubscribeToUpdate(UpdateSignal::slot_type slot)
{
return m_update.connect(slot);
}
// default behaviour:
// LogSources starts with 1 logsource, the loopback source
// At startup normally 1 DBWinReader is added by m_logSources.AddDBWinReader
void LogSources::Listen()
{
try
{
ListenUntilUpdateEvent();
if (!m_end)
{
m_listenThread.CallAsync([this] { Listen(); });
}
}
catch (const std::exception& e)
{
FUSION_REPORT_EXCEPTION(e.what());
throw;
}
}
void LogSources::ListenUntilUpdateEvent()
{
UpdateSources();
std::vector<HANDLE> waitHandles;
std::vector<LogSource*> sources;
{
for (auto& source : m_sources)
{
if (source->AtEnd())
{
continue;
}
HANDLE handle = source->GetHandle();
assert(handle != nullptr && "GetHandle() cant return nullptr");
if (handle != INVALID_HANDLE_VALUE)
{
waitHandles.push_back(handle);
sources.push_back(source.get());
}
// here LogSource::Initialize is called on the m_listenThread, currently only FileReader::Initialize uses this to start reading from the file.
// This will block all other logsources while the file is being read (which is normally not a problem)
source->Initialize();
}
}
auto updateEventIndex = waitHandles.size();
waitHandles.push_back(m_updateEvent.get());
while (!m_end)
{
auto res = Win32::WaitForAnyObject(waitHandles, INFINITE);
if (m_end)
{
return;
}
if (res.signaled)
{
int index = res.index - WAIT_OBJECT_0;
if (index == updateEventIndex)
{
break;
}
assert((index < static_cast<int>(sources.size())) && "res.index out of range");
auto logsource = sources[index];
logsource->Notify();
m_throttledUpdate();
}
}
}
void LogSources::UpdateSources()
{
std::vector<std::unique_ptr<LogSource>> sourcesToAdd;
std::vector<LogSource*> sourcesToRemove;
{
std::lock_guard<std::mutex> lock(m_sourcesSchedule_mutex);
std::swap(sourcesToAdd, m_sourcesScheduleToAdd);
std::swap(sourcesToRemove, m_sourcesScheduledToRemove);
}
if (m_end)
{
for (auto const& pLogSource : m_sources)
{
pLogSource->Abort();
}
m_sources.clear();
return;
}
for (auto pLogSource : sourcesToRemove)
{
pLogSource->Abort();
InternalRemove(pLogSource);
}
for (auto&& pLogSource : sourcesToAdd)
{
UpdateSettings(pLogSource);
m_sources.emplace_back(std::move(pLogSource));
}
m_throttledUpdate(); // notify observers to process internal messages
}
std::string FormatExitCode(DWORD exitCode)
{
auto longCode = static_cast<long>(exitCode);
if (std::abs(longCode) < 16)
{
if (longCode < 0)
{
return stringbuilder() << "exit code " << longCode << " (" << std::hex << std::showbase << std::internal << exitCode << ")";
}
return stringbuilder() << "exit code " << longCode;
}
if (exitCode >= 0xC0000000) // >= 0xC0000000 in case of an SEH exception
{
return stringbuilder() << std::hex << std::showbase << std::internal << exitCode << " (" << Win32::GetSEHcodeDescription(exitCode) << ")";
}
if (exitCode >= 0x80000000) // >= 0x80000000 in case of a HRESULT rethrown as an SEH
{
return stringbuilder() << "HRESULT " << std::hex << std::showbase << std::internal << exitCode << " (" << Win32::GetHresultDescription(exitCode) << ")";
}
return stringbuilder() << "exit code " << longCode << " (" << std::hex << std::showbase << std::internal << exitCode << ")";
}
void LogSources::AddTerminateMessage(DWORD pid, HANDLE handle) const
{
auto processName = Str(ProcessInfo::GetProcessName(handle)).str();
auto startTime = ProcessInfo::GetStartTime(handle);
std::string terminateMessage = stringbuilder() << "<process started at " << startTime << " has terminated";
DWORD exitCode = 0;
auto result = ::GetExitCodeProcess(handle, &exitCode);
if (result == 0)
{
terminateMessage += " with unknown exit code";
}
else
{
terminateMessage += " with ";
terminateMessage += FormatExitCode(exitCode);
}
terminateMessage += ">";
m_loopback->Add(pid, processName, terminateMessage);
}
void LogSources::OnProcessEnded(DWORD pid, HANDLE handle)
{
m_executor.CallAsync([this, pid, handle] {
m_update();
auto flushedLines = m_newlineFilter.FlushLinesFromTerminatedProcess(pid, handle);
for (auto& line : flushedLines)
{
m_loopback->Add(line.pid, line.processName, line.message);
}
AddTerminateMessage(pid, handle);
m_throttledUpdate();
auto it = m_pidMap.find(pid);
if (it != m_pidMap.end())
{
m_pidMap.erase(it);
}
});
}
bool LogSources::IsRemoved(const LogSource* logsource) const
{
// the loopback is a special LogSource that is never added to m_sources
if (logsource == m_loopback.get())
return false;
bool present = false;
CallSources([&](const LogSource* l) {
if (l == logsource)
{
present = true;
}
});
return !present;
}
Lines LogSources::GetLines()
{
assert(m_executor.IsExecutorThread());
Lines lines;
for (auto&& inputLine : m_linebuffer.GetLines())
{
if (IsRemoved(inputLine.pLogSource))
{
std::cerr << "'" << inputLine.message << "' ignored because source was removed\n";
continue;
}
// let the logsource decide how to create processname
if (inputLine.pLogSource != nullptr)
{
inputLine.pLogSource->PreProcess(inputLine);
}
if (inputLine.handle != nullptr)
{
Win32::Handle handle(inputLine.handle);
inputLine.pid = GetProcessId(inputLine.handle);
auto it = m_pidMap.find(inputLine.pid);
if (it == m_pidMap.end())
{
m_pidMap[inputLine.pid] = std::move(handle);
m_processMonitor.Add(inputLine.pid, inputLine.handle);
}
else
{
inputLine.handle = nullptr;
}
}
if (inputLine.message.empty())
{
lines.emplace_back(std::move(inputLine));
}
else
{
// since a line can contain multiple newlines, processing 1 line can output
// multiple lines, in this case the timestamp for each line is the same.
// NewlineFilter::Process will also eat any \r\n's
for (auto&& line : m_newlineFilter.Process(std::move(inputLine)))
{
lines.emplace_back(std::move(line));
}
}
}
return lines;
}
DBWinReader* LogSources::AddDBWinReader(bool global)
{
assert(m_executor.IsExecutorThread());
auto pDbWinReader = std::make_unique<DBWinReader>(m_timer, m_linebuffer, global);
auto pResult = pDbWinReader.get();
Add(std::move(pDbWinReader));
return pResult;
}
KernelReader* LogSources::AddKernelReader()
{
assert(m_executor.IsExecutorThread());
auto kernelReader = std::make_unique<KernelReader>(m_timer, m_linebuffer);
auto pResult = kernelReader.get();
Add(std::move(kernelReader));
return pResult;
}
TestSource* LogSources::AddTestSource()
{
assert(m_executor.IsExecutorThread());
auto pTestSource = std::make_unique<TestSource>(m_timer, m_linebuffer);
pTestSource->SetDescription(L"TestSource");
auto pResult = pTestSource.get();
Add(std::move(pTestSource));
UpdateSources();
return pResult;
}
ProcessReader* LogSources::AddProcessReader(const std::wstring& pathName, const std::wstring& args)
{
assert(m_executor.IsExecutorThread());
auto pProcessReader = std::make_unique<ProcessReader>(m_timer, m_linebuffer, pathName, args);
auto pResult = pProcessReader.get();
Add(std::move(pProcessReader));
return pResult;
}
BinaryFileReader* LogSources::AddBinaryFileReader(const std::wstring& filename)
{
assert(m_executor.IsExecutorThread());
auto filetype = IdentifyFile(filename);
AddMessage(stringbuilder() << "Started tailing " << filename << " identified as '" << FileTypeToString(filetype) << "'\n");
auto pFileReader = std::make_unique<BinaryFileReader>(m_timer, m_linebuffer, filetype, filename);
pFileReader->SubscribeToUpdate([&]() { m_throttledUpdate(); });
auto pResult = pFileReader.get();
Add(std::move(pFileReader));
return pResult;
}
AnyFileReader* LogSources::AddAnyFileReader(const std::wstring& filename, bool keeptailing)
{
assert(m_executor.IsExecutorThread());
auto filetype = IdentifyFile(filename);
if (filetype == FileType::Unknown)
{
AddMessage(stringbuilder() << "Unable to open '" << filename << "'\n");
return nullptr;
}
if (keeptailing)
{
AddMessage(stringbuilder() << "Started tailing " << filename << " identified as '" << FileTypeToString(filetype) << "'\n");
}
else
{
AddMessage(stringbuilder() << "Loading " << filename << " identified as '" << FileTypeToString(filetype) << "'\n");
}
auto pAnyFileReader = std::make_unique<AnyFileReader>(m_timer, m_linebuffer, filetype, filename, keeptailing);
pAnyFileReader->SubscribeToUpdate([&]() { m_throttledUpdate(); });
auto pResult = pAnyFileReader.get();
Add(std::move(pAnyFileReader));
return pResult;
}
PipeReader* LogSources::AddPipeReader(DWORD pid, HANDLE hPipe)
{
assert(m_executor.IsExecutorThread());
auto pPipeReader = std::make_unique<PipeReader>(m_timer, m_linebuffer, hPipe, pid, Str(ProcessInfo::GetProcessNameByPid(pid)).str(), 40);
auto pResult = pPipeReader.get();
Add(std::move(pPipeReader));
return pResult;
}
DbgviewReader* LogSources::AddDbgviewReader(const std::string& hostname)
{
assert(m_executor.IsExecutorThread());
auto pDbgViewReader = std::make_unique<DbgviewReader>(m_timer, m_linebuffer, hostname);
m_loopback->Add(stringbuilder() << "Source '" << pDbgViewReader->GetDescription() << "' was added.");
auto pResult = pDbgViewReader.get();
Add(std::move(pDbgViewReader));
return pResult;
}
SocketReader* LogSources::AddUDPReader(int port)
{
assert(m_executor.IsExecutorThread());
auto pSocketReader = std::make_unique<SocketReader>(m_timer, m_linebuffer, port);
m_loopback->Add(stringbuilder() << "Source '" << pSocketReader->GetDescription() << "' was added.");
auto pResult = pSocketReader.get();
Add(std::move(pSocketReader));
return pResult;
}
} // namespace debugviewpp
} // namespace fusion