2020-02-10 06:53:01 +08:00
|
|
|
#if _WIN32
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
#include "includes.h"
|
|
|
|
#include "test_sink.h"
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
#include "spdlog/sinks/win_eventlog_sink.h"
|
2020-02-09 17:52:26 +08:00
|
|
|
|
2020-02-10 06:37:48 +08:00
|
|
|
static const LPCSTR TEST_SOURCE = "spdlog_test";
|
2020-02-09 17:52:26 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
static void test_single_print(std::function<void(std::string const &)> do_log,
|
|
|
|
std::string const &expected_contents,
|
|
|
|
WORD expected_ev_type) {
|
2020-02-04 21:38:26 +08:00
|
|
|
using namespace std::chrono;
|
2020-02-10 06:37:48 +08:00
|
|
|
do_log(expected_contents);
|
2023-09-25 21:05:07 +08:00
|
|
|
const auto expected_time_generated =
|
|
|
|
duration_cast<seconds>(system_clock::now().time_since_epoch()).count();
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
struct handle_t {
|
2020-02-04 21:38:26 +08:00
|
|
|
HANDLE handle_;
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
~handle_t() {
|
|
|
|
if (handle_) {
|
2020-02-04 21:38:26 +08:00
|
|
|
REQUIRE(CloseEventLog(handle_));
|
2020-02-10 06:37:48 +08:00
|
|
|
}
|
2020-02-04 21:38:26 +08:00
|
|
|
}
|
2021-01-06 16:55:46 +08:00
|
|
|
} event_log{::OpenEventLogA(nullptr, TEST_SOURCE)};
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2020-02-10 06:37:48 +08:00
|
|
|
REQUIRE(event_log.handle_);
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2020-02-10 23:13:41 +08:00
|
|
|
DWORD read_bytes{}, size_needed{};
|
2023-09-25 21:05:07 +08:00
|
|
|
auto ok = ::ReadEventLogA(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ,
|
|
|
|
0, &read_bytes, 0, &read_bytes, &size_needed);
|
2020-02-10 23:13:41 +08:00
|
|
|
REQUIRE(!ok);
|
2020-02-10 06:37:48 +08:00
|
|
|
REQUIRE(::GetLastError() == ERROR_INSUFFICIENT_BUFFER);
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2020-02-10 06:37:48 +08:00
|
|
|
std::vector<char> record_buffer(size_needed);
|
|
|
|
PEVENTLOGRECORD record = (PEVENTLOGRECORD)record_buffer.data();
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
ok = ::ReadEventLogA(event_log.handle_, EVENTLOG_SEQUENTIAL_READ | EVENTLOG_BACKWARDS_READ, 0,
|
|
|
|
record, size_needed, &read_bytes, &size_needed);
|
2020-02-04 21:38:26 +08:00
|
|
|
REQUIRE(ok);
|
|
|
|
|
|
|
|
REQUIRE(record->NumStrings == 1);
|
2020-02-10 06:37:48 +08:00
|
|
|
REQUIRE(record->EventType == expected_ev_type);
|
2020-03-28 18:59:59 +08:00
|
|
|
REQUIRE((expected_time_generated - record->TimeGenerated) <= 3u);
|
2020-02-04 21:38:26 +08:00
|
|
|
|
2020-02-10 23:13:41 +08:00
|
|
|
std::string message_in_log(((char *)record + record->StringOffset));
|
2020-02-10 06:37:48 +08:00
|
|
|
REQUIRE(message_in_log == expected_contents + spdlog::details::os::default_eol);
|
2020-02-04 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
TEST_CASE("eventlog", "[eventlog]") {
|
2020-02-04 21:38:26 +08:00
|
|
|
using namespace spdlog;
|
|
|
|
|
2020-02-09 17:52:26 +08:00
|
|
|
auto test_sink = std::make_shared<sinks::win_eventlog_sink_mt>(TEST_SOURCE);
|
2020-02-04 21:38:26 +08:00
|
|
|
|
|
|
|
spdlog::logger test_logger("eventlog", test_sink);
|
2023-09-23 22:59:51 +08:00
|
|
|
test_logger.set_level(level::trace);
|
2020-02-04 21:38:26 +08:00
|
|
|
|
|
|
|
test_sink->set_pattern("%v");
|
|
|
|
|
2023-09-25 21:05:07 +08:00
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.trace(msg); },
|
|
|
|
"my trace message", EVENTLOG_SUCCESS);
|
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.debug(msg); },
|
|
|
|
"my debug message", EVENTLOG_SUCCESS);
|
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.info(msg); },
|
|
|
|
"my info message", EVENTLOG_INFORMATION_TYPE);
|
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.warn(msg); },
|
|
|
|
"my warn message", EVENTLOG_WARNING_TYPE);
|
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.error(msg); },
|
|
|
|
"my error message", EVENTLOG_ERROR_TYPE);
|
|
|
|
test_single_print([&test_logger](std::string const &msg) { test_logger.critical(msg); },
|
|
|
|
"my critical message", EVENTLOG_ERROR_TYPE);
|
2020-02-04 21:38:26 +08:00
|
|
|
}
|
|
|
|
|
2020-03-28 18:59:59 +08:00
|
|
|
#endif //_WIN32
|