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

logging: move test utility functions into a helper module

Summary:
Move some utility functions from ConfigParserTest and ConfigUpdateTest into a
new ConfigHelpers module.  This eliminates some duplicated logic and will make
it easier to re-use these functions in other tests in the future.

Reviewed By: yfeldblum

Differential Revision: D7164831

fbshipit-source-id: 115a227e4cb2db486f57079483b8ae9aadaa0187
parent bfe2b889
/*
* Copyright 2004-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <folly/experimental/logging/test/ConfigHelpers.h>
#include <ostream>
#include <folly/String.h>
#include <folly/experimental/logging/LogConfig.h>
#include <folly/experimental/logging/LogConfigParser.h>
#include <folly/experimental/logging/LogHandler.h>
namespace folly {
std::ostream& operator<<(std::ostream& os, const LogConfig& config) {
// We could just use folly::toPrettyJson(logConfigToDynamic(config))
// However, the format here is much more compact and easier to read if there
// are discrepancies between configs in a test check.
// Sort the categories by name before printing
os << "{\n categories: {\n";
std::vector<std::string> names;
const auto& catConfigs = config.getCategoryConfigs();
for (const auto& cc : catConfigs) {
names.push_back(cc.first);
}
std::sort(names.begin(), names.end());
for (const auto& name : names) {
os << " " << name << "=" << catConfigs.at(name) << "\n";
}
// Sort the handlers by name before printing
os << " }\n handlers: {\n";
const auto& handlerConfigs = config.getHandlerConfigs();
names.clear();
for (const auto& cc : handlerConfigs) {
names.push_back(cc.first);
}
std::sort(names.begin(), names.end());
for (const auto& name : names) {
os << " " << name << "=" << handlerConfigs.at(name) << "\n";
}
os << " }\n}";
return os;
}
std::ostream& operator<<(std::ostream& os, const LogCategoryConfig& config) {
// Rather than printing the JSON configuration, we print a shorter
// representation closer to the basic config string format.
os << logLevelToString(config.level);
if (!config.inheritParentLevel) {
os << "!";
}
if (config.handlers.hasValue()) {
os << ":" << join(",", config.handlers.value());
}
return os;
}
std::ostream& operator<<(std::ostream& os, const LogHandlerConfig& config) {
// Rather than printing the JSON configuration, we print a shorter
// representation closer to the basic config string format.
os << (config.type ? config.type.value() : "[no type]");
bool first = true;
for (const auto& opt : config.options) {
if (!first) {
os << ",";
} else {
os << ":";
first = false;
}
os << opt.first << "=" << opt.second;
}
return os;
}
void PrintTo(const std::shared_ptr<LogHandler>& handler, std::ostream* os) {
*os << "Handler(" << handler->getConfig() << ")";
}
} // namespace folly
/*
* Copyright 2004-present Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma once
#include <iosfwd>
#include <memory>
namespace folly {
class LogCategoryConfig;
class LogConfig;
class LogHandler;
class LogHandlerConfig;
/*
* ostream<< operators so that various objects can be printed nicely in test
* failure messages and other locations.
*/
std::ostream& operator<<(std::ostream& os, const LogConfig& config);
std::ostream& operator<<(std::ostream& os, const LogCategoryConfig& config);
std::ostream& operator<<(std::ostream& os, const LogHandlerConfig& config);
/*
* Print std::shared_ptr<LogHandler> nicely so that unit tests matching against
* LogCategory::getHandlers() can print output nicely.
*/
void PrintTo(const std::shared_ptr<LogHandler>& handler, std::ostream* os);
} // namespace folly
......@@ -18,6 +18,7 @@
#include <folly/experimental/logging/LogCategory.h>
#include <folly/experimental/logging/LogConfig.h>
#include <folly/experimental/logging/LogConfigParser.h>
#include <folly/experimental/logging/test/ConfigHelpers.h>
#include <folly/json.h>
#include <folly/portability/GMock.h>
#include <folly/portability/GTest.h>
......@@ -28,34 +29,6 @@ using namespace folly;
using ::testing::Pair;
using ::testing::UnorderedElementsAre;
namespace folly {
std::ostream& operator<<(std::ostream& os, const LogCategoryConfig& config) {
os << logLevelToString(config.level);
if (!config.inheritParentLevel) {
os << "!";
}
if (config.handlers.hasValue()) {
os << ":" << join(",", config.handlers.value());
}
return os;
}
std::ostream& operator<<(std::ostream& os, const LogHandlerConfig& config) {
os << (config.type ? config.type.value() : "[no type]");
bool first = true;
for (const auto& opt : config.options) {
if (!first) {
os << ",";
} else {
os << ":";
first = false;
}
os << opt.first << "=" << opt.second;
}
return os;
}
} // namespace folly
TEST(LogConfig, parseBasic) {
auto config = parseLogConfig("");
EXPECT_THAT(config.getCategoryConfigs(), UnorderedElementsAre());
......
......@@ -19,6 +19,7 @@
#include <folly/experimental/logging/LogConfigParser.h>
#include <folly/experimental/logging/LogHandlerFactory.h>
#include <folly/experimental/logging/LoggerDB.h>
#include <folly/experimental/logging/test/ConfigHelpers.h>
#include <folly/experimental/logging/test/TestLogHandler.h>
#include <folly/json.h>
#include <folly/portability/GMock.h>
......@@ -50,39 +51,6 @@ auto MatchLogHandler(const LogHandlerConfig& config) {
} // namespace
namespace folly {
/**
* Print TestLogHandler objects nicely in test failure messages
*/
std::ostream& operator<<(
std::ostream& os,
const std::shared_ptr<LogHandler>& handler) {
auto configHandler = std::dynamic_pointer_cast<TestLogHandler>(handler);
if (!configHandler) {
os << "unknown handler type";
return os;
}
auto config = configHandler->getConfig();
os << "ConfigHandler(" << (config.type ? config.type.value() : "[no type]");
for (const auto& entry : config.options) {
os << ", " << entry.first << "=" << entry.second;
}
os << ")";
return os;
}
std::ostream& operator<<(std::ostream& os, const LogConfig& config) {
os << toPrettyJson(logConfigToDynamic(config));
return os;
}
std::ostream& operator<<(std::ostream& os, const LogHandlerConfig& config) {
os << toPrettyJson(logConfigToDynamic(config));
return os;
}
} // namespace folly
TEST(ConfigUpdate, updateLogLevels) {
LoggerDB db{LoggerDB::TESTING};
db.updateConfig(parseLogConfig("foo.bar=dbg5"));
......
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