From cf5441c169801523ba229c11f4146912a0256f04 Mon Sep 17 00:00:00 2001 From: Janosch Machowinski Date: Wed, 25 Oct 2023 11:05:47 +0200 Subject: [PATCH] fix: Don't recursively call init if log directories can not be created. If there is no permission to create the log directories, the FileLogger will call itself recutsive, as is used ignerr for logging the error, which will call FileLogger::Init again. This commit fixed this, by using std::cerr for the log output in FileLogger::Init. Signed-off-by: Janosch Machowinski --- include/gz/common/Filesystem.hh | 8 +++++++- src/Console.cc | 11 ++++++++--- src/Filesystem.cc | 10 ++++++++-- 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/include/gz/common/Filesystem.hh b/include/gz/common/Filesystem.hh index d1c2ea326..8541d80c5 100644 --- a/include/gz/common/Filesystem.hh +++ b/include/gz/common/Filesystem.hh @@ -63,7 +63,13 @@ namespace ignition /// \return True if directory creation was successful, false otherwise. bool IGNITION_COMMON_VISIBLE createDirectory(const std::string &_path); - /// \brief Create directories for the given path + /// \brief Create directories for the given path errors are printed to the given stream + /// \param[in] _path Path to create directories from + /// \param[in] _errorOut Stream for error output + /// \return true on success + bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path, std::ostream &_errorOut); + + /// \brief Create directories for the given path errors are printed on ignerr /// \param[in] _path Path to create directories from /// \return true on success bool IGNITION_COMMON_VISIBLE createDirectories(const std::string &_path); diff --git a/src/Console.cc b/src/Console.cc index d6604448e..1ed461273 100644 --- a/src/Console.cc +++ b/src/Console.cc @@ -261,8 +261,8 @@ void FileLogger::Init(const std::string &_directory, { if (!env(IGN_HOMEDIR, logPath)) { - ignerr << "Missing HOME environment variable." - << "No log file will be generated."; + std::cerr << "Missing HOME environment variable." + << "No log file will be generated."; return; } logPath = joinPaths(logPath, _directory); @@ -275,7 +275,12 @@ void FileLogger::Init(const std::string &_directory, auto* buf = dynamic_cast(this->rdbuf()); // Create the directory if it doesn't exist. - createDirectories(logPath); + if(!createDirectories(logPath, std::cerr)) + { + std::cerr << "Failed to generate log directories." + << "No log file will be generated."; + return; + } logPath = joinPaths(logPath, _filename); diff --git a/src/Filesystem.cc b/src/Filesystem.cc index b363ad91e..6cf911b7b 100644 --- a/src/Filesystem.cc +++ b/src/Filesystem.cc @@ -465,6 +465,12 @@ bool common::copyDirectory(const std::string &_existingDirname, ///////////////////////////////////////////////// bool common::createDirectories(const std::string &_path) +{ + return createDirectories(_path, ignerr); +} + +///////////////////////////////////////////////// +bool common::createDirectories(const std::string &_path, std::ostream &_errorOut) { size_t index = 0; while (index < _path.size()) @@ -482,8 +488,8 @@ bool common::createDirectories(const std::string &_path) if (mkdir(dir.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0) { #endif - ignerr << "Failed to create directory [" + dir + "]: " - << std::strerror(errno) << std::endl; + _errorOut << "Failed to create directory [" + dir + "]: " + << std::strerror(errno) << std::endl; return false; } }