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