Skip to content

Commit

Permalink
Merge pull request #218 from openclimatedata/rc2.0.0_logging
Browse files Browse the repository at this point in the history
Make component loggers inherit settings from global logger
  • Loading branch information
rplzzz committed Feb 8, 2018
2 parents 1c9da2e + 1766798 commit 6a9e60c
Show file tree
Hide file tree
Showing 21 changed files with 36 additions and 24 deletions.
16 changes: 14 additions & 2 deletions headers/core/logger.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ class Logger {
Logger();
~Logger();

void open( const std::string& logName, const bool echoToScreen,
LogLevel minLogLevel, const bool echoToFile = true) throw ( h_exception );
void open( const std::string& logName, bool echoToScreen,
bool echoToFile, LogLevel minLogLevel ) throw ( h_exception );

bool shouldWrite( const LogLevel writeLevel ) const;

Expand All @@ -110,6 +110,18 @@ class Logger {
void close();

static Logger& getGlobalLogger();

LogLevel getMinLogLevel() const {
return minLogLevel;
}

bool getEchoToFile() const {
return echoToFile;
}

bool isEnabled() const {
return enabled;
}
};

}
Expand Down
2 changes: 1 addition & 1 deletion source/components/bc_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ string BlackCarbonComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void BlackCarbonComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/ch4_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ string CH4Component::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void CH4Component::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/dummy_model_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ const tseries<double>& DummyModelComponent::getC() const {
//------------------------------------------------------------------------------
// documentation is inherited
void DummyModelComponent::init( Core* core ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
}

Expand Down
2 changes: 1 addition & 1 deletion source/components/forcing_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ string ForcingComponent::getComponentName() const {
// documentation is inherited
void ForcingComponent::init( Core* coreptr ) {

logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;

core = coreptr;
Expand Down
2 changes: 1 addition & 1 deletion source/components/halocarbon_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ string HalocarbonComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void HalocarbonComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
// concentration.name = myGasName;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/n2o_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ string N2OComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void N2OComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;
oldDate = core->getStartDate(); //old date will start wehere we begin.
Expand Down
2 changes: 1 addition & 1 deletion source/components/o3_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ string OzoneComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void OzoneComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/oc_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ string OrganicCarbonComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void OrganicCarbonComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/ocean_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ string OceanComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void OceanComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;

max_timestep = OCEAN_MAX_TIMESTEP;
Expand Down
2 changes: 1 addition & 1 deletion source/components/oh_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ string OHComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void OHComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/onelineocean_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ string OneLineOceanComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void OneLineOceanComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;

core = coreptr;
Expand Down
2 changes: 1 addition & 1 deletion source/components/slr_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ string slrComponent::getComponentName() const {
// documentation is inherited
void slrComponent::init( Core* coreptr ) {

logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;

core = coreptr;
Expand Down
2 changes: 1 addition & 1 deletion source/components/so2_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ string SulfurComponent::getComponentName() const {
//------------------------------------------------------------------------------
// documentation is inherited
void SulfurComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;
core = coreptr;

Expand Down
2 changes: 1 addition & 1 deletion source/components/temperature_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ void TemperatureComponent::invert_1d_2x2_matrix(double * x, double * y) {
//------------------------------------------------------------------------------
// documentation is inherited
void TemperatureComponent::init( Core* coreptr ) {
logger.open( getComponentName(), false, Logger::DEBUG );
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG( logger, Logger::DEBUG ) << "hello " << getComponentName() << std::endl;

tgaveq.set( 0.0, U_DEGC, 0.0 );
Expand Down
2 changes: 1 addition & 1 deletion source/core/carbon-cycle-solver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ CarbonCycleSolver::~CarbonCycleSolver()
//------------------------------------------------------------------------------
// documentation is inherited
void CarbonCycleSolver::init( Core* coreptr ) {
logger.open( getComponentName(), true, Logger::WARNING );
logger.open( getComponentName(), Logger::getGlobalLogger().isEnabled(), Logger::getGlobalLogger().getEchoToFile(), Logger::WARNING );
H_LOG( logger, Logger::DEBUG ) << getComponentName() << " initialized." << std::endl;

core = coreptr;
Expand Down
4 changes: 2 additions & 2 deletions source/core/logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ Logger::~Logger() {
* initialized.
*
*/
void Logger::open( const string& logName, const bool echoToScreen,
LogLevel minLogLevel, const bool echoToFile ) throw ( h_exception ) {
void Logger::open( const string& logName, bool echoToScreen,
bool echoToFile, LogLevel minLogLevel ) throw ( h_exception ) {
H_ASSERT( !isInitialized, "This log has already been initialized." );

this->minLogLevel = minLogLevel;
Expand Down
2 changes: 1 addition & 1 deletion source/main-api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ int main (int argc, char * const argv[]) {

// Create the global log
Logger& glog = Logger::getGlobalLogger();
glog.open( string( MODEL_NAME ), true, Logger::DEBUG );
glog.open( string( MODEL_NAME ), true, true, Logger::DEBUG );
H_LOG( glog, Logger::NOTICE ) << MODEL_NAME << " wrapper start" << endl;

// Parse the main configuration file
Expand Down
2 changes: 1 addition & 1 deletion source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ int main (int argc, char * const argv[]) {
try {
// Create the global log
Logger& glog = Logger::getGlobalLogger();
glog.open( string( MODEL_NAME ), true, Logger::DEBUG );
glog.open( string( MODEL_NAME ), true, true, Logger::DEBUG );
H_LOG( glog, Logger::NOTICE ) << MODEL_NAME << " wrapper start" << endl;

// Parse the main configuration file
Expand Down
2 changes: 1 addition & 1 deletion source/models/carbon-cycle-model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace Hector {
//------------------------------------------------------------------------------
// documentation is inherited
void CarbonCycleModel::init( Core* core ) {
logger.open( getComponentName(), false, Logger::DEBUG);
logger.open( getComponentName(), false, Logger::getGlobalLogger().getEchoToFile(), Logger::getGlobalLogger().getMinLogLevel() );
H_LOG(logger, Logger::DEBUG) << getComponentName() << " initialized." << std::endl;
}

Expand Down
4 changes: 2 additions & 2 deletions source/testing/test_logger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ class TestLogger : public testing::Test {
H_ASSERT( !fileExists( testFile1.c_str() ), "testfile1 exists" );
H_ASSERT( !fileExists( testFile2.c_str() ), "testfile2 exists" );

loggerNoEcho.open( testFile1, false, Logger::WARNING );
loggerNoEcho.open( testFile1, false, true, Logger::WARNING );
std::streambuf* tmpBuff = std::cout.rdbuf( &consoleTestBuff );
loggerEcho.open( testFile2, true, Logger::WARNING );
loggerEcho.open( testFile2, true, true, Logger::WARNING );

// now that the logger has attached to our test buffer put the
// original back into cout
Expand Down

0 comments on commit 6a9e60c

Please sign in to comment.