logging: add a LogHandler::getConfig() method
authorAdam Simpkins <simpkins@fb.com>
Thu, 30 Nov 2017 01:35:09 +0000 (17:35 -0800)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 30 Nov 2017 01:51:07 +0000 (17:51 -0800)
Summary:
Add a method to LogHandler to return its current configuration.  This will
make it possible to query the LoggerDB for its current configuration state.

Reviewed By: bolinfest

Differential Revision: D6200563

fbshipit-source-id: 2b8b9752bbeb26c8aac28d1a73b7e2312fd198c8

folly/experimental/logging/FileHandlerFactory.cpp
folly/experimental/logging/Init.cpp
folly/experimental/logging/LogHandler.h
folly/experimental/logging/StandardLogHandler.cpp
folly/experimental/logging/StandardLogHandler.h
folly/experimental/logging/test/StandardLogHandlerTest.cpp
folly/experimental/logging/test/TestLogHandler.h

index 43d3a18e94a8cada583637987fa6fb511189ebd8..93dc13a4d1db504cd92c1da7ddf20496731f165a 100644 (file)
@@ -119,7 +119,8 @@ std::shared_ptr<LogHandler> FileHandlerFactory::createHandler(
     writer = make_shared<ImmediateFileWriter>(std::move(outputFile));
   }
 
-  return make_shared<StandardLogHandler>(formatter, writer);
+  return make_shared<StandardLogHandler>(
+      LogHandlerConfig{getType(), options}, formatter, writer);
 }
 
 } // namespace folly
index 3c6c3957ab30e3541be37e3b0f2b16aa321f7e96..d1bc050feadc89bd69f2c160bc1b1ce77f541127 100644 (file)
@@ -19,6 +19,7 @@
 #include <folly/experimental/logging/GlogStyleFormatter.h>
 #include <folly/experimental/logging/ImmediateFileWriter.h>
 #include <folly/experimental/logging/LogCategory.h>
+#include <folly/experimental/logging/LogHandlerConfig.h>
 #include <folly/experimental/logging/LoggerDB.h>
 #include <folly/experimental/logging/StandardLogHandler.h>
 
@@ -51,13 +52,16 @@ void initLoggingGlogStyle(
   // Create the LogHandler
   std::shared_ptr<LogWriter> writer;
   folly::File file{STDERR_FILENO, false};
+  LogHandlerConfig handlerConfig{"file", {{"stream", "stderr"}}};
   if (asyncWrites) {
     writer = std::make_shared<AsyncFileWriter>(std::move(file));
+    handlerConfig.options.emplace("async", "true");
   } else {
     writer = std::make_shared<ImmediateFileWriter>(std::move(file));
+    handlerConfig.options.emplace("async", "false");
   }
   auto handler = std::make_shared<StandardLogHandler>(
-      std::make_shared<GlogStyleFormatter>(), std::move(writer));
+      handlerConfig, std::make_shared<GlogStyleFormatter>(), std::move(writer));
 
   // Add the handler to the root category.
   LoggerDB::get()->getCategory(".")->addHandler(std::move(handler));
index 525eea4dff3006888b277b4cc42e57c78e67807b..8791ab0724c9c45c0c6c498e2efef580d99bf3f6 100644 (file)
@@ -22,6 +22,7 @@
 namespace folly {
 
 class LogCategory;
+class LogHandlerConfig;
 class LogMessage;
 
 /**
@@ -79,5 +80,11 @@ class LogHandler {
    * started will not necessarily be processed by the flush call.
    */
   virtual void flush() = 0;
+
+  /**
+   * Return a LogHandlerConfig object describing the configuration of this
+   * LogHandler.
+   */
+  virtual LogHandlerConfig getConfig() const = 0;
 };
 } // namespace folly
