Skip to content

Commit

Permalink
Add logger timestamp/threadstamp parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
nathanwbrei committed Sep 27, 2024
1 parent a1dc577 commit f29a93a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
25 changes: 12 additions & 13 deletions src/libraries/JANA/JLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

#include <JANA/Compatibility/JStreamLog.h>

#include <atomic>
#include <iostream>
#include <sstream>
#include <chrono>
Expand All @@ -19,25 +18,25 @@ struct JLogger {
enum class Level { TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF };
Level level;
std::ostream *destination;
std::string className;
std::string group;
bool show_level = true;
bool show_classname = false;
bool show_group = false;
bool show_timestamp = true;
bool show_threadstamp = false;

explicit JLogger(JLogger::Level level = JLogger::Level::INFO,
std::ostream* destination = &std::cout,
std::string className = "")
: level(level), destination(destination), className(std::move(className)) {};
std::string group = "")
: level(level), destination(destination), group(std::move(group)) {};

JLogger(const JLogger&) = default;
JLogger& operator=(const JLogger&) = default;

void SetTag(std::string tag) {className = tag; }
void SetTimestampFlag() {show_timestamp = true; }
void UnsetTimestampFlag() {show_timestamp = false; }
void SetThreadstampFlag() {show_threadstamp = true; }
void UnsetThreadstampFlag() {show_threadstamp = false; }
void SetGroup(std::string group) {this->group = group; }
void ShowGroup(bool show) {show_group = show; }
void ShowLevel(bool show) {show_level = show; }
void ShowTimestamp(bool show) {show_timestamp = show; }
void ShowThreadstamp(bool show) {show_threadstamp = show; }
};

static JLogger default_cout_logger = JLogger(JLogger::Level::TRACE, &std::cout, "JANA");
Expand Down Expand Up @@ -94,10 +93,10 @@ struct JLogMessage {
}
}
if (logger.show_threadstamp) {
builder << "{" << std::this_thread::get_id() << "} ";
builder << std::this_thread::get_id() << " ";
}
if (logger.show_classname) {
builder << logger.className << ": ";
if (logger.show_group) {
builder << logger.group << ": ";
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/libraries/JANA/Services/JParameterManager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -396,10 +396,15 @@ void JParameterManager::FilterParameters(std::map<std::string, std::string> &par
JLogger JParameterManager::GetLogger(const std::string& component_prefix) {

JLogger logger;
logger.className = component_prefix;
logger.group = component_prefix;

auto global_log_level = RegisterParameter("jana:global_loglevel", JLogger::Level::INFO, "Global log level");

bool enable_timestamp = RegisterParameter("jana:log:show_timestamp", true, "Show timestamp in log output");
auto enable_threadstamp = RegisterParameter("jana:log:show_threadstamp", false, "Show threadstamp in log output");
auto enable_group = RegisterParameter("jana:log:show_group", false, "Show threadstamp in log output");
auto enable_level = RegisterParameter("jana:log:show_level", false, "Show threadstamp in log output");

if (component_prefix.empty()) {
logger.level = global_log_level;
}
Expand All @@ -408,6 +413,11 @@ JLogger JParameterManager::GetLogger(const std::string& component_prefix) {
os << component_prefix << ":loglevel";
logger.level = RegisterParameter(os.str(), global_log_level, "Component log level");
}
logger.ShowLevel(enable_level);
logger.ShowTimestamp(enable_timestamp);
logger.ShowThreadstamp(enable_threadstamp);
logger.ShowGroup(enable_group);
logger.show_threadstamp = enable_threadstamp;
return logger;
}

Expand Down

0 comments on commit f29a93a

Please sign in to comment.