Commit 48407345 authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot

add a `FOLLY_INIT_LOGGING_CONFIG()` helper macro

Summary:
Add a macro to help users define the `getBaseLoggingConfig()` function.

While I would prefer to avoid macros if possible, this seems worthwhile.  This
saves 4 or 5 lines of boilerplate code in each program that sets a custom base
logger setting.  It also reduces the likelihood of a developer accidentally
having a typo in the function name, which would still build successfully but
not have the desired results.

Reviewed By: chadaustin

Differential Revision: D7457652

fbshipit-source-id: 1c316c7ea6949c16bd7b61c0440cc1ee69ecb83e
parent e374ef6a
...@@ -77,3 +77,21 @@ void initLoggingOrDie(folly::StringPiece configString = ""); ...@@ -77,3 +77,21 @@ void initLoggingOrDie(folly::StringPiece configString = "");
const char* getBaseLoggingConfig(); const char* getBaseLoggingConfig();
} // namespace folly } // namespace folly
/**
* A helper macro to set the default logging configuration in a program.
*
* This defines the folly::getBaseLoggingConfig() function, and makes it return
* the specified string.
*
* This macro should be used at the top-level namespace in a .cpp file in your
* program.
*/
#define FOLLY_INIT_LOGGING_CONFIG(config) \
namespace folly { \
const char* getBaseLoggingConfig() { \
static constexpr StringPiece configSP((config)); \
return configSP.data(); \
} \
} \
static_assert(true, "require a semicolon after FOLLY_INIT_LOGGING_CONFIG()")
...@@ -29,15 +29,11 @@ using folly::LogLevel; ...@@ -29,15 +29,11 @@ using folly::LogLevel;
// This will use default log settings defined by folly::initializeLoggerDB(). // This will use default log settings defined by folly::initializeLoggerDB().
static ExampleObject staticInitialized("static"); static ExampleObject staticInitialized("static");
namespace folly { // Configure folly to enable INFO+ messages, and everything else to
const char* getBaseLoggingConfig() { // enable WARNING+.
// Configure folly to enable INFO+ messages, and everything else to //
// enable WARNING+. // Set the default log handler to log asynchronously by default.
// FOLLY_INIT_LOGGING_CONFIG(".=WARNING,folly=INFO; default:async=true");
// Set the default log handler to log asynchronously by default.
return ".=WARNING,folly=INFO; default:async=true";
}
} // namespace folly
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Using log macros before calling folly::initLogging() will use the default // Using log macros before calling folly::initLogging() will use the default
......
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