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.

125 line
3.3 KiB

  1. #include <gtest/gtest.h>
  2. #include <gmock/gmock.h>
  3. #include <cpputils/logging/matcher.h>
  4. #include <cpputils/logging/consumer.h>
  5. #include <cpputils/logging/logger_impl.h>
  6. using namespace ::testing;
  7. using namespace ::utl::logging;
  8. struct LoggingReset
  9. {
  10. ~LoggingReset()
  11. { reset_logging(); }
  12. };
  13. struct consumer_mock : public consumer
  14. {
  15. MOCK_METHOD1(log, void (data_ptr_s data));
  16. consumer_mock(const std::string& n) :
  17. consumer(n, true)
  18. { }
  19. };
  20. MATCHER_P5(MatchLogData, level, sender, thread, name, message, "")
  21. {
  22. if (!arg)
  23. return false;
  24. auto& d = *arg;
  25. return d.level == level
  26. && d.sender == sender
  27. && d.thread == thread
  28. && d.name == name
  29. && d.message == message;
  30. }
  31. TEST(LoggingTests, matcher_all)
  32. {
  33. LoggingReset loggingReset;
  34. consumer_stream c0("TestConsumer1", std::cout, false, false);
  35. consumer_stream c1("TestConsumer2", std::cout, false, false);
  36. auto& l0 = get_logger();
  37. auto& l1 = get_logger("TestLogger");
  38. matcher_all matcher;
  39. EXPECT_TRUE(matcher.match(c0));
  40. EXPECT_TRUE(matcher.match(c1));
  41. EXPECT_TRUE(matcher.match(l0));
  42. EXPECT_TRUE(matcher.match(l1));
  43. }
  44. TEST(LoggingTests, matcher_default)
  45. {
  46. LoggingReset loggingReset;
  47. consumer_stream c0("TestConsumer1", std::cout, false, false);
  48. consumer_stream c1("TestConsumer2", std::cout, false, false);
  49. auto& l0 = get_logger();
  50. auto& l1 = get_logger("TestLogger");
  51. matcher_default matcher;
  52. EXPECT_FALSE(matcher.match(c0));
  53. EXPECT_FALSE(matcher.match(c1));
  54. EXPECT_TRUE (matcher.match(l0));
  55. EXPECT_FALSE(matcher.match(l1));
  56. }
  57. TEST(LoggingTests, matcher_regex)
  58. {
  59. LoggingReset loggingReset;
  60. consumer_stream c0("TestConsumer1", std::cout, false, false);
  61. consumer_stream c1("TestConsumer2", std::cout, false, false);
  62. auto& l0 = get_logger();
  63. auto& l1 = get_logger("TestLogger");
  64. matcher_regex matcher0("TestConsumer1");
  65. matcher_regex matcher1("ASEF", true);
  66. EXPECT_TRUE (matcher0.match(c0));
  67. EXPECT_FALSE(matcher0.match(c1));
  68. EXPECT_FALSE(matcher1.match(l0));
  69. EXPECT_TRUE (matcher1.match(l1));
  70. }
  71. TEST(LoggingTests, log)
  72. {
  73. LoggingReset loggingReset;
  74. StrictMock<consumer_mock> c0("consumer0");
  75. StrictMock<consumer_mock> c1("Consumer1");
  76. EXPECT_CALL(c0, log(MatchLogData(
  77. log_level::info,
  78. (void*)12,
  79. std::this_thread::get_id(),
  80. std::string("logger0"),
  81. std::string("test1 info"))));
  82. EXPECT_CALL(c0, log(MatchLogData(
  83. log_level::warn,
  84. (void*)13,
  85. std::this_thread::get_id(),
  86. std::string("logger0"),
  87. std::string("test1 warn"))));
  88. define_rule(matcher_ptr_u(new matcher_regex("logger0")), matcher_ptr_u(new matcher_regex("consumer0")), log_level::info, log_level::warn);
  89. auto& l0 = get_logger("logger0");
  90. auto& l1 = get_logger("logger1");
  91. log_message(l0, debug, (void*)11, "test1 ") << "debug";
  92. log_message(l0, info, (void*)12, "test1 ") << "info";
  93. log_message(l0, warn, (void*)13, "test1 ") << "warn";
  94. log_message(l0, error, (void*)14, "test1 ") << "error";
  95. log_message(l1, debug, (void*)21, "test2 ") << "debug";
  96. log_message(l1, info, (void*)22, "test2 ") << "info";
  97. log_message(l1, warn, (void*)23, "test2 ") << "warn";
  98. log_message(l1, error, (void*)24, "test2 ") << "error";
  99. }