logging: add initialization convenience functions
authorAdam Simpkins <simpkins@fb.com>
Thu, 15 Jun 2017 18:04:00 +0000 (11:04 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 15 Jun 2017 18:06:06 +0000 (11:06 -0700)
Summary:
Add a logging/Init.h header file with a couple convenience functions for
initializing log levels and log handlers.

This is pretty basic for now, but simplifies usage for small programs that just
want to easily initialize the logging library.

Reviewed By: wez

Differential Revision: D5083106

fbshipit-source-id: 73c1fd00df2eaf506b9c1485d6afd12570412a0f

folly/Makefile.am
folly/experimental/logging/Init.cpp [new file with mode: 0644]
folly/experimental/logging/Init.h [new file with mode: 0644]
folly/experimental/logging/Makefile.am

index 734be3ddc8766e44d487262dbd6c55db9cf96fa2..558a48596139771dd507c0a27180180364415849 100644 (file)
@@ -124,6 +124,7 @@ nobase_follyinclude_HEADERS = \
        experimental/logging/AsyncFileWriter.h \
        experimental/logging/GlogStyleFormatter.h \
        experimental/logging/ImmediateFileWriter.h \
+       experimental/logging/Init.h \
        experimental/logging/LogCategory.h \
        experimental/logging/LogFormatter.h \
        experimental/logging/Logger.h \
diff --git a/folly/experimental/logging/Init.cpp b/folly/experimental/logging/Init.cpp
new file mode 100644 (file)
index 0000000..d1e6a6a
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * Copyright 2004-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.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#include <folly/experimental/logging/Init.h>
+
+#include <folly/experimental/logging/AsyncFileWriter.h>
+#include <folly/experimental/logging/GlogStyleFormatter.h>
+#include <folly/experimental/logging/ImmediateFileWriter.h>
+#include <folly/experimental/logging/LogCategory.h>
+#include <folly/experimental/logging/LoggerDB.h>
+#include <folly/experimental/logging/StandardLogHandler.h>
+
+using std::shared_ptr;
+using std::string;
+using std::vector;
+
+namespace folly {
+
+void initLogLevels(StringPiece configString, LogLevel defaultRootLevel) {
+  // Set the default root category log level first
+  LoggerDB::get()->getCategory(".")->setLevel(defaultRootLevel);
+
+  // Then apply the configuration string
+  if (!configString.empty()) {
+    auto ret = LoggerDB::get()->processConfigString(configString);
+    if (!ret.empty()) {
+      throw LoggingConfigError(ret);
+    }
+  }
+}
+
+void initLoggingGlogStyle(
+    StringPiece configString,
+    LogLevel defaultRootLevel,
+    bool asyncWrites) {
+  // Configure log levels
+  initLogLevels(configString, defaultRootLevel);
+
+  // Create the LogHandler
+  std::shared_ptr<LogWriter> writer;
+  folly::File file{STDERR_FILENO, false};
+  if (asyncWrites) {
+    writer = std::make_shared<AsyncFileWriter>(std::move(file));
+  } else {
+    writer = std::make_shared<ImmediateFileWriter>(std::move(file));
+  }
+  auto handler = std::make_shared<StandardLogHandler>(
+      std::make_shared<GlogStyleFormatter>(), std::move(writer));
+
+  // Add the handler to the root category.
+  LoggerDB::get()->getCategory(".")->addHandler(std::move(handler));
+}
+
+LoggingConfigError::LoggingConfigError(const vector<string>& errors)
+    : invalid_argument{computeMessage(errors)} {}
+
+std::string LoggingConfigError::computeMessage(const vector<string>& errors) {
+  string msg = "error parsing logging configuration:";
+  for (const auto& error : errors) {
+    msg += "\n" + error;
+  }
+  return msg;
+}
+}
diff --git a/folly/experimental/logging/Init.h b/folly/experimental/logging/Init.h
new file mode 100644 (file)
index 0000000..5491974
--- /dev/null
@@ -0,0 +1,65 @@
+/*
+ * Copyright 2004-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.
+ * You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+#pragma once
+
+/*
+ * This file contains function to help configure the logging library behavior
+ * during program start-up.
+ */
+
+#include <stdexcept>
+#include <string>
+#include <vector>
+
+#include <folly/Range.h>
+#include <folly/experimental/logging/LogLevel.h>
+
+namespace folly {
+
+/**
+ * Configure log category levels based on a configuration string.
+ *
+ * This can be used to process a logging configuration string (such as received
+ * via a command line flag) during program start-up.
+ */
+void initLogLevels(
+    folly::StringPiece configString = "",
+    LogLevel defaultRootLevel = LogLevel::WARNING);
+
+/**
+ * Initialize the logging library to write glog-style messages to stderr.
+ *
+ * This initializes the log category levels as specified (using
+ * initLogLevels()), and adds a log handler that prints messages in glog-style
+ * format to stderr.
+ */
+void initLoggingGlogStyle(
+    folly::StringPiece configString = "",
+    LogLevel defaultRootLevel = LogLevel::WARNING,
+    bool asyncWrites = true);
+
+/**
+ * LoggingConfigError may be thrown by initLogLevels() if an error occurs
+ * parsing the configuration string.
+ */
+class LoggingConfigError : public std::invalid_argument {
+ public:
+  explicit LoggingConfigError(const std::vector<std::string>& errors);
+
+ private:
+  std::string computeMessage(const std::vector<std::string>& errors);
+};
+}
index c3fc0219ebc2f431d433fa6315e6ccb814079836..bd38e611f830f13ad369f77ad8f2cb915382604c 100644 (file)
@@ -6,6 +6,7 @@ libfollylogging_la_SOURCES = \
        AsyncFileWriter.cpp \
        GlogStyleFormatter.cpp \
        ImmediateFileWriter.cpp \
+       Init.cpp \
        LogCategory.cpp \
        Logger.cpp \
        LoggerDB.cpp \