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 | |
| 18 | BOOST_AUTO_TEST_CASE(Warn) |
| 19 | { |
| 20 | std::stringstream input; |
| 21 | input << "warnTest"; |
| 22 | |
| 23 | std::stringstream buffer; |
| 24 | |
| 25 | // save cerr's buffer here |
| 26 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 27 | |
| 28 | // redirect cerr to the buffer |
| 29 | std::cerr.rdbuf(buffer.rdbuf()); |
| 30 | |
| 31 | NFD_LOG_INIT("WarnTest"); |
| 32 | NFD_LOG_WARN(input); |
| 33 | |
| 34 | // redirect cerr back |
| 35 | std::cerr.rdbuf(sbuf); |
| 36 | |
| 37 | std::stringstream trueValue; |
| 38 | trueValue << "WARNING: [WarnTest] " << input <<"\n"; |
| 39 | |
| 40 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 41 | } |
| 42 | |
| 43 | BOOST_AUTO_TEST_CASE(Debug) |
| 44 | { |
| 45 | std::stringstream input; |
| 46 | input << "debugTest"; |
| 47 | |
| 48 | std::stringstream buffer; |
| 49 | |
| 50 | // save cerr's buffer here |
| 51 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 52 | |
| 53 | // redirect cerr to the buffer |
| 54 | std::cerr.rdbuf(buffer.rdbuf()); |
| 55 | |
| 56 | NFD_LOG_INIT("DebugTest"); |
| 57 | NFD_LOG_DEBUG(input); |
| 58 | |
| 59 | // redirect cerr back |
| 60 | std::cerr.rdbuf(sbuf); |
| 61 | |
| 62 | std::stringstream trueValue; |
| 63 | trueValue << "DEBUG: [DebugTest] " << input <<"\n"; |
| 64 | |
| 65 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 66 | } |
| 67 | |
| 68 | BOOST_AUTO_TEST_CASE(Info) |
| 69 | { |
| 70 | std::stringstream input; |
| 71 | input << "infotest"; |
| 72 | |
| 73 | std::stringstream buffer; |
| 74 | |
| 75 | // save cerr's buffer here |
| 76 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 77 | |
| 78 | // redirect cerr to the buffer |
| 79 | std::cerr.rdbuf(buffer.rdbuf()); |
| 80 | |
| 81 | NFD_LOG_INIT("InfoTest"); |
| 82 | NFD_LOG_INFO(input); |
| 83 | |
| 84 | // redirect cerr back |
| 85 | std::cerr.rdbuf(sbuf); |
| 86 | |
| 87 | std::stringstream trueValue; |
| 88 | trueValue << "INFO: [InfoTest] " << input <<"\n"; |
| 89 | |
| 90 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 91 | } |
| 92 | |
| 93 | BOOST_AUTO_TEST_CASE(Fatal) |
| 94 | { |
| 95 | std::stringstream input; |
| 96 | input << "fataltest"; |
| 97 | |
| 98 | std::stringstream buffer; |
| 99 | |
| 100 | // save cerr's buffer here |
| 101 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 102 | |
| 103 | // redirect cerr to the buffer |
| 104 | std::cerr.rdbuf(buffer.rdbuf()); |
| 105 | |
| 106 | NFD_LOG_INIT("FatalTest"); |
| 107 | NFD_LOG_FATAL(input); |
| 108 | |
| 109 | // redirect cerr back |
| 110 | std::cerr.rdbuf(sbuf); |
| 111 | |
| 112 | std::stringstream trueValue; |
| 113 | trueValue << "FATAL: [FatalTest] " << input <<"\n"; |
| 114 | |
| 115 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 116 | } |
| 117 | |
| 118 | BOOST_AUTO_TEST_CASE(Error) |
| 119 | { |
| 120 | std::stringstream input; |
| 121 | input << "errortest"; |
| 122 | |
| 123 | std::stringstream buffer; |
| 124 | |
| 125 | // save cerr's buffer here |
| 126 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 127 | |
| 128 | // redirect cerr to the buffer |
| 129 | std::cerr.rdbuf(buffer.rdbuf()); |
| 130 | |
| 131 | NFD_LOG_INIT("ErrorTest"); |
| 132 | NFD_LOG_ERROR(input); |
| 133 | |
| 134 | // redirect cerr back |
| 135 | std::cerr.rdbuf(sbuf); |
| 136 | |
| 137 | std::stringstream trueValue; |
| 138 | trueValue << "ERROR: [ErrorTest] " << input <<"\n"; |
| 139 | |
| 140 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 141 | } |
| 142 | |
| 143 | BOOST_AUTO_TEST_CASE(Trace) |
| 144 | { |
| 145 | std::stringstream input; |
| 146 | input << "tracetest"; |
| 147 | |
| 148 | std::stringstream buffer; |
| 149 | |
| 150 | // save cerr's buffer here |
| 151 | std::streambuf* sbuf = std::cerr.rdbuf(); |
| 152 | |
| 153 | // redirect cerr to the buffer |
| 154 | std::cerr.rdbuf(buffer.rdbuf()); |
| 155 | |
| 156 | NFD_LOG_INIT("TraceTest"); |
| 157 | NFD_LOG_TRACE(input); |
| 158 | |
| 159 | // redirect cerr back |
| 160 | std::cerr.rdbuf(sbuf); |
| 161 | |
| 162 | std::stringstream trueValue; |
| 163 | trueValue << "TRACE: [TraceTest] " << input <<"\n"; |
| 164 | |
| 165 | BOOST_CHECK_EQUAL(buffer.str(), trueValue.str()); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_SUITE_END() |
| 169 | |
| 170 | } // namespace nfd |