46 namespace win_eventlog {
72 static std::string
format(std::string
const &user_message, DWORD error_code = GetLastError())
74 std::string system_message;
77 auto format_message_succeeded =
78 ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
79 error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result.hlocal_, 0,
nullptr);
81 if (format_message_succeeded && format_message_result.hlocal_)
83 system_message =
fmt_lib::format(
" ({})", (LPSTR)format_message_result.hlocal_);
86 return fmt_lib::format(
"{}: {}{}", user_message, error_code, system_message);
89 explicit win32_error(std::string
const &func_name, DWORD error = GetLastError())
105 if (!::IsValidSid(psid))
110 auto const sid_length{::GetLengthSid(psid)};
113 result.buffer_.resize(sid_length);
114 if (!::CopySid(sid_length, (PSID)
result.as_sid(), psid))
132 struct process_token_t
134 HANDLE token_handle_ = INVALID_HANDLE_VALUE;
135 explicit process_token_t(HANDLE process)
137 if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_))
145 ::CloseHandle(token_handle_);
148 } current_process_token(::GetCurrentProcess());
152 if (::GetTokenInformation(current_process_token.token_handle_, TokenUser, NULL, 0, &tusize))
158 std::vector<unsigned char> buffer(
static_cast<size_t>(tusize));
159 if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)buffer.data(), tusize, &tusize))
177 return EVENTLOG_SUCCESS;
180 return EVENTLOG_INFORMATION_TYPE;
183 return EVENTLOG_WARNING_TYPE;
188 return EVENTLOG_ERROR_TYPE;
191 return EVENTLOG_INFORMATION_TYPE;
197 return (WORD)msg.
level;
206 template<
typename Mutex>
232 using namespace internal;
239 #ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
243 LPCWSTR lp_wstr = buf.data();
244 succeeded = ::ReportEventW(
event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_,
247 LPCSTR lp_str = formatted.
data();
248 succeeded = ::ReportEventA(
event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_,