log_buffer_unittest.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include "config.h"
  15. #include <gtest/gtest.h>
  16. #include <log/macros.h>
  17. #include <log/logger_support.h>
  18. #include <log/log_messages.h>
  19. #include <log/log_buffer.h>
  20. #include <log4cplus/loggingmacros.h>
  21. #include <log4cplus/logger.h>
  22. #include <log4cplus/nullappender.h>
  23. #include <log4cplus/spi/loggingevent.h>
  24. using namespace isc::log;
  25. namespace isc {
  26. namespace log {
  27. class LogBufferTest : public ::testing::Test {
  28. protected:
  29. LogBufferTest() : appender1(new BufferAppender(buffer1)),
  30. appender2(new BufferAppender(buffer2)),
  31. logger(log4cplus::Logger::getInstance("buffer"))
  32. {
  33. logger.setLogLevel(log4cplus::TRACE_LOG_LEVEL);
  34. }
  35. ~LogBufferTest() {
  36. // If any log messages are left, we don't care, get rid of them,
  37. // by flushing them to a null appender
  38. // Given the 'messages should not get lost' approach of the logging
  39. // system, not flushing them to a null appender would cause them
  40. // to be dumped to stdout as the test is destroyed, making
  41. // unnecessarily messy test output.
  42. log4cplus::SharedAppenderPtr null_appender(
  43. new log4cplus::NullAppender());
  44. logger.removeAllAppenders();
  45. logger.addAppender(null_appender);
  46. buffer1.flush();
  47. buffer2.flush();
  48. }
  49. LogBuffer buffer1;
  50. LogBuffer buffer2;
  51. log4cplus::SharedAppenderPtr appender1;
  52. log4cplus::SharedAppenderPtr appender2;
  53. log4cplus::Logger logger;
  54. };
  55. // Test that log events are indeed stored, and that they are
  56. // flushed to the new appenders of their logger
  57. TEST_F(LogBufferTest, flush) {
  58. ASSERT_EQ(0, buffer1.getBufferSize());
  59. ASSERT_EQ(0, buffer2.getBufferSize());
  60. // Create a Logger, log a few messages with the first appender
  61. logger.addAppender(appender1);
  62. LOG4CPLUS_INFO(logger, "Foo");
  63. ASSERT_EQ(1, buffer1.getBufferSize());
  64. LOG4CPLUS_INFO(logger, "Foo");
  65. ASSERT_EQ(2, buffer1.getBufferSize());
  66. LOG4CPLUS_INFO(logger, "Foo");
  67. ASSERT_EQ(3, buffer1.getBufferSize());
  68. // Second buffer should still be empty
  69. ASSERT_EQ(0, buffer2.getBufferSize());
  70. // Replace the appender by the second one, and call flush;
  71. // this should cause all events to be moved to the second buffer
  72. logger.removeAllAppenders();
  73. logger.addAppender(appender2);
  74. buffer1.flush();
  75. ASSERT_EQ(0, buffer1.getBufferSize());
  76. ASSERT_EQ(3, buffer2.getBufferSize());
  77. }
  78. // Once flushed, logging new messages with the same buffer should fail
  79. TEST_F(LogBufferTest, addAfterFlush) {
  80. logger.addAppender(appender1);
  81. buffer1.flush();
  82. EXPECT_THROW(LOG4CPLUS_INFO(logger, "Foo"), LogBufferAddAfterFlush);
  83. // It should not have been added
  84. ASSERT_EQ(0, buffer1.getBufferSize());
  85. // But logging should work again as long as a different buffer is used
  86. logger.removeAllAppenders();
  87. logger.addAppender(appender2);
  88. LOG4CPLUS_INFO(logger, "Foo");
  89. ASSERT_EQ(1, buffer2.getBufferSize());
  90. }
  91. TEST_F(LogBufferTest, addDirectly) {
  92. // A few direct calls
  93. log4cplus::spi::InternalLoggingEvent event("buffer",
  94. log4cplus::INFO_LOG_LEVEL,
  95. "Bar", "file", 123);
  96. buffer1.add(event);
  97. ASSERT_EQ(1, buffer1.getBufferSize());
  98. // Do one from a smaller scope to make sure destruction doesn't harm
  99. {
  100. log4cplus::spi::InternalLoggingEvent event2("buffer",
  101. log4cplus::INFO_LOG_LEVEL,
  102. "Bar", "file", 123);
  103. buffer1.add(event2);
  104. }
  105. ASSERT_EQ(2, buffer1.getBufferSize());
  106. // And flush them to the next
  107. logger.removeAllAppenders();
  108. logger.addAppender(appender2);
  109. buffer1.flush();
  110. ASSERT_EQ(0, buffer1.getBufferSize());
  111. ASSERT_EQ(2, buffer2.getBufferSize());
  112. }
  113. }
  114. }