Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .codeql-prebuild-cpp-Linux.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# install dependencies for C++ analysis
set -e

gcc_version=13
gcc_version=11

sudo apt-get update -y

Expand Down
2 changes: 1 addition & 1 deletion .codeql-prebuild-cpp-macOS.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# install dependencies for C++ analysis
set -e

gcc_version=13
gcc_version=11

# install dependencies
brew install \
Expand Down
35 changes: 23 additions & 12 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// system includes
#include <chrono>
#include <format>
#include <iomanip>
#include <iostream>
#include <mutex>

Expand Down Expand Up @@ -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<std::chrono::milliseconds>(now.time_since_epoch()) };
const auto now_s { std::chrono::duration_cast<std::chrono::seconds>(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<std::chrono::milliseconds>(now.time_since_epoch()) };
const auto now_s { std::chrono::duration_cast<std::chrono::seconds>(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) {
Expand Down