Fix copyright lines
[folly.git] / folly / experimental / logging / LoggerDB.h
index c2c1d423f226016a6cf2b5a95c6fa6ca746fbb6e..ac5a492136522215b6f2ada1b80b076a76056998 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.
@@ -29,6 +29,7 @@
 namespace folly {
 
 class LogCategory;
+class LogConfig;
 class LogHandler;
 class LogHandlerFactory;
 enum class LogLevel : uint32_t;
@@ -77,16 +78,38 @@ class LoggerDB {
   void setLevel(LogCategory* category, LogLevel level, bool inherit = true);
 
   /**
-   * Apply a configuration string specifying a series a log levels.
+   * Get a LogConfig object describing the current state of the LoggerDB.
    *
-   * The string format is a comma separated list of <name>=<level> sections.
-   * e.g.: "foo=DBG3,log.bar=WARN"
+   * Note that this may not 100% accurately describe the current configuration
+   * if callers have manually added LogHandlers to some categories without
+   * using the updateConfig() or resetConfig() functions.  In this case
+   * getConfig() will simply report these handlers as "unknown_handler" when
+   * returning handler names for the categories in question.
+   */
+  LogConfig getConfig() const;
+
+  /**
+   * Update the current LoggerDB state with the specified LogConfig settings.
+   *
+   * Log categories and handlers listed in the LogConfig object will be updated
+   * to the new state listed in the LogConfig.  Settings on categories and
+   * handlers not listed in the config will be left as-is.
+   */
+  void updateConfig(const LogConfig& config);
+
+  /**
+   * Reset the current LoggerDB state to the specified LogConfig settings.
+   *
+   * All LogCategories not mentioned in the new LogConfig will have all
+   * currently configured log handlers removed and their log level set to its
+   * default state.  For the root category the default log level is ERR; for
+   * all other categories the default level is MAX_LEVEL with log level
+   * inheritance enabled.
    *
-   * Returns a list of error messages for each error encountered trying to
-   * parse the config string.  The return value will be an empty vector if no
-   * errors were encountered.
+   * LogCategories listed in the new config but without LogHandler information
+   * defined will have all existing handlers removed.
    */
-  std::vector<std::string> processConfigString(folly::StringPiece config);
+  void resetConfig(const LogConfig& config);
 
   /**
    * Remove all registered LogHandlers on all LogCategory objects.
@@ -225,6 +248,24 @@ class LoggerDB {
       folly::StringPiece name,
       LogCategory* parent);
 
+  using NewHandlerMap =
+      std::unordered_map<std::string, std::shared_ptr<LogHandler>>;
+  using OldToNewHandlerMap = std::
+      unordered_map<std::shared_ptr<LogHandler>, std::shared_ptr<LogHandler>>;
+  void startConfigUpdate(
+      const Synchronized<HandlerInfo>::LockedPtr& handlerInfo,
+      const LogConfig& config,
+      NewHandlerMap* handlers,
+      OldToNewHandlerMap* oldToNewHandlerMap);
+  void finishConfigUpdate(
+      const Synchronized<HandlerInfo>::LockedPtr& handlerInfo,
+      NewHandlerMap* handlers,
+      OldToNewHandlerMap* oldToNewHandlerMap);
+  std::vector<std::shared_ptr<LogHandler>> buildCategoryHandlerList(
+      const NewHandlerMap& handlerMap,
+      StringPiece categoryName,
+      const std::vector<std::string>& categoryHandlerNames);
+
   static void internalWarningImpl(
       folly::StringPiece filename,
       int lineNumber,
@@ -244,6 +285,9 @@ class LoggerDB {
 
   /**
    * The LogHandlers and LogHandlerFactories.
+   *
+   * For lock ordering purposes, if you need to acquire both the loggersByName_
+   * and handlerInfo_ locks, the handlerInfo_ lock must be acquired first.
    */
   folly::Synchronized<HandlerInfo> handlerInfo_;