Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2014, Regents of the University of California. |
| 4 | * |
| 5 | * This file is part of NDNS (Named Data Networking Domain Name Service). |
| 6 | * See AUTHORS.md for complete list of NDNS authors and contributors. |
| 7 | * |
| 8 | * NDNS is free software: you can redistribute it and/or modify it under the terms |
| 9 | * of the GNU General Public License as published by the Free Software Foundation, |
| 10 | * either version 3 of the License, or (at your option) any later version. |
| 11 | * |
| 12 | * NDNS is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 13 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 14 | * PURPOSE. See the GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License along with |
| 17 | * NDNS, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include "logger.hpp" |
| 21 | #include "boost-test.hpp" |
| 22 | |
| 23 | #include <fstream> |
| 24 | |
| 25 | #include <boost/algorithm/string.hpp> |
| 26 | #include <boost/algorithm/string/classification.hpp> |
| 27 | #include <boost/filesystem.hpp> |
| 28 | |
| 29 | namespace ndn { |
| 30 | namespace ndns { |
| 31 | namespace tests { |
| 32 | |
| 33 | BOOST_AUTO_TEST_SUITE(Logger) |
| 34 | |
| 35 | class Fixture |
| 36 | { |
| 37 | public: |
| 38 | Fixture() |
| 39 | : m_logConfigFilename(boost::filesystem::unique_path().native()) |
| 40 | , m_logFilename(boost::filesystem::unique_path().native()) |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | ~Fixture() |
| 45 | { |
| 46 | boost::filesystem::remove(boost::filesystem::path(getLogConfigFilename())); |
| 47 | boost::filesystem::remove(boost::filesystem::path(getLogFilename())); |
| 48 | |
| 49 | log::init("unit-tests.log4cxx"); |
| 50 | } |
| 51 | |
| 52 | const std::string& |
| 53 | getLogConfigFilename() |
| 54 | { |
| 55 | return m_logConfigFilename; |
| 56 | } |
| 57 | |
| 58 | const std::string& |
| 59 | getLogFilename() |
| 60 | { |
| 61 | return m_logFilename; |
| 62 | } |
| 63 | |
| 64 | void |
| 65 | verifyOutput(const std::string expected[], size_t nExpected) |
| 66 | { |
| 67 | std::ifstream is(getLogFilename().c_str()); |
| 68 | std::string buffer((std::istreambuf_iterator<char>(is)), |
| 69 | (std::istreambuf_iterator<char>())); |
| 70 | |
| 71 | std::vector<std::string> components; |
| 72 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 73 | |
| 74 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 75 | BOOST_REQUIRE_EQUAL(components.size(), nExpected); |
| 76 | |
| 77 | for (size_t i = 0; i < nExpected; ++i) { |
| 78 | if (expected[i] == "") |
| 79 | continue; |
| 80 | |
| 81 | BOOST_CHECK_EQUAL(components[i], expected[i]); |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | private: |
| 86 | std::string m_logConfigFilename; |
| 87 | std::string m_logFilename; |
| 88 | }; |
| 89 | |
| 90 | BOOST_FIXTURE_TEST_CASE(AllLevels, Fixture) |
| 91 | { |
| 92 | { |
| 93 | std::ofstream of(getLogConfigFilename().c_str()); |
| 94 | of << "log4j.rootLogger=TRACE, FILE\n" |
| 95 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 96 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 97 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 98 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 99 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n"; |
| 100 | } |
| 101 | |
| 102 | log::init(getLogConfigFilename()); |
| 103 | |
| 104 | NDNS_LOG_INIT("DefaultConfig"); |
| 105 | |
| 106 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 107 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 108 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 109 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 110 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 111 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 112 | |
| 113 | const std::string EXPECTED[] = |
| 114 | { |
| 115 | "", "TRACE", "DefaultConfig", "-", "trace-message-JHGFDSR^1", |
| 116 | "", "DEBUG", "DefaultConfig", "-", "debug-message-IGg2474fdksd-fo-151617", |
| 117 | "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13", |
| 118 | "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x", |
| 119 | "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@", |
| 120 | "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng", |
| 121 | "", |
| 122 | }; |
| 123 | |
| 124 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 125 | } |
| 126 | |
| 127 | BOOST_FIXTURE_TEST_CASE(UpToInfo, Fixture) |
| 128 | { |
| 129 | { |
| 130 | std::ofstream of(getLogConfigFilename().c_str()); |
| 131 | of << "log4j.rootLogger=INFO, FILE\n" |
| 132 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 133 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 134 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 135 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 136 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n"; |
| 137 | } |
| 138 | |
| 139 | log::init(getLogConfigFilename()); |
| 140 | |
| 141 | NDNS_LOG_INIT("DefaultConfig"); |
| 142 | |
| 143 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 144 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 145 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 146 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 147 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 148 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 149 | |
| 150 | const std::string EXPECTED[] = |
| 151 | { |
| 152 | "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13", |
| 153 | "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x", |
| 154 | "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@", |
| 155 | "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng", |
| 156 | "", |
| 157 | }; |
| 158 | |
| 159 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 160 | } |
| 161 | |
| 162 | BOOST_FIXTURE_TEST_CASE(OnlySelectedModule, Fixture) |
| 163 | { |
| 164 | { |
| 165 | std::ofstream of(getLogConfigFilename().c_str()); |
| 166 | of << "log4j.rootLogger=OFF, FILE\n" |
| 167 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 168 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 169 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 170 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 171 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c - %m%n\n" |
| 172 | << "log4j.logger.SelectedModule=TRACE\n" |
| 173 | << "log4j.logger.SelectedModule.Submodule=FATAL\n"; |
| 174 | } |
| 175 | |
| 176 | log::init(getLogConfigFilename()); |
| 177 | |
| 178 | { |
| 179 | NDNS_LOG_INIT("DefaultConfig"); |
| 180 | |
| 181 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 182 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 183 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 184 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 185 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 186 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 187 | } |
| 188 | |
| 189 | { |
| 190 | NDNS_LOG_INIT("SelectedModule"); |
| 191 | |
| 192 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 193 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 194 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 195 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 196 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 197 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 198 | } |
| 199 | |
| 200 | { |
| 201 | NDNS_LOG_INIT("SelectedModule.Submodule"); |
| 202 | |
| 203 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 204 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 205 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 206 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 207 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 208 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 209 | } |
| 210 | |
| 211 | const std::string EXPECTED[] = |
| 212 | { |
| 213 | "", "TRACE", "SelectedModule", "-", "trace-message-JHGFDSR^1", |
| 214 | "", "DEBUG", "SelectedModule", "-", "debug-message-IGg2474fdksd-fo-151617", |
| 215 | "", "INFO", "SelectedModule", "-", "info-message-Jjxjshj13", |
| 216 | "", "WARN", "SelectedModule", "-", "warning-message-XXXhdhd111x", |
| 217 | "", "ERROR", "SelectedModule", "-", "error-message-!#$&^%$#@", |
| 218 | "", "FATAL", "SelectedModule", "-", "fatal-message-JJSjaamcng", |
| 219 | "", "FATAL", "SelectedModule.Submodule", "-", "fatal-message-JJSjaamcng", |
| 220 | "", |
| 221 | }; |
| 222 | |
| 223 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 224 | } |
| 225 | |
| 226 | |
| 227 | BOOST_AUTO_TEST_SUITE_END() |
| 228 | |
| 229 | } // namespace tests |
| 230 | } // namespace ndns |
| 231 | } // namespace ndn |