Skip to content

Commit

Permalink
JLogger uses short threadstamps
Browse files Browse the repository at this point in the history
This had previously been blocked by the ODR violations
  • Loading branch information
nathanwbrei committed Dec 16, 2024
1 parent 29a4760 commit 1d4de6a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/libraries/JANA/JLogger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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;

15 changes: 11 additions & 4 deletions src/libraries/JANA/JLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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 << " > ";
}
Expand Down

0 comments on commit 1d4de6a

Please sign in to comment.