Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | * |
| 6 | * Author: Ilya Moiseenko <iliamo@ucla.edu> |
| 7 | */ |
| 8 | |
| 9 | #include "core/logger.hpp" |
| 10 | #include <boost/test/unit_test.hpp> |
| 11 | #include <iostream> |
| 12 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 13 | #include "tests/test-common.hpp" |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 14 | |
| 15 | namespace nfd { |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 16 | namespace tests { |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 17 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 18 | BOOST_FIXTURE_TEST_SUITE(CoreLogger, BaseFixture) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 19 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 20 | struct LoggerFixture : protected BaseFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 21 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 22 | LoggerFixture() |
| 23 | : m_savedBuf(std::cerr.rdbuf()) |
| 24 | { |
| 25 | std::cerr.rdbuf(m_buffer.rdbuf()); |
| 26 | } |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 28 | ~LoggerFixture() |
| 29 | { |
| 30 | std::cerr.rdbuf(m_savedBuf); |
| 31 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 32 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 33 | std::stringstream m_buffer; |
| 34 | std::streambuf* m_savedBuf; |
| 35 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 36 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 37 | BOOST_FIXTURE_TEST_CASE(Basic, LoggerFixture) |
| 38 | { |
| 39 | NFD_LOG_INIT("BasicTests"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 40 | g_logger.setLogLevel(LOG_ALL); |
| 41 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 42 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 43 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 44 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 45 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 46 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 47 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 48 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 49 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 50 | "TRACE: [BasicTests] trace message JHGFDSR^1\n" |
| 51 | "DEBUG: [BasicTests] debug message IGg2474fdksd fo 151617\n" |
| 52 | "WARNING: [BasicTests] warning message XXXhdhd111x\n" |
| 53 | "INFO: [BasicTests] info message Jjxjshj13\n" |
| 54 | "ERROR: [BasicTests] error message !#$&^%$#@\n" |
| 55 | "FATAL: [BasicTests] fatal message JJSjaamcng\n" |
| 56 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 57 | } |
| 58 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 59 | class InClassLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 60 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 61 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 62 | InClassLogger() |
| 63 | { |
| 64 | g_logger.setLogLevel(LOG_ALL); |
| 65 | } |
| 66 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 67 | void |
| 68 | writeLogs() |
| 69 | { |
| 70 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 71 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 72 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 73 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 74 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 75 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 76 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 77 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 78 | private: |
| 79 | NFD_LOG_INCLASS_DECLARE(); |
| 80 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 81 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 82 | NFD_LOG_INCLASS_DEFINE(InClassLogger, "InClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 83 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 84 | BOOST_FIXTURE_TEST_CASE(InClass, InClassLogger) |
| 85 | { |
| 86 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 87 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 88 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 89 | "TRACE: [InClassLogger] trace message JHGFDSR^1\n" |
| 90 | "DEBUG: [InClassLogger] debug message IGg2474fdksd fo 151617\n" |
| 91 | "WARNING: [InClassLogger] warning message XXXhdhd111x\n" |
| 92 | "INFO: [InClassLogger] info message Jjxjshj13\n" |
| 93 | "ERROR: [InClassLogger] error message !#$&^%$#@\n" |
| 94 | "FATAL: [InClassLogger] fatal message JJSjaamcng\n" |
| 95 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 96 | } |
| 97 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 98 | |
| 99 | template<class T> |
| 100 | class InClassTemplateLogger : public LoggerFixture |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 101 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 102 | public: |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 103 | InClassTemplateLogger() |
| 104 | { |
| 105 | g_logger.setLogLevel(LOG_ALL); |
| 106 | } |
| 107 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 108 | void |
| 109 | writeLogs() |
| 110 | { |
| 111 | NFD_LOG_TRACE("trace message JHGFDSR^1"); |
| 112 | NFD_LOG_DEBUG("debug message IGg2474fdksd fo " << 15 << 16 << 17); |
| 113 | NFD_LOG_WARN("warning message XXXhdhd11" << 1 << "x"); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 114 | NFD_LOG_INFO("info message Jjxjshj13"); |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 115 | NFD_LOG_ERROR("error message !#$&^%$#@"); |
| 116 | NFD_LOG_FATAL("fatal message JJSjaamcng"); |
| 117 | } |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 118 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 119 | private: |
| 120 | NFD_LOG_INCLASS_DECLARE(); |
| 121 | }; |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 122 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 123 | NFD_LOG_INCLASS_TEMPLATE_DEFINE(InClassTemplateLogger, "GenericInClassTemplateLogger"); |
| 124 | NFD_LOG_INCLASS_TEMPLATE_SPECIALIZATION_DEFINE(InClassTemplateLogger, int, "IntInClassLogger"); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 125 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 126 | BOOST_FIXTURE_TEST_CASE(GenericInTemplatedClass, InClassTemplateLogger<bool>) |
| 127 | { |
| 128 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 129 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 130 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 131 | "TRACE: [GenericInClassTemplateLogger] trace message JHGFDSR^1\n" |
| 132 | "DEBUG: [GenericInClassTemplateLogger] debug message IGg2474fdksd fo 151617\n" |
| 133 | "WARNING: [GenericInClassTemplateLogger] warning message XXXhdhd111x\n" |
| 134 | "INFO: [GenericInClassTemplateLogger] info message Jjxjshj13\n" |
| 135 | "ERROR: [GenericInClassTemplateLogger] error message !#$&^%$#@\n" |
| 136 | "FATAL: [GenericInClassTemplateLogger] fatal message JJSjaamcng\n" |
| 137 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 140 | BOOST_FIXTURE_TEST_CASE(SpecializedInTemplatedClass, InClassTemplateLogger<int>) |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 141 | { |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 142 | writeLogs(); |
Alexander Afanasyev | b84cc92 | 2014-02-24 17:52:58 -0800 | [diff] [blame] | 143 | |
Alexander Afanasyev | 6688681 | 2014-01-31 14:48:48 -0800 | [diff] [blame] | 144 | BOOST_CHECK_EQUAL(m_buffer.str(), |
| 145 | "TRACE: [IntInClassLogger] trace message JHGFDSR^1\n" |
| 146 | "DEBUG: [IntInClassLogger] debug message IGg2474fdksd fo 151617\n" |
| 147 | "WARNING: [IntInClassLogger] warning message XXXhdhd111x\n" |
| 148 | "INFO: [IntInClassLogger] info message Jjxjshj13\n" |
| 149 | "ERROR: [IntInClassLogger] error message !#$&^%$#@\n" |
| 150 | "FATAL: [IntInClassLogger] fatal message JJSjaamcng\n" |
| 151 | ); |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | BOOST_AUTO_TEST_SUITE_END() |
| 155 | |
Junxiao Shi | d9ee45c | 2014-02-27 15:38:11 -0700 | [diff] [blame] | 156 | } // namespace tests |
Ilya Moiseenko | a807e65 | 2014-01-28 11:51:01 -0800 | [diff] [blame] | 157 | } // namespace nfd |