1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 sts=2 et cindent: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_WindowsEventLog_h
8 #define mozilla_WindowsEventLog_h
11 * Report messages to the Windows Event Log.
17 #include "mozilla/Attributes.h"
18 #include "mozilla/UniquePtr.h"
21 * This header is intended for self-contained, header-only, utility code for
22 * Win32. It may be used outside of xul.dll, in places such as
23 * default-browser-agent.exe or notificationrouter.dll. If your code creates
24 * dependencies on Mozilla libraries, you should put it elsewhere.
27 #define MOZ_WIN_EVENT_LOG_ERROR(source, hr) \
28 mozilla::WriteWindowsEventLogHresult(source, hr, __FUNCTION__, __LINE__)
29 #define MOZ_WIN_EVENT_LOG_ERROR_MESSAGE(source, format, ...) \
30 mozilla::WriteWindowsEventLogErrorMessage(source, format, __FUNCTION__, \
31 __LINE__, ##__VA_ARGS__)
35 static void WriteWindowsEventLogErrorBuffer(const wchar_t* eventSourceName
,
36 const wchar_t* buffer
,
38 HANDLE source
= RegisterEventSourceW(nullptr, eventSourceName
);
40 // Not much we can do about this.
44 const wchar_t* stringsArray
[] = {buffer
};
45 ReportEventW(source
, EVENTLOG_ERROR_TYPE
, 0, eventId
, nullptr, 1, 0,
46 stringsArray
, nullptr);
48 DeregisterEventSource(source
);
51 void WriteWindowsEventLogHresult(const wchar_t* eventSourceName
, HRESULT hr
,
52 const char* sourceFile
, int sourceLine
) {
53 const wchar_t* format
= L
"0x%X in %S:%d";
54 int bufferSize
= _scwprintf(format
, hr
, sourceFile
, sourceLine
);
55 ++bufferSize
; // Extra character for terminating null
56 mozilla::UniquePtr
<wchar_t[]> errorStr
=
57 mozilla::MakeUnique
<wchar_t[]>(bufferSize
);
59 _snwprintf_s(errorStr
.get(), bufferSize
, _TRUNCATE
, format
, hr
, sourceFile
,
62 WriteWindowsEventLogErrorBuffer(eventSourceName
, errorStr
.get(), hr
);
65 MOZ_FORMAT_WPRINTF(1, 4)
66 void WriteWindowsEventLogErrorMessage(const wchar_t* eventSourceName
,
67 const wchar_t* messageFormat
,
68 const char* sourceFile
, int sourceLine
,
70 // First assemble the passed message
72 va_start(ap
, sourceLine
);
73 int bufferSize
= _vscwprintf(messageFormat
, ap
);
74 ++bufferSize
; // Extra character for terminating null
76 mozilla::UniquePtr
<wchar_t[]> message
=
77 mozilla::MakeUnique
<wchar_t[]>(bufferSize
);
79 va_start(ap
, sourceLine
);
80 vswprintf(message
.get(), bufferSize
, messageFormat
, ap
);
83 // Next, assemble the complete error message to print
84 const wchar_t* errorFormat
= L
"Error: %s (%S:%d)";
85 bufferSize
= _scwprintf(errorFormat
, message
.get(), sourceFile
, sourceLine
);
86 ++bufferSize
; // Extra character for terminating null
87 mozilla::UniquePtr
<wchar_t[]> errorStr
=
88 mozilla::MakeUnique
<wchar_t[]>(bufferSize
);
90 _snwprintf_s(errorStr
.get(), bufferSize
, _TRUNCATE
, errorFormat
,
91 message
.get(), sourceFile
, sourceLine
);
93 WriteWindowsEventLogErrorBuffer(eventSourceName
, errorStr
.get(), 0);
96 } // namespace mozilla
98 #endif // mozilla_WindowsEventLog_h