X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=blobdiff_plain;f=folly%2Fexperimental%2Flogging%2FLoggerDB.cpp;h=953570b485c21ed5db10a5c213ca871193c1f2d7;hp=bf887144669c5badf94b2ad780e94a2ce0db2f34;hb=9e55caa86b1d838d9962442255363f37a2ee49fc;hpb=1d83c51f9df5c66b3a61048efbbe4ab3d97cf959 diff --git a/folly/experimental/logging/LoggerDB.cpp b/folly/experimental/logging/LoggerDB.cpp index bf887144..953570b4 100644 --- a/folly/experimental/logging/LoggerDB.cpp +++ b/folly/experimental/logging/LoggerDB.cpp @@ -170,4 +170,49 @@ void LoggerDB::cleanupHandlers() { category->clearHandlers(); } } + +LogLevel LoggerDB::xlogInit( + StringPiece categoryName, + std::atomic* xlogCategoryLevel, + LogCategory** xlogCategory) { + // Hold the lock for the duration of the operation + // xlogInit() may be called from multiple threads simultaneously. + // Only one needs to perform the initialization. + auto loggersByName = loggersByName_.wlock(); + if (xlogCategory != nullptr && *xlogCategory != nullptr) { + // The xlogCategory was already initialized before we acquired the lock + return (*xlogCategory)->getEffectiveLevel(); + } + + auto* category = getOrCreateCategoryLocked(*loggersByName, categoryName); + if (xlogCategory) { + // Set *xlogCategory before we update xlogCategoryLevel below. + // This is important, since the XLOG() macros check xlogCategoryLevel to + // tell if *xlogCategory has been initialized yet. + *xlogCategory = category; + } + auto level = category->getEffectiveLevel(); + xlogCategoryLevel->store(level, std::memory_order_release); + category->registerXlogLevel(xlogCategoryLevel); + return level; +} + +LogCategory* LoggerDB::xlogInitCategory( + StringPiece categoryName, + LogCategory** xlogCategory, + std::atomic* isInitialized) { + // Hold the lock for the duration of the operation + // xlogInitCategory() may be called from multiple threads simultaneously. + // Only one needs to perform the initialization. + auto loggersByName = loggersByName_.wlock(); + if (isInitialized->load(std::memory_order_acquire)) { + // The xlogCategory was already initialized before we acquired the lock + return *xlogCategory; + } + + auto* category = getOrCreateCategoryLocked(*loggersByName, categoryName); + *xlogCategory = category; + isInitialized->store(true, std::memory_order_release); + return category; +} }