You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
4.4 KiB

  1. #include <gtest/gtest.h>
  2. #include <gmock/gmock.h>
  3. #include <cpplogging/manager.h>
  4. using namespace ::testing;
  5. using namespace ::cpplogging;
  6. struct LoggingReset
  7. {
  8. ~LoggingReset()
  9. { manager::reset(); }
  10. };
  11. struct consumer_mock
  12. : public consumer
  13. {
  14. MOCK_CONST_METHOD1(write_entry, void (const log_entry_ptr_s& entry));
  15. consumer_mock(const std::string& n) :
  16. consumer(n)
  17. {
  18. manager::register_consumer(*this);
  19. }
  20. };
  21. MATCHER_P5(MatchLogData, level, sender, thread, name, message, "")
  22. {
  23. if (!arg)
  24. return false;
  25. auto& d = *arg;
  26. return d.level == level
  27. && d.sender == sender
  28. && d.thread == thread
  29. && d.name == name
  30. && d.message == message;
  31. }
  32. TEST(LoggingTests, matcher_all)
  33. {
  34. LoggingReset loggingReset;
  35. consumer_stream c0("TestConsumer1", std::cout);
  36. consumer_stream c1("TestConsumer2", std::cout);
  37. auto& l0 = logger::get();
  38. auto& l1 = logger::get("TestLogger");
  39. matcher_all matcher;
  40. EXPECT_TRUE(matcher.match(c0));
  41. EXPECT_TRUE(matcher.match(c1));
  42. EXPECT_TRUE(matcher.match(l0));
  43. EXPECT_TRUE(matcher.match(l1));
  44. }
  45. TEST(LoggingTests, matcher_default)
  46. {
  47. LoggingReset loggingReset;
  48. consumer_stream c0("TestConsumer1", std::cout);
  49. consumer_stream c1("TestConsumer2", std::cout);
  50. auto& l0 = logger::get();
  51. auto& l1 = logger::get("TestLogger");
  52. matcher_default_logger matcher;
  53. EXPECT_FALSE(matcher.match(c0));
  54. EXPECT_FALSE(matcher.match(c1));
  55. EXPECT_TRUE (matcher.match(l0));
  56. EXPECT_FALSE(matcher.match(l1));
  57. }
  58. TEST(LoggingTests, matcher_regex)
  59. {
  60. LoggingReset loggingReset;
  61. consumer_stream c0("TestConsumer1", std::cout);
  62. consumer_stream c1("TestConsumer2", std::cout);
  63. auto& l0 = logger::get();
  64. auto& l1 = logger::get("TestLogger");
  65. matcher_regex matcher0("TestConsumer1");
  66. matcher_regex matcher1("ASEF", true);
  67. EXPECT_TRUE (matcher0.match(c0));
  68. EXPECT_FALSE(matcher0.match(c1));
  69. EXPECT_FALSE(matcher1.match(l0));
  70. EXPECT_TRUE (matcher1.match(l1));
  71. }
  72. TEST(LoggingTests, log_base)
  73. {
  74. LoggingReset loggingReset;
  75. StrictMock<consumer_mock> c0("consumer0");
  76. StrictMock<consumer_mock> c1("Consumer1");
  77. EXPECT_CALL(c0, write_entry(MatchLogData(
  78. log_level::info,
  79. (void*)0x12,
  80. std::this_thread::get_id(),
  81. std::string("logger0"),
  82. std::string("test1 info"))));
  83. EXPECT_CALL(c0, write_entry(MatchLogData(
  84. log_level::warn,
  85. (void*)0x13,
  86. std::this_thread::get_id(),
  87. std::string("logger0"),
  88. std::string("test1 warn"))));
  89. manager::define_rule(
  90. std::make_unique<matcher_regex>("logger0"),
  91. std::make_unique<matcher_regex>("consumer0"),
  92. log_level::info,
  93. log_level::warn);
  94. auto& l0 = logger::get("logger0");
  95. auto& l1 = logger::get("logger1");
  96. cpplogging_log(l0, debug).sender((void*)0x11).message("test1") << " debug";
  97. cpplogging_log(l0, info ).sender((void*)0x12).message("test1") << " info";
  98. cpplogging_log(l0, warn ).sender((void*)0x13).message("test1") << " warn";
  99. cpplogging_log(l0, error).sender((void*)0x14).message("test1") << " error";
  100. cpplogging_log(l1, debug).sender((void*)0x21).message("test2") << " debug";
  101. cpplogging_log(l1, info ).sender((void*)0x22).message("test2") << " info";
  102. cpplogging_log(l1, warn ).sender((void*)0x23).message("test2") << " warn";
  103. cpplogging_log(l1, error).sender((void*)0x24).message("test2") << " error";
  104. }
  105. TEST(LoggingTests, log_inverted_regex)
  106. {
  107. LoggingReset loggingReset;
  108. StrictMock<consumer_mock> c0("consumer0");
  109. EXPECT_CALL(c0, write_entry(MatchLogData(
  110. log_level::debug,
  111. (void*)0x12,
  112. std::this_thread::get_id(),
  113. std::string("logger2"),
  114. std::string("test2"))));
  115. manager::define_rule(
  116. std::make_unique<matcher_regex>("^logger[0-1]+$", true),
  117. std::make_unique<matcher_regex>("consumer0"),
  118. log_level::debug,
  119. log_level::error);
  120. auto& l0 = logger::get("logger0");
  121. auto& l1 = logger::get("logger1");
  122. auto& l2 = logger::get("logger2");
  123. cpplogging_log(l0, debug).sender((void*)0x10) << "test0";
  124. cpplogging_log(l1, debug).sender((void*)0x11) << "test1";
  125. cpplogging_log(l2, debug).sender((void*)0x12) << "test2";
  126. }
  127. #ifdef CPPLOGGING_HAS_LOAD_CONFIG
  128. TEST(LoggingTests, load)
  129. {
  130. manager::load("./cpplogging/test_config.json");
  131. }
  132. #endif