Skip to content

Commit

Permalink
Merge pull request #395 from JeffersonLab/nbrei_logger
Browse files Browse the repository at this point in the history
Stop using JStreamLog
  • Loading branch information
nathanwbrei authored Dec 15, 2024
2 parents 9c88e63 + 50e393b commit a41c730
Show file tree
Hide file tree
Showing 13 changed files with 90 additions and 51 deletions.
1 change: 1 addition & 0 deletions src/libraries/JANA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ set(JANA2_SOURCES
JService.cc
JVersion.cc
JEvent.cc
JLogger.cc

Engine/JExecutionEngine.cc

Expand Down
11 changes: 5 additions & 6 deletions src/libraries/JANA/Calibrations/JCalibration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,18 @@
// Subject to the terms in the LICENSE file found in the top-level directory.

#include <JANA/JLogger.h>
#include <JANA/Compatibility/JStreamLog.h>
#include "JCalibration.h"

#include <unistd.h>
#include <errno.h>
#include <cstring>
#include <dirent.h>

#include <fstream>
#include <iostream>
#include <string>
#include <fstream>
#include <sys/stat.h>
#include <unistd.h>

using namespace std;

#include <sys/stat.h>


//---------------------------------
Expand Down
1 change: 0 additions & 1 deletion src/libraries/JANA/Calibrations/JCalibrationFile.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
// Subject to the terms in the LICENSE file found in the top-level directory.

#include "JCalibrationFile.h"
#include <JANA/Compatibility/JStreamLog.h>
#include <JANA/JLogger.h>

#include <dirent.h>
Expand Down
9 changes: 5 additions & 4 deletions src/libraries/JANA/Calibrations/JLargeCalibration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@
// Creator: davidl (on Darwin eleanor.jlab.org 12.2.0 i386)
//

#include <JANA/JLogger.h>
#include <JANA/Services/JParameterManager.h>
#include <JANA/Calibrations/JLargeCalibration.h>

#include <md5.h>

#include <JANA/Compatibility/JStreamLog.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/stat.h>
Expand All @@ -20,9 +24,6 @@ using namespace std;
#include <curl/curl.h>
#endif // HAVE_CURL

#include <JANA/Services/JParameterManager.h>
#include <JANA/Calibrations/JLargeCalibration.h>
#include <md5.h>


static pthread_mutex_t resource_manager_mutex = PTHREAD_MUTEX_INITIALIZER;
Expand Down
2 changes: 1 addition & 1 deletion src/libraries/JANA/Compatibility/JGeometryXML.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
#pragma once
#include <iostream>

#include <JANA/JLogger.h>
#include <JANA/Compatibility/JGeometry.h>
#include <JANA/Compatibility/JStreamLog.h>
#include <JANA/Calibrations/JCalibration.h>
#include <JANA/JVersion.h>

Expand Down
3 changes: 0 additions & 3 deletions src/libraries/JANA/Compatibility/JStreamLog.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{}

Expand Down
9 changes: 4 additions & 5 deletions src/libraries/JANA/Compatibility/JStreamLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
#include <string>
#include "JStreamLogBuffer.h"

#include <JANA/JLogger.h>
// 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
Expand Down Expand Up @@ -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__<<std::endl


3 changes: 0 additions & 3 deletions src/libraries/JANA/Compatibility/jerror.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@

#pragma once

#define _DBG_ std::cerr<<__FILE__<<":"<<__LINE__<<" "
#define _DBG__ std::cerr<<__FILE__<<":"<<__LINE__<<std::endl

enum jerror_t{
NOERROR = 0,
UNKNOWN_ERROR = -1000,
Expand Down
7 changes: 7 additions & 0 deletions src/libraries/JANA/JLogger.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#include <JANA/JLogger.h>

JLogger jout {JLogger::Level::INFO, &std::cout, "jana"};
JLogger jerr {JLogger::Level::ERROR, &std::cerr, "jana"};


44 changes: 38 additions & 6 deletions src/libraries/JANA/JLogger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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();
Expand Down Expand Up @@ -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 <typename T>
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()
Expand All @@ -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__<<std::endl

29 changes: 13 additions & 16 deletions src/libraries/JANA/Utils/JCallGraphRecorder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,15 @@
// Created by Nathan Brei on 10/25/21.
//



#include "JCallGraphRecorder.h"

#include <sstream>
#include <JANA/Compatibility/JStreamLog.h>
#include <queue>
#include <JANA/JLogger.h>

#include <algorithm>
#include <map>
#include <queue>
#include <sstream>

using std::vector;
using std::string;
using std::endl;

void JCallGraphRecorder::Reset() {
m_call_graph.clear();
Expand All @@ -24,10 +21,10 @@ void JCallGraphRecorder::Reset() {
void JCallGraphRecorder::PrintErrorCallStack() const {

// Create a list of the call strings while finding the longest one
vector<string> routines;
std::vector<std::string> routines;
unsigned int max_length = 0;
for(unsigned int i=0; i<m_error_call_stack.size(); i++){
string routine = m_error_call_stack[i].factory_name;
std::string routine = m_error_call_stack[i].factory_name;
if(m_error_call_stack[i].tag.length()){
routine = routine + ":" + m_error_call_stack[i].tag;
}
Expand All @@ -36,17 +33,17 @@ void JCallGraphRecorder::PrintErrorCallStack() const {
}

std::stringstream sstr;
sstr<<" Factory Call Stack"<<endl;
sstr<<"============================"<<endl;
sstr<<" Factory Call Stack"<<std::endl;
sstr<<"============================"<<std::endl;
for(unsigned int i=0; i<m_error_call_stack.size(); i++){
string routine = routines[i];
sstr<<" "<<routine<<string(max_length+2 - routine.size(),' ');
std::string routine = routines[i];
sstr<<" "<<routine<<std::string(max_length+2 - routine.size(),' ');
if(m_error_call_stack[i].filename){
sstr<<"-- "<<" line:"<<m_error_call_stack[i].line<<" "<<m_error_call_stack[i].filename;
}
sstr<<endl;
sstr<<std::endl;
}
sstr<<"----------------------------"<<endl;
sstr<<"----------------------------"<<std::endl;

jout<<sstr.str();

Expand Down
11 changes: 5 additions & 6 deletions src/plugins/regressiontest/JEventProcessor_regressiontest.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@
#ifndef _JEventProcessor_regressiontest_
#define _JEventProcessor_regressiontest_

#include <string>
#include <map>
#include <ostream>
#include <istream>

#include <JANA/JEventProcessor.h>

#include <fstream>
#include <map>
#include <string>


class JEventProcessor_regressiontest : public JEventProcessor
{
Expand Down Expand Up @@ -42,4 +41,4 @@ class JEventProcessor_regressiontest : public JEventProcessor
std::pair<std::string, int> ParseFactorySummary(std::string line);
};

#endif // _JEventProcessor_regressiontest_
#endif // _JEventProcessor_regressiontest_
11 changes: 11 additions & 0 deletions src/programs/unit_tests/Utils/JLoggerTests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}



0 comments on commit a41c730

Please sign in to comment.