From b1fa7521114faf42d8f3fc331c14b789c6247a76 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 13 Dec 2024 16:16:23 -0500 Subject: [PATCH 1/6] Stream directly into JLogger This way, `jout` and `jerr` can both be global JLoggers instead of weird macros --- src/libraries/JANA/JLogger.h | 13 +++++++++++++ src/programs/unit_tests/Utils/JLoggerTests.cc | 10 ++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/libraries/JANA/JLogger.h b/src/libraries/JANA/JLogger.h index ebbc2138c..0fa8d6702 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -66,6 +66,10 @@ class JLogMessage : public std::stringstream { JLogMessage(const std::string& prefix="") : m_prefix(prefix){ } + JLogMessage(JLogMessage&& moved_from) : std::stringstream(std::move(moved_from)) { + m_prefix = moved_from.m_prefix; + } + JLogMessage(const JLogger& logger, JLogger::Level level) { std::ostringstream builder; if (logger.show_timestamp) { @@ -114,6 +118,14 @@ class JLogMessage : public std::stringstream { }; +template +JLogMessage operator<<(const JLogger& logger, T&& t) { + JLogMessage message(logger, logger.level); + message << t; + return message; +} + + /// Macros #define LOG JLogMessage() @@ -131,3 +143,4 @@ class JLogMessage : public std::stringstream { #define LOG_DEBUG(logger) LOG_AT_LEVEL(logger, JLogger::Level::DEBUG) #define LOG_TRACE(logger) LOG_AT_LEVEL(logger, JLogger::Level::TRACE) + diff --git a/src/programs/unit_tests/Utils/JLoggerTests.cc b/src/programs/unit_tests/Utils/JLoggerTests.cc index 23bc3d221..f673eebba 100644 --- a/src/programs/unit_tests/Utils/JLoggerTests.cc +++ b/src/programs/unit_tests/Utils/JLoggerTests.cc @@ -64,3 +64,13 @@ TEST_CASE("JLogMessage_Newlines") { LOG_INFO(logger) << "This message has a trailing newline containing log metadata " << std::endl << LOG_END; } + +TEST_CASE("JLogMessage_StreamIntoLogger") { + JLogger logger {JLogger::Level::ERROR, &std::cout, "jana"}; + logger.ShowGroup(true); + logger << "This is a test. x = " << 22 << std::endl; + logger << "This should be the next line" << std::endl << "And another" << std::endl << "And another"; +} + + + From 1f9d73b02076e141fcaf579af140f2d8d532f27f Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 13 Dec 2024 16:36:07 -0500 Subject: [PATCH 2/6] JLogMessage uses JLogger's destination ostream --- src/libraries/JANA/JLogger.h | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/libraries/JANA/JLogger.h b/src/libraries/JANA/JLogger.h index 0fa8d6702..42b20ef95 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -61,16 +61,19 @@ inline std::ostream& operator<<(std::ostream& s, JLogger::Level l) { class JLogMessage : public std::stringstream { private: std::string m_prefix; + std::ostream* m_destination; public: - JLogMessage(const std::string& prefix="") : m_prefix(prefix){ + JLogMessage(const std::string& prefix="") : m_prefix(prefix), m_destination(&std::cout){ } JLogMessage(JLogMessage&& moved_from) : std::stringstream(std::move(moved_from)) { m_prefix = moved_from.m_prefix; + m_destination = moved_from.m_destination; } JLogMessage(const JLogger& logger, JLogger::Level level) { + m_destination = logger.destination; std::ostringstream builder; if (logger.show_timestamp) { auto now = std::chrono::system_clock::now(); @@ -112,8 +115,8 @@ class JLogMessage : public std::stringstream { while (std::getline(*this, line)) { oss << m_prefix << line << std::endl; } - std::cout << oss.str(); - std::cout.flush(); + *m_destination << oss.str(); + m_destination->flush(); } }; From 1cd40ad97544a2696f22f1b3c4565718b8620616 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Fri, 13 Dec 2024 17:43:05 -0500 Subject: [PATCH 3/6] `jout` and `jerr` use JLogger instead of JStreamLog --- src/libraries/JANA/CMakeLists.txt | 1 + .../JANA/Calibrations/JCalibration.cc | 11 ++++--- .../JANA/Calibrations/JCalibrationFile.cc | 1 - .../JANA/Calibrations/JLargeCalibration.cc | 9 +++--- .../JANA/Compatibility/JGeometryXML.h | 2 +- .../JANA/Compatibility/JStreamLog.cc | 3 -- src/libraries/JANA/Compatibility/JStreamLog.h | 5 ---- src/libraries/JANA/JLogger.cc | 7 +++++ src/libraries/JANA/JLogger.h | 15 ++++++---- .../JANA/Utils/JCallGraphRecorder.cc | 29 +++++++++---------- .../JEventProcessor_regressiontest.h | 11 ++++--- 11 files changed, 47 insertions(+), 47 deletions(-) create mode 100644 src/libraries/JANA/JLogger.cc diff --git a/src/libraries/JANA/CMakeLists.txt b/src/libraries/JANA/CMakeLists.txt index 39da10619..e5ab86d7e 100644 --- a/src/libraries/JANA/CMakeLists.txt +++ b/src/libraries/JANA/CMakeLists.txt @@ -12,6 +12,7 @@ set(JANA2_SOURCES JService.cc JVersion.cc JEvent.cc + JLogger.cc Engine/JExecutionEngine.cc diff --git a/src/libraries/JANA/Calibrations/JCalibration.cc b/src/libraries/JANA/Calibrations/JCalibration.cc index 8e9532ab8..df3cbdd52 100644 --- a/src/libraries/JANA/Calibrations/JCalibration.cc +++ b/src/libraries/JANA/Calibrations/JCalibration.cc @@ -3,19 +3,18 @@ // Subject to the terms in the LICENSE file found in the top-level directory. #include -#include #include "JCalibration.h" -#include -#include +#include #include - +#include #include #include -#include +#include +#include + using namespace std; -#include //--------------------------------- diff --git a/src/libraries/JANA/Calibrations/JCalibrationFile.cc b/src/libraries/JANA/Calibrations/JCalibrationFile.cc index 7d0363040..eb4494db7 100644 --- a/src/libraries/JANA/Calibrations/JCalibrationFile.cc +++ b/src/libraries/JANA/Calibrations/JCalibrationFile.cc @@ -3,7 +3,6 @@ // Subject to the terms in the LICENSE file found in the top-level directory. #include "JCalibrationFile.h" -#include #include #include diff --git a/src/libraries/JANA/Calibrations/JLargeCalibration.cc b/src/libraries/JANA/Calibrations/JLargeCalibration.cc index c017d0ed8..5b0e292fb 100644 --- a/src/libraries/JANA/Calibrations/JLargeCalibration.cc +++ b/src/libraries/JANA/Calibrations/JLargeCalibration.cc @@ -5,8 +5,12 @@ // Creator: davidl (on Darwin eleanor.jlab.org 12.2.0 i386) // +#include +#include +#include + +#include -#include #include #include #include @@ -20,9 +24,6 @@ using namespace std; #include #endif // HAVE_CURL -#include -#include -#include static pthread_mutex_t resource_manager_mutex = PTHREAD_MUTEX_INITIALIZER; diff --git a/src/libraries/JANA/Compatibility/JGeometryXML.h b/src/libraries/JANA/Compatibility/JGeometryXML.h index 7924c0bed..ae6ef4548 100644 --- a/src/libraries/JANA/Compatibility/JGeometryXML.h +++ b/src/libraries/JANA/Compatibility/JGeometryXML.h @@ -8,8 +8,8 @@ #pragma once #include +#include #include -#include #include #include diff --git a/src/libraries/JANA/Compatibility/JStreamLog.cc b/src/libraries/JANA/Compatibility/JStreamLog.cc index b4f2d010b..49fd6d4fb 100644 --- a/src/libraries/JANA/Compatibility/JStreamLog.cc +++ b/src/libraries/JANA/Compatibility/JStreamLog.cc @@ -5,9 +5,6 @@ #include "JStreamLog.h" -JStreamLog jout(std::cout, "JANA >>"); -JStreamLog jerr(std::cerr, "JANA ERROR>>"); - JStreamLog::JStreamLog(std::streambuf* buf, const char* tag) : std::ostream(new JStreamLogBuffer(buf, tag)), own_rdbuf(true) {} diff --git a/src/libraries/JANA/Compatibility/JStreamLog.h b/src/libraries/JANA/Compatibility/JStreamLog.h index a84be9fdf..77719c914 100644 --- a/src/libraries/JANA/Compatibility/JStreamLog.h +++ b/src/libraries/JANA/Compatibility/JStreamLog.h @@ -58,11 +58,6 @@ class JStreamLog : public std::ostream std::ostream& endMsg(std::ostream& os); -extern JStreamLog jout; -extern JStreamLog jerr; -#define jendl std::endl -#define _DBG_ std::cerr<<__FILE__<<":"<<__LINE__<<" " -#define _DBG__ std::cerr<<__FILE__<<":"<<__LINE__< + +JLogger jout {JLogger::Level::INFO, &std::cout, "jana"}; +JLogger jerr {JLogger::Level::ERROR, &std::cerr, "jana"}; + + diff --git a/src/libraries/JANA/JLogger.h b/src/libraries/JANA/JLogger.h index 42b20ef95..0c3e4e3a1 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -4,8 +4,6 @@ #pragma once -#include - #include #include #include @@ -41,9 +39,6 @@ struct JLogger { }; -static JLogger default_cout_logger = JLogger(JLogger::Level::TRACE, &std::cout, "JANA"); -static JLogger default_cerr_logger = JLogger(JLogger::Level::TRACE, &std::cerr, "JERR"); - inline std::ostream& operator<<(std::ostream& s, JLogger::Level l) { switch (l) { @@ -147,3 +142,13 @@ JLogMessage operator<<(const JLogger& logger, T&& t) { #define LOG_TRACE(logger) LOG_AT_LEVEL(logger, JLogger::Level::TRACE) +/// Backwards compatibility with JANA1 logger + +extern JLogger jout; +extern JLogger jerr; +#define jendl std::endl +#define default_cout_logger jout; +#define default_cerr_logger jerr; +#define _DBG_ jerr<<__FILE__<<":"<<__LINE__<<" " +#define _DBG__ jerr<<__FILE__<<":"<<__LINE__< -#include -#include +#include + #include +#include +#include +#include -using std::vector; -using std::string; -using std::endl; void JCallGraphRecorder::Reset() { m_call_graph.clear(); @@ -24,10 +21,10 @@ void JCallGraphRecorder::Reset() { void JCallGraphRecorder::PrintErrorCallStack() const { // Create a list of the call strings while finding the longest one - vector routines; + std::vector routines; unsigned int max_length = 0; for(unsigned int i=0; i -#include -#include -#include - #include +#include +#include +#include + class JEventProcessor_regressiontest : public JEventProcessor { @@ -42,4 +41,4 @@ class JEventProcessor_regressiontest : public JEventProcessor std::pair ParseFactorySummary(std::string line); }; -#endif // _JEventProcessor_regressiontest_ \ No newline at end of file +#endif // _JEventProcessor_regressiontest_ From 6097b1012fc97b7659a44c858aca2672487944e2 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Sat, 14 Dec 2024 01:29:01 -0500 Subject: [PATCH 4/6] Fixes for CI --- src/libraries/JANA/Compatibility/jerror.h | 3 --- src/libraries/JANA/JLogger.h | 12 ++++++++++-- src/programs/unit_tests/Utils/JLoggerTests.cc | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/libraries/JANA/Compatibility/jerror.h b/src/libraries/JANA/Compatibility/jerror.h index 9fdf13d62..12de67b20 100644 --- a/src/libraries/JANA/Compatibility/jerror.h +++ b/src/libraries/JANA/Compatibility/jerror.h @@ -8,9 +8,6 @@ #pragma once -#define _DBG_ std::cerr<<__FILE__<<":"<<__LINE__<<" " -#define _DBG__ std::cerr<<__FILE__<<":"<<__LINE__< + #include #include #include @@ -123,6 +125,12 @@ JLogMessage operator<<(const JLogger& logger, T&& t) { return message; } +inline JLogMessage operator<<(const JLogger& logger, std::ostream& (*manip)(std::ostream&)) { + JLogMessage message(logger, logger.level); + message << manip; + return message; +} + /// Macros @@ -147,8 +155,8 @@ JLogMessage operator<<(const JLogger& logger, T&& t) { extern JLogger jout; extern JLogger jerr; #define jendl std::endl -#define default_cout_logger jout; -#define default_cerr_logger jerr; +#define default_cout_logger jout +#define default_cerr_logger jerr #define _DBG_ jerr<<__FILE__<<":"<<__LINE__<<" " #define _DBG__ jerr<<__FILE__<<":"<<__LINE__< Date: Sat, 14 Dec 2024 12:09:20 -0500 Subject: [PATCH 5/6] jout/jerr also provided via JStreamLog --- src/libraries/JANA/Compatibility/JStreamLog.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/libraries/JANA/Compatibility/JStreamLog.h b/src/libraries/JANA/Compatibility/JStreamLog.h index 77719c914..249c4c807 100644 --- a/src/libraries/JANA/Compatibility/JStreamLog.h +++ b/src/libraries/JANA/Compatibility/JStreamLog.h @@ -14,6 +14,10 @@ #include #include "JStreamLogBuffer.h" +#include +// Quick-and-dirty cyclic dependency to ensure that jout/jerr are +// provided regardless of whether JLogger.h or JStreamLog.h is included + /// JStreamLog provides an interface for for writing messages /// in a way that buffers them by thread to prevent multiple /// threads from simultaneously writing to the screen (via From 50e393b7093b9f8ac3d29c3f94b002206b4cd589 Mon Sep 17 00:00:00 2001 From: Nathan Brei Date: Sat, 14 Dec 2024 22:28:40 -0500 Subject: [PATCH 6/6] Add JLogger::SetTag() temporarily --- src/libraries/JANA/JLogger.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/libraries/JANA/JLogger.h b/src/libraries/JANA/JLogger.h index 75c9e7cf1..1047af88f 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -38,6 +38,9 @@ struct JLogger { void ShowLevel(bool show) {show_level = show; } void ShowTimestamp(bool show) {show_timestamp = show; } void ShowThreadstamp(bool show) {show_threadstamp = show; } + + [[ deprecated("Use SetGroup() instead")]] + void SetTag(std::string tag) {this->group = tag; } };