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