mirror of
https://github.com/slsdetectorgroup/aare.git
synced 2026-11-10 03:03:53 +01:00
Make the logger timestamp portable (#401)
logger.hpp pulled in <sys/time.h> and used gettimeofday and localtime_r, which do not exist on MSVC. Build the timestamp from std::chrono instead and use localtime_s on MSVC. The output format is unchanged: <colour>- HH:MM:SS.mmm LEVEL: message<reset>. The unused __SHORT_FORM_OF_FILE__ helper now also strips backslash separators. Add logger tests that pin the timestamp and line format. closes #394 Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
1 parent
3e90eb2539
commit
060a0ec9e7
3 files changed
+90
-20
No files matched your search
@@ -0,0 +1,41 @@
|
||||
// SPDX-License-Identifier: MPL-2.0
|
||||
#include "aare/logger.hpp"
|
||||
|
||||
#include <catch2/catch_test_macros.hpp>
|
||||
#include <catch2/matchers/catch_matchers_string.hpp>
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
|
||||
using Catch::Matchers::EndsWith;
|
||||
using Catch::Matchers::Matches;
|
||||
using Catch::Matchers::StartsWith;
|
||||
|
||||
TEST_CASE("Log timestamp is HH:MM:SS.mmm", "[logger]") {
|
||||
const std::string ts = aare::Logger::Timestamp();
|
||||
REQUIRE_THAT(ts, Matches(R"(\d{2}:\d{2}:\d{2}\.\d{3})"));
|
||||
}
|
||||
|
||||
TEST_CASE("short_file_name strips both kinds of directory separators",
|
||||
"[logger]") {
|
||||
REQUIRE(std::string(aare::short_file_name("/a/b/File.cpp")) == "File.cpp");
|
||||
REQUIRE(std::string(aare::short_file_name("C:\\a\\b\\File.cpp")) ==
|
||||
"File.cpp");
|
||||
REQUIRE(std::string(aare::short_file_name("File.cpp")) == "File.cpp");
|
||||
}
|
||||
|
||||
TEST_CASE("LOG writes one coloured line with timestamp and level", "[logger]") {
|
||||
std::ostringstream captured;
|
||||
auto *previous = std::clog.rdbuf(captured.rdbuf());
|
||||
LOG(aare::logWARNING) << "hello " << 42;
|
||||
std::clog.rdbuf(previous);
|
||||
|
||||
const std::string line = captured.str();
|
||||
// <colour>- HH:MM:SS.mmm WARNING: hello 42<reset>\n
|
||||
REQUIRE_THAT(line, StartsWith(std::string(YELLOW BOLD) + "- "));
|
||||
REQUIRE_THAT(line, Matches(std::string("\x1b\\[33m\x1b\\[1m- ") +
|
||||
R"(\d{2}:\d{2}:\d{2}\.\d{3} WARNING: hello 42)" +
|
||||
"\x1b\\[0m\n"));
|
||||
REQUIRE_THAT(line, EndsWith(std::string(RESET) + "\n"));
|
||||
}
|
||||
Reference in new issue
Block a user