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