Adds writer test case for RCU
[folly.git] / folly / experimental / logging / LogCategory.cpp
index 66640df37a4645dc2472b31203fedb72afae4828..4cc76670d10b1a804a4480899bdb1c8bd61a850d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2004-present Facebook, Inc.
+ * Copyright 2017-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.
 #include <cstdio>
 #include <cstdlib>
 
+#include <folly/ConstexprMath.h>
 #include <folly/ExceptionString.h>
 #include <folly/FileUtil.h>
+#include <folly/MapUtil.h>
 #include <folly/experimental/logging/LogHandler.h>
 #include <folly/experimental/logging/LogMessage.h>
 #include <folly/experimental/logging/LogName.h>
@@ -143,6 +145,23 @@ std::vector<std::shared_ptr<LogHandler>> LogCategory::getHandlers() const {
   return *(handlers_.rlock());
 }
 
+void LogCategory::replaceHandlers(
+    std::vector<std::shared_ptr<LogHandler>> handlers) {
+  return handlers_.wlock()->swap(handlers);
+}
+
+void LogCategory::updateHandlers(const std::unordered_map<
+                                 std::shared_ptr<LogHandler>,
+                                 std::shared_ptr<LogHandler>>& handlerMap) {
+  auto handlers = handlers_.wlock();
+  for (auto& entry : *handlers) {
+    auto* ptr = get_ptr(handlerMap, entry);
+    if (ptr) {
+      entry = *ptr;
+    }
+  }
+}
+
 void LogCategory::setLevel(LogLevel level, bool inherit) {
   // We have to set the level through LoggerDB, since we require holding
   // the LoggerDB lock to iterate through our children in case our effective
@@ -155,16 +174,7 @@ void LogCategory::setLevelLocked(LogLevel level, bool inherit) {
   //
   // This makes sure that UNINITIALIZED is always less than any valid level
   // value, and that level values cannot conflict with our flag bits.
-  //
-  // In debug builds we clamp the maximum to DFATAL rather than MAX_LEVEL
-  // (FATAL), to ensure that fatal log messages can never be disabled.
-  constexpr LogLevel maxLogLevel =
-      kIsDebug ? LogLevel::DFATAL : LogLevel::MAX_LEVEL;
-  if (level > maxLogLevel) {
-    level = maxLogLevel;
-  } else if (level < LogLevel::MIN_LEVEL) {
-    level = LogLevel::MIN_LEVEL;
-  }
+  level = constexpr_clamp(level, LogLevel::MIN_LEVEL, LogLevel::MAX_LEVEL);
 
   // Make sure the inherit flag is always off for the root logger.
   if (!parent_) {