proxygen
|
#include <LoggerDB.h>
Classes | |
struct | HandlerInfo |
Public Types | |
enum | TestConstructorArg { TESTING } |
using | InternalWarningHandler = void(*)(folly::StringPiece file, int lineNumber, std::string &&) |
Public Member Functions | |
~LoggerDB () | |
LogCategory * | getCategory (folly::StringPiece name) |
LogCategory *FOLLY_NULLABLE | getCategoryOrNull (folly::StringPiece name) |
void | setLevel (folly::StringPiece name, LogLevel level, bool inherit=true) |
void | setLevel (LogCategory *category, LogLevel level, bool inherit=true) |
LogConfig | getConfig () const |
LogConfig | getFullConfig () const |
void | updateConfig (const LogConfig &config) |
void | resetConfig (const LogConfig &config) |
void | cleanupHandlers () |
size_t | flushAllHandlers () |
void | registerHandlerFactory (std::unique_ptr< LogHandlerFactory > factory, bool replaceExisting=false) |
void | unregisterHandlerFactory (folly::StringPiece type) |
LogLevel | xlogInit (folly::StringPiece categoryName, std::atomic< LogLevel > *xlogCategoryLevel, LogCategory **xlogCategory) |
LogCategory * | xlogInitCategory (folly::StringPiece categoryName, LogCategory **xlogCategory, std::atomic< bool > *isInitialized) |
LoggerDB (TestConstructorArg) | |
Static Public Member Functions | |
static LoggerDB & | get () |
template<typename... Args> | |
static void | internalWarning (folly::StringPiece file, int lineNumber, Args &&...args) noexcept |
static void | setInternalWarningHandler (InternalWarningHandler handler) |
Private Types | |
using | LoggerNameMap = std::unordered_map< folly::StringPiece, std::unique_ptr< LogCategory >, LogName::Hash, LogName::Equals > |
using | HandlerFactoryMap = std::unordered_map< std::string, std::unique_ptr< LogHandlerFactory >> |
using | HandlerMap = std::unordered_map< std::string, std::weak_ptr< LogHandler >> |
using | NewHandlerMap = std::unordered_map< std::string, std::shared_ptr< LogHandler >> |
using | OldToNewHandlerMap = std::unordered_map< std::shared_ptr< LogHandler >, std::shared_ptr< LogHandler >> |
Private Member Functions | |
LoggerDB (LoggerDB const &)=delete | |
LoggerDB & | operator= (LoggerDB const &)=delete |
LoggerDB () | |
LogCategory * | getOrCreateCategoryLocked (LoggerNameMap &loggersByName, folly::StringPiece name) |
LogCategory * | createCategoryLocked (LoggerNameMap &loggersByName, folly::StringPiece name, LogCategory *parent) |
LogConfig | getConfigImpl (bool includeAllCategories) const |
void | startConfigUpdate (const Synchronized< HandlerInfo >::LockedPtr &handlerInfo, const LogConfig &config, NewHandlerMap *handlers, OldToNewHandlerMap *oldToNewHandlerMap) |
void | finishConfigUpdate (const Synchronized< HandlerInfo >::LockedPtr &handlerInfo, NewHandlerMap *handlers, OldToNewHandlerMap *oldToNewHandlerMap) |
std::vector< std::shared_ptr< LogHandler > > | buildCategoryHandlerList (const NewHandlerMap &handlerMap, StringPiece categoryName, const std::vector< std::string > &categoryHandlerNames) |
Static Private Member Functions | |
static void | internalWarningImpl (folly::StringPiece filename, int lineNumber, std::string &&msg) noexcept |
static void | defaultInternalWarningImpl (folly::StringPiece filename, int lineNumber, std::string &&msg) noexcept |
Private Attributes | |
folly::Synchronized< LoggerNameMap > | loggersByName_ |
folly::Synchronized< HandlerInfo > | handlerInfo_ |
Static Private Attributes | |
static std::atomic< InternalWarningHandler > | warningHandler_ |
LoggerDB stores the set of LogCategory objects.
Definition at line 40 of file LoggerDB.h.
|
private |
Definition at line 234 of file LoggerDB.h.
|
private |
Definition at line 235 of file LoggerDB.h.
using folly::LoggerDB::InternalWarningHandler = void (*)(folly::StringPiece file, int lineNumber, std::string&&) |
Definition at line 210 of file LoggerDB.h.
|
private |
Definition at line 231 of file LoggerDB.h.
|
private |
Definition at line 255 of file LoggerDB.h.
|
private |
Definition at line 257 of file LoggerDB.h.
folly::LoggerDB::~LoggerDB | ( | ) |
Definition at line 131 of file LoggerDB.cpp.
|
explicit |
Construct a LoggerDB for testing purposes.
Most callers should not need this function, and should use LoggerDB::get() to obtain the main LoggerDB singleton. This function exists mainly to allow testing LoggerDB objects in unit tests. It requires an explicit argument just to prevent callers from calling it unintentionally.
Definition at line 129 of file LoggerDB.cpp.
|
privatedelete |
|
private |
Definition at line 118 of file LoggerDB.cpp.
References folly::kDefaultLogLevel, and folly::gen::move.
|
private |
void folly::LoggerDB::cleanupHandlers | ( | ) |
Remove all registered LogHandlers on all LogCategory objects.
This is called on the main LoggerDB object during shutdown.
Definition at line 520 of file LoggerDB.cpp.
References handlerInfo_, and loggersByName_.
|
private |
Definition at line 509 of file LoggerDB.cpp.
References folly::gen::move, name, and parent.
Referenced by getOrCreateCategoryLocked().
|
staticprivatenoexcept |
Definition at line 679 of file LoggerDB.cpp.
References folly::kIsDebug, and folly::writeFull().
Referenced by internalWarningImpl().
|
private |
Update handlerInfo_ at the end of a config update operation.
Definition at line 346 of file LoggerDB.cpp.
References folly::Synchronized< T, Mutex >::swap().
Referenced by resetConfig(), and updateConfig().
size_t folly::LoggerDB::flushAllHandlers | ( | ) |
Call flush() on all LogHandler objects registered on any LogCategory in this LoggerDB.
Returns the number of registered LogHandlers.
Definition at line 551 of file LoggerDB.cpp.
References handler(), and loggersByName_.
Referenced by folly::LogCategory::admitMessage().
|
static |
Get the main LoggerDB singleton.
Definition at line 112 of file LoggerDB.cpp.
References folly::singleton.
Referenced by folly::XlogCategoryInfo< IsInHeaderFile >::init(), folly::initLogging(), folly::XlogLevelInfo< IsInHeaderFile >::loadLevelFull(), folly::Logger::Logger(), TEST(), and TEST_F().
LogCategory * folly::LoggerDB::getCategory | ( | folly::StringPiece | name | ) |
Get the LogCategory for the specified name.
This creates the LogCategory for the specified name if it does not exist already.
Definition at line 133 of file LoggerDB.cpp.
References getOrCreateCategoryLocked(), loggersByName_, and name.
Referenced by folly::Logger::Logger(), and TEST_F().
LogCategory *FOLLY_NULLABLE folly::LoggerDB::getCategoryOrNull | ( | folly::StringPiece | name | ) |
Get the LogCategory for the specified name, if it already exists.
This returns nullptr if no LogCategory has been created yet for the specified name.
Definition at line 137 of file LoggerDB.cpp.
References loggersByName_.
LogConfig folly::LoggerDB::getConfig | ( | ) | const |
Get a LogConfig object describing the current state of the LoggerDB.
Definition at line 158 of file LoggerDB.cpp.
References getConfigImpl().
Referenced by TEST().
|
private |
Definition at line 166 of file LoggerDB.cpp.
References handler(), handlerInfo_, loggersByName_, folly::MAX_LEVEL, folly::gen::move, and name.
Referenced by getConfig(), and getFullConfig().
LogConfig folly::LoggerDB::getFullConfig | ( | ) | const |
Get a LogConfig object fully describing the state of the LoggerDB.
This is similar to getConfig(), but it returns LogCategoryConfig objects for all defined log categories, including ones that are using the default configuration settings.
Definition at line 162 of file LoggerDB.cpp.
References getConfigImpl().
|
private |
Definition at line 496 of file LoggerDB.cpp.
References createCategoryLocked(), folly::LogName::getParent(), and parent.
Referenced by getCategory(), resetConfig(), setLevel(), updateConfig(), xlogInit(), and xlogInitCategory().
|
inlinestaticnoexcept |
internalWarning() is used to report a problem when something goes wrong internally in the logging library.
We can't log these messages through the normal logging flow since logging itself has failed.
Example scenarios where this is used:
Definition at line 201 of file LoggerDB.h.
Referenced by folly::AsyncFileWriter::onIoError(), folly::LogCategory::processMessage(), and folly::ImmediateFileWriter::writeMessage().
|
staticprivatenoexcept |
Definition at line 644 of file LoggerDB.cpp.
References defaultInternalWarningImpl(), handler(), folly::gen::move, and warningHandler_.
void folly::LoggerDB::registerHandlerFactory | ( | std::unique_ptr< LogHandlerFactory > | factory, |
bool | replaceExisting = false |
||
) |
Register a LogHandlerFactory.
The LogHandlerFactory will be used to create LogHandler objects from a LogConfig object during updateConfig() and resetConfig() calls.
Only one factory can be registered for a given handler type name. LogHandlerFactory::getType() returns the handler type supported by this LogHandlerFactory.
If an existing LogHandlerFactory is already registered with this type name and replaceExisting is false a std::range_error will be thrown. Otherwise, if replaceExisting is true, the new factory will replace the existing factory.
Definition at line 572 of file LoggerDB.cpp.
References handlerInfo_, folly::gen::move, and type.
Referenced by folly::initializeLoggerDB().
void folly::LoggerDB::resetConfig | ( | const LogConfig & | config | ) |
Reset the current LoggerDB state to the specified LogConfig settings.
All LogCategories not mentioned in the new LogConfig will have all currently configured log handlers removed and their log level set to its default state. For the root category the default log level is kDefaultLogLevel (see LogLevel.h); for all other categories the default level is MAX_LEVEL with log level inheritance enabled.
LogCategories listed in the new config but without LogHandler information defined will have all existing handlers removed.
Definition at line 435 of file LoggerDB.cpp.
References buildCategoryHandlerList(), finishConfigUpdate(), folly::LogConfig::getCategoryConfigs(), getOrCreateCategoryLocked(), handlerInfo_, folly::kDefaultLogLevel, loggersByName_, folly::MAX_LEVEL, folly::gen::move, and startConfigUpdate().
|
static |
Set a function to be called when the logging library generates an internal warning.
The supplied handler should never throw exceptions.
If a null handler is supplied, the default built-in handler will be used.
The default handler reports the message with _CrtDbgReport(_CRT_WARN) on Windows, and prints the message to stderr on other platforms. It also rate limits messages if they are arriving too quickly.
Definition at line 656 of file LoggerDB.cpp.
References warningHandler_.
Referenced by TEST().
void folly::LoggerDB::setLevel | ( | folly::StringPiece | name, |
LogLevel | level, | ||
bool | inherit = true |
||
) |
Set the log level for the specified category.
Messages logged to a specific log category will be ignored unless the message log level is greater than the LogCategory's effective log level.
If inherit is true, LogCategory's effective log level is the minimum of its level and it's parent category's effective log level. If inherit is false, the LogCategory's effective log level is simply its log level. (Setting inherit to false is necessary if you want a child LogCategory to use a less verbose level than its parent categories.)
Definition at line 147 of file LoggerDB.cpp.
References getOrCreateCategoryLocked(), loggersByName_, and folly::LogCategory::setLevelLocked().
Referenced by folly::LogCategory::setLevel(), and TEST_F().
void folly::LoggerDB::setLevel | ( | LogCategory * | category, |
LogLevel | level, | ||
bool | inherit = true |
||
) |
Definition at line 153 of file LoggerDB.cpp.
References loggersByName_, and folly::LogCategory::setLevelLocked().
|
private |
Process handler config information when starting a config update operation.
Definition at line 237 of file LoggerDB.cpp.
References folly::exceptionStr(), folly::LogConfig::getCategoryConfigs(), folly::LogConfig::getHandlerConfigs(), handler(), folly::Optional< Value >::hasValue(), folly::gen::move, folly::LogHandlerConfig::options, folly::LogHandlerConfig::type, folly::LogHandlerConfig::update(), and folly::Optional< Value >::value().
Referenced by resetConfig(), and updateConfig().
void folly::LoggerDB::unregisterHandlerFactory | ( | folly::StringPiece | type | ) |
Remove a registered LogHandlerFactory.
The type parameter should be the name of the handler type, as returned by LogHandlerFactory::getType().
Throws std::range_error if no handler factory with this type name exists.
Definition at line 588 of file LoggerDB.cpp.
References handlerInfo_, and folly::Range< Iter >::str().
void folly::LoggerDB::updateConfig | ( | const LogConfig & | config | ) |
Update the current LoggerDB state with the specified LogConfig settings.
Log categories and handlers listed in the LogConfig object will be updated to the new state listed in the LogConfig. Settings on categories and handlers not listed in the config will be left as-is.
Definition at line 395 of file LoggerDB.cpp.
References buildCategoryHandlerList(), finishConfigUpdate(), folly::LogConfig::getCategoryConfigs(), getOrCreateCategoryLocked(), handlerInfo_, loggersByName_, folly::gen::move, folly::LogCategory::replaceHandlers(), folly::LogCategory::setLevelLocked(), and startConfigUpdate().
Referenced by folly::initializeLoggerDB(), and folly::initLogging().
LogLevel folly::LoggerDB::xlogInit | ( | folly::StringPiece | categoryName, |
std::atomic< LogLevel > * | xlogCategoryLevel, | ||
LogCategory ** | xlogCategory | ||
) |
Initialize the LogCategory* and std::atomic<LogLevel> used by an XLOG() statement.
Returns the current effective LogLevel of the category.
Definition at line 597 of file LoggerDB.cpp.
References folly::LogCategory::getEffectiveLevel(), getOrCreateCategoryLocked(), and loggersByName_.
Referenced by folly::XlogCategoryInfo< IsInHeaderFile >::init(), and folly::XlogLevelInfo< IsInHeaderFile >::loadLevelFull().
LogCategory * folly::LoggerDB::xlogInitCategory | ( | folly::StringPiece | categoryName, |
LogCategory ** | xlogCategory, | ||
std::atomic< bool > * | isInitialized | ||
) |
Definition at line 623 of file LoggerDB.cpp.
References getOrCreateCategoryLocked(), loggersByName_, and warningHandler_.
Referenced by folly::XlogCategoryInfo< IsInHeaderFile >::init().
|
private |
The LogHandlers and LogHandlerFactories.
For lock ordering purposes, if you need to acquire both the loggersByName_ and handlerInfo_ locks, the handlerInfo_ lock must be acquired first.
Definition at line 296 of file LoggerDB.h.
Referenced by cleanupHandlers(), getConfigImpl(), registerHandlerFactory(), resetConfig(), unregisterHandlerFactory(), and updateConfig().
|
private |
A map of LogCategory objects by name.
Lookups can be performed using arbitrary StringPiece values that do not have to be in canonical form.
Definition at line 288 of file LoggerDB.h.
Referenced by cleanupHandlers(), flushAllHandlers(), getCategory(), getCategoryOrNull(), getConfigImpl(), resetConfig(), setLevel(), updateConfig(), xlogInit(), and xlogInitCategory().
|
staticprivate |
Definition at line 298 of file LoggerDB.h.
Referenced by internalWarningImpl(), setInternalWarningHandler(), and xlogInitCategory().