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

logging: add a LoggerDB::getFullConfig() method

Summary:
This method can be used to get configuration about all currently defined log
categories, including ones that are using the default config settings.

The existing LoggerDB::getConfig() method only returns information about
categories that have non-default config settings.

Reviewed By: yfeldblum

Differential Revision: D7164833

fbshipit-source-id: e1c043553dc4f69be58c33f2bc5b1a63763f0984
parent d8345822
......@@ -156,6 +156,14 @@ void LoggerDB::setLevel(LogCategory* category, LogLevel level, bool inherit) {
}
LogConfig LoggerDB::getConfig() const {
return getConfigImpl(/* includeAllCategories = */ false);
}
LogConfig LoggerDB::getFullConfig() const {
return getConfigImpl(/* includeAllCategories = */ true);
}
LogConfig LoggerDB::getConfigImpl(bool includeAllCategories) const {
auto handlerInfo = handlerInfo_.rlock();
LogConfig::HandlerConfigMap handlerConfigs;
......@@ -190,9 +198,10 @@ LogConfig LoggerDB::getConfig() const {
auto levelInfo = category->getLevelInfo();
auto handlers = category->getHandlers();
// Don't report categories that have default settings.
if (handlers.empty() && levelInfo.first == LogLevel::MAX_LEVEL &&
levelInfo.second) {
// Don't report categories that have default settings
// if includeAllCategories is false
if (!includeAllCategories && handlers.empty() &&
levelInfo.first == LogLevel::MAX_LEVEL && levelInfo.second) {
continue;
}
......
......@@ -79,15 +79,18 @@ class LoggerDB {
/**
* Get a LogConfig object describing the current state of the LoggerDB.
*
* Note that this may not 100% accurately describe the current configuration
* if callers have manually added LogHandlers to some categories without
* using the updateConfig() or resetConfig() functions. In this case
* getConfig() will simply report these handlers as "unknown_handler" when
* returning handler names for the categories in question.
*/
LogConfig getConfig() 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.
*/
LogConfig getFullConfig() const;
/**
* Update the current LoggerDB state with the specified LogConfig settings.
*
......@@ -252,6 +255,7 @@ class LoggerDB {
std::unordered_map<std::string, std::shared_ptr<LogHandler>>;
using OldToNewHandlerMap = std::
unordered_map<std::shared_ptr<LogHandler>, std::shared_ptr<LogHandler>>;
LogConfig getConfigImpl(bool includeAllCategories) const;
void startConfigUpdate(
const Synchronized<HandlerInfo>::LockedPtr& handlerInfo,
const LogConfig& config,
......
......@@ -345,3 +345,44 @@ TEST(ConfigUpdate, getConfigAnonymousHandlers) {
"anonymousHandler2=foo: abc=xyz"),
db.getConfig());
}
TEST(ConfigUpdate, getFullConfig) {
LoggerDB db{LoggerDB::TESTING};
db.registerHandlerFactory(
std::make_unique<TestLogHandlerFactory>("handlerA"));
db.registerHandlerFactory(
std::make_unique<TestLogHandlerFactory>("handlerB"));
EXPECT_EQ(parseLogConfig(".:=ERROR:"), db.getConfig());
db.getCategory("src.libfoo.foo.c");
db.getCategory("src.libfoo.foo.h");
db.getCategory("src.libfoo.bar.h");
db.getCategory("src.libfoo.bar.c");
db.getCategory("test.foo.test.c");
db.updateConfig(
parseLogConfig(".=ERR:stdout,"
"src.libfoo=dbg5; "
"stdout=handlerA:stream=stdout"));
EXPECT_EQ(
parseLogConfig(".:=ERR:stdout,"
"src.libfoo=dbg5:; "
"stdout=handlerA:stream=stdout"),
db.getConfig());
EXPECT_EQ(
parseLogConfig(".:=ERR:stdout,"
"src=FATAL:, "
"src.libfoo=dbg5:, "
"src.libfoo.foo=FATAL:, "
"src.libfoo.foo.c=FATAL:, "
"src.libfoo.foo.h=FATAL:, "
"src.libfoo.bar=FATAL:, "
"src.libfoo.bar.c=FATAL:, "
"src.libfoo.bar.h=FATAL:, "
"test=FATAL:, "
"test.foo=FATAL:, "
"test.foo.test=FATAL:, "
"test.foo.test.c=FATAL:; "
"stdout=handlerA:stream=stdout"),
db.getFullConfig());
}
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