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

logging: update LoggerDB::get() to return a reference

Summary:
Change `LoggerDB::get()` to a reference instead of a pointer since this
function can never return null.

Reviewed By: yfeldblum

Differential Revision: D6893206

fbshipit-source-id: af47063918a79c851fd39b838d6c63755166e033
parent 18c4beb3
......@@ -48,7 +48,7 @@ constexpr StringPiece kDefaultLoggingConfig =
void initLogging(StringPiece configString) {
// Register the StreamHandlerFactory
LoggerDB::get()->registerHandlerFactory(
LoggerDB::get().registerHandlerFactory(
std::make_unique<StreamHandlerFactory>());
// TODO: In the future it would be nice to build a better mechanism so that
......@@ -74,7 +74,7 @@ void initLogging(StringPiece configString) {
}
// Now apply the configuration to the LoggerDB
LoggerDB::get()->updateConfig(config);
LoggerDB::get().updateConfig(config);
}
} // namespace folly
......@@ -25,7 +25,7 @@ using std::string;
namespace folly {
Logger::Logger(StringPiece name) : Logger{LoggerDB::get()->getCategory(name)} {}
Logger::Logger(StringPiece name) : Logger{LoggerDB::get().getCategory(name)} {}
Logger::Logger(LogCategory* cat) : category_(cat) {}
......
......@@ -52,8 +52,8 @@ class LoggerDBSingleton {
db_->cleanupHandlers();
}
LoggerDB* getDB() const {
return db_;
LoggerDB& getDB() const {
return *db_;
}
private:
......@@ -61,7 +61,7 @@ class LoggerDBSingleton {
};
} // namespace
LoggerDB* LoggerDB::get() {
LoggerDB& LoggerDB::get() {
// Intentionally leaky singleton
static LoggerDBSingleton singleton{new LoggerDB()};
return singleton.getDB();
......
......@@ -42,7 +42,7 @@ class LoggerDB {
/**
* Get the main LoggerDB singleton.
*/
static LoggerDB* get();
static LoggerDB& get();
~LoggerDB();
......
......@@ -45,7 +45,7 @@ TEST(Xlog, xlogName) {
TEST(Xlog, xlog) {
auto handler = make_shared<TestLogHandler>();
LoggerDB::get()->getCategory("xlog_test")->addHandler(handler);
LoggerDB::get().getCategory("xlog_test")->addHandler(handler);
auto& messages = handler->getMessages();
// info messages are not enabled initially.
......@@ -56,7 +56,7 @@ TEST(Xlog, xlog) {
messages.clear();
// Increase the log level, then log a message.
LoggerDB::get()->setLevel("xlog_test.main_file", LogLevel::DBG1);
LoggerDB::get().setLevel("xlog_test.main_file", LogLevel::DBG1);
XLOG(DBG1, "testing: ", 1, 2, 3);
ASSERT_EQ(1, messages.size());
......@@ -99,9 +99,9 @@ TEST(Xlog, perFileCategoryHandling) {
auto handler = make_shared<TestLogHandler>();
LoggerDB::get()
->getCategory("folly.experimental.logging.test")
.getCategory("folly.experimental.logging.test")
->addHandler(handler);
LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::DBG9);
LoggerDB::get().setLevel("folly.experimental.logging.test", LogLevel::DBG9);
auto& messages = handler->getMessages();
// Use the simple helper function in XlogHeader2
......@@ -141,7 +141,7 @@ TEST(Xlog, perFileCategoryHandling) {
// Reduce the log level so that the messages inside the loop
// should not be logged.
LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::DBG2);
LoggerDB::get().setLevel("folly.experimental.logging.test", LogLevel::DBG2);
testXlogHdrLoop(300, "hello world");
ASSERT_EQ(2, messages.size());
EXPECT_EQ("starting: hello world", messages[0].first.getMessage());
......@@ -168,13 +168,13 @@ TEST(Xlog, perFileCategoryHandling) {
// Adjust the log level and make sure the changes take effect for the .cpp
// file categories
LoggerDB::get()->setLevel("folly.experimental.logging.test", LogLevel::INFO);
LoggerDB::get().setLevel("folly.experimental.logging.test", LogLevel::INFO);
testXlogFile1Dbg1("log check should fail now");
testXlogFile2Dbg1("this should fail too");
EXPECT_EQ(0, messages.size());
messages.clear();
LoggerDB::get()->setLevel(
LoggerDB::get().setLevel(
"folly.experimental.logging.test.XlogFile1", LogLevel::DBG1);
testXlogFile1Dbg1("this log check should pass now");
testXlogFile2Dbg1("but this one should still fail");
......
......@@ -84,7 +84,7 @@ LogLevel XlogLevelInfo<IsInHeaderFile>::loadLevelFull(
bool isOverridden) {
auto currentLevel = level_.load(std::memory_order_acquire);
if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
return LoggerDB::get()->xlogInit(
return LoggerDB::get().xlogInit(
isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
&level_,
nullptr);
......@@ -96,7 +96,7 @@ template <bool IsInHeaderFile>
LogCategory* XlogCategoryInfo<IsInHeaderFile>::init(
folly::StringPiece categoryName,
bool isOverridden) {
return LoggerDB::get()->xlogInitCategory(
return LoggerDB::get().xlogInitCategory(
isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
&category_,
&isInitialized_);
......@@ -109,7 +109,7 @@ LogLevel XlogLevelInfo<false>::loadLevelFull(
XlogFileScopeInfo* fileScopeInfo) {
auto currentLevel = fileScopeInfo->level.load(std::memory_order_acquire);
if (UNLIKELY(currentLevel == ::folly::LogLevel::UNINITIALIZED)) {
return LoggerDB::get()->xlogInit(
return LoggerDB::get().xlogInit(
isOverridden ? categoryName : getXlogCategoryNameForFile(categoryName),
&fileScopeInfo->level,
&fileScopeInfo->category);
......
......@@ -204,7 +204,7 @@
* expand to the correct filename based on where the macro is used.
*/
#define XLOG_GET_CATEGORY() \
folly::LoggerDB::get()->getCategory(XLOG_GET_CATEGORY_NAME())
folly::LoggerDB::get().getCategory(XLOG_GET_CATEGORY_NAME())
/**
* XLOG_SET_CATEGORY_NAME() can be used to explicitly define the log category
......
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