Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California. |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 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" |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 21 | |
| 22 | #include <boost/algorithm/string.hpp> |
| 23 | #include <boost/algorithm/string/classification.hpp> |
Alexander Afanasyev | fde570c | 2016-12-19 16:02:55 -0800 | [diff] [blame] | 24 | |
| 25 | #include "test-common.hpp" |
Alexander Afanasyev | 59a42ec | 2014-09-02 18:21:16 -0700 | [diff] [blame] | 26 | |
| 27 | namespace ndn { |
| 28 | namespace ndns { |
| 29 | namespace tests { |
| 30 | |
| 31 | BOOST_AUTO_TEST_SUITE(Logger) |
| 32 | |
| 33 | class Fixture |
| 34 | { |
| 35 | public: |
| 36 | Fixture() |
| 37 | : m_logConfigFilename(boost::filesystem::unique_path().native()) |
| 38 | , m_logFilename(boost::filesystem::unique_path().native()) |
| 39 | { |
| 40 | } |
| 41 | |
| 42 | ~Fixture() |
| 43 | { |
| 44 | boost::filesystem::remove(boost::filesystem::path(getLogConfigFilename())); |
| 45 | boost::filesystem::remove(boost::filesystem::path(getLogFilename())); |
| 46 | |
| 47 | log::init("unit-tests.log4cxx"); |
| 48 | } |
| 49 | |
| 50 | const std::string& |
| 51 | getLogConfigFilename() |
| 52 | { |
| 53 | return m_logConfigFilename; |
| 54 | } |
| 55 | |
| 56 | const std::string& |
| 57 | getLogFilename() |
| 58 | { |
| 59 | return m_logFilename; |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | verifyOutput(const std::string expected[], size_t nExpected) |
| 64 | { |
| 65 | std::ifstream is(getLogFilename().c_str()); |
| 66 | std::string buffer((std::istreambuf_iterator<char>(is)), |
| 67 | (std::istreambuf_iterator<char>())); |
| 68 | |
| 69 | std::vector<std::string> components; |
| 70 | boost::split(components, buffer, boost::is_any_of(" ,\n")); |
| 71 | |
| 72 | // expected + number of timestamps (one per log statement) + trailing newline of last statement |
| 73 | BOOST_REQUIRE_EQUAL(components.size(), nExpected); |
| 74 | |
| 75 | for (size_t i = 0; i < nExpected; ++i) { |
| 76 | if (expected[i] == "") |
| 77 | continue; |
| 78 | |
| 79 | BOOST_CHECK_EQUAL(components[i], expected[i]); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | private: |
| 84 | std::string m_logConfigFilename; |
| 85 | std::string m_logFilename; |
| 86 | }; |
| 87 | |
| 88 | BOOST_FIXTURE_TEST_CASE(AllLevels, Fixture) |
| 89 | { |
| 90 | { |
| 91 | std::ofstream of(getLogConfigFilename().c_str()); |
| 92 | of << "log4j.rootLogger=TRACE, FILE\n" |
| 93 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 94 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 95 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 96 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 97 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n"; |
| 98 | } |
| 99 | |
| 100 | log::init(getLogConfigFilename()); |
| 101 | |
| 102 | NDNS_LOG_INIT("DefaultConfig"); |
| 103 | |
| 104 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 105 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 106 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 107 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 108 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 109 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 110 | |
| 111 | const std::string EXPECTED[] = |
| 112 | { |
| 113 | "", "TRACE", "DefaultConfig", "-", "trace-message-JHGFDSR^1", |
| 114 | "", "DEBUG", "DefaultConfig", "-", "debug-message-IGg2474fdksd-fo-151617", |
| 115 | "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13", |
| 116 | "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x", |
| 117 | "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@", |
| 118 | "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng", |
| 119 | "", |
| 120 | }; |
| 121 | |
| 122 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 123 | } |
| 124 | |
| 125 | BOOST_FIXTURE_TEST_CASE(UpToInfo, Fixture) |
| 126 | { |
| 127 | { |
| 128 | std::ofstream of(getLogConfigFilename().c_str()); |
| 129 | of << "log4j.rootLogger=INFO, FILE\n" |
| 130 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 131 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 132 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 133 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 134 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c{1} - %m%n\n"; |
| 135 | } |
| 136 | |
| 137 | log::init(getLogConfigFilename()); |
| 138 | |
| 139 | NDNS_LOG_INIT("DefaultConfig"); |
| 140 | |
| 141 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 142 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 143 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 144 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 145 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 146 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 147 | |
| 148 | const std::string EXPECTED[] = |
| 149 | { |
| 150 | "", "INFO", "DefaultConfig", "-", "info-message-Jjxjshj13", |
| 151 | "", "WARN", "DefaultConfig", "-", "warning-message-XXXhdhd111x", |
| 152 | "", "ERROR", "DefaultConfig", "-", "error-message-!#$&^%$#@", |
| 153 | "", "FATAL", "DefaultConfig", "-", "fatal-message-JJSjaamcng", |
| 154 | "", |
| 155 | }; |
| 156 | |
| 157 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 158 | } |
| 159 | |
| 160 | BOOST_FIXTURE_TEST_CASE(OnlySelectedModule, Fixture) |
| 161 | { |
| 162 | { |
| 163 | std::ofstream of(getLogConfigFilename().c_str()); |
| 164 | of << "log4j.rootLogger=OFF, FILE\n" |
| 165 | << "log4j.appender.FILE=org.apache.log4j.FileAppender\n" |
| 166 | << "log4j.appender.FILE.layout=org.apache.log4j.PatternLayout\n" |
| 167 | << "log4j.appender.FILE.File=" << getLogFilename() << "\n" |
| 168 | << "log4j.appender.FILE.ImmediateFlush=true\n" |
| 169 | << "log4j.appender.FILE.layout.ConversionPattern=%d{HH:mm:ss} %p %c - %m%n\n" |
| 170 | << "log4j.logger.SelectedModule=TRACE\n" |
| 171 | << "log4j.logger.SelectedModule.Submodule=FATAL\n"; |
| 172 | } |
| 173 | |
| 174 | log::init(getLogConfigFilename()); |
| 175 | |
| 176 | { |
| 177 | NDNS_LOG_INIT("DefaultConfig"); |
| 178 | |
| 179 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 180 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 181 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 182 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 183 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 184 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 185 | } |
| 186 | |
| 187 | { |
| 188 | NDNS_LOG_INIT("SelectedModule"); |
| 189 | |
| 190 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 191 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 192 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 193 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 194 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 195 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 196 | } |
| 197 | |
| 198 | { |
| 199 | NDNS_LOG_INIT("SelectedModule.Submodule"); |
| 200 | |
| 201 | NDNS_LOG_TRACE("trace-message-JHGFDSR^1"); |
| 202 | NDNS_LOG_DEBUG("debug-message-IGg2474fdksd-fo-" << 15 << 16 << 17); |
| 203 | NDNS_LOG_INFO("info-message-Jjxjshj13"); |
| 204 | NDNS_LOG_WARN("warning-message-XXXhdhd11" << 1 <<"x"); |
| 205 | NDNS_LOG_ERROR("error-message-!#$&^%$#@"); |
| 206 | NDNS_LOG_FATAL("fatal-message-JJSjaamcng"); |
| 207 | } |
| 208 | |
| 209 | const std::string EXPECTED[] = |
| 210 | { |
| 211 | "", "TRACE", "SelectedModule", "-", "trace-message-JHGFDSR^1", |
| 212 | "", "DEBUG", "SelectedModule", "-", "debug-message-IGg2474fdksd-fo-151617", |
| 213 | "", "INFO", "SelectedModule", "-", "info-message-Jjxjshj13", |
| 214 | "", "WARN", "SelectedModule", "-", "warning-message-XXXhdhd111x", |
| 215 | "", "ERROR", "SelectedModule", "-", "error-message-!#$&^%$#@", |
| 216 | "", "FATAL", "SelectedModule", "-", "fatal-message-JJSjaamcng", |
| 217 | "", "FATAL", "SelectedModule.Submodule", "-", "fatal-message-JJSjaamcng", |
| 218 | "", |
| 219 | }; |
| 220 | |
| 221 | verifyOutput(EXPECTED, sizeof(EXPECTED) / sizeof(std::string)); |
| 222 | } |
| 223 | |
| 224 | |
| 225 | BOOST_AUTO_TEST_SUITE_END() |
| 226 | |
| 227 | } // namespace tests |
| 228 | } // namespace ndns |
| 229 | } // namespace ndn |