From a3ae965e930fde0a6b7a5483e888cb235c8b8826 Mon Sep 17 00:00:00 2001 From: FrogTheFrog Date: Wed, 17 Apr 2024 09:49:56 +0300 Subject: [PATCH 1/3] fix: downgrade to gcc 11 --- .codeql-prebuild-cpp-Linux.sh | 2 +- .codeql-prebuild-cpp-macOS.sh | 2 +- src/logging.cpp | 39 ++++++++++++++++++++++++++--------- 3 files changed, 31 insertions(+), 12 deletions(-) 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..25d4400 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -3,7 +3,6 @@ // system includes #include -#include #include #include @@ -45,17 +44,37 @@ 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) }; + { + // 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] "; + } + } - // 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 }; + // // 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. + // std::time_t time { std::chrono::system_clock::to_time_t(now) }; + // // 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); + // stream << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S"); // Log level switch (log_level) { From 22e559e422044c9c6e33bbd17b824f6487e4af20 Mon Sep 17 00:00:00 2001 From: FrogTheFrog Date: Wed, 17 Apr 2024 09:55:36 +0300 Subject: [PATCH 2/3] Add missing header --- src/logging.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/src/logging.cpp b/src/logging.cpp index 25d4400..3f81be7 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -3,6 +3,7 @@ // system includes #include +#include #include #include From c9800748b9c7ddb57373f0505c0d9da4d00b240a Mon Sep 17 00:00:00 2001 From: FrogTheFrog Date: Wed, 17 Apr 2024 16:22:29 +0300 Subject: [PATCH 3/3] Remove leftover comments --- src/logging.cpp | 9 --------- 1 file changed, 9 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index 3f81be7..f78d46e 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -68,15 +68,6 @@ namespace display_device { } } - // // 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. - // std::time_t time { std::chrono::system_clock::to_time_t(now) }; - // // const auto now_local_seconds { std::chrono::local_seconds(now_s) }; - // // const auto now_decimal_part { now_ms - now_s }; - - // stream << std::put_time(std::localtime(&time), "%Y-%m-%d %H:%M:%S"); - // Log level switch (log_level) { case log_level_e::verbose: