forked from CobaltFusion/DebugViewPP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDebugViewConsole.cpp
More file actions
310 lines (283 loc) · 7.7 KB
/
Copy pathDebugViewConsole.cpp
File metadata and controls
310 lines (283 loc) · 7.7 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
// (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)
// Repository at: https://github.com/djeedjay/DebugViewPP/
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <fstream>
#include <vector>
#include <memory>
#include <algorithm>
#include <boost/asio.hpp>
#include <boost/algorithm/string.hpp>
#include "Win32/Utilities.h"
#include "CobaltFusion/scope_guard.h"
#include "CobaltFusion/Str.h"
#include "CobaltFusion/ExecutorClient.h"
#include "CobaltFusion/Executor.h"
#include "DebugView++Lib/DBWinBuffer.h"
#include "DebugView++Lib/DBWinReader.h"
#include "DebugView++Lib/FileIO.h"
#include "DebugView++Lib/ProcessInfo.h"
#include "DebugView++Lib/LogSources.h"
#include "DebugView++Lib/Conversions.h"
#include "DebugView++Lib/LineBuffer.h"
#include "../DebugView++/version.h"
namespace fusion {
namespace debugviewpp {
struct Settings
{
bool timestamp;
bool performanceCounter;
bool tabs;
bool pid;
bool processName;
bool autonewline;
bool flush;
bool linenumber;
bool console;
std::string filename;
};
void OutputDetails(Settings settings, const Line& line)
{
std::string separator = settings.tabs ? "\t" : " ";
if (settings.timestamp)
{
std::cout << GetTimeText(line.systemTime) << separator;
}
if (settings.performanceCounter)
{
std::cout << GetTimeText(line.time) << separator;
}
if (settings.pid)
{
std::cout << line.pid << separator;
}
if (settings.processName)
{
std::cout << line.processName.c_str() << separator;
}
}
static bool g_quit = false;
void Quit()
{
g_quit = true;
}
void LogMessages(Settings settings)
{
using namespace std::chrono_literals;
ActiveExecutorClient executor;
LogSources logsources(executor);
executor.Call([&] {
logsources.AddDBWinReader(false);
if (HasGlobalDBWinReaderRights())
logsources.AddDBWinReader(true);
logsources.SetAutoNewLine(settings.autonewline);
});
std::ofstream fs;
if (!settings.filename.empty())
{
OpenLogFile(fs, WStr(settings.filename));
fs.flush();
}
auto guard = make_guard([&fs, &settings]()
{
if (!settings.filename.empty())
{
fs.flush();
fs.close();
std::cout << "Log file closed.\n";
}
});
std::string separator = settings.tabs ? "\t" : " ";
while (!g_quit)
{
Lines lines;
executor.Call([&] {
lines = logsources.GetLines();
});
int linenumber = 0;
for (auto& line : lines)
{
if (settings.console)
{
if (settings.linenumber)
{
++linenumber;
std::cout << std::setw(5) << std::setfill('0') << linenumber << std::setfill(' ') << separator;
}
OutputDetails(settings, line);
std::cout << separator << line.message.c_str() << "\n";
}
if (!settings.filename.empty())
{
WriteLogFileMessage(fs, line.time, line.systemTime, line.pid, line.processName, line.message);
}
}
if (settings.flush)
{
std::cout.flush();
fs.flush();
}
std::this_thread::sleep_for(250ms);
}
std::cout.flush();
}
} // namespace debugviewpp
} // namespace fusion
char* getCmdOption(char** begin, char** end, const std::string& option)
{
char** itr = std::find(begin, end, option);
return itr != end && ++itr != end ? *itr : nullptr;
}
bool cmdOptionExists(char** begin, char** end, const std::string& option)
{
return std::find(begin, end, option) != end;
}
BOOL WINAPI ConsoleHandler(DWORD dwType)
{
switch (dwType)
{
case CTRL_C_EVENT:
case CTRL_LOGOFF_EVENT:
case CTRL_SHUTDOWN_EVENT:
fusion::debugviewpp::Quit();
// report as handled, so no other handler gets called.
return TRUE;
default:
break;
}
return FALSE;
}
int main(int argc, char* argv[])
try
{
using namespace fusion::debugviewpp;
// install CTRL-C handler
SetConsoleCtrlHandler(ConsoleHandler, TRUE);
Settings settings = {0};
std::cout << "DebugViewConsole v" << VERSION_STR << std::endl;
settings.timestamp = false;
if (cmdOptionExists(argv, argv+argc, "-h") || cmdOptionExists(argv, argv+argc, "--help"))
{
std::cout << "options:\n";
std::cout << " -h: this help message\n";
std::cout << " -a: auto-newline (automatically adds newline to messages without a newline, most people want this on)\n";
std::cout << " -f: flush (aggressively flush buffers, if unsure, do not use)\n";
std::cout << " -v: verbose output\n";
std::cout << " -d <file>: write to .dblog file\n";
std::cout << " -c enable console output\n";
std::cout << "console output options: (do not effect the dblog file)\n";
//std::cout << "-u: send a UDP test-message (used only for debugging)\n";
std::cout << " -l: prefix line number\n";
std::cout << " -s: prefix messages with system time\n";
std::cout << " -q: prefix message with high-precision (<1us) offset (from QueryPerformanceCounter)\n";
std::cout << " -t: tab-separated output\n";
std::cout << " -p: add PID (process ID)\n";
std::cout << " -n: add process name\n";
exit(0);
}
bool verbose = false;
if (cmdOptionExists(argv, argv + argc, "-v"))
{
if (verbose) std::cout << "-s: verbose output\n";
verbose = true;
}
if (cmdOptionExists(argv, argv + argc, "-l"))
{
if (verbose)
std::cout << "-l: prefix line number\n";
settings.linenumber = true;
}
if (cmdOptionExists(argv, argv + argc, "-s"))
{
if (verbose)
std::cout << "-s: including systemtime\n";
settings.timestamp = true;
}
if (cmdOptionExists(argv, argv + argc, "-q"))
{
if (verbose)
std::cout << "-q: including relative high-precision offset\n";
settings.performanceCounter = true;
}
if (cmdOptionExists(argv, argv + argc, "-t"))
{
if (verbose)
std::cout << "-t: output tab-separated output\n";
settings.tabs = true;
}
if (cmdOptionExists(argv, argv + argc, "-p"))
{
if (verbose)
std::cout << "-p: add PID (process ID)\n";
settings.pid = true;
}
if (cmdOptionExists(argv, argv + argc, "-n"))
{
if (verbose)
std::cout << "-n: add process name\n";
settings.processName = true;
}
if (cmdOptionExists(argv, argv + argc, "-a"))
{
if (verbose)
std::cout << "-a: auto-newline (each message will add a new line even if it does not end with a newline-character)\n";
settings.autonewline = true;
}
if (cmdOptionExists(argv, argv + argc, "-f"))
{
if (verbose)
std::cout << "-f: auto flush (write to disk more often)\n";
settings.flush = true;
}
if (cmdOptionExists(argv, argv + argc, "-c"))
{
if (verbose)
std::cout << "-c: enable console output\n";
settings.console = true;
}
if (cmdOptionExists(argv, argv + argc, "-d"))
{
settings.filename = getCmdOption(argv, argv + argc, "-d");
if (verbose)
std::cout << "-d: write to: " << settings.filename << "\n";
}
if (cmdOptionExists(argv, argv + argc, "-u"))
{
if (verbose)
std::cout << "Send UDP test message...\n";
using namespace boost::asio::ip;
boost::asio::io_service io_service;
udp::resolver resolver(io_service);
udp::resolver::query query(udp::v4(), "255.255.255.255", "2999");
udp::endpoint receiver_endpoint = *resolver.resolve(query);
udp::socket socket(io_service);
socket.open(udp::v4());
// enable broadcast
boost::asio::socket_base::broadcast option(true);
socket.set_option(option);
std::string msg = argv[1];
std::cout << msg << std::endl;
socket.send_to(boost::asio::buffer(msg), receiver_endpoint);
}
else
{
std::cout << "Listening for OutputDebugString messages..." << std::endl;
LogMessages(settings);
std::cout << "Process ended normally.\n";
}
return 0;
}
catch (std::exception& e)
{
std::cerr << "Unexpected error occurred: " << e.what() << std::endl;
std::string message(e.what());
if (message.find("CreateDBWinBufferMapping") != std::string::npos)
{
std::cerr << "Another DebugView++ (or similar application) might be running. " << std::endl;
}
return 1;
}