-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathLoggerService.cpp
486 lines (386 loc) · 12.7 KB
/
LoggerService.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
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "stdafx.h"
#include "LoggerService.h"
#include "../InstrumentationEngine.Api/InstrumentationEngine.h"
#ifndef PLATFORM_UNIX
#include "DebugLoggerSink.h"
#include "EventLoggerSink.h"
#endif
#include "FileLoggerSink.h"
#include "HostLoggerSink.h"
using namespace MicrosoftInstrumentationEngine;
using namespace std;
CLoggerService::CLoggerService() :
m_defaultFlags(LoggingFlags_None),
m_effectiveFlags(LoggingFlags_None),
m_instrumentationMethodFlags(LoggingFlags_None),
m_fLogToDebugPort(false),
m_loggingFlagsToInstrumentationMethodsMap({
{ LoggingFlags_Errors, {} },
{ LoggingFlags_Trace, {} },
{ LoggingFlags_InstrumentationResults, {} },
}),
m_initialize([=]() { return InitializeCore(); })
{
}
CLoggerService::~CLoggerService()
{
}
HRESULT CLoggerService::GetLoggingHost(_Out_ IProfilerManagerLoggingHost** ppLoggingHost)
{
IfNullRetPointerNoLog(ppLoggingHost);
*ppLoggingHost = nullptr;
IfNotInitRetUnexpected(m_initialize);
CCriticalSectionHolder holder(&m_cs);
return m_pLoggingHost.CopyTo(ppLoggingHost);
}
HRESULT CLoggerService::SetLoggingHost(_In_opt_ IProfilerManagerLoggingHost* pLoggingHost)
{
IfNotInitRetUnexpected(m_initialize);
CCriticalSectionHolder holder(&m_cs);
m_pLoggingHost = pLoggingHost;
return RecalculateLoggingFlags();
}
HRESULT CLoggerService::GetLoggingFlags(_Out_ LoggingFlags* pLoggingFlags)
{
HRESULT hr = S_OK;
IfNullRetPointerNoLog(pLoggingFlags);
*pLoggingFlags = LoggingFlags_None;
IfNotInitRetUnexpected(m_initialize);
// Intentionally returning the effective flags instead of the default flags
// (ones set by default or by SetLoggingFlags). The GetLoggingFlags method
// is primarily meant for consumers external of the Instrumentation Engine
// assembly to determine which flags are supported rather than literally
// reporting what the default was or what was set by SetLoggingFlags.
*pLoggingFlags = m_effectiveFlags;
return S_OK;
}
HRESULT CLoggerService::UpdateInstrumentationMethodLoggingFlags(_In_ GUID classId, _In_ LoggingFlags loggingFlags)
{
HRESULT hr = S_OK;
IfNotInitRetUnexpected(m_initialize);
CCriticalSectionHolder holder(&m_cs);
m_instrumentationMethodFlags = LoggingFlags_None;
IfFailRetNoLog(UpdateInstrumentationMethodFlags(classId, loggingFlags));
// Updates m_instrumentationMethodFlags with any changes to the supported LoggingFlags has at least one InstrumentationMethod
// requiring logging for that level.
for (unordered_map<LoggingFlags, vector<GUID>>::iterator it = m_loggingFlagsToInstrumentationMethodsMap.begin();
it != m_loggingFlagsToInstrumentationMethodsMap.end();
it++)
{
if (!(it->second.empty()))
{
m_instrumentationMethodFlags = (LoggingFlags)(m_instrumentationMethodFlags | it->first);
}
}
return RecalculateLoggingFlags();
}
HRESULT CLoggerService::SetLoggingFlags(_In_ LoggingFlags loggingFlags)
{
IfNotInitRetUnexpected(m_initialize);
CCriticalSectionHolder holder(&m_cs);
m_defaultFlags = loggingFlags;
return RecalculateLoggingFlags();
}
bool CLoggerService::GetLogToDebugPort()
{
IfNotInitRetFalse(m_initialize);
return m_fLogToDebugPort;
}
void CLoggerService::SetLogToDebugPort(_In_ bool enable)
{
IfNotInitRet(m_initialize);
CCriticalSectionHolder holder(&m_cs);
m_fLogToDebugPort = enable;
RecalculateLoggingFlags();
}
HRESULT CLoggerService::Initialize(_In_ std::function<void(const LoggingFlags&)> loggingFlagsCallback)
{
m_LoggingFlagsCallback = loggingFlagsCallback;
return m_initialize.Get();
}
HRESULT CLoggerService::InitializeCore()
{
HRESULT hr = S_OK;
// Get the general logging level from the environment variable
WCHAR wszLogLevel[MAX_PATH];
ZeroMemory(wszLogLevel, MAX_PATH);
if (GetEnvironmentVariable(LogLevelEnvironmentVariableName, wszLogLevel, MAX_PATH) > 0)
{
m_defaultFlags = ExtractLoggingFlags(wszLogLevel);
}
// Get the list of sinks to which logging shall be sent
if (FAILED(CreateSinks(m_allSinks)))
{
return hr;
}
// Initialize each sink with the current logger service
for (shared_ptr<ILoggerSink>& pSink : m_allSinks)
{
IfFailRetNoLog(pSink->Initialize(this));
}
return RecalculateLoggingFlags();
}
void CLoggerService::LogMessage(_In_ LPCWSTR wszMessage, _In_ va_list argptr)
{
IfNotInitRet(m_initialize);
CCriticalSectionHolder holder(&m_cs);
// Check if messages are allowed
if (!AllowLogEntry(LoggingFlags_Trace))
{
return;
}
// Format and truncate the message
WCHAR szLogEntry[LogEntryMaxSize];
_vsnwprintf_s(szLogEntry, LogEntryMaxSize, _TRUNCATE, wszMessage, argptr);
// Send formatted message to each sink
for (shared_ptr<ILoggerSink>& pSink : m_messageSinks)
{
pSink->LogMessage(szLogEntry);
}
}
void CLoggerService::LogMessage(_In_ LPCWSTR wszMessage, ...)
{
va_list argptr;
va_start (argptr, wszMessage);
LogMessage(wszMessage, argptr);
va_end (argptr);
}
void CLoggerService::LogError(_In_ LPCWSTR wszError, _In_ va_list argptr)
{
IfNotInitRet(m_initialize);
CCriticalSectionHolder holder(&m_cs);
// Check if errors are allowed
if (!AllowLogEntry(LoggingFlags_Errors))
{
return;
}
// Format and truncate the error
WCHAR szLogEntry[LogEntryMaxSize];
_vsnwprintf_s(szLogEntry, LogEntryMaxSize, _TRUNCATE, wszError, argptr);
// Send formatted error to each sink
for (shared_ptr<ILoggerSink>& pSink : m_errorSinks)
{
pSink->LogError(szLogEntry);
}
if (IsDebuggerPresent())
{
try
{
__debugbreak();
}
// SEH Exceptions are sent through C++ handlers in the instrumentation engine.
catch (...)
{
}
}
}
void CLoggerService::LogError(_In_ LPCWSTR wszError, ...)
{
va_list argptr;
va_start(argptr, wszError);
LogError(wszError, argptr);
va_end(argptr);
}
void CLoggerService::LogDumpMessage(_In_ LPCWSTR wszMessage, _In_ va_list argptr)
{
IfNotInitRet(m_initialize);
CCriticalSectionHolder holder(&m_cs);
// Check if dumps are allowed
if (!AllowLogEntry(LoggingFlags_InstrumentationResults))
{
return;
}
// Format and truncate the message
WCHAR szLogEntry[LogEntryMaxSize];
_vsnwprintf_s(szLogEntry, LogEntryMaxSize, _TRUNCATE, wszMessage, argptr);
// Send formatted message to each sink
for (shared_ptr<ILoggerSink>& pSink : m_dumpSinks)
{
pSink->LogDumpMessage(szLogEntry);
}
}
void CLoggerService::LogDumpMessage(_In_ LPCWSTR wszMessage, ...)
{
va_list argptr;
va_start(argptr, wszMessage);
LogDumpMessage(wszMessage, argptr);
va_end(argptr);
}
bool CLoggerService::AllowLogEntry(_In_ LoggingFlags flags)
{
IfNotInitRetFalse(m_initialize);
return IsFlagSet((LoggingFlags)(m_effectiveFlags | m_instrumentationMethodFlags), flags);
}
// static
LoggingFlags CLoggerService::ExtractLoggingFlags(_In_ LPCWSTR wszRequestedFlagNames)
{
if (nullptr == wszRequestedFlagNames)
{
return LoggingFlags_None;
}
// If the request is for all logging flags, just return the allowable flags
if (wcsstr(wszRequestedFlagNames, _T("All")) != nullptr)
{
return LoggingFlags_All;
}
// For each logging flag, check that the named flag was specified. Combine the results
// of each (since each result will be a single flag) and return the combination.
return (LoggingFlags)(
ExtractLoggingFlag(wszRequestedFlagNames, _T("Errors"), LoggingFlags_Errors) |
ExtractLoggingFlag(wszRequestedFlagNames, _T("Messages"), LoggingFlags_Trace) |
ExtractLoggingFlag(wszRequestedFlagNames, _T("Dumps"), LoggingFlags_InstrumentationResults)
);
}
// static
LoggingFlags CLoggerService::ExtractLoggingFlag(
_In_ LPCWSTR wszRequestedFlagNames,
_In_ LPCWSTR wszSingleTestFlagName,
_In_ LoggingFlags singleTestFlag
)
{
if (nullptr == wszRequestedFlagNames || nullptr == wszSingleTestFlagName)
{
return LoggingFlags_None;
}
// Check that the name of the flag is in the string that specifies what types
// of messages are requested to be logged.
if (wcsstr(wszRequestedFlagNames, wszSingleTestFlagName) != nullptr)
{
return singleTestFlag;
}
return LoggingFlags_None;
}
HRESULT CLoggerService::RecalculateLoggingFlags()
{
HRESULT hr = S_OK;
m_dumpSinks.clear();
m_errorSinks.clear();
m_messageSinks.clear();
LoggingFlags effectiveFlags = LoggingFlags_None;
for (shared_ptr<ILoggerSink>& pSink : m_allSinks)
{
// Reset the sink; get its desired logging level
LoggingFlags sinkFlags;
LoggingFlags instrumentationMethodSinkFlags;
IfFailRetNoLog(pSink->Reset(m_defaultFlags, &sinkFlags));
IfFailRetNoLog(pSink->Reset(m_instrumentationMethodFlags, &instrumentationMethodSinkFlags));
// Add the sink to each logging level vector
if (IsFlagSet(sinkFlags | instrumentationMethodSinkFlags, LoggingFlags_Errors))
{
m_errorSinks.push_back(pSink);
}
if (IsFlagSet(sinkFlags | instrumentationMethodSinkFlags, LoggingFlags_InstrumentationResults))
{
m_dumpSinks.push_back(pSink);
}
if (IsFlagSet(sinkFlags | instrumentationMethodSinkFlags, LoggingFlags_Trace))
{
m_messageSinks.push_back(pSink);
}
// Aggregate all logging levels together to get the overall logging level.
effectiveFlags = static_cast<LoggingFlags>(effectiveFlags | sinkFlags);
}
m_effectiveFlags = effectiveFlags;
if (m_LoggingFlagsCallback != nullptr)
{
m_LoggingFlagsCallback(effectiveFlags);
}
return S_OK;
}
HRESULT CLoggerService::Shutdown()
{
CCriticalSectionHolder holder(&m_cs);
HRESULT hr = S_OK;
for (shared_ptr<ILoggerSink>& pSink : m_allSinks)
{
IfFailRetNoLog(pSink->Shutdown());
}
m_allSinks.clear();
m_dumpSinks.clear();
m_errorSinks.clear();
m_messageSinks.clear();
m_pLoggingHost.Release();
m_initialize.Reset();
return S_OK;
}
HRESULT CLoggerService::CreateSinks(vector<shared_ptr<ILoggerSink>>& sinks)
{
#ifndef PLATFORM_UNIX
sinks.push_back(make_shared<CDebugLoggerSink>());
sinks.push_back(make_shared<CEventLoggerSink>());
#endif
sinks.push_back(make_shared<CFileLoggerSink>());
sinks.push_back(make_shared<CHostLoggerSink>());
return S_OK;
}
HRESULT CLoggerService::UpdateInstrumentationMethodFlags(_In_ GUID classId, _In_ LoggingFlags loggingFlags)
{
HRESULT hr = S_OK;
IfFailRetNoLog(UpdateInstrumentationMethodFlagsInternal(
classId,
loggingFlags,
LoggingFlags_Errors));
IfFailRetNoLog(UpdateInstrumentationMethodFlagsInternal(
classId,
loggingFlags,
LoggingFlags_Trace));
IfFailRetNoLog(UpdateInstrumentationMethodFlagsInternal(
classId,
loggingFlags,
LoggingFlags_InstrumentationResults));
return S_OK;
}
HRESULT CLoggerService::UpdateInstrumentationMethodFlagsInternal(_In_ GUID classId, _In_ LoggingFlags loggingFlags, _In_ LoggingFlags loggingLevel)
{
vector<GUID>* pInstrumentationMethods = &(m_loggingFlagsToInstrumentationMethodsMap[loggingLevel]);
bool exists = false;
vector<GUID>::iterator it;
for (it = pInstrumentationMethods->begin();
it != pInstrumentationMethods->end();
it++)
{
if ((*it) == classId)
{
exists = true;
break;
}
}
bool shouldExist = (loggingLevel & loggingFlags) != 0;
if (exists && !shouldExist)
{
pInstrumentationMethods->erase(it);
}
else if (!exists && shouldExist)
{
pInstrumentationMethods->push_back(classId);
}
return S_OK;
}
HRESULT CLoggerService::SetLogFilePath(_In_ LPCWSTR wszLogFilePath)
{
HRESULT hr = S_OK;
for (shared_ptr<ILoggerSink>& pSink : m_allSinks)
{
IFileLoggerSink* pFileSink = dynamic_cast<IFileLoggerSink*>(pSink.get());
if (pFileSink)
{
IfFailRetNoLog(pFileSink->SetLogFilePath(wszLogFilePath));
}
}
return RecalculateLoggingFlags();
}
HRESULT CLoggerService::SetLogFileLevel(_In_ LoggingFlags fileLogFlags)
{
HRESULT hr = S_OK;
for (shared_ptr<ILoggerSink>& pSink : m_allSinks)
{
IFileLoggerSink* pFileSink = dynamic_cast<IFileLoggerSink*>(pSink.get());
if (pFileSink)
{
IfFailRetNoLog(pFileSink->SetLogFileLevel(fileLogFlags));
}
}
return RecalculateLoggingFlags();
}