From: Adam Simpkins Date: Thu, 30 Nov 2017 01:35:09 +0000 (-0800) Subject: logging: add a LogHandler::getConfig() method X-Git-Tag: v2017.12.04.00~20 X-Git-Url: http://plrg.eecs.uci.edu/git/?p=folly.git;a=commitdiff_plain;h=95d9935053bd95825ecd84fd647d697df1113daf logging: add a LogHandler::getConfig() method 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 --- diff --git a/folly/experimental/logging/FileHandlerFactory.cpp b/folly/experimental/logging/FileHandlerFactory.cpp index 43d3a18e..93dc13a4 100644 --- a/folly/experimental/logging/FileHandlerFactory.cpp +++ b/folly/experimental/logging/FileHandlerFactory.cpp @@ -119,7 +119,8 @@ std::shared_ptr FileHandlerFactory::createHandler( writer = make_shared(std::move(outputFile)); } - return make_shared(formatter, writer); + return make_shared( + LogHandlerConfig{getType(), options}, formatter, writer); } } // namespace folly diff --git a/folly/experimental/logging/Init.cpp b/folly/experimental/logging/Init.cpp index 3c6c3957..d1bc050f 100644 --- a/folly/experimental/logging/Init.cpp +++ b/folly/experimental/logging/Init.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include @@ -51,13 +52,16 @@ void initLoggingGlogStyle( // Create the LogHandler std::shared_ptr writer; folly::File file{STDERR_FILENO, false}; + LogHandlerConfig handlerConfig{"file", {{"stream", "stderr"}}}; if (asyncWrites) { writer = std::make_shared(std::move(file)); + handlerConfig.options.emplace("async", "true"); } else { writer = std::make_shared(std::move(file)); + handlerConfig.options.emplace("async", "false"); } auto handler = std::make_shared( - std::make_shared(), std::move(writer)); + handlerConfig, std::make_shared(), std::move(writer)); // Add the handler to the root category. LoggerDB::get()->getCategory(".")->addHandler(std::move(handler)); diff --git a/folly/experimental/logging/LogHandler.h b/folly/experimental/logging/LogHandler.h index 525eea4d..8791ab07 100644 --- a/folly/experimental/logging/LogHandler.h +++ b/folly/experimental/logging/LogHandler.h @@ -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 diff --git a/folly/experimental/logging/StandardLogHandler.cpp b/folly/experimental/logging/StandardLogHandler.cpp index 9e43cd6d..00019aba 100644 --- a/folly/experimental/logging/StandardLogHandler.cpp +++ b/folly/experimental/logging/StandardLogHandler.cpp @@ -22,9 +22,12 @@ namespace folly { StandardLogHandler::StandardLogHandler( + LogHandlerConfig config, std::shared_ptr formatter, std::shared_ptr 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 diff --git a/folly/experimental/logging/StandardLogHandler.h b/folly/experimental/logging/StandardLogHandler.h index 3a1e4e48..3ca5ef98 100644 --- a/folly/experimental/logging/StandardLogHandler.h +++ b/folly/experimental/logging/StandardLogHandler.h @@ -20,6 +20,7 @@ #include #include #include +#include namespace folly { @@ -40,6 +41,7 @@ class LogWriter; class StandardLogHandler : public LogHandler { public: StandardLogHandler( + LogHandlerConfig config, std::shared_ptr formatter, std::shared_ptr writer); ~StandardLogHandler(); @@ -83,16 +85,19 @@ class StandardLogHandler : public LogHandler { void flush() override; + LogHandlerConfig getConfig() const override; + private: std::atomic 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 formatter_; const std::shared_ptr writer_; + const LogHandlerConfig config_; }; } // namespace folly diff --git a/folly/experimental/logging/test/StandardLogHandlerTest.cpp b/folly/experimental/logging/test/StandardLogHandlerTest.cpp index 2c9a65b5..8fcdf3da 100644 --- a/folly/experimental/logging/test/StandardLogHandlerTest.cpp +++ b/folly/experimental/logging/test/StandardLogHandlerTest.cpp @@ -13,14 +13,16 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include + #include #include #include +#include #include #include #include #include -#include #include using namespace folly; @@ -69,7 +71,8 @@ class TestLogWriter : public LogWriter { TEST(StandardLogHandler, simple) { auto writer = make_shared(); - StandardLogHandler handler(make_shared(), writer); + LogHandlerConfig config{"std_test"}; + StandardLogHandler handler(config, make_shared(), 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(); - StandardLogHandler handler(make_shared(), writer); + LogHandlerConfig config{"std_test"}; + StandardLogHandler handler(config, make_shared(), writer); LoggerDB db{LoggerDB::TESTING}; auto logCategory = db.getCategory("log_cat"); diff --git a/folly/experimental/logging/test/TestLogHandler.h b/folly/experimental/logging/test/TestLogHandler.h index c2dd00b9..6fa21733 100644 --- a/folly/experimental/logging/test/TestLogHandler.h +++ b/folly/experimental/logging/test/TestLogHandler.h @@ -15,10 +15,13 @@ */ #pragma once +#include +#include #include #include #include +#include #include 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>& getMessages() { return messages_; } @@ -49,8 +56,14 @@ class TestLogHandler : public LogHandler { return flushCount_; } + LogHandlerConfig getConfig() const override { + return config_; + } + private: std::vector> messages_; uint64_t flushCount_{0}; + std::map options_; + LogHandlerConfig config_; }; } // namespace folly