diff --git a/.codeql-prebuild-cpp-Linux.sh b/.codeql-prebuild-cpp-Linux.sh index 9459f2f..bc947e3 100644 --- a/.codeql-prebuild-cpp-Linux.sh +++ b/.codeql-prebuild-cpp-Linux.sh @@ -1,7 +1,7 @@ # install dependencies for C++ analysis set -e -gcc_version=13 +gcc_version=11 sudo apt-get update -y diff --git a/.codeql-prebuild-cpp-macOS.sh b/.codeql-prebuild-cpp-macOS.sh index 267e8eb..9d14b2d 100644 --- a/.codeql-prebuild-cpp-macOS.sh +++ b/.codeql-prebuild-cpp-macOS.sh @@ -1,7 +1,7 @@ # install dependencies for C++ analysis set -e -gcc_version=13 +gcc_version=11 # install dependencies brew install \ diff --git a/src/logging.cpp b/src/logging.cpp index 5d67e53..f78d46e 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -3,7 +3,7 @@ // system includes #include -#include +#include #include #include @@ -45,17 +45,28 @@ namespace display_device { std::stringstream stream; { // Time - const auto now { std::chrono::current_zone()->to_local(std::chrono::system_clock::now()) }; - const auto now_ms { std::chrono::duration_cast(now.time_since_epoch()) }; - const auto now_s { std::chrono::duration_cast(now_ms) }; - - // we need to ensure that the time formatter does not print decimal numbers for seconds as - // it currently has inconsistent formatting logic between platforms... - // Instead we will do it manually. - const auto now_local_seconds { std::chrono::local_seconds(now_s) }; - const auto now_decimal_part { now_ms - now_s }; - - stream << std::format("[{:%Y-%m-%d %H:%M:%S}.{:0>3%Q}] ", now_local_seconds, now_decimal_part); + { + // The stupid std::localtime function may not be thread-safe?! + static std::mutex time_mutex; + std::lock_guard lock { time_mutex }; + + const auto now { std::chrono::system_clock::now() }; + const auto now_ms { std::chrono::duration_cast(now.time_since_epoch()) }; + const auto now_s { std::chrono::duration_cast(now_ms) }; + + std::time_t time { std::chrono::system_clock::to_time_t(now) }; + const auto *localtime { std::localtime(&time) }; + if (localtime) { // It theoretically can fail for unspecified reasons... + const auto now_decimal_part { now_ms - now_s }; + + const auto old_flags { stream.flags() }; // Save formatting flags so that they can be restored... + stream << std::put_time(std::localtime(&time), "[%Y-%m-%d %H:%M:%S.") << std::setfill('0') << std::setw(3) << now_decimal_part.count() << "] "; + stream.flags(old_flags); + } + else { + stream << "[NULL TIME] "; + } + } // Log level switch (log_level) {