index 9e43cd6da84806d7f78fb9df71217792fe1cbb17..00019aba15d3255be65f28ce32f9b007f9d7c7f4 100644 (file)
 namespace folly {
 
 StandardLogHandler::StandardLogHandler(
+    LogHandlerConfig config,
     std::shared_ptr<LogFormatter> formatter,
     std::shared_ptr<LogWriter> writer)
-    : formatter_{std::move(formatter)}, writer_{std::move(writer)} {}
+    : formatter_{std::move(formatter)},
+      writer_{std::move(writer)},
+      config_{config} {}
 
 StandardLogHandler::~StandardLogHandler() {}
 
@@ -40,4 +43,9 @@ void StandardLogHandler::handleMessage(
 void StandardLogHandler::flush() {
   writer_->flush();
 }
+
+LogHandlerConfig StandardLogHandler::getConfig() const {
+  return config_;
+}
+
 } // namespace folly
index 3a1e4e48db857cbcfbabf1a6d54fe99eab00029a..3ca5ef98b1992ddf2538ec94f567a4fb88691a0d 100644 (file)
@@ -20,6 +20,7 @@
 #include <folly/File.h>
 #include <folly/Range.h>
 #include <folly/experimental/logging/LogHandler.h>
+#include <folly/experimental/logging/LogHandlerConfig.h>
 
 namespace folly {
 
@@ -40,6 +41,7 @@ class LogWriter;
 class StandardLogHandler : public LogHandler {
  public:
   StandardLogHandler(
+      LogHandlerConfig config,
       std::shared_ptr<LogFormatter> formatter,
       std::shared_ptr<LogWriter> writer);
   ~StandardLogHandler();
@@ -83,16 +85,19 @@ class StandardLogHandler : public LogHandler {
 
   void flush() override;
 
+  LogHandlerConfig getConfig() const override;
+
  private:
   std::atomic<LogLevel> level_{LogLevel::NONE};
 
-  // The formatter_ and writer_ member variables are const, and cannot be
-  // modified after the StandardLogHandler is constructed.  This allows them to
-  // be accessed without locking when handling a message.  To change these
-  // values, create a new StandardLogHandler object and replace the old handler
-  // with the new one in the LoggerDB.
+  // The following variables are const, and cannot be modified after the
+  // log handler is constructed.  This allows us to access them without
+  // locking when handling a message.  To change these values, create a new
+  // StandardLogHandler object and replace the old handler with the new one in
+  // the LoggerDB.
 
   const std::shared_ptr<LogFormatter> formatter_;
   const std::shared_ptr<LogWriter> writer_;
+  const LogHandlerConfig config_;
 };
 } // namespace folly
index 2c9a65b57dd3422ee20138e1f900a9660d18b22b..8fcdf3da5f6bb39fe410c0d0c54724dd9bdb59d3 100644 (file)
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
+#include <folly/experimental/logging/StandardLogHandler.h>
+
 #include <folly/Conv.h>
 #include <folly/experimental/logging/LogCategory.h>
 #include <folly/experimental/logging/LogFormatter.h>
+#include <folly/experimental/logging/LogHandlerConfig.h>
 #include <folly/experimental/logging/LogLevel.h>
 #include <folly/experimental/logging/LogMessage.h>
 #include <folly/experimental/logging/LogWriter.h>
 #include <folly/experimental/logging/LoggerDB.h>
-#include <folly/experimental/logging/StandardLogHandler.h>
 #include <folly/portability/GTest.h>
 
 using namespace folly;
@@ -69,7 +71,8 @@ class TestLogWriter : public LogWriter {
 
 TEST(StandardLogHandler, simple) {
   auto writer = make_shared<TestLogWriter>();
-  StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
+  LogHandlerConfig config{"std_test"};
+  StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
 
   LoggerDB db{LoggerDB::TESTING};
   auto logCategory = db.getCategory("log_cat");
@@ -89,7 +92,8 @@ TEST(StandardLogHandler, simple) {
 
 TEST(StandardLogHandler, levelCheck) {
   auto writer = make_shared<TestLogWriter>();
-  StandardLogHandler handler(make_shared<TestLogFormatter>(), writer);
+  LogHandlerConfig config{"std_test"};
+  StandardLogHandler handler(config, make_shared<TestLogFormatter>(), writer);
 
   LoggerDB db{LoggerDB::TESTING};
   auto logCategory = db.getCategory("log_cat");
index c2dd00b9169ccade466bbf12183ebb5afa64ac6a..6fa2173377a9be9aa27840b8bbcad9bbdad3f618 100644 (file)
  */
 #pragma once
 
+#include <map>
+#include <string>
 #include <utility>
 #include <vector>
 
 #include <folly/experimental/logging/LogHandler.h>
+#include <folly/experimental/logging/LogHandlerConfig.h>
 #include <folly/experimental/logging/LogMessage.h>
 
 namespace folly {
@@ -31,6 +34,10 @@ namespace folly {
  */
 class TestLogHandler : public LogHandler {
  public:
+  TestLogHandler() : config_{"test"} {}
+  explicit TestLogHandler(LogHandlerConfig config)
+      : config_{std::move(config)} {}
+
   std::vector<std::pair<LogMessage, const LogCategory*>>& getMessages() {
     return messages_;
   }
@@ -49,8 +56,14 @@ class TestLogHandler : public LogHandler {
     return flushCount_;
   }
 
+  LogHandlerConfig getConfig() const override {
+    return config_;
+  }
+
  private:
   std::vector<std::pair<LogMessage, const LogCategory*>> messages_;
   uint64_t flushCount_{0};
+  std::map<std::string, std::string> options_;
+  LogHandlerConfig config_;
 };
 } // namespace folly