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..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 @@ -58,11 +62,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 ebbc2138c..1047af88f 100644 --- a/src/libraries/JANA/JLogger.h +++ b/src/libraries/JANA/JLogger.h @@ -38,11 +38,11 @@ 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; } +}; -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) { @@ -61,12 +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(); @@ -108,12 +115,26 @@ 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(); } }; +template +JLogMessage operator<<(const JLogger& logger, T&& t) { + JLogMessage message(logger, logger.level); + message << t; + return message; +} + +inline JLogMessage operator<<(const JLogger& logger, std::ostream& (*manip)(std::ostream&)) { + JLogMessage message(logger, logger.level); + message << manip; + return message; +} + + /// Macros #define LOG JLogMessage() @@ -131,3 +152,14 @@ 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) + +/// 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_ diff --git a/src/programs/unit_tests/Utils/JLoggerTests.cc b/src/programs/unit_tests/Utils/JLoggerTests.cc index 23bc3d221..50ad3daee 100644 --- a/src/programs/unit_tests/Utils/JLoggerTests.cc +++ b/src/programs/unit_tests/Utils/JLoggerTests.cc @@ -64,3 +64,14 @@ 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; + logger << std::endl << "There should be a blank line above this"; +} + + +