proxygen
|
#include <LogCategory.h>
Public Member Functions | |
LogCategory (LoggerDB *db) | |
LogCategory (folly::StringPiece name, LogCategory *parent) | |
const std::string & | getName () const |
LogLevel | getLevel () const |
std::pair< LogLevel, bool > | getLevelInfo () const |
LogLevel | getEffectiveLevel () const |
LogLevel | getEffectiveLevelRelaxed () const |
bool | logCheck (LogLevel level) const |
void | setLevel (LogLevel level, bool inherit=true) |
LoggerDB * | getDB () const |
void | addHandler (std::shared_ptr< LogHandler > handler) |
void | clearHandlers () |
std::vector< std::shared_ptr< LogHandler > > | getHandlers () const |
void | replaceHandlers (std::vector< std::shared_ptr< LogHandler >> handlers) |
void | updateHandlers (const std::unordered_map< std::shared_ptr< LogHandler >, std::shared_ptr< LogHandler >> &handlerMap) |
void | admitMessage (const LogMessage &message) const |
void | setLevelLocked (LogLevel level, bool inherit) |
void | registerXlogLevel (std::atomic< LogLevel > *levelPtr) |
Private Types | |
enum | : uint32_t { FLAG_INHERIT = 0x80000000 } |
Private Member Functions | |
LogCategory (LogCategory const &)=delete | |
LogCategory & | operator= (LogCategory const &)=delete |
LogCategory (LogCategory &&)=delete | |
LogCategory & | operator= (LogCategory &&)=delete |
void | processMessage (const LogMessage &message) const |
void | updateEffectiveLevel (LogLevel newEffectiveLevel) |
void | parentLevelUpdated (LogLevel parentEffectiveLevel) |
Private Attributes | |
std::atomic< LogLevel > | effectiveLevel_ {LogLevel::MAX_LEVEL} |
std::atomic< uint32_t > | level_ {0} |
LogCategory *const | parent_ {nullptr} |
const std::string | name_ |
folly::Synchronized< std::vector< std::shared_ptr< LogHandler > > > | handlers_ |
LoggerDB *const | db_ {nullptr} |
LogCategory * | firstChild_ {nullptr} |
LogCategory * | nextSibling_ {nullptr} |
std::vector< std::atomic< LogLevel > * > | xlogLevels_ |
LogCategory stores all of the logging configuration for a specific log category.
This class is separate from Logger to allow multiple Logger objects to all refer to the same log category. Logger can be thought of as a small wrapper class that behaves like a pointer to a LogCategory object.
Definition at line 41 of file LogCategory.h.
|
private |
|
explicit |
Create the root LogCategory.
This should generally only be invoked by LoggerDB.
Definition at line 32 of file LogCategory.cpp.
References db_, folly::ERR, level_, name_, parent_, and uint32_t.
folly::LogCategory::LogCategory | ( | folly::StringPiece | name, |
LogCategory * | parent | ||
) |
Create a new LogCategory.
This should only be invoked by LoggerDB, while holding the main LoggerDB lock.
The name argument should already be in canonical form.
This constructor automatically adds this new LogCategory to the parent category's firstChild_ linked-list.
Definition at line 39 of file LogCategory.cpp.
References folly::LogName::canonicalize(), db_, firstChild_, FLAG_INHERIT, getEffectiveLevel(), level_, folly::MAX_LEVEL, name, name_, nextSibling_, parent, parent_, and uint32_t.
|
privatedelete |
|
privatedelete |
void folly::LogCategory::addHandler | ( | std::shared_ptr< LogHandler > | handler | ) |
Attach a LogHandler to this category.
Definition at line 127 of file LogCategory.cpp.
References handlers_, and folly::gen::move.
Referenced by getDB(), and TEST_F().
void folly::LogCategory::admitMessage | ( | const LogMessage & | message | ) | const |
Admit a message into the LogCategory hierarchy to be logged.
The caller is responsible for having already performed log level admittance checks.
This method generally should be invoked only through the logging macros, rather than calling this directly.
Definition at line 49 of file LogCategory.cpp.
References db_, folly::LoggerDB::flushAllHandlers(), folly::LogMessage::getFileName(), folly::LogMessage::getLevel(), folly::LogMessage::getLineNumber(), folly::LogMessage::getMessage(), folly::isLogLevelFatal(), processMessage(), and folly::writeFull().
Referenced by getDB(), and folly::LogStreamProcessor::logNow().
void folly::LogCategory::clearHandlers | ( | ) |
Remove all LogHandlers from this category.
Definition at line 132 of file LogCategory.cpp.
References handlers_.
Referenced by getDB().
|
inline |
Get the LoggerDB that this LogCategory belongs to.
This is almost always the main LoggerDB singleton returned by LoggerDB::get(). The logging unit tests are the main location that creates alternative LoggerDB objects.
Definition at line 148 of file LogCategory.h.
References addHandler(), admitMessage(), clearHandlers(), db_, getHandlers(), handler(), registerXlogLevel(), replaceHandlers(), setLevelLocked(), and updateHandlers().
|
inline |
Get the effective level for this log category.
This is the minimum log level of this category and all of its parents. Log messages below this level will be ignored, while messages at or above this level need to be processed by this category or one of its parents.
Definition at line 95 of file LogCategory.h.
References effectiveLevel_.
Referenced by LogCategory(), setLevelLocked(), and folly::LoggerDB::xlogInit().
|
inline |
Get the effective log level using std::memory_order_relaxed.
This is primarily used for log message checks. Most other callers should use getEffectiveLevel() above to be more conservative with regards to memory ordering.
Definition at line 106 of file LogCategory.h.
References effectiveLevel_.
std::vector< std::shared_ptr< LogHandler > > folly::LogCategory::getHandlers | ( | ) | const |
Get the list of LogHandlers attached to this category.
Definition at line 144 of file LogCategory.cpp.
References handlers_.
Referenced by getDB().
|
inline |
Get the level for this log category.
Definition at line 73 of file LogCategory.h.
References FLAG_INHERIT, and level_.
|
inline |
Get the log level and inheritance flag.
Definition at line 81 of file LogCategory.h.
References FLAG_INHERIT, level_, and folly::value().
|
inline |
|
inline |
Check whether this Logger or any of its parent Loggers would do anything with a log message at the given level.
Definition at line 114 of file LogCategory.h.
References effectiveLevel_, and setLevel().
|
privatedelete |
|
privatedelete |
|
private |
Definition at line 227 of file LogCategory.cpp.
References FLAG_INHERIT, level_, min, uint32_t, and updateEffectiveLevel().
Referenced by updateEffectiveLevel().
|
private |
Definition at line 74 of file LogCategory.cpp.
References folly::exceptionStr(), handlers_, folly::LoggerDB::internalWarning(), name_, parent_, processMessage(), and uint32_t.
Referenced by admitMessage(), and processMessage().
void folly::LogCategory::registerXlogLevel | ( | std::atomic< LogLevel > * | levelPtr | ) |
Register a std::atomic<LogLevel> value used by XLOG*() macros to check the effective level for this category.
The LogCategory will keep this value updated whenever its effective log level changes.
This function should only be invoked by LoggerDB, and the LoggerDB lock must be held when calling it.
Definition at line 239 of file LogCategory.cpp.
References xlogLevels_.
Referenced by getDB().
void folly::LogCategory::replaceHandlers | ( | std::vector< std::shared_ptr< LogHandler >> | handlers | ) |
Replace the list of LogHandlers with a completely new list.
Definition at line 148 of file LogCategory.cpp.
References handlers_.
Referenced by getDB(), and folly::LoggerDB::updateConfig().
void folly::LogCategory::setLevel | ( | LogLevel | level, |
bool | inherit = true |
||
) |
Set the log level for this LogCategory.
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 its 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 165 of file LogCategory.cpp.
References db_, and folly::LoggerDB::setLevel().
Referenced by logCheck().
void folly::LogCategory::setLevelLocked | ( | LogLevel | level, |
bool | inherit | ||
) |
Note: setLevelLocked() may only be called while holding the LoggerDB loggersByName_ lock. It is safe to call this while holding the loggersByName_ lock in read-mode; holding it exclusively is not required.
This method should only be invoked by LoggerDB.
Definition at line 172 of file LogCategory.cpp.
References folly::constexpr_clamp(), FLAG_INHERIT, getEffectiveLevel(), level_, folly::MAX_LEVEL, min, folly::MIN_LEVEL, parent_, uint32_t, and updateEffectiveLevel().
Referenced by getDB(), folly::LoggerDB::setLevel(), and folly::LoggerDB::updateConfig().
|
private |
Definition at line 206 of file LogCategory.cpp.
References child, effectiveLevel_, firstChild_, nextSibling_, parentLevelUpdated(), and xlogLevels_.
Referenced by parentLevelUpdated(), and setLevelLocked().
void folly::LogCategory::updateHandlers | ( | const std::unordered_map< std::shared_ptr< LogHandler >, std::shared_ptr< LogHandler >> & | handlerMap | ) |
Update the LogHandlers attached to this LogCategory by replacing currently attached handlers with new LogHandler objects.
The handlerMap argument is a map of (old_handler -> new_handler) If any of the LogHandlers currently attached to this category are found in the handlerMap, replace them with the new handler indicated in the map.
This is used when the LogHandler configuration is changed requiring one or more LogHandler objects to be replaced with new ones.
Definition at line 153 of file LogCategory.cpp.
References folly::get_ptr(), handlers_, and ptr.
Referenced by getDB().
A pointer to the LoggerDB that we belong to.
This is almost always the main LoggerDB singleton. Unit tests are the main place where we use other LoggerDB objects besides the singleton.
Definition at line 280 of file LogCategory.h.
Referenced by admitMessage(), getDB(), LogCategory(), and setLevel().
|
private |
The minimum log level of this category and all of its parents.
Definition at line 246 of file LogCategory.h.
Referenced by getEffectiveLevel(), getEffectiveLevelRelaxed(), logCheck(), and updateEffectiveLevel().
|
private |
Pointers to children and sibling loggers. These pointers should only ever be accessed while holding the LoggerDB::loggersByName_ lock. (These are only modified when creating new loggers, which occurs with the main LoggerDB lock held.)
Definition at line 288 of file LogCategory.h.
Referenced by LogCategory(), and updateEffectiveLevel().
|
private |
The list of LogHandlers attached to this category.
Definition at line 272 of file LogCategory.h.
Referenced by addHandler(), clearHandlers(), getHandlers(), processMessage(), replaceHandlers(), and updateHandlers().
|
private |
The current log level for this category.
The most significant bit is used to indicate if this logger should inherit its parent's effective log level.
Definition at line 254 of file LogCategory.h.
Referenced by getLevel(), getLevelInfo(), LogCategory(), parentLevelUpdated(), and setLevelLocked().
|
private |
Our log category name.
Definition at line 267 of file LogCategory.h.
Referenced by getName(), LogCategory(), and processMessage().
|
private |
Definition at line 289 of file LogCategory.h.
Referenced by LogCategory(), and updateEffectiveLevel().
|
private |
Our parent LogCategory in the category hierarchy.
For instance, if our log name is "foo.bar.abc", our parent category is "foo.bar".
Definition at line 262 of file LogCategory.h.
Referenced by LogCategory(), processMessage(), and setLevelLocked().
|
private |
A list of LogLevel values used by XLOG*() statements for this LogCategory. The XLOG*() statements will check these values. We ensure they are kept up-to-date each time the effective log level changes for this category.
This list may only be accessed while holding the main LoggerDB lock.
Definition at line 298 of file LogCategory.h.
Referenced by registerXlogLevel(), and updateEffectiveLevel().