48 namespace win_eventlog
78 static std::string
format(std::string
const& user_message, DWORD error_code = GetLastError())
80 std::string system_message;
83 auto format_message_succeeded =
84 ::FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
nullptr,
85 error_code, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&format_message_result.hlocal_, 0,
nullptr);
87 if (format_message_succeeded && format_message_result.hlocal_)
89 system_message = fmt_lib::format(
" ({})", (LPSTR)format_message_result.hlocal_);
92 return fmt_lib::format(
"{}: {}{}", user_message, error_code, system_message);
114 if (!::IsValidSid(psid))
119 auto const sid_length{::GetLengthSid(psid)};
122 result.buffer_.resize(sid_length);
123 if (!::CopySid(sid_length, (PSID)result.as_sid(), psid))
141 struct process_token_t
143 HANDLE token_handle_ = INVALID_HANDLE_VALUE;
144 explicit process_token_t(HANDLE process)
146 if (!::OpenProcessToken(process, TOKEN_QUERY, &token_handle_))
154 ::CloseHandle(token_handle_);
157 } current_process_token(::GetCurrentProcess());
161 if (::GetTokenInformation(current_process_token.token_handle_, TokenUser,
NULL, 0, &tusize))
167 std::vector<unsigned char>
buffer(
static_cast<size_t>(tusize));
168 if (!::GetTokenInformation(current_process_token.token_handle_, TokenUser, (LPVOID)
buffer.
data(), tusize, &tusize))
186 return EVENTLOG_SUCCESS;
189 return EVENTLOG_INFORMATION_TYPE;
192 return EVENTLOG_WARNING_TYPE;
197 return EVENTLOG_ERROR_TYPE;
200 return EVENTLOG_INFORMATION_TYPE;
206 return (WORD)msg.
level;
215 template <
typename Mutex>
241 using namespace internal;
246 formatted.push_back(
'\0');
248#ifdef SPDLOG_WCHAR_TO_UTF8_SUPPORT
250 details::os::utf8_to_wstrbuf(
string_view_t(formatted.data(), formatted.size()), buf);
252 LPCWSTR lp_wstr = buf.data();
253 succeeded =
static_cast<bool>(::ReportEventW(
event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_,
256 LPCSTR lp_str = formatted.data();
257 succeeded =
static_cast<bool>(::ReportEventA(
event_log_handle(), eventlog::get_event_type(msg), eventlog::get_event_category(msg),
event_id_,
FMT_CONSTEXPR auto data() noexcept -> T *
Definition core.h:1102
Definition base_sink.h:22
Definition win_eventlog_sink.h:217
HANDLE event_log_handle()
Definition win_eventlog_sink.h:224
win_eventlog_sink(std::string const &source, DWORD event_id=1000)
Definition win_eventlog_sink.h:272
internal::sid_t current_user_sid_
Definition win_eventlog_sink.h:220
std::string source_
Definition win_eventlog_sink.h:221
~win_eventlog_sink()
Definition win_eventlog_sink.h:286
void sink_it_(const details::log_msg &msg) override
Definition win_eventlog_sink.h:239
DWORD event_id_
Definition win_eventlog_sink.h:222
void flush_() override
Definition win_eventlog_sink.h:267
HANDLE hEventLog_
Definition win_eventlog_sink.h:219
#define SPDLOG_NOEXCEPT
Definition common.h:69
#define SPDLOG_CONSTEXPR
Definition common.h:70
#define SPDLOG_THROW(ex)
Definition common.h:108
@ critical
Definition common.h:239
@ err
Definition common.h:238
@ warn
Definition common.h:237
@ info
Definition common.h:236
@ off
Definition common.h:240
@ trace
Definition common.h:234
@ debug
Definition common.h:235
void error(format_string_t< Args... > fmt, Args &&... args)
Definition spdlog.h:181
fmt::basic_string_view< char > string_view_t
Definition common.h:172
SPDLOG_INLINE void throw_spdlog_ex(const std::string &msg, int last_errno)
Definition common-inl.h:75
fmt::basic_memory_buffer< char, 250 > memory_buf_t
Definition common.h:173
#define NULL
Definition strtod.cpp:30
level::level_enum level
Definition log_msg.h:23
Definition win_eventlog_sink.h:179
static WORD get_event_category(details::log_msg const &msg)
Definition win_eventlog_sink.h:204
static WORD get_event_type(details::log_msg const &msg)
Definition win_eventlog_sink.h:180
Definition win_eventlog_sink.h:55
SPDLOG_CONSTEXPR local_alloc_t() SPDLOG_NOEXCEPT
Definition win_eventlog_sink.h:58
local_alloc_t(local_alloc_t const &)=delete
~local_alloc_t() SPDLOG_NOEXCEPT
Definition win_eventlog_sink.h:65
local_alloc_t & operator=(local_alloc_t const &)=delete
HLOCAL hlocal_
Definition win_eventlog_sink.h:56
Definition win_eventlog_sink.h:103
std::vector< char > buffer_
Definition win_eventlog_sink.h:104
static sid_t duplicate_sid(PSID psid)
Definition win_eventlog_sink.h:112
sid_t()
Definition win_eventlog_sink.h:107
SID * as_sid() const
Definition win_eventlog_sink.h:132
static sid_t get_current_user_sid()
Definition win_eventlog_sink.h:138
Definition win_eventlog_sink.h:76
static std::string format(std::string const &user_message, DWORD error_code=GetLastError())
Definition win_eventlog_sink.h:78
win32_error(std::string const &func_name, DWORD error=GetLastError())
Definition win_eventlog_sink.h:95
source
Definition tag_strings.h:83