2 * Copyright (c) 2014 Samsung Electronics Co., Ltd All Rights Reserved
4 * Contact: Pawel Broda <p.broda@partner.samsung.com>
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License
22 * @author Pawel Broda (p.broda@partner.samsung.com)
23 * @brief Unit tests of the log utility
28 #include "logger/logger.hpp"
29 #include "logger/logger-scope.hpp"
30 #include "logger/formatter.hpp"
31 #include "logger/backend.hpp"
32 #include "logger/backend-stderr.hpp"
36 BOOST_AUTO_TEST_SUITE(LoggerSuite)
38 using namespace logger;
42 class StubbedBackend : public LogBackend {
44 StubbedBackend(std::ostringstream& s) : mLogStream(s) {}
47 void log(LogLevel logLevel,
48 const std::string& file,
49 const unsigned int& line,
50 const std::string& func,
51 const std::string& message) override
53 mLogStream << '[' + toString(logLevel) + ']' + ' '
54 << file + ':' + std::to_string(line) + ' ' + func + ':'
55 << message << std::endl;
59 std::ostringstream& mLogStream;
64 TestLog(LogLevel level)
66 Logger::setLogLevel(level);
67 Logger::setLogBackend(new StubbedBackend(mLogStream));
72 Logger::setLogLevel(LogLevel::TRACE);
73 Logger::setLogBackend(new StderrBackend());
77 bool logContains(const std::string& expression) const
79 std::string s = mLogStream.str();
80 if (s.find(expression) != std::string::npos) {
86 std::ostringstream mLogStream;
89 void exampleTestLogs(void)
91 LOGE("test log error " << "1");
92 LOGW("test log warn " << "2");
93 LOGI("test log info " << "3");
94 LOGD("test log debug " << "4");
95 LOGT("test log trace " << "5");
100 BOOST_AUTO_TEST_CASE(LogLevelSetAndGet)
102 Logger::setLogLevel(LogLevel::TRACE);
103 BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
105 Logger::setLogLevel(LogLevel::DEBUG);
106 BOOST_CHECK(LogLevel::DEBUG == Logger::getLogLevel());
108 Logger::setLogLevel(LogLevel::INFO);
109 BOOST_CHECK(LogLevel::INFO == Logger::getLogLevel());
111 Logger::setLogLevel(LogLevel::WARN);
112 BOOST_CHECK(LogLevel::WARN == Logger::getLogLevel());
114 Logger::setLogLevel(LogLevel::ERROR);
115 BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
118 BOOST_AUTO_TEST_CASE(StringLogLevelSetAndGet)
120 Logger::setLogLevel("TRACE");
121 BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
123 Logger::setLogLevel("traCE");
124 BOOST_CHECK(LogLevel::TRACE == Logger::getLogLevel());
126 Logger::setLogLevel("DEBUG");
127 BOOST_CHECK(LogLevel::DEBUG == Logger::getLogLevel());
129 Logger::setLogLevel("INFO");
130 BOOST_CHECK(LogLevel::INFO == Logger::getLogLevel());
132 Logger::setLogLevel("WARN");
133 BOOST_CHECK(LogLevel::WARN == Logger::getLogLevel());
135 Logger::setLogLevel("ERROR");
136 BOOST_CHECK(LogLevel::ERROR == Logger::getLogLevel());
138 BOOST_REQUIRE_EXCEPTION(Logger::setLogLevel("UNKNOWN"),
140 WhatEquals("Invalid LogLevel to parse")); //TODO change message
143 BOOST_AUTO_TEST_CASE(LogsLevelError)
145 TestLog tf(LogLevel::ERROR);
148 BOOST_CHECK(tf.logContains("[ERROR]") == true);
149 BOOST_CHECK(tf.logContains("[WARN]") == false);
150 BOOST_CHECK(tf.logContains("[INFO]") == false);
151 BOOST_CHECK(tf.logContains("[DEBUG]") == false);
152 BOOST_CHECK(tf.logContains("[TRACE]") == false);
155 BOOST_AUTO_TEST_CASE(LogsLevelWarn)
157 TestLog tf(LogLevel::WARN);
160 BOOST_CHECK(tf.logContains("[ERROR]") == true);
161 BOOST_CHECK(tf.logContains("[WARN]") == true);
162 BOOST_CHECK(tf.logContains("[INFO]") == false);
163 BOOST_CHECK(tf.logContains("[DEBUG]") == false);
164 BOOST_CHECK(tf.logContains("[TRACE]") == false);
167 BOOST_AUTO_TEST_CASE(LogsLevelInfo)
169 TestLog tf(LogLevel::INFO);
172 BOOST_CHECK(tf.logContains("[ERROR]") == true);
173 BOOST_CHECK(tf.logContains("[WARN]") == true);
174 BOOST_CHECK(tf.logContains("[INFO]") == true);
175 BOOST_CHECK(tf.logContains("[DEBUG]") == false);
176 BOOST_CHECK(tf.logContains("[TRACE]") == false);
179 BOOST_AUTO_TEST_CASE(LogsLevelDebug)
181 TestLog tf(LogLevel::DEBUG);
184 BOOST_CHECK(tf.logContains("[ERROR]") == true);
185 BOOST_CHECK(tf.logContains("[WARN]") == true);
186 BOOST_CHECK(tf.logContains("[INFO]") == true);
187 BOOST_CHECK(tf.logContains("[DEBUG]") == true);
188 BOOST_CHECK(tf.logContains("[TRACE]") == false);
191 BOOST_AUTO_TEST_CASE(LogsLevelTrace)
193 TestLog tf(LogLevel::TRACE);
196 BOOST_CHECK(tf.logContains("[ERROR]") == true);
197 BOOST_CHECK(tf.logContains("[WARN]") == true);
198 BOOST_CHECK(tf.logContains("[INFO]") == true);
199 BOOST_CHECK(tf.logContains("[DEBUG]") == true);
200 BOOST_CHECK(tf.logContains("[TRACE]") == true);
203 BOOST_AUTO_TEST_CASE(LoggerScope)
205 LOGS("Main function scope");
208 LOGS("Scope inside function");
209 LOGD("Some additional information in-between scoped logs");
211 LOGS("Additional scope with " << "stringstream" << ' ' << "test" << 3 << ' ' << 3.42);
212 LOGD("More additional information in-between scoped logs");
217 BOOST_AUTO_TEST_SUITE_END()