Commit 04b3ad7f authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook GitHub Bot

make it possible to use folly logging with exceptions disabled

Summary:
This updates the logging code so that it is possible to include `folly/xlog.h`
and write log statements in code that is compiled with `-fno-exceptions`

The code for parsing log configuration strings does still use exceptions in
some of its config parsing logic.  It shouldn't be too difficult to disable
that in a subsequent diff, but for now this change at least makes it possible
for calling code to write log messages.

Reviewed By: yfeldblum

Differential Revision: D21630914

fbshipit-source-id: ce9ed422f40066e57433a85182e1a8fc6d8628db
parent cf7284d0
......@@ -20,6 +20,7 @@
#include <folly/CPortability.h>
#include <folly/Conv.h>
#include <folly/Portability.h>
#include <folly/lang/Exception.h>
#include <folly/logging/LogCategory.h>
#include <folly/logging/LogMessage.h>
#include <folly/logging/LogStream.h>
......@@ -321,35 +322,37 @@ class LogStreamProcessor {
*/
template <typename... Args>
FOLLY_NOINLINE std::string createLogString(Args&&... args) noexcept {
try {
return folly::to<std::string>(std::forward<Args>(args)...);
} catch (const std::exception& ex) {
// This most likely means there was some error converting the arguments
// to strings. Handle the exception here, rather than letting it
// propagate up, since callers generally do not expect log statements to
// throw.
return folly::catch_exception<const std::exception&>(
[&] { return folly::to<std::string>(std::forward<Args>(args)...); },
[&](const std::exception& ex) {
// This most likely means there was some error converting the
// arguments to strings. Handle the exception here, rather than
// letting it propagate up, since callers generally do not expect log
// statements to throw.
//
// Just log an error message letting indicating that something went wrong
// formatting the log message.
// Just log an error message letting indicating that something went
// wrong formatting the log message.
return folly::to<std::string>(
"error constructing log message: ", ex.what());
}
});
}
FOLLY_NOINLINE std::string vformatLogString(
folly::StringPiece fmt,
fmt::format_args args,
bool& failed) noexcept {
try {
return folly::catch_exception<const std::exception&>(
[&] {
return fmt::vformat(fmt::string_view(fmt.data(), fmt.size()), args);
} catch (const std::exception& ex) {
},
[&](const std::exception& ex) {
// This most likely means that the caller had a bug in their format
// string/arguments. Handle the exception here, rather than letting it
// propagate up, since callers generally do not expect log statements to
// throw.
// string/arguments. Handle the exception here, rather than letting
// it propagate up, since callers generally do not expect log
// statements to throw.
//
// Log the format string and as much of the arguments as we can convert,
// to aid debugging.
// Log the format string and as much of the arguments as we can
// convert, to aid debugging.
failed = true;
std::string result;
result.append("error formatting log message: ");
......@@ -358,7 +361,7 @@ class LogStreamProcessor {
result.append(fmt.data(), fmt.size());
result.append("\", arguments: ");
return result;
}
});
}
/**
......
......@@ -18,6 +18,7 @@
#include <folly/Conv.h>
#include <folly/Portability.h>
#include <folly/lang/Exception.h>
#include <folly/lang/TypeInfo.h>
/*
......@@ -91,12 +92,13 @@ template <typename Arg>
auto appendObjectToString(std::string& str, const Arg* arg, int) -> decltype(
toAppend(std::declval<Arg>(), std::declval<std::string*>()),
std::declval<void>()) {
try {
toAppend(*arg, &str);
} catch (const std::exception&) {
// If anything goes wrong in `toAppend()` fall back to appendRawObjectInfo()
::folly::catch_exception<const std::exception&>(
[&] { toAppend(*arg, &str); },
[&](const std::exception&) {
// If anything goes wrong in `toAppend()` fall back to
// appendRawObjectInfo()
::folly::logging::appendRawObjectInfo(str, arg);
}
});
}
template <typename Arg>
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment