Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ENH] Customization feature to choose a directory for dumping of log files during runtime #270

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/global.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@

#include <string>
#include <sstream>
#if __has_include(<filesystem>)
#include <filesystem>
namespace fs = std::filesystem;
#elif __has_include(<experimental/filesystem>)
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
#else
#error "Missing the <filesystem> header."
#endif

#include "idefix.hpp"
#include "global.hpp"
#include "profiler.hpp"
Expand All @@ -26,6 +36,7 @@ bool warningsAreErrors{false};

IdefixOutStream cout;
IdefixErrStream cerr;
std::string logFileDir;
Profiler prof;
LoopPattern defaultLoopPattern;

Expand Down Expand Up @@ -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());
this->my_fstream.open(logFileName.c_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;
}

Expand Down
1 change: 1 addition & 0 deletions src/global.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 13 additions & 6 deletions src/input.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -108,12 +110,20 @@ Input::Input(int argc, char* argv[] ) {
}
}
file.close();

if(this->enableLogs) {
if(CheckEntry("Output","log_dir")>=0) {
idfx::logFileDir = Get<std::string>("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") {
Expand Down Expand Up @@ -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") {
Expand All @@ -190,9 +200,6 @@ void Input::ParseCommandLine(int argc, char **argv) {
IDEFIX_ERROR(msg);
}
}
if(enableLogs) {
idfx::cout.enableLogFile();
}
}


Expand Down
3 changes: 3 additions & 0 deletions src/input.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ class Input {
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)
// These specialised functions are deprecated. Use the template Get<T> function.
Expand Down