-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathFileLoggerSink.cpp
273 lines (232 loc) · 7.17 KB
/
FileLoggerSink.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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
#include "stdafx.h"
#include "FileLoggerSink.h"
using namespace MicrosoftInstrumentationEngine;
CFileLoggerSink::CFileLoggerSink() :
m_flags(LoggingFlags_None),
m_pOutputFile(nullptr)
{
}
CFileLoggerSink::~CFileLoggerSink()
{
CloseLogFile();
}
HRESULT CFileLoggerSink::Initialize(_In_ CLoggerService* pLogging)
{
return GetPathAndFlags(&m_tsPathCandidate, &m_flags);
}
void CFileLoggerSink::LogMessage(_In_ LPCWSTR wszMessage)
{
WritePrefix(LoggingFlags_Trace);
WriteLine(wszMessage);
}
void CFileLoggerSink::LogError(_In_ LPCWSTR wszMessage)
{
WritePrefix(LoggingFlags_Errors);
WriteLine(wszMessage);
}
void CFileLoggerSink::LogDumpMessage(_In_ LPCWSTR wszMessage)
{
WritePrefix(LoggingFlags_InstrumentationResults);
WriteLine(wszMessage);
}
HRESULT CFileLoggerSink::Reset(_In_ LoggingFlags defaultFlags, _Out_ LoggingFlags* pEffectiveFlags)
{
if (!pEffectiveFlags)
{
return E_POINTER;
}
*pEffectiveFlags = LoggingFlags_None;
// Use flags from FileLog environment variable first. If not defined, then use the requested flags.
LoggingFlags effectiveFlags = m_flags;
if (LoggingFlags_None == effectiveFlags)
{
effectiveFlags = defaultFlags;
}
// Start logging to file if logging has been enabled (has flags, not already enabled, and has file path).
if (LoggingFlags_None != effectiveFlags && !m_pOutputFile && !m_tsPathCandidate.empty())
{
// Split the provided path in parts
WCHAR wszDrive[_MAX_DRIVE];
WCHAR wszDirectory[_MAX_DIR];
WCHAR wszFile[_MAX_FNAME];
WCHAR wszExtension[_MAX_EXT];
_wsplitpath_s(
m_tsPathCandidate.c_str(),
wszDrive,
_MAX_DRIVE,
wszDirectory,
_MAX_DIR,
wszFile,
_MAX_FNAME,
wszExtension,
_MAX_EXT);
tstring tsFile(wszFile);
tstring tsExtension(wszExtension);
// The file name is empty if (a) the last character in the path is a path separator
// or (b) if no file name is given e.g. the file starts with a period.
// Generate a file name if we're given a directory.
bool isDirectory = false;
if (tsFile.empty())
{
isDirectory = (0 == tsExtension.compare(_T(".")) || 0 == tsExtension.compare(_T("")));
}
else
{
// Check if fileName is actually a directory, meaning the path separator was left off
DWORD fileFlags = GetFileAttributes(m_tsPathCandidate.c_str());
if (fileFlags != INVALID_FILE_ATTRIBUTES && // path exists
(fileFlags & FILE_ATTRIBUTE_DIRECTORY) != 0) // is a directory
{
wcscat_s(wszDirectory, _MAX_DIR, wszFile);
isDirectory = true;
}
}
if (isDirectory)
{
tsFile.assign(_T("ProfilerLog_"));
WCHAR szPid[MAX_PATH];
swprintf_s(szPid, MAX_PATH, _T("%u"), GetCurrentProcessId());
tsFile.append(szPid);
tsExtension.clear();
tsExtension.assign(_T(".txt"));
}
// Combine path parts together for overall path
WCHAR wszFilePath[MAX_PATH];
_wmakepath_s(
wszFilePath,
MAX_PATH,
wszDrive,
wszDirectory,
tsFile.c_str(),
tsExtension.c_str());
// _wfsopen just returns nullptr on PLATFORM_UNIX
#ifdef PLATFORM_UNIX
m_pOutputFile.reset(_wfopen(wszFilePath, _T("a")));
#else
// will create a new file if there is permission to in the
// given path, and permissions will be inherited.
m_pOutputFile.reset(_wfsopen(wszFilePath, _T("a"), _SH_DENYWR)); // lgtm[cpp/world-writable-file-creation]
#endif
m_tsPathActual.clear();
if (m_pOutputFile)
{
m_tsPathActual = wszFilePath;
}
}
// Only return non-None flags if the output file has been opened.
if (m_pOutputFile)
{
*pEffectiveFlags = effectiveFlags;
}
return S_OK;
}
HRESULT CFileLoggerSink::Shutdown()
{
CloseLogFile();
return S_OK;
}
void CFileLoggerSink::CloseLogFile()
{
FILE* pOutputFile = m_pOutputFile.get();
if (pOutputFile)
{
fflush(pOutputFile);
// Pointer has deleter that will close the file.
m_pOutputFile.reset(nullptr);
}
}
void CFileLoggerSink::WritePrefix(_In_ LoggingFlags flags)
{
// Use UTF-16 on Windows to avoid string encoding conversions from multi-byte to Unicode.
#ifdef PLATFORM_UNIX
const char* szFormat = nullptr;
#else
const WCHAR* szFormat = nullptr;
#endif
switch (flags)
{
case LoggingFlags_Errors:
szFormat = ERROR_PREFIX_FORMAT;
break;
case LoggingFlags_Trace:
szFormat = MESSAGE_PREFIX_FORMAT;
break;
}
if (nullptr != szFormat)
{
std::time_t currentTime = std::time(nullptr);
std::tm localTime = { 0 };
#ifdef PLATFORM_UNIX
if (nullptr != localtime_r(¤tTime, &localTime))
#else
if (localtime_s(&localTime, ¤tTime) == 0)
#endif
{
FILE* pOutputFile = m_pOutputFile.get();
if (pOutputFile)
{
#ifdef PLATFORM_UNIX
char szFormatted[MAX_PATH];
strftime(szFormatted, MAX_PATH, szFormat, &localTime);
fprintf(pOutputFile, szFormatted);
#else
WCHAR szFormatted[MAX_PATH];
wcsftime(szFormatted, MAX_PATH, szFormat, &localTime);
fwprintf(pOutputFile, L"%s", szFormatted);
#endif
}
}
}
}
void CFileLoggerSink::WriteLine(_In_ LPCWSTR wszMessage)
{
FILE* pOutputFile = m_pOutputFile.get();
if (pOutputFile)
{
fwprintf(pOutputFile, wszMessage);
fwprintf(pOutputFile, _T("\n"));
fflush(pOutputFile);
}
}
const tstring& CFileLoggerSink::GetPathActual()
{
return m_tsPathActual;
}
HRESULT CFileLoggerSink::GetPathAndFlags(_Out_ tstring* ptsPath, _Out_ LoggingFlags* pFlags)
{
if (!ptsPath || !pFlags)
{
return E_POINTER;
}
ptsPath->clear();
*pFlags = LoggingFlags_None;
WCHAR wszFileLogLevel[MAX_PATH];
ZeroMemory(wszFileLogLevel, MAX_PATH);
if (GetEnvironmentVariable(LogLevelEnvironmentVariableName, wszFileLogLevel, MAX_PATH) > 0)
{
*pFlags = CLoggerService::ExtractLoggingFlags(wszFileLogLevel);
}
WCHAR wszFileLogPath[MAX_PATH];
ZeroMemory(wszFileLogPath, MAX_PATH);
if (GetEnvironmentVariable(LogPathEnvironmentVariableName, wszFileLogPath, MAX_PATH) > 0)
{
*ptsPath = wszFileLogPath;
}
return S_OK;
}
HRESULT CFileLoggerSink::SetLogFilePath(_In_ LPCWSTR wszLogFilePath)
{
m_tsPathCandidate = wszLogFilePath;
m_pOutputFile = nullptr; // resets the pointer & releases the FILE object
m_tsPathActual.clear();
return S_OK;
}
HRESULT CFileLoggerSink::SetLogFileLevel(_In_ LoggingFlags fileLogFlags)
{
m_flags = fileLogFlags;
m_pOutputFile = nullptr; // resets the pointer & releases the FILE object
m_tsPathActual.clear();
return S_OK;
}