TestLogHandler.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. * Copyright 2017-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. #include <map>
  18. #include <string>
  19. #include <utility>
  20. #include <vector>
  21. #include <folly/logging/LogHandler.h>
  22. #include <folly/logging/LogHandlerConfig.h>
  23. #include <folly/logging/LogHandlerFactory.h>
  24. #include <folly/logging/LogMessage.h>
  25. namespace folly {
  26. /**
  27. * A LogHandler that simply keeps a vector of all LogMessages it receives.
  28. *
  29. * This class is not thread-safe. It is intended to be used in single-threaded
  30. * tests.
  31. */
  32. class TestLogHandler : public LogHandler {
  33. public:
  34. using Options = LogHandlerConfig::Options;
  35. TestLogHandler() : config_{"test"} {}
  36. explicit TestLogHandler(LogHandlerConfig config)
  37. : config_{std::move(config)} {}
  38. std::vector<std::pair<LogMessage, const LogCategory*>>& getMessages() {
  39. return messages_;
  40. }
  41. std::vector<std::string> getMessageValues() const;
  42. void clearMessages() {
  43. messages_.clear();
  44. }
  45. void handleMessage(
  46. const LogMessage& message,
  47. const LogCategory* handlerCategory) override {
  48. messages_.emplace_back(message, handlerCategory);
  49. }
  50. void flush() override {
  51. ++flushCount_;
  52. }
  53. uint64_t getFlushCount() const {
  54. return flushCount_;
  55. }
  56. LogHandlerConfig getConfig() const override {
  57. return config_;
  58. }
  59. void setOptions(const Options& options) {
  60. config_.options = options;
  61. }
  62. protected:
  63. std::vector<std::pair<LogMessage, const LogCategory*>> messages_;
  64. uint64_t flushCount_{0};
  65. std::map<std::string, std::string> options_;
  66. LogHandlerConfig config_;
  67. };
  68. /**
  69. * A LogHandlerFactory to create TestLogHandler objects.
  70. */
  71. class TestLogHandlerFactory : public LogHandlerFactory {
  72. public:
  73. explicit TestLogHandlerFactory(StringPiece type) : type_{type.str()} {}
  74. StringPiece getType() const override {
  75. return type_;
  76. }
  77. std::shared_ptr<LogHandler> createHandler(const Options& options) override;
  78. std::shared_ptr<LogHandler> updateHandler(
  79. const std::shared_ptr<LogHandler>& existingHandler,
  80. const Options& options) override;
  81. private:
  82. std::string type_;
  83. };
  84. } // namespace folly