Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / widget / windows / WindowsEventLog.h
blobe98d5077a07d2864226df3a2c6fd8f669b0c3329
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
10 /**
11 * Report messages to the Windows Event Log.
14 #include <stdio.h>
15 #include <windows.h>
17 #include "mozilla/Attributes.h"
18 #include "mozilla/UniquePtr.h"
20 /**
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__)
33 namespace mozilla {
35 static void WriteWindowsEventLogErrorBuffer(const wchar_t* eventSourceName,
36 const wchar_t* buffer,
37 DWORD eventId) {
38 HANDLE source = RegisterEventSourceW(nullptr, eventSourceName);
39 if (!source) {
40 // Not much we can do about this.
41 return;
44 const wchar_t* stringsArray[] = {buffer};
45 ReportEventW(source, EVENTLOG_ERROR_TYPE, 0, eventId, nullptr, 1, 0,
46 stringsArray, nullptr);
48 DeregisterEventSource(source);
51 inline void WriteWindowsEventLogHresult(const wchar_t* eventSourceName,
52 HRESULT hr, const char* sourceFile,
53 int sourceLine) {
54 const wchar_t* format = L"0x%X in %S:%d";
55 int bufferSize = _scwprintf(format, hr, sourceFile, sourceLine);
56 ++bufferSize; // Extra character for terminating null
57 mozilla::UniquePtr<wchar_t[]> errorStr =
58 mozilla::MakeUnique<wchar_t[]>(bufferSize);
60 _snwprintf_s(errorStr.get(), bufferSize, _TRUNCATE, format, hr, sourceFile,
61 sourceLine);
63 WriteWindowsEventLogErrorBuffer(eventSourceName, errorStr.get(), hr);
66 MOZ_FORMAT_WPRINTF(1, 4)
67 inline void WriteWindowsEventLogErrorMessage(const wchar_t* eventSourceName,
68 const wchar_t* messageFormat,
69 const char* sourceFile,
70 int sourceLine, ...) {
71 // First assemble the passed message
72 va_list ap;
73 va_start(ap, sourceLine);
74 int bufferSize = _vscwprintf(messageFormat, ap);
75 ++bufferSize; // Extra character for terminating null
76 va_end(ap);
77 mozilla::UniquePtr<wchar_t[]> message =
78 mozilla::MakeUnique<wchar_t[]>(bufferSize);
80 va_start(ap, sourceLine);
81 vswprintf(message.get(), bufferSize, messageFormat, ap);
82 va_end(ap);
84 // Next, assemble the complete error message to print
85 const wchar_t* errorFormat = L"Error: %s (%S:%d)";
86 bufferSize = _scwprintf(errorFormat, message.get(), sourceFile, sourceLine);
87 ++bufferSize; // Extra character for terminating null
88 mozilla::UniquePtr<wchar_t[]> errorStr =
89 mozilla::MakeUnique<wchar_t[]>(bufferSize);
91 _snwprintf_s(errorStr.get(), bufferSize, _TRUNCATE, errorFormat,
92 message.get(), sourceFile, sourceLine);
94 WriteWindowsEventLogErrorBuffer(eventSourceName, errorStr.get(), 0);
97 } // namespace mozilla
99 #endif // mozilla_WindowsEventLog_h