logger_unittest.cc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. // Copyright (C) 2011 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 <iostream>
  15. #include <string>
  16. #include <gtest/gtest.h>
  17. #include <log/logger.h>
  18. #include <log/logger_manager.h>
  19. #include <log/logger_name.h>
  20. #include <log/messagedef.h>
  21. using namespace isc;
  22. using namespace isc::log;
  23. using namespace std;
  24. namespace {
  25. string ROOT_NAME = "loggertest";
  26. }
  27. /// \brief Logger Test
  28. ///
  29. /// As the logger is only a shell around the implementation, this tests also
  30. /// checks the logger implementation class as well.
  31. class LoggerTest : public ::testing::Test {
  32. public:
  33. LoggerTest() {
  34. LoggerManager::init(ROOT_NAME);
  35. }
  36. ~LoggerTest() {
  37. LoggerManager::reset();
  38. }
  39. };
  40. // Checks that the logger is named correctly.
  41. TEST_F(LoggerTest, Name) {
  42. // Create a logger
  43. Logger logger("alpha");
  44. // ... and check the name
  45. EXPECT_EQ(ROOT_NAME + string(".alpha"), logger.getName());
  46. }
  47. // This test attempts to get two instances of a logger with the same name
  48. // and checks that they are in fact the same logger.
  49. TEST_F(LoggerTest, GetLogger) {
  50. const string name1 = "alpha";
  51. const string name2 = "beta";
  52. // Instantiate two loggers that should be the same
  53. Logger logger1(name1);
  54. Logger logger2(name1);
  55. // And check they equal
  56. EXPECT_TRUE(logger1 == logger2);
  57. // Instantiate another logger with another name and check that it
  58. // is different to the previously instantiated ones.
  59. Logger logger3(name2);
  60. EXPECT_FALSE(logger1 == logger3);
  61. }
  62. // Check that the logger levels are get set properly.
  63. TEST_F(LoggerTest, Severity) {
  64. // Create a logger
  65. Logger logger("alpha");
  66. // Now check the levels
  67. logger.setSeverity(isc::log::NONE);
  68. EXPECT_EQ(isc::log::NONE, logger.getSeverity());
  69. logger.setSeverity(isc::log::FATAL);
  70. EXPECT_EQ(isc::log::FATAL, logger.getSeverity());
  71. logger.setSeverity(isc::log::ERROR);
  72. EXPECT_EQ(isc::log::ERROR, logger.getSeverity());
  73. logger.setSeverity(isc::log::WARN);
  74. EXPECT_EQ(isc::log::WARN, logger.getSeverity());
  75. logger.setSeverity(isc::log::INFO);
  76. EXPECT_EQ(isc::log::INFO, logger.getSeverity());
  77. logger.setSeverity(isc::log::DEBUG);
  78. EXPECT_EQ(isc::log::DEBUG, logger.getSeverity());
  79. logger.setSeverity(isc::log::DEFAULT);
  80. EXPECT_EQ(isc::log::DEFAULT, logger.getSeverity());
  81. }
  82. // Check that the debug level is set correctly.
  83. TEST_F(LoggerTest, DebugLevels) {
  84. // Create a logger
  85. Logger logger("alpha");
  86. // Debug level should be 0 if not at debug severity
  87. logger.setSeverity(isc::log::NONE, 20);
  88. EXPECT_EQ(0, logger.getDebugLevel());
  89. logger.setSeverity(isc::log::INFO, 42);
  90. EXPECT_EQ(0, logger.getDebugLevel());
  91. // Should be the value set if the severity is set to DEBUG though.
  92. logger.setSeverity(isc::log::DEBUG, 32);
  93. EXPECT_EQ(32, logger.getDebugLevel());
  94. logger.setSeverity(isc::log::DEBUG, 97);
  95. EXPECT_EQ(97, logger.getDebugLevel());
  96. // Try the limits
  97. logger.setSeverity(isc::log::DEBUG, -1);
  98. EXPECT_EQ(0, logger.getDebugLevel());
  99. logger.setSeverity(isc::log::DEBUG, 0);
  100. EXPECT_EQ(0, logger.getDebugLevel());
  101. logger.setSeverity(isc::log::DEBUG, 1);
  102. EXPECT_EQ(1, logger.getDebugLevel());
  103. logger.setSeverity(isc::log::DEBUG, 98);
  104. EXPECT_EQ(98, logger.getDebugLevel());
  105. logger.setSeverity(isc::log::DEBUG, 99);
  106. EXPECT_EQ(99, logger.getDebugLevel());
  107. logger.setSeverity(isc::log::DEBUG, 100);
  108. EXPECT_EQ(99, logger.getDebugLevel());
  109. }
  110. // Check that changing the parent and child severity does not affect the
  111. // other.
  112. TEST_F(LoggerTest, SeverityInheritance) {
  113. // Create two loggers. We cheat here as we know that the underlying
  114. // implementation will set a parent-child relationship if the loggers
  115. // are named <parent> and <parent>.<child>.
  116. Logger parent("alpha");
  117. Logger child("alpha.beta");
  118. // By default, newly created loggers should have a level of DEFAULT
  119. // (i.e. default to parent)
  120. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  121. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  122. // Set the severity of the parent to debug and check what is
  123. // reported by the child.
  124. parent.setSeverity(isc::log::DEBUG, 42);
  125. EXPECT_EQ(42, parent.getDebugLevel());
  126. EXPECT_EQ(0, child.getDebugLevel());
  127. EXPECT_EQ(42, child.getEffectiveDebugLevel());
  128. // Setting the child to DEBUG severity should set its own
  129. // debug level.
  130. child.setSeverity(isc::log::DEBUG, 53);
  131. EXPECT_EQ(53, child.getDebugLevel());
  132. EXPECT_EQ(53, child.getEffectiveDebugLevel());
  133. // If the child severity is set to something other than DEBUG,
  134. // the debug level should be reported as 0.
  135. child.setSeverity(isc::log::ERROR);
  136. EXPECT_EQ(0, child.getDebugLevel());
  137. EXPECT_EQ(0, child.getEffectiveDebugLevel());
  138. }
  139. // Check that changing the parent and child debug level does not affect
  140. // the other.
  141. TEST_F(LoggerTest, DebugLevelInheritance) {
  142. // Create two loggers. We cheat here as we know that the underlying
  143. // implementation will set a parent-child relationship if the loggers
  144. // are named <parent> and <parent>.<child>.
  145. Logger parent("alpha");
  146. Logger child("alpha.beta");
  147. // By default, newly created loggers should have a level of DEFAULT
  148. // (i.e. default to parent)
  149. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  150. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  151. // Set the severity of the child to something other than the default -
  152. // check it changes and that of the parent does not.
  153. child.setSeverity(isc::log::INFO);
  154. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  155. EXPECT_EQ(isc::log::INFO, child.getSeverity());
  156. // Reset the child severity and set that of the parent
  157. child.setSeverity(isc::log::DEFAULT);
  158. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  159. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  160. parent.setSeverity(isc::log::WARN);
  161. EXPECT_EQ(isc::log::WARN, parent.getSeverity());
  162. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  163. }
  164. // Check that severity is inherited.
  165. TEST_F(LoggerTest, EffectiveSeverityInheritance) {
  166. // Create two loggers. We cheat here as we know that the underlying
  167. // implementation will set a parent-child relationship if the loggers
  168. // are named <parent> and <parent>.<child>.
  169. Logger parent("test6");
  170. Logger child("test6.beta");
  171. // By default, newly created loggers should have a level of DEFAULT
  172. // (i.e. default to parent) and the root should have a default severity
  173. // of INFO. However, the latter is only enforced when created by the
  174. // RootLogger class, so explicitly set it for the parent for now.
  175. parent.setSeverity(isc::log::INFO);
  176. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  177. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  178. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  179. // Set the severity of the child to something other than the default -
  180. // check it changes and that of the parent does not.
  181. child.setSeverity(isc::log::FATAL);
  182. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  183. EXPECT_EQ(isc::log::FATAL, child.getEffectiveSeverity());
  184. // Reset the child severity and check again.
  185. child.setSeverity(isc::log::DEFAULT);
  186. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  187. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  188. // Change the parwnt's severity and check it is reflects in the child.
  189. parent.setSeverity(isc::log::WARN);
  190. EXPECT_EQ(isc::log::WARN, parent.getEffectiveSeverity());
  191. EXPECT_EQ(isc::log::WARN, child.getEffectiveSeverity());
  192. }
  193. // Test the isXxxxEnabled methods.
  194. TEST_F(LoggerTest, IsXxxEnabled) {
  195. Logger logger("test7");
  196. logger.setSeverity(isc::log::INFO);
  197. EXPECT_FALSE(logger.isDebugEnabled());
  198. EXPECT_TRUE(logger.isInfoEnabled());
  199. EXPECT_TRUE(logger.isWarnEnabled());
  200. EXPECT_TRUE(logger.isErrorEnabled());
  201. EXPECT_TRUE(logger.isFatalEnabled());
  202. logger.setSeverity(isc::log::WARN);
  203. EXPECT_FALSE(logger.isDebugEnabled());
  204. EXPECT_FALSE(logger.isInfoEnabled());
  205. EXPECT_TRUE(logger.isWarnEnabled());
  206. EXPECT_TRUE(logger.isErrorEnabled());
  207. EXPECT_TRUE(logger.isFatalEnabled());
  208. logger.setSeverity(isc::log::ERROR);
  209. EXPECT_FALSE(logger.isDebugEnabled());
  210. EXPECT_FALSE(logger.isInfoEnabled());
  211. EXPECT_FALSE(logger.isWarnEnabled());
  212. EXPECT_TRUE(logger.isErrorEnabled());
  213. EXPECT_TRUE(logger.isFatalEnabled());
  214. logger.setSeverity(isc::log::FATAL);
  215. EXPECT_FALSE(logger.isDebugEnabled());
  216. EXPECT_FALSE(logger.isInfoEnabled());
  217. EXPECT_FALSE(logger.isWarnEnabled());
  218. EXPECT_FALSE(logger.isErrorEnabled());
  219. EXPECT_TRUE(logger.isFatalEnabled());
  220. // Check various debug levels
  221. logger.setSeverity(isc::log::DEBUG);
  222. EXPECT_TRUE(logger.isDebugEnabled());
  223. EXPECT_TRUE(logger.isInfoEnabled());
  224. EXPECT_TRUE(logger.isWarnEnabled());
  225. EXPECT_TRUE(logger.isErrorEnabled());
  226. EXPECT_TRUE(logger.isFatalEnabled());
  227. logger.setSeverity(isc::log::DEBUG, 45);
  228. EXPECT_TRUE(logger.isDebugEnabled());
  229. EXPECT_TRUE(logger.isInfoEnabled());
  230. EXPECT_TRUE(logger.isWarnEnabled());
  231. EXPECT_TRUE(logger.isErrorEnabled());
  232. EXPECT_TRUE(logger.isFatalEnabled());
  233. // Create a child logger with no severity set, and check that it reflects
  234. // the severity of the parent logger.
  235. Logger child("test7.child");
  236. logger.setSeverity(isc::log::FATAL);
  237. EXPECT_FALSE(child.isDebugEnabled());
  238. EXPECT_FALSE(child.isInfoEnabled());
  239. EXPECT_FALSE(child.isWarnEnabled());
  240. EXPECT_FALSE(child.isErrorEnabled());
  241. EXPECT_TRUE(child.isFatalEnabled());
  242. logger.setSeverity(isc::log::INFO);
  243. EXPECT_FALSE(child.isDebugEnabled());
  244. EXPECT_TRUE(child.isInfoEnabled());
  245. EXPECT_TRUE(child.isWarnEnabled());
  246. EXPECT_TRUE(child.isErrorEnabled());
  247. EXPECT_TRUE(child.isFatalEnabled());
  248. }
  249. // Within the Debug level there are 100 debug levels. Test that we know
  250. // when to issue a debug message.
  251. TEST_F(LoggerTest, IsDebugEnabledLevel) {
  252. Logger logger("test8");
  253. int MID_LEVEL = (MIN_DEBUG_LEVEL + MAX_DEBUG_LEVEL) / 2;
  254. logger.setSeverity(isc::log::DEBUG);
  255. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  256. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  257. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  258. logger.setSeverity(isc::log::DEBUG, MIN_DEBUG_LEVEL);
  259. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  260. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  261. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  262. logger.setSeverity(isc::log::DEBUG, MID_LEVEL);
  263. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  264. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL - 1));
  265. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  266. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL + 1));
  267. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  268. logger.setSeverity(isc::log::DEBUG, MAX_DEBUG_LEVEL);
  269. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  270. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  271. EXPECT_TRUE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  272. }