From 5b97e546719973be9717a87d7c038eda7465d925 Mon Sep 17 00:00:00 2001 From: Alankar Dutta Date: Fri, 27 Sep 2024 13:23:54 +0200 Subject: [PATCH 1/5] [ENH] customize log_dir location from runtime ini --- src/global.cpp | 36 +++++++++++++++++++++++++++++++++--- src/global.hpp | 1 + src/input.cpp | 19 +++++++++++++------ src/input.hpp | 3 +++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index 1e9c11eef..25098f3a5 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -7,6 +7,16 @@ #include #include +#if __has_include() + #include + namespace fs = std::filesystem; +#elif __has_include() + #include + namespace fs = std::experimental::filesystem; +#else + #error "Missing the header." +#endif + #include "idefix.hpp" #include "global.hpp" #include "profiler.hpp" @@ -26,6 +36,7 @@ bool warningsAreErrors{false}; IdefixOutStream cout; IdefixErrStream cerr; +std::string logFileDir; Profiler prof; LoopPattern defaultLoopPattern; @@ -103,11 +114,30 @@ void IdefixOutStream::init(int rank) { // disable the log file void IdefixOutStream::enableLogFile() { std::stringstream sslogFileName; - sslogFileName << "idefix." << idfx::prank << ".log"; - + + sslogFileName << idfx::logFileDir << "/./" << "idefix." << idfx::prank << ".log"; std::string logFileName(sslogFileName.str()); + + if(idfx::prank==0) { + if(!fs::is_directory(logFileDir)) { + try { + if(!fs::create_directories(logFileDir)) { + std::stringstream msg; + msg << "Cannot create directory " << logFileDir << std::endl; + IDEFIX_ERROR(msg); + } + } catch(std::exception &e) { + std::stringstream msg; + msg << "Cannot create directory " << logFileDir << std::endl; + msg << e.what(); + IDEFIX_ERROR(msg); + } + } + } +#ifdef WITH_MPI + MPI_Barrier(MPI_COMM_WORLD); +#endif this->my_fstream.open(logFileName.c_str()); - this->logFileEnabled = true; } diff --git a/src/global.hpp b/src/global.hpp index 47fca42ee..78747f57e 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -21,6 +21,7 @@ class Profiler; extern int prank; //< parallel rank extern int psize; +extern std::string logFileDir; //< logfileDir extern IdefixOutStream cout; //< custom cout for idefix extern IdefixErrStream cerr; //< custom cerr for idefix extern Profiler prof; //< profiler (for memory & performance usage) diff --git a/src/input.cpp b/src/input.cpp index 336414953..d5c6d9bff 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -43,6 +43,8 @@ Input::Input(int argc, char* argv[] ) { bool haveBlock = false; std::stringstream msg; int nParameters = 0; // # of parameters in current block + // Log files are enabled by default + this->enableLogs = true; // Tell the system we want to catch the SIGUSR2 signals signal(SIGUSR2, signalHandler); @@ -108,12 +110,20 @@ Input::Input(int argc, char* argv[] ) { } } file.close(); + + if(this->enableLogs) { + if(CheckEntry("Output","log_dir")>=0) { + idfx::logFileDir = Get("Output", "log_dir", 0); + } else { + idfx::logFileDir = "./"; + } + idfx::cout.enableLogFile(); + } } // This routine parse command line options void Input::ParseCommandLine(int argc, char **argv) { std::stringstream msg; - bool enableLogs = true; for(int i = 1 ; i < argc ; i++) { // MPI decomposition argument if(std::string(argv[i]) == "-dec") { @@ -171,9 +181,9 @@ void Input::ParseCommandLine(int argc, char **argv) { this->forceInitRequested = true; } else if(std::string(argv[i]) == "-nowrite") { this->forceNoWrite = true; - enableLogs = false; + this->enableLogs = false; } else if(std::string(argv[i]) == "-nolog") { - enableLogs = false; + this->enableLogs = false; } else if(std::string(argv[i]) == "-profile") { idfx::prof.EnablePerformanceProfiling(); } else if(std::string(argv[i]) == "-Werror") { @@ -190,9 +200,6 @@ void Input::ParseCommandLine(int argc, char **argv) { IDEFIX_ERROR(msg); } } - if(enableLogs) { - idfx::cout.enableLogFile(); - } } diff --git a/src/input.hpp b/src/input.hpp index 18fcf2601..947b65625 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -24,6 +24,9 @@ class Input { // Constructor from a file Input (int, char ** ); void ShowConfig(); + + // flag if logging is to be done + bool enableLogs; // Accessor to input parameters // the parameters are always: BlockName, EntryName, ParameterNumber (starting from 0) From 73fc68b553d8aecfcfe9ebe775f32cf3af56eba1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci-lite[bot]" <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:28:40 +0000 Subject: [PATCH 2/5] [pre-commit.ci lite] apply automatic fixes --- src/global.cpp | 4 ++-- src/input.cpp | 2 +- src/input.hpp | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index 25098f3a5..26dfcc096 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -114,10 +114,10 @@ void IdefixOutStream::init(int rank) { // disable the log file void IdefixOutStream::enableLogFile() { std::stringstream sslogFileName; - + sslogFileName << idfx::logFileDir << "/./" << "idefix." << idfx::prank << ".log"; std::string logFileName(sslogFileName.str()); - + if(idfx::prank==0) { if(!fs::is_directory(logFileDir)) { try { diff --git a/src/input.cpp b/src/input.cpp index d5c6d9bff..2e628f855 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -110,7 +110,7 @@ Input::Input(int argc, char* argv[] ) { } } file.close(); - + if(this->enableLogs) { if(CheckEntry("Output","log_dir")>=0) { idfx::logFileDir = Get("Output", "log_dir", 0); diff --git a/src/input.hpp b/src/input.hpp index 947b65625..727920597 100644 --- a/src/input.hpp +++ b/src/input.hpp @@ -24,7 +24,7 @@ class Input { // Constructor from a file Input (int, char ** ); void ShowConfig(); - + // flag if logging is to be done bool enableLogs; From f2987b72b766a6325318d65b1b4325279fea5571 Mon Sep 17 00:00:00 2001 From: Alankar Dutta Date: Mon, 20 Jul 2026 09:29:50 +0200 Subject: [PATCH 3/5] Update src/global.cpp Co-authored-by: Geoffroy Lesur --- src/global.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/global.cpp b/src/global.cpp index b9a160420..3cf13bee4 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -119,7 +119,7 @@ void IdefixOutStream::init(int rank) { void IdefixOutStream::enableLogFile() { std::stringstream sslogFileName; - sslogFileName << idfx::logFileDir << "/./" << "idefix." << idfx::prank << ".log"; + sslogFileName << idfx::logFileDir << "/" << "idefix." << idfx::prank << ".log"; std::string logFileName(sslogFileName.str()); if(idfx::prank==0) { From 237089a69960ab9ffb08d2096fb5eb56b23ddfa6 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 31 Aug 2026 12:29:47 +0200 Subject: [PATCH 4/5] add documentation for log file directory --- doc/source/reference/idefix.ini.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/source/reference/idefix.ini.rst b/doc/source/reference/idefix.ini.rst index 8b558722f..a6bc52bdb 100644 --- a/doc/source/reference/idefix.ini.rst +++ b/doc/source/reference/idefix.ini.rst @@ -411,6 +411,9 @@ This section describes the outputs *Idefix* produces. For more details about eac +================+=========================+==================================================================================================+ | log | integer | | Time interval between log outputs, in code steps (default 100). | +----------------+-------------------------+--------------------------------------------------------------------------------------------------+ +| log_dir | string | | directory for log file outputs. Default to "./" | +| | | | The directory is automatically created if it does not exist. | ++----------------+-------------------------+--------------------------------------------------------------------------------------------------+ | dmp | float, float+char | | 1st parameter: Code time interval between dump outputs, in code units. | | | | | If negative, the first parameter is ignored. | | | | | 2nd parameter (optional): Wallclock time interval between two dumps. The ending character | From 5771a1284756fcc7a41bd3428ce4466543e17e70 Mon Sep 17 00:00:00 2001 From: Geoffroy Lesur Date: Mon, 31 Aug 2026 14:28:19 +0200 Subject: [PATCH 5/5] - make linter happy - do not store logdir as a global variable --- src/global.cpp | 8 +++----- src/global.hpp | 2 +- src/input.cpp | 11 +++++------ 3 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/global.cpp b/src/global.cpp index 3cf13bee4..ba94b4cb9 100644 --- a/src/global.cpp +++ b/src/global.cpp @@ -9,7 +9,7 @@ #include #include #if __has_include() - #include + #include // NOLINT [build/c++17] namespace fs = std::filesystem; #elif __has_include() #include @@ -38,7 +38,6 @@ bool warningsAreErrors{false}; IdefixOutStream cout; IdefixErrStream cerr; -std::string logFileDir; Profiler prof; LoopPattern defaultLoopPattern; Units units; @@ -116,10 +115,9 @@ void IdefixOutStream::init(int rank) { // disable the log file -void IdefixOutStream::enableLogFile() { +void IdefixOutStream::enableLogFile(std::string logFileDir) { std::stringstream sslogFileName; - - sslogFileName << idfx::logFileDir << "/" << "idefix." << idfx::prank << ".log"; + sslogFileName << logFileDir << "/" << "idefix." << idfx::prank << ".log"; std::string logFileName(sslogFileName.str()); if(idfx::prank==0) { diff --git a/src/global.hpp b/src/global.hpp index 06663f001..55c16c84f 100644 --- a/src/global.hpp +++ b/src/global.hpp @@ -67,7 +67,7 @@ void DumpArray(std::string filename, ArrayType array) { class idfx::IdefixOutStream { public: void init(int); - void enableLogFile(); + void enableLogFile(std::string logFileDir); // for regular output of variables and stuff template IdefixOutStream& operator<<(const T& something) { if(toscreen) std::cout << something; diff --git a/src/input.cpp b/src/input.cpp index 942167a7e..8bc17174e 100644 --- a/src/input.cpp +++ b/src/input.cpp @@ -113,13 +113,12 @@ Input::Input(int argc, char* argv[] ) { file.close(); if(this->enableLogs) { - if(CheckEntry("Output","log_dir")>=0) { - idfx::logFileDir = Get("Output", "log_dir", 0); - } else { - idfx::logFileDir = "./"; - } - idfx::cout.enableLogFile(); + std::string logFileDir{"./"}; + if(CheckEntry("Output","log_dir")>=0) { + logFileDir = Get("Output", "log_dir", 0); } + idfx::cout.enableLogFile(logFileDir); + } } // This routine parse command line options