Commit a04d119c authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Use StaticSingletonManager for LoggerDB::get()

Summary:
[Folly] Use `StaticSingletonManager` for `LoggerDB::get()` inline fast path.

This also simplifies the singleton setup overall.

Note that the inline slow path here is not quite as small as doing the strategy directly would be since three pointers need to be passed to the outline slow path.

Reviewed By: simpkins

Differential Revision: D14016983

fbshipit-source-id: ea2646f8efd5c2dbcdece4ebd3b88949bc3d4294
parent 08a5c359
......@@ -68,46 +68,6 @@ FOLLY_ATTR_WEAK void initializeLoggerDB(LoggerDB& db) {
db.updateConfig(config);
}
namespace {
class LoggerDBCleanup {
public:
explicit LoggerDBCleanup(LoggerDB* FOLLY_NONNULL db) : db_{db} {}
~LoggerDBCleanup() {
db_->cleanupHandlers();
}
private:
LoggerDB* const db_;
};
} // namespace
LoggerDB* LoggerDB::createSingleton() {
// Use a unique_ptr() to clean up the LoggerDB if initializeLoggerDB() throws
std::unique_ptr<LoggerDB> db(new LoggerDB());
initializeLoggerDB(*db);
// Once initializeLoggerDB() has succeeded extract the raw pointer to return
// to our caller.
//
// We intentionally leak the LoggerDB singleton and all of the LogCategory
// objects it contains.
//
// We want Logger objects to remain valid for the entire lifetime of the
// program, without having to worry about destruction ordering issues, or
// making the Logger perform reference counting on the LoggerDB.
return db.release();
}
LoggerDB& LoggerDB::get() {
// Intentionally leaky LoggerDB singleton
static LoggerDB* const db = createSingleton();
// LoggerDBCleanup is responsible for calling db->cleanupHandlers() on program
// shutdown. This allows log handlers to flush any buffered messages before
// the program exits.
static LoggerDBCleanup cleanup(db);
return *db;
}
LoggerDB::LoggerDB() {
// Create the root log category and set its log level
auto rootUptr = std::make_unique<LogCategory>(this);
......
......@@ -15,15 +15,17 @@
*/
#pragma once
#include <folly/Conv.h>
#include <folly/CppAttributes.h>
#include <folly/Range.h>
#include <folly/Synchronized.h>
#include <memory>
#include <string>
#include <unordered_map>
#include <vector>
#include <folly/Conv.h>
#include <folly/CppAttributes.h>
#include <folly/Range.h>
#include <folly/ScopeGuard.h>
#include <folly/Synchronized.h>
#include <folly/detail/StaticSingletonManager.h>
#include <folly/logging/LogName.h>
namespace folly {
......@@ -270,7 +272,6 @@ class LoggerDB {
StringPiece categoryName,
const std::vector<std::string>& categoryHandlerNames);
static LoggerDB* createSingleton();
static void internalWarningImpl(
folly::StringPiece filename,
int lineNumber,
......@@ -328,4 +329,24 @@ class LoggerDB {
*/
void initializeLoggerDB(LoggerDB& db);
FOLLY_ALWAYS_INLINE LoggerDB& LoggerDB::get() {
struct Singleton : LoggerDB {
Singleton() {
initializeLoggerDB(*this);
// This allows log handlers to flush any buffered messages before
// the program exits.
/* library-local */ static auto guard =
makeGuard([this] { cleanupHandlers(); });
}
};
// We intentionally leak the LoggerDB singleton and all of the LogCategory
// objects it contains.
//
// We want Logger objects to remain valid for the entire lifetime of the
// program, without having to worry about destruction ordering issues, or
// making the Logger perform reference counting on the LoggerDB.
return detail::createGlobal<Singleton, void>();
}
} // namespace folly
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