2 * Copyright (c) 2017 - 2020 Samsung Electronics Co., Ltd All Rights Reserved
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License
18 #include <boost_macros_wrapper.h>
20 #include <dpl/log/old_style_log_provider.h>
21 #include <dpl/log/journal_log_provider.h>
22 #include <dpl/log/dlog_log_provider.h>
23 #include <dpl/log/log.h>
26 using namespace CKM::Log;
30 std::vector<AbstractLogProvider::LogLevel> levels({
31 AbstractLogProvider::LogLevel::Error,
32 AbstractLogProvider::LogLevel::Warning,
33 AbstractLogProvider::LogLevel::Info,
34 AbstractLogProvider::LogLevel::Debug,
35 AbstractLogProvider::LogLevel::Pedantic,
36 static_cast<AbstractLogProvider::LogLevel>(999)
39 void testProvider(AbstractLogProvider &provider)
41 for (auto level : levels)
42 BOOST_REQUIRE_NO_THROW(provider.Log(
43 level, "message", __FILE__, __LINE__, __func__));
45 BOOST_REQUIRE_NO_THROW(provider.SetTag("tag"));
48 constexpr AbstractLogProvider::LogLevel trimLogLevel(AbstractLogProvider::LogLevel level)
50 #ifndef BUILD_TYPE_DEBUG
51 if (level > AbstractLogProvider::LogLevel::Error)
52 level = AbstractLogProvider::LogLevel::Error;
57 constexpr AbstractLogProvider::LogLevel defaultLogLevel()
59 #ifdef BUILD_TYPE_DEBUG
60 return AbstractLogProvider::LogLevel::Debug;
62 return AbstractLogProvider::LogLevel::Error;
70 explicit Env(const std::string& name) : name(name)
72 char* val = getenv(name.c_str());
77 void Set(const std::string& value)
79 BOOST_REQUIRE(0 == setenv(name.c_str(), value.c_str(), 1));
84 BOOST_REQUIRE(0 == unsetenv(name.c_str()));
90 BOOST_CHECK(0 == unsetenv(name.c_str()));
92 BOOST_CHECK(0 == setenv(name.c_str(), backup.c_str(), 1));
99 } // namespace anonymous
101 BOOST_AUTO_TEST_SUITE(LOG_PROVIDER_TEST)
103 POSITIVE_TEST_CASE(oldstyle_backend)
105 OldStyleLogProvider provider;
107 testProvider(provider);
110 POSITIVE_TEST_CASE(journal_backend)
112 JournalLogProvider provider;
114 testProvider(provider);
117 POSITIVE_TEST_CASE(dlog_backend)
119 DLOGLogProvider provider;
121 testProvider(provider);
124 POSITIVE_TEST_CASE(log_system)
128 system.AddProvider(new OldStyleLogProvider);
129 system.AddProvider(new JournalLogProvider);
130 system.AddProvider(new DLOGLogProvider);
132 for (auto level : levels)
133 BOOST_REQUIRE_NO_THROW(system.Log(
134 level, "message", __FILE__, __LINE__, __func__));
136 BOOST_REQUIRE_NO_THROW(system.SetTag("Test"));
138 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("5"));
139 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
140 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("4"));
141 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Debug));
142 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("3"));
143 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Info));
144 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("2"));
145 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Warning));
146 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("1"));
147 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Error));
148 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("0"));
149 BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
151 BOOST_REQUIRE_NO_THROW(system.SelectProvider("DLOG"));
152 BOOST_REQUIRE_NO_THROW(system.SelectProvider("JOURNALD"));
155 NEGATIVE_TEST_CASE(log_system)
159 BOOST_REQUIRE_NO_THROW(system.SetLogLevel(""));
160 BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
161 BOOST_REQUIRE_NO_THROW(system.SetLogLevel("whatever"));
162 BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
164 BOOST_REQUIRE_THROW(system.SelectProvider("WRONG_PROVIDER"), std::out_of_range);
165 BOOST_REQUIRE_THROW(system.SelectProvider(""), std::out_of_range);
168 POSITIVE_TEST_CASE(env_log_provider)
170 Env env("CKM_LOG_PROVIDER");
173 BOOST_REQUIRE_NO_THROW(LogSystem());
175 BOOST_REQUIRE_NO_THROW(LogSystem());
177 BOOST_REQUIRE_NO_THROW(LogSystem());
180 NEGATIVE_TEST_CASE(env_log_provider)
182 Env env("CKM_LOG_PROVIDER");
185 BOOST_REQUIRE_NO_THROW(LogSystem());
187 BOOST_REQUIRE_NO_THROW(LogSystem());
188 env.Set("WRONG_PROVIDER");
189 BOOST_REQUIRE_NO_THROW(LogSystem());
192 POSITIVE_TEST_CASE(env_log_level)
194 Env env("CKM_LOG_LEVEL");
196 typedef std::underlying_type<AbstractLogProvider::LogLevel>::type underlying;
197 for (underlying i = 0; i < 6; i++) {
198 std::stringstream ss;
200 env.Set(ss.str().c_str());
202 auto level = trimLogLevel(static_cast<AbstractLogProvider::LogLevel>(i));
203 BOOST_REQUIRE(LogSystem().GetLogLevel() == level);
207 NEGATIVE_TEST_CASE(env_log_level)
209 Env env("CKM_LOG_LEVEL");
212 BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
215 BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
218 BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
221 BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
223 env.Set("WRONG_LEVEL");
224 BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
228 BOOST_AUTO_TEST_SUITE_END()