diff --git a/Applications/ApplicationsLib/LogogSetup.h b/Applications/ApplicationsLib/LogogSetup.h index 7f4bd524a420c8ffb3b10c86b3a2dd1cbd19117a..81b03d612756c7f6f5006ec5a7c4411e09ea45c6 100644 --- a/Applications/ApplicationsLib/LogogSetup.h +++ b/Applications/ApplicationsLib/LogogSetup.h @@ -12,6 +12,9 @@ #include <logog/include/logog.hpp> +#include <map> +#include <memory> + #include "BaseLib/LogogSimpleFormatter.h" namespace ApplicationsLib @@ -25,21 +28,59 @@ public: LogogSetup() { LOGOG_INITIALIZE(); - fmt = new BaseLib::LogogSimpleFormatter; - logog_cout = new logog::Cout; - logog_cout->SetFormatter(*fmt); + logog_cout = std::unique_ptr<logog::Cout>(new logog::Cout); + SetFormatter(std::unique_ptr<BaseLib::LogogSimpleFormatter> + (new BaseLib::LogogSimpleFormatter)); } ~LogogSetup() { - delete fmt; - delete logog_cout; + // Objects have to be deleted before shutdown + fmt.reset(nullptr); + logog_cout.reset(nullptr); LOGOG_SHUTDOWN(); } + void SetFormatter(std::unique_ptr<logog::Formatter>&& formatter) + { + fmt = std::move(formatter); + logog_cout->SetFormatter(*fmt); + } + + void SetLevel(LOGOG_LEVEL_TYPE level) + { + logog::SetDefaultLevel(level); + } + + void SetLevel(std::string const & level) + { + std::map<std::string, LOGOG_LEVEL_TYPE> foo = + { + { "none", LOGOG_LEVEL_NONE }, + { "emergency", LOGOG_LEVEL_EMERGENCY }, + { "alert", LOGOG_LEVEL_ALERT}, + { "critical", LOGOG_LEVEL_CRITICAL }, + { "error", LOGOG_LEVEL_ERROR }, + { "warn", LOGOG_LEVEL_WARN }, + { "info", LOGOG_LEVEL_INFO }, + { "debug", LOGOG_LEVEL_DEBUG }, + { "all", LOGOG_LEVEL_ALL } + }; + + + //LOGOG_LEVEL_TYPE level_type; + if(foo.find(level) != foo.end()) + SetLevel(foo[level]); + else + { + ERR("%s is not a valid log level! Aborting.", level.c_str()); + std::abort(); + } + } + private: - BaseLib::LogogSimpleFormatter* fmt; - logog::Cout* logog_cout; + std::unique_ptr<logog::Formatter> fmt; + std::unique_ptr<logog::Cout> logog_cout; }; } // ApplicationsLib diff --git a/Applications/CLI/ogs.cpp b/Applications/CLI/ogs.cpp index 0551cc5e432ec833b84803838f41dfc6842cd3f9..6ef21c793ec25e9a4238973d7b5fc95a07032b63 100644 --- a/Applications/CLI/ogs.cpp +++ b/Applications/CLI/ogs.cpp @@ -79,7 +79,8 @@ int main(int argc, char *argv[]) "(http://www.opengeosys.org) " "Distributed under a Modified BSD License. " "See accompanying file LICENSE.txt or " - "http://www.opengeosys.org/project/license", + "http://www.opengeosys.org/project/license\n" + "version: " + BaseLib::BuildInfo::git_describe, ' ', BaseLib::BuildInfo::git_describe); @@ -99,6 +100,14 @@ int main(int argc, char *argv[]) "output directory"); cmd.add(outdir_arg); + TCLAP::ValueArg<std::string> log_level_arg( + "l", "log-level", + "the verbosity of logging messages: none, error, warn, info, debug, all", + false, + "all", + "log level"); + cmd.add(log_level_arg); + TCLAP::SwitchArg nonfatal_arg("", "config-warnings-nonfatal", "warnings from parsing the configuration file will not trigger program abortion"); @@ -107,6 +116,7 @@ int main(int argc, char *argv[]) cmd.parse(argc, argv); ApplicationsLib::LogogSetup logog_setup; + logog_setup.SetLevel(log_level_arg.getValue()); ApplicationsLib::LinearSolverLibrarySetup linear_solver_library_setup( argc, argv); diff --git a/CHANGELOG.md b/CHANGELOG.md index 45b38e1afa99ebe7b1161b3a5b61800371cae120..0ff63fbeaecad128e5f2d8b7f7e84e595acf5948 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ - New configuration tree parser - Checks configuration parameters more strictly, automatically prints error/warning messages. - Requires Boost >= 1.56 because of boost::optional with move semantics. + - Command line argument `-l` for OGS cli and testrunner to specify verbosity of logging, #1056 ### Infrastructure diff --git a/Tests/CMakeLists.txt b/Tests/CMakeLists.txt index 0c856095e3aae247fd60e5378dd930689a7cbbac..88daaa438204030120d637a6c56637b2dabc4712 100644 --- a/Tests/CMakeLists.txt +++ b/Tests/CMakeLists.txt @@ -73,6 +73,7 @@ if(DEFINED ENV{CI}) --gtest_shuffle --gtest_repeat=3) endif() set(TESTRUNNER_ADDITIONAL_ARGUMENTS ${TESTRUNNER_ADDITIONAL_ARGUMENTS} + -l warn --gtest_output=xml:./testrunner.xml) add_custom_target(tests-cleanup ${CMAKE_COMMAND} -E remove testrunner.xml) diff --git a/Tests/testrunner.cpp b/Tests/testrunner.cpp index 2c233a8b4cb449f10b0ba66d3472eb182df1c991..3c958d5cd5cc80c684b85ff9128f88dffc249443 100644 --- a/Tests/testrunner.cpp +++ b/Tests/testrunner.cpp @@ -16,7 +16,6 @@ #include <clocale> #include "gtest/gtest.h" -#include "logog/include/logog.hpp" #ifdef USE_MPI #include <mpi.h> @@ -30,7 +29,8 @@ #include <petscksp.h> #endif -#include "BaseLib/LogogCustomCout.h" +#include "Applications/ApplicationsLib/LogogSetup.h" +#include "BaseLib/BuildInfo.h" #include "BaseLib/TemplateLogogFormatterSuppressedGCC.h" #ifdef OGS_BUILD_GUI #include <QApplication> @@ -39,62 +39,70 @@ /// Implementation of the googletest testrunner int main(int argc, char* argv[]) { + std::string logLevel("all"); + for (int i = 1; i < argc; i++) + { + if(i + 1 == argc) + break; + if(std::strcmp(argv[i], "-l") == 0) + logLevel = argv[i + 1]; + } + setlocale(LC_ALL, "C"); #ifdef OGS_BUILD_GUI QApplication app(argc, argv, false); #endif int ret = 0; - LOGOG_INITIALIZE(); - { #ifdef USE_MPI - MPI_Init(&argc, &argv); + MPI_Init(&argc, &argv); #endif - BaseLib::LogogCustomCout out; - BaseLib::TemplateLogogFormatterSuppressedGCC<TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG> custom_format; - out.SetFormatter(custom_format); + ApplicationsLib::LogogSetup logog_setup; + logog_setup.SetFormatter(std::unique_ptr<BaseLib::TemplateLogogFormatterSuppressedGCC + <TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG> > + (new BaseLib::TemplateLogogFormatterSuppressedGCC + <TOPIC_LEVEL_FLAG | TOPIC_FILE_NAME_FLAG | TOPIC_LINE_NUMBER_FLAG>())); + logog_setup.SetLevel(logLevel); + #ifdef USE_PETSC - char help[] = "ogs6 with PETSc \n"; - PetscInitialize(&argc,&argv,(char *)0,help); + char help[] = "ogs6 with PETSc \n"; + PetscInitialize(&argc,&argv,(char *)0,help); #endif - try - { - // initialize libraries which will be used while testing + try + { + // initialize libraries which will be used while testing #ifdef USE_LIS - lis_initialize(&argc, &argv); + lis_initialize(&argc, &argv); #endif - // start google test - testing::InitGoogleTest ( &argc, argv ); - ret = RUN_ALL_TESTS(); - } - catch (char* e) - { - ERR(e); - } - catch (std::exception& e) - { - ERR(e.what()); - } - catch (...) - { - ERR("Unknown exception occurred!"); - } - // finalize libraries + // start google test + testing::InitGoogleTest ( &argc, argv ); + ret = RUN_ALL_TESTS(); + } + catch (char* e) + { + ERR(e); + } + catch (std::exception& e) + { + ERR(e.what()); + } + catch (...) + { + ERR("Unknown exception occurred!"); + } + // finalize libraries #ifdef USE_LIS - lis_finalize(); + lis_finalize(); #endif #ifdef USE_PETSC - PetscFinalize(); + PetscFinalize(); #endif #ifdef USE_MPI - MPI_Finalize(); + MPI_Finalize(); #endif - } // make sure no logog objects exist when LOGOG_SHUTDOWN() is called. - LOGOG_SHUTDOWN(); - return ret; }