From 1d4de6a2e06a994045daa1a35c42162266de61d4 Mon Sep 17 00:00:00 2001 From: Nathan Brei <nbrei@jlab.org> Date: Sun, 15 Dec 2024 21:39:11 -0500 Subject: [PATCH] JLogger uses short threadstamps This had previously been blocked by the ODR violations --- src/libraries/JANA/JLogger.cc | 2 ++ src/libraries/JANA/JLogger.h | 15 +++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/libraries/JANA/JLogger.cc b/src/libraries/JANA/JLogger.cc index 61d97c2fd..f4680037d 100644 --- a/src/libraries/JANA/JLogger.cc +++ b/src/libraries/JANA/JLogger.cc @@ -4,4 +4,6 @@ JLogger jout {JLogger::Level::INFO, &std::cout, "jana"}; JLogger jerr {JLogger::Level::ERROR, &std::cerr, "jana"}; +thread_local int JLogger::thread_id = -1; +std::atomic_int JLogger::next_thread_id = 0; diff --git a/src/libraries/JANA/JLogger.h b/src/libraries/JANA/JLogger.h index 1047af88f..27956c0cd 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -9,13 +9,16 @@ #include <iostream> #include <sstream> #include <chrono> -#include <thread> #include <iomanip> #include <time.h> #include <mutex> +#include <atomic> struct JLogger { + static thread_local int thread_id; + static std::atomic_int next_thread_id; + enum class Level { TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF }; Level level; std::ostream *destination; @@ -86,6 +89,13 @@ class JLogMessage : public std::stringstream { builder << std::put_time(&tm_buf, "%H:%M:%S."); builder << std::setfill('0') << std::setw(3) << milliseconds.count() << std::setfill(' ') << " "; } + if (logger.show_threadstamp) { + if (logger.thread_id == -1) { + logger.thread_id = logger.next_thread_id; + logger.next_thread_id += 1; + } + builder << "#" << std::setw(3) << std::setfill('0') << logger.thread_id << " "; + } if (logger.show_level) { switch (level) { case JLogger::Level::TRACE: builder << "[trace] "; break; @@ -97,9 +107,6 @@ class JLogMessage : public std::stringstream { default: builder << "[?????] "; } } - if (logger.show_threadstamp) { - builder << std::this_thread::get_id() << " "; - } if (logger.show_group && !logger.group.empty()) { builder << logger.group << " > "; }