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