-
Notifications
You must be signed in to change notification settings - Fork 4
/
logging.cpp
351 lines (290 loc) · 8.03 KB
/
logging.cpp
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
#define _CRT_SECURE_NO_WARNINGS /* Technical debt? I think so. */
#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <share.h>
#include <map>
#include <string>
#include <iterator>
#include "logging.h"
#include "cmdlog_defs.h"
BOOL _LogInitAsPrimary();
void _LogFmtTimestampFilename(char *fmt, char *buf, size_t buflen);
BOOL _LogNormalizePath(char *fname, DWORD len_fname, char *tmp, DWORD len_tmp);
BOOL _LogGetPathFromEnv(char path[MAX_PATH]);
BOOL _LogSetPathToEnv(char path[MAX_PATH]);
void _FmtAndLogToFile(FILE *out, const char *fmt, va_list argp);
void _LogToFile(FILE *out, const char *data);
/* Plumbing for client and server sides of named pipe-based logging
* communication that was thought to be necessary due to anticipated race
* conditions. Instead of a file path, an environment variable would be used
* to define a pseudo-random name of the pipe created by the first DLL instance
* to load (by noting the absence of the variable definition in the
* environment). Race conditions have yet to manifest, so this design appears
* to be merely a vestige of an unwarranted concern. It can be further
* developed if race conditions do arise during testing. Currently only the
* "output" buffer (client side) is used, and it is only
*/
CRITICAL_SECTION cs_output;
char cmdlog_fname[MAX_PATH];
char buf_output[BUFSIZE_OUTPUT];
#if 0
char buf_input[BUFSIZE_OUTPUT];
#endif
std::map<std::string, std::string> last_adapters;
BOOL
LogInit()
{
BOOL Ok = FALSE;
InitializeCriticalSection(&cs_output);
Ok = _LogGetPathFromEnv(cmdlog_fname);
if (!Ok) {
Ok = _LogInitAsPrimary();
}
return Ok;
}
/* Useful: http://c-faq.com/varargs/handoff.html */
void
LogOutput(FILE *out, const char *fmt, ...)
{
va_list argp;
va_start(argp, fmt);
_FmtAndLogToFile(out, fmt, argp);
va_end(argp);
}
BOOL
_AdaptersChanged(PIP_ADAPTER_INFO pAdapterList)
{
PIP_ADAPTER_INFO pAdapter = NULL;
std::map<std::string, std::string> current_adapters;
for (pAdapter = pAdapterList; pAdapter; pAdapter = pAdapter->Next) {
std::string ipinfo;
switch (pAdapter->Type)
{
case IF_TYPE_IEEE80211:
ipinfo = std::string("Wireless: ");
break;
case MIB_IF_TYPE_OTHER:
case MIB_IF_TYPE_ETHERNET:
ipinfo = std::string("Ethernet: ");
break;
}
if (ipinfo.size()) {
ipinfo += pAdapter->IpAddressList.IpAddress.String;
ipinfo += " / ";
ipinfo += pAdapter->IpAddressList.IpMask.String;
ipinfo += " gw ";
ipinfo += pAdapter->GatewayList.IpAddress.String;
current_adapters.insert(
std::make_pair(pAdapter->AdapterName, ipinfo));
}
}
if (current_adapters != last_adapters) {
last_adapters = current_adapters;
return TRUE;
}
return FALSE;
}
void
LogAdapterInfoToFile(FILE *out)
{
DWORD Ret;
ULONG Buflen = 0;
PIP_ADAPTER_INFO pAdapterList = NULL;
Ret = GetAdaptersInfo(NULL, &Buflen);
if (Ret != ERROR_BUFFER_OVERFLOW) { goto exit_LogAdapterInfo; }
pAdapterList = (PIP_ADAPTER_INFO)malloc(Buflen);
if (pAdapterList == NULL) { goto exit_LogAdapterInfo; }
Ret = GetAdaptersInfo(pAdapterList, &Buflen);
if (Ret != NO_ERROR) { goto exit_LogAdapterInfo; }
if (!_AdaptersChanged(pAdapterList)) {
goto exit_LogAdapterInfo;
}
for (auto it = last_adapters.begin(); it != last_adapters.end(); it++) {
LogOutput(out, "%s\r\n", it->second.c_str());
}
exit_LogAdapterInfo:
if (pAdapterList) { free(pAdapterList); }
}
void
LogTimeStampToFile(FILE *out)
{
SYSTEMTIME tm;
TIME_ZONE_INFORMATION tzi;
DWORD tzd;
GetLocalTime(&tm);
tzd = GetTimeZoneInformation(&tzi);
LogOutput(
out,
"Timestamp: %04d-%02d-%02d %02d:%02d:%02d.%02d %S\r\n",
tm.wYear,
tm.wMonth,
tm.wDay,
tm.wHour,
tm.wMinute,
tm.wSecond,
tm.wMilliseconds,
(tzd == TIME_ZONE_ID_STANDARD)? tzi.StandardName: tzi.DaylightName
);
}
void
LogInfoStampToFile(FILE *out)
{
LogOutput(out, "\r\n%s", BAR);
#if 0
LogOutput(out, "Executable module name: %s\r\n", exename);
#ifdef _WIN64
LogOutput(out, "Architecture width: 64-bit\r\n");
#else
LogOutput(out, "Architecture width: 32-bit\r\n");
#endif
#endif
LogTimeStampToFile(out);
LogAdapterInfoToFile(out);
LogOutput(out, "%s", BAR);
}
void
QueryStatus()
{
printf("Logging to %s\n", cmdlog_fname);
LogInfoStampToFile(stdout);
}
BOOL
_LogInitAsPrimary()
{
DWORD n = 0;
BOOL Ok = FALSE;
BOOL Ret = FALSE;
FILE *test = NULL;
char tmp[MAX_PATH];
memset(tmp, 0, STATIC_BUFFER_LEN(tmp));
/* Fill in timestamp format of log file name. */
_LogFmtTimestampFilename(CMDLOG_FNAME_FMT, tmp, STATIC_BUFFER_LEN(tmp));
/* Expand environment variables if applicable */
n = ExpandEnvironmentStringsA(tmp, cmdlog_fname, MAX_PATH);
Ok = (n && (n < MAX_PATH));
if (!Ok) {
fprintf( stderr,
"Failed to expand environment strings, logging disabled (a)\n"
);
goto exit__InitLoggingPrimary;
}
/* Normalize relative paths to absolute ones */
Ok = _LogNormalizePath(
cmdlog_fname,
STATIC_BUFFER_LEN(cmdlog_fname),
tmp,
STATIC_BUFFER_LEN(tmp)
);
if (!Ok) {
fprintf(stderr, "Failed to normalize path, logging disabled (b)\n");
goto exit__InitLoggingPrimary;
}
/* Test access */
test = _fsopen(cmdlog_fname, "ab", _SH_DENYWR);
if (!test) {
fprintf(
stderr,
"fopen_s(%s, \"ab\") failed, logging disabled (c)\n",
cmdlog_fname
);
goto exit__InitLoggingPrimary;
}
fclose(test);
printf("Logging to %s\n", cmdlog_fname);
_LogSetPathToEnv(cmdlog_fname);
Ret = TRUE;
exit__InitLoggingPrimary:
return Ret;
}
/* Expand format string similar to strftime */
void
_LogFmtTimestampFilename(char *fmt, char *buf, size_t buflen)
{
SYSTEMTIME tm = {0};
TIME_ZONE_INFORMATION tzi = {0};
char tmp[MAX_PATH];
memset(tmp, 0, STATIC_BUFFER_LEN(tmp));
GetLocalTime(&tm);
GetTimeZoneInformation(&tzi);
/* My recollection is that strftime can't do correct time zones without
* serious rigamarole. */
_snprintf(
buf,
buflen,
fmt,
tm.wYear,
tm.wMonth,
tm.wDay,
tm.wHour,
tm.wMinute,
tm.wSecond,
tm.wMilliseconds,
((float)tzi.Bias)/60,
GetCurrentProcessId()
);
}
/* Normalize to an absolute path to ensure the log file doesn't split across
* directories when the working directory changes. Unsure if GetFullPathNameA
* handles lpFileName == lpBuffer, hence the temporary copy. */
BOOL
_LogNormalizePath(char *fname, DWORD len_fname, char *tmp, DWORD len_tmp)
{
DWORD Ret = 0;
BOOL Ok = FALSE;
strncpy(tmp, cmdlog_fname, len_tmp);
Ret = GetFullPathNameA(
tmp,
len_fname,
fname,
NULL
);
Ok = (Ret && (Ret < STATIC_BUFFER_LEN(cmdlog_fname)));
return Ok;
}
BOOL
_LogGetPathFromEnv(char path[MAX_PATH])
{
DWORD n = 0;
BOOL Ok = FALSE;
n = GetEnvironmentVariableA(ENVVAR_CMDLOG_LOG_PATH, path, MAX_PATH);
Ok = n && (n < MAX_PATH);
return Ok;
}
BOOL
_LogSetPathToEnv(char path[MAX_PATH])
{
return SetEnvironmentVariableA(ENVVAR_CMDLOG_LOG_PATH, path);
}
void
_LogToFile(FILE *out, const char *data)
{
int do_fclose = 0;
if (!out) {
do_fclose = 1;
out = _fsopen(cmdlog_fname, "ab", _SH_DENYWR);
}
if (!out) {
fprintf(
stderr,
"fopen_s(%s, \"ab\") failed, logging disabled (d)\n",
cmdlog_fname
);
} else {
fprintf(out, "%s", data);
fflush(out);
if (do_fclose) {
fclose(out);
}
}
}
void
_FmtAndLogToFile(FILE *out, const char *fmt, va_list argp)
{
EnterCriticalSection(&cs_output);
memset(buf_output, 0, BUFSIZE_OUTPUT);
vsnprintf(buf_output, BUFSIZE_OUTPUT, fmt, argp);
buf_output[BUFSIZE_OUTPUT-1] = '\0';
_LogToFile(out, buf_output);
LeaveCriticalSection(&cs_output);
}