Skip to content

Commit

Permalink
Handle failures when setting logging to file
Browse files Browse the repository at this point in the history
  • Loading branch information
marta-lokhova committed Feb 16, 2021
1 parent 24afe7d commit 8e400a9
Showing 1 changed file with 30 additions and 3 deletions.
33 changes: 30 additions & 3 deletions src/util/Logging.cpp
Expand Up @@ -134,10 +134,25 @@ Logging::init(bool truncate)
// and always pass 'truncate=false' to spdlog, so it always opens in
// "ab" == O_APPEND mode. The "portable" way to truncate is to open
// an ofstream in out|trunc mode, then immediately close it.
std::ofstream out;
if (truncate)
{
std::ofstream truncator(filename, std::ios_base::out |
std::ios_base::trunc);
out.open(filename, std::ios_base::out | std::ios_base::trunc);
}
else
{
out.open(filename, std::ios_base::out | std::ios_base::app);
}

if (out.fail())
{
throw std::runtime_error(fmt::format(
"Could not open log file {}, check access rights",
filename));
}
else
{
out.close();
}

sinks.emplace_back(
Expand Down Expand Up @@ -209,7 +224,19 @@ Logging::setLoggingToFile(std::string const& filename)
std::lock_guard<std::recursive_mutex> guard(mLogMutex);
mLastFilenamePattern = filename;
deinit();
init();
try
{
init();
}
catch (std::runtime_error& e)
{
// Could not initialize logging to file, fallback on
// console-only logging and throw
mLastFilenamePattern.clear();
deinit();
init();
throw;
}
#endif
}

Expand Down

5 comments on commit 8e400a9

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from MonsieurNicolas
at marta-lokhova@8e400a9

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging marta-lokhova/stellar-core/spdlog_logging_fix = 8e400a9 into auto

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

marta-lokhova/stellar-core/spdlog_logging_fix = 8e400a9 merged ok, testing candidate = 6b99ef8

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@latobarita
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 6b99ef8

Please sign in to comment.