logger_unittest.cc 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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 <gtest/gtest.h>
  15. #include <util/unittests/resource.h>
  16. #include <util/unittests/check_valgrind.h>
  17. #include <log/logger.h>
  18. #include <log/logger_manager.h>
  19. #include <log/logger_name.h>
  20. #include <log/log_messages.h>
  21. #include "log/tests/log_test_messages.h"
  22. #include <util/interprocess_sync_file.h>
  23. #include <iostream>
  24. #include <string>
  25. using namespace isc;
  26. using namespace isc::log;
  27. using namespace std;
  28. /// \brief Logger Test
  29. ///
  30. /// As the logger is only a shell around the implementation, this tests also
  31. /// checks the logger implementation class as well.
  32. class LoggerTest : public ::testing::Test {
  33. public:
  34. LoggerTest() {
  35. // Initialization of logging is done in main()
  36. }
  37. ~LoggerTest() {
  38. LoggerManager::reset();
  39. }
  40. };
  41. // Checks that the logger is named correctly.
  42. TEST_F(LoggerTest, Name) {
  43. // Create a logger
  44. Logger logger("alpha");
  45. // ... and check the name
  46. EXPECT_EQ(getRootLoggerName() + string(".alpha"), logger.getName());
  47. }
  48. // This test attempts to get two instances of a logger with the same name
  49. // and checks that they are in fact the same logger.
  50. TEST_F(LoggerTest, GetLogger) {
  51. const char* name1 = "alpha";
  52. const char* name2 = "beta";
  53. // Instantiate two loggers that should be the same
  54. Logger logger1(name1);
  55. Logger logger2(name1);
  56. // And check they equal
  57. EXPECT_TRUE(logger1 == logger2);
  58. // Instantiate another logger with another name and check that it
  59. // is different to the previously instantiated ones.
  60. Logger logger3(name2);
  61. EXPECT_FALSE(logger1 == logger3);
  62. }
  63. // Check that the logger levels are get set properly.
  64. TEST_F(LoggerTest, Severity) {
  65. // Create a logger
  66. Logger logger("alpha");
  67. // Now check the levels
  68. logger.setSeverity(isc::log::NONE);
  69. EXPECT_EQ(isc::log::NONE, logger.getSeverity());
  70. logger.setSeverity(isc::log::FATAL);
  71. EXPECT_EQ(isc::log::FATAL, logger.getSeverity());
  72. logger.setSeverity(isc::log::ERROR);
  73. EXPECT_EQ(isc::log::ERROR, logger.getSeverity());
  74. logger.setSeverity(isc::log::WARN);
  75. EXPECT_EQ(isc::log::WARN, logger.getSeverity());
  76. logger.setSeverity(isc::log::INFO);
  77. EXPECT_EQ(isc::log::INFO, logger.getSeverity());
  78. logger.setSeverity(isc::log::DEBUG);
  79. EXPECT_EQ(isc::log::DEBUG, logger.getSeverity());
  80. logger.setSeverity(isc::log::DEFAULT);
  81. EXPECT_EQ(isc::log::DEFAULT, logger.getSeverity());
  82. }
  83. // Check that the debug level is set correctly.
  84. TEST_F(LoggerTest, DebugLevels) {
  85. // Create a logger
  86. Logger logger("alpha");
  87. // Debug level should be 0 if not at debug severity
  88. logger.setSeverity(isc::log::NONE, 20);
  89. EXPECT_EQ(0, logger.getDebugLevel());
  90. logger.setSeverity(isc::log::INFO, 42);
  91. EXPECT_EQ(0, logger.getDebugLevel());
  92. // Should be the value set if the severity is set to DEBUG though.
  93. logger.setSeverity(isc::log::DEBUG, 32);
  94. EXPECT_EQ(32, logger.getDebugLevel());
  95. logger.setSeverity(isc::log::DEBUG, 97);
  96. EXPECT_EQ(97, logger.getDebugLevel());
  97. // Try the limits
  98. logger.setSeverity(isc::log::DEBUG, -1);
  99. EXPECT_EQ(0, logger.getDebugLevel());
  100. logger.setSeverity(isc::log::DEBUG, 0);
  101. EXPECT_EQ(0, logger.getDebugLevel());
  102. logger.setSeverity(isc::log::DEBUG, 1);
  103. EXPECT_EQ(1, logger.getDebugLevel());
  104. logger.setSeverity(isc::log::DEBUG, 98);
  105. EXPECT_EQ(98, logger.getDebugLevel());
  106. logger.setSeverity(isc::log::DEBUG, 99);
  107. EXPECT_EQ(99, logger.getDebugLevel());
  108. logger.setSeverity(isc::log::DEBUG, 100);
  109. EXPECT_EQ(99, logger.getDebugLevel());
  110. }
  111. // Check that changing the parent and child severity does not affect the
  112. // other.
  113. TEST_F(LoggerTest, SeverityInheritance) {
  114. // Create two loggers. We cheat here as we know that the underlying
  115. // implementation will set a parent-child relationship if the loggers
  116. // are named <parent> and <parent>.<child>.
  117. Logger parent("alpha");
  118. Logger child("alpha.beta");
  119. // By default, newly created loggers should have a level of DEFAULT
  120. // (i.e. default to parent)
  121. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  122. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  123. // Set the severity of the parent to debug and check what is
  124. // reported by the child.
  125. parent.setSeverity(isc::log::DEBUG, 42);
  126. EXPECT_EQ(42, parent.getDebugLevel());
  127. EXPECT_EQ(0, child.getDebugLevel());
  128. EXPECT_EQ(42, child.getEffectiveDebugLevel());
  129. // Setting the child to DEBUG severity should set its own
  130. // debug level.
  131. child.setSeverity(isc::log::DEBUG, 53);
  132. EXPECT_EQ(53, child.getDebugLevel());
  133. EXPECT_EQ(53, child.getEffectiveDebugLevel());
  134. // If the child severity is set to something other than DEBUG,
  135. // the debug level should be reported as 0.
  136. child.setSeverity(isc::log::ERROR);
  137. EXPECT_EQ(0, child.getDebugLevel());
  138. EXPECT_EQ(0, child.getEffectiveDebugLevel());
  139. }
  140. // Check that changing the parent and child debug level does not affect
  141. // the other.
  142. TEST_F(LoggerTest, DebugLevelInheritance) {
  143. // Create two loggers. We cheat here as we know that the underlying
  144. // implementation will set a parent-child relationship if the loggers
  145. // are named <parent> and <parent>.<child>.
  146. Logger parent("alpha");
  147. Logger child("alpha.beta");
  148. // By default, newly created loggers should have a level of DEFAULT
  149. // (i.e. default to parent)
  150. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  151. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  152. // Set the severity of the child to something other than the default -
  153. // check it changes and that of the parent does not.
  154. child.setSeverity(isc::log::INFO);
  155. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  156. EXPECT_EQ(isc::log::INFO, child.getSeverity());
  157. // Reset the child severity and set that of the parent
  158. child.setSeverity(isc::log::DEFAULT);
  159. EXPECT_EQ(isc::log::DEFAULT, parent.getSeverity());
  160. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  161. parent.setSeverity(isc::log::WARN);
  162. EXPECT_EQ(isc::log::WARN, parent.getSeverity());
  163. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  164. }
  165. // Check that severity is inherited.
  166. TEST_F(LoggerTest, EffectiveSeverityInheritance) {
  167. // Create two loggers. We cheat here as we know that the underlying
  168. // implementation will set a parent-child relationship if the loggers
  169. // are named <parent> and <parent>.<child>.
  170. Logger parent("test6");
  171. Logger child("test6.beta");
  172. // By default, newly created loggers should have a level of DEFAULT
  173. // (i.e. default to parent) and the root should have a default severity
  174. // of INFO. However, the latter is only enforced when created by the
  175. // RootLogger class, so explicitly set it for the parent for now.
  176. parent.setSeverity(isc::log::INFO);
  177. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  178. EXPECT_EQ(isc::log::DEFAULT, child.getSeverity());
  179. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  180. // Set the severity of the child to something other than the default -
  181. // check it changes and that of the parent does not.
  182. child.setSeverity(isc::log::FATAL);
  183. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  184. EXPECT_EQ(isc::log::FATAL, child.getEffectiveSeverity());
  185. // Reset the child severity and check again.
  186. child.setSeverity(isc::log::DEFAULT);
  187. EXPECT_EQ(isc::log::INFO, parent.getEffectiveSeverity());
  188. EXPECT_EQ(isc::log::INFO, child.getEffectiveSeverity());
  189. // Change the parwnt's severity and check it is reflects in the child.
  190. parent.setSeverity(isc::log::WARN);
  191. EXPECT_EQ(isc::log::WARN, parent.getEffectiveSeverity());
  192. EXPECT_EQ(isc::log::WARN, child.getEffectiveSeverity());
  193. }
  194. // Test the isXxxxEnabled methods.
  195. TEST_F(LoggerTest, IsXxxEnabled) {
  196. Logger logger("test7");
  197. logger.setSeverity(isc::log::INFO);
  198. EXPECT_FALSE(logger.isDebugEnabled());
  199. EXPECT_TRUE(logger.isInfoEnabled());
  200. EXPECT_TRUE(logger.isWarnEnabled());
  201. EXPECT_TRUE(logger.isErrorEnabled());
  202. EXPECT_TRUE(logger.isFatalEnabled());
  203. logger.setSeverity(isc::log::WARN);
  204. EXPECT_FALSE(logger.isDebugEnabled());
  205. EXPECT_FALSE(logger.isInfoEnabled());
  206. EXPECT_TRUE(logger.isWarnEnabled());
  207. EXPECT_TRUE(logger.isErrorEnabled());
  208. EXPECT_TRUE(logger.isFatalEnabled());
  209. logger.setSeverity(isc::log::ERROR);
  210. EXPECT_FALSE(logger.isDebugEnabled());
  211. EXPECT_FALSE(logger.isInfoEnabled());
  212. EXPECT_FALSE(logger.isWarnEnabled());
  213. EXPECT_TRUE(logger.isErrorEnabled());
  214. EXPECT_TRUE(logger.isFatalEnabled());
  215. logger.setSeverity(isc::log::FATAL);
  216. EXPECT_FALSE(logger.isDebugEnabled());
  217. EXPECT_FALSE(logger.isInfoEnabled());
  218. EXPECT_FALSE(logger.isWarnEnabled());
  219. EXPECT_FALSE(logger.isErrorEnabled());
  220. EXPECT_TRUE(logger.isFatalEnabled());
  221. // Check various debug levels
  222. logger.setSeverity(isc::log::DEBUG);
  223. EXPECT_TRUE(logger.isDebugEnabled());
  224. EXPECT_TRUE(logger.isInfoEnabled());
  225. EXPECT_TRUE(logger.isWarnEnabled());
  226. EXPECT_TRUE(logger.isErrorEnabled());
  227. EXPECT_TRUE(logger.isFatalEnabled());
  228. logger.setSeverity(isc::log::DEBUG, 45);
  229. EXPECT_TRUE(logger.isDebugEnabled());
  230. EXPECT_TRUE(logger.isInfoEnabled());
  231. EXPECT_TRUE(logger.isWarnEnabled());
  232. EXPECT_TRUE(logger.isErrorEnabled());
  233. EXPECT_TRUE(logger.isFatalEnabled());
  234. // Create a child logger with no severity set, and check that it reflects
  235. // the severity of the parent logger.
  236. Logger child("test7.child");
  237. logger.setSeverity(isc::log::FATAL);
  238. EXPECT_FALSE(child.isDebugEnabled());
  239. EXPECT_FALSE(child.isInfoEnabled());
  240. EXPECT_FALSE(child.isWarnEnabled());
  241. EXPECT_FALSE(child.isErrorEnabled());
  242. EXPECT_TRUE(child.isFatalEnabled());
  243. logger.setSeverity(isc::log::INFO);
  244. EXPECT_FALSE(child.isDebugEnabled());
  245. EXPECT_TRUE(child.isInfoEnabled());
  246. EXPECT_TRUE(child.isWarnEnabled());
  247. EXPECT_TRUE(child.isErrorEnabled());
  248. EXPECT_TRUE(child.isFatalEnabled());
  249. }
  250. // Within the Debug level there are 100 debug levels. Test that we know
  251. // when to issue a debug message.
  252. TEST_F(LoggerTest, IsDebugEnabledLevel) {
  253. Logger logger("test8");
  254. int MID_LEVEL = (MIN_DEBUG_LEVEL + MAX_DEBUG_LEVEL) / 2;
  255. logger.setSeverity(isc::log::DEBUG);
  256. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  257. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  258. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  259. logger.setSeverity(isc::log::DEBUG, MIN_DEBUG_LEVEL);
  260. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  261. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL));
  262. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  263. logger.setSeverity(isc::log::DEBUG, MID_LEVEL);
  264. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  265. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL - 1));
  266. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  267. EXPECT_FALSE(logger.isDebugEnabled(MID_LEVEL + 1));
  268. EXPECT_FALSE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  269. logger.setSeverity(isc::log::DEBUG, MAX_DEBUG_LEVEL);
  270. EXPECT_TRUE(logger.isDebugEnabled(MIN_DEBUG_LEVEL));
  271. EXPECT_TRUE(logger.isDebugEnabled(MID_LEVEL));
  272. EXPECT_TRUE(logger.isDebugEnabled(MAX_DEBUG_LEVEL));
  273. }
  274. // Check that if a logger name is too long, it triggers the appropriate
  275. // assertion.
  276. TEST_F(LoggerTest, LoggerNameLength) {
  277. // The following statements should just declare a logger and nothing
  278. // should happen.
  279. string ok1(Logger::MAX_LOGGER_NAME_SIZE - 1, 'x');
  280. Logger l1(ok1.c_str());
  281. EXPECT_EQ(getRootLoggerName() + "." + ok1, l1.getName());
  282. string ok2(Logger::MAX_LOGGER_NAME_SIZE, 'x');
  283. Logger l2(ok2.c_str());
  284. EXPECT_EQ(getRootLoggerName() + "." + ok2, l2.getName());
  285. // Note: Not all systems have EXPECT_DEATH. As it is a macro we can just
  286. // test for its presence and bypass the test if not available.
  287. #ifdef EXPECT_DEATH
  288. // Too long a logger name should trigger an assertion failure.
  289. // Note that we just check that it dies - we don't check what message is
  290. // output.
  291. if (!isc::util::unittests::runningOnValgrind()) {
  292. EXPECT_DEATH({
  293. isc::util::unittests::dontCreateCoreDumps();
  294. string ok3(Logger::MAX_LOGGER_NAME_SIZE + 1, 'x');
  295. Logger l3(ok3.c_str());
  296. }, ".*");
  297. }
  298. #endif
  299. }
  300. TEST_F(LoggerTest, setInterprocessSync) {
  301. // Create a logger
  302. Logger logger("alpha");
  303. EXPECT_THROW(logger.setInterprocessSync(NULL), BadInterprocessSync);
  304. }
  305. class MockSync : public isc::util::InterprocessSync {
  306. public:
  307. /// \brief Constructor
  308. MockSync(const std::string& component_name) :
  309. InterprocessSync(component_name), was_locked_(false),
  310. was_unlocked_(false)
  311. {}
  312. bool wasLocked() const {
  313. return (was_locked_);
  314. }
  315. bool wasUnlocked() const {
  316. return (was_unlocked_);
  317. }
  318. protected:
  319. bool lock() {
  320. was_locked_ = true;
  321. return (true);
  322. }
  323. bool tryLock() {
  324. return (true);
  325. }
  326. bool unlock() {
  327. was_unlocked_ = true;
  328. return (true);
  329. }
  330. private:
  331. bool was_locked_;
  332. bool was_unlocked_;
  333. };
  334. // Checks that the logger logs exclusively and other BIND 10 components
  335. // are locked out.
  336. TEST_F(LoggerTest, Lock) {
  337. // Create a logger
  338. Logger logger("alpha");
  339. // Setup our own mock sync object so that we can intercept the lock
  340. // call and check if a lock has been taken.
  341. MockSync* sync = new MockSync("logger");
  342. logger.setInterprocessSync(sync);
  343. // Log a message and put things into play.
  344. logger.setSeverity(isc::log::INFO, 100);
  345. logger.info(LOG_LOCK_TEST_MESSAGE);
  346. EXPECT_TRUE(sync->wasLocked());
  347. EXPECT_TRUE(sync->wasUnlocked());
  348. }