Fix copyright lines
[folly.git] / folly / experimental / logging / LogCategory.h
index e589400e910f0ab0f8d460e463259f81679741b2..6ea06cef48d7515b05a175b6f6093f815d91fe4d 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.
@@ -75,6 +75,15 @@ class LogCategory {
         level_.load(std::memory_order_acquire) & ~FLAG_INHERIT);
   }
 
+  /**
+   * Get the log level and inheritance flag.
+   */
+  std::pair<LogLevel, bool> getLevelInfo() const {
+    auto value = level_.load(std::memory_order_acquire);
+    return {static_cast<LogLevel>(value & ~FLAG_INHERIT),
+            bool(value & FLAG_INHERIT)};
+  }
+
   /**
    * Get the effective level for this log category.
    *
@@ -150,24 +159,48 @@ class LogCategory {
    */
   void clearHandlers();
 
+  /**
+   * Get the list of LogHandlers attached to this category.
+   */
+  std::vector<std::shared_ptr<LogHandler>> getHandlers() const;
+
+  /**
+   * Replace the list of LogHandlers with a completely new list.
+   */
+  void replaceHandlers(std::vector<std::shared_ptr<LogHandler>> handlers);
+
+  /**
+   * Update the LogHandlers attached to this LogCategory by replacing
+   * currently attached handlers with new LogHandler objects.
+   *
+   * The handlerMap argument is a map of (old_handler -> new_handler)
+   * If any of the LogHandlers currently attached to this category are found in
+   * the handlerMap, replace them with the new handler indicated in the map.
+   *
+   * This is used when the LogHandler configuration is changed requiring one or
+   * more LogHandler objects to be replaced with new ones.
+   */
+  void updateHandlers(const std::unordered_map<
+                      std::shared_ptr<LogHandler>,
+                      std::shared_ptr<LogHandler>>& handlerMap);
+
   /* Internal methods for use by other parts of the logging library code */
 
   /**
-   * Process a log message.
+   * Admit a message into the LogCategory hierarchy to be logged.
+   *
+   * The caller is responsible for having already performed log level
+   * admittance checks.
    *
    * This method generally should be invoked only through the logging macros,
    * rather than calling this directly.
-   *
-   * This method assumes that log level admittance checks have already been
-   * performed.  This method unconditionally passes the message to the
-   * LogHandlers attached to this LogCategory, without any additional log level
-   * checks (apart from the ones done in the LogHandlers).
    */
-  void processMessage(const LogMessage& message) const;
+  void admitMessage(const LogMessage& message) const;
 
   /**
-   * Note: setLevelLocked() may only be called while holding the main
-   * LoggerDB lock.
+   * Note: setLevelLocked() may only be called while holding the
+   * LoggerDB loggersByName_ lock.  It is safe to call this while holding the
+   * loggersByName_ lock in read-mode; holding it exclusively is not required.
    *
    * This method should only be invoked by LoggerDB.
    */
@@ -188,10 +221,17 @@ class LogCategory {
  private:
   enum : uint32_t { FLAG_INHERIT = 0x80000000 };
 
+  // FLAG_INHERIT is the stored in the uppermost bit of the LogLevel field.
+  // assert that it does not conflict with valid LogLevel values.
+  static_assert(
+      static_cast<uint32_t>(LogLevel::MAX_LEVEL) < FLAG_INHERIT,
+      "The FLAG_INHERIT bit must not be set in any valid LogLevel value");
+
   // Forbidden copy constructor and assignment operator
   LogCategory(LogCategory const&) = delete;
   LogCategory& operator=(LogCategory const&) = delete;
 
+  void processMessage(const LogMessage& message) const;
   void updateEffectiveLevel(LogLevel newEffectiveLevel);
   void parentLevelUpdated(LogLevel parentEffectiveLevel);
 
@@ -236,9 +276,9 @@ class LogCategory {
 
   /**
    * Pointers to children and sibling loggers.
-   * These pointers should only ever be accessed while holding the main
-   * LoggerDB lock.  (These are only modified when creating new loggers,
-   * which occurs with the main LoggerDB lock held.)
+   * These pointers should only ever be accessed while holding the
+   * LoggerDB::loggersByName_ lock.  (These are only modified when creating new
+   * loggers, which occurs with the main LoggerDB lock held.)
    */
   LogCategory* firstChild_{nullptr};
   LogCategory* nextSibling_{nullptr};
@@ -252,4 +292,4 @@ class LogCategory {
    */
   std::vector<std::atomic<LogLevel>*> xlogLevels_;
 };
-}
+} // namespace folly