{ JOURNALD, []{ return static_cast<AbstractLogProvider *>(new JournalLogProvider()); } }
})
{
- SetLogLevel(getenv(CKM_LOG_LEVEL));
+ char* level = getenv(CKM_LOG_LEVEL);
+ SetLogLevel(level ? level : "");
AbstractLogProvider *prv = NULL;
- try {
- prv = m_providerCtor.at(getenv(CKM_LOG_PROVIDER))();
- } catch (const std::exception &) {
- prv = m_providerCtor[DLOG]();
+ char* provider = getenv(CKM_LOG_PROVIDER);
+ if (provider) {
+ auto it = m_providerCtor.find(provider);
+ if (it != m_providerCtor.end())
+ prv = it->second();
}
+ if (!prv)
+ prv = m_providerCtor[DLOG]();
AddProvider(prv);
}
AddProvider(prv());
}
-void LogSystem::SetLogLevel(const char *level)
+void LogSystem::SetLogLevel(const std::string& level)
{
try {
m_level = static_cast<AbstractLogProvider::LogLevel>(std::stoi(level));
* See the License for the specific language governing permissions and
* limitations under the License
*/
+#include <cstdlib>
+
#include <boost_macros_wrapper.h>
#include <dpl/log/old_style_log_provider.h>
BOOST_REQUIRE_NO_THROW(provider.SetTag("tag"));
}
+constexpr AbstractLogProvider::LogLevel trimLogLevel(AbstractLogProvider::LogLevel level)
+{
+#ifndef BUILD_TYPE_DEBUG
+ if (level > AbstractLogProvider::LogLevel::Error)
+ level = AbstractLogProvider::LogLevel::Error;
+#endif
+ return level;
+}
+
+constexpr AbstractLogProvider::LogLevel defaultLogLevel()
+{
+#ifdef BUILD_TYPE_DEBUG
+ return AbstractLogProvider::LogLevel::Debug;
+#else
+ return AbstractLogProvider::LogLevel::Error;
+#endif
+}
+
+
+class Env
+{
+public:
+ explicit Env(const std::string& name) : name(name)
+ {
+ char* val = getenv(name.c_str());
+ if (val)
+ backup = val;
+ }
+
+ void Set(const std::string& value)
+ {
+ BOOST_REQUIRE(0 == setenv(name.c_str(), value.c_str(), 1));
+ }
+
+ void Unset()
+ {
+ BOOST_REQUIRE(0 == unsetenv(name.c_str()));
+ }
+
+ ~Env()
+ {
+ if (backup.empty())
+ BOOST_CHECK(0 == unsetenv(name.c_str()));
+ else
+ BOOST_CHECK(0 == setenv(name.c_str(), backup.c_str(), 1));
+ }
+private:
+ std::string name;
+ std::string backup;
+};
+
} // namespace anonymous
BOOST_AUTO_TEST_SUITE(LOG_PROVIDER_TEST)
BOOST_REQUIRE_NO_THROW(system.SetTag("Test"));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("5"));
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("4"));
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Debug));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("3"));
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Info));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("2"));
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Warning));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("1"));
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Error));
BOOST_REQUIRE_NO_THROW(system.SetLogLevel("0"));
- BOOST_REQUIRE(system.GetLogLevel() == AbstractLogProvider::LogLevel::None);
+ BOOST_REQUIRE(system.GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
BOOST_REQUIRE_NO_THROW(system.SelectProvider("DLOG"));
BOOST_REQUIRE_NO_THROW(system.SelectProvider("JOURNALD"));
}
+NEGATIVE_TEST_CASE(log_system)
+{
+ LogSystem system;
+
+ BOOST_REQUIRE_NO_THROW(system.SetLogLevel(""));
+ BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
+ BOOST_REQUIRE_NO_THROW(system.SetLogLevel("whatever"));
+ BOOST_REQUIRE(system.GetLogLevel() == defaultLogLevel());
+
+ BOOST_REQUIRE_THROW(system.SelectProvider("WRONG_PROVIDER"), std::out_of_range);
+ BOOST_REQUIRE_THROW(system.SelectProvider(""), std::out_of_range);
+}
+
+POSITIVE_TEST_CASE(env_log_provider)
+{
+ Env env("CKM_LOG_PROVIDER");
+
+ env.Set("CONSOLE");
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+ env.Set("DLOG");
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+ env.Set("JOURNALD");
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+}
+
+NEGATIVE_TEST_CASE(env_log_provider)
+{
+ Env env("CKM_LOG_PROVIDER");
+
+ env.Unset();
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+ env.Set("");
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+ env.Set("WRONG_PROVIDER");
+ BOOST_REQUIRE_NO_THROW(LogSystem());
+}
+
+POSITIVE_TEST_CASE(env_log_level)
+{
+ Env env("CKM_LOG_LEVEL");
+
+ typedef std::underlying_type<AbstractLogProvider::LogLevel>::type underlying;
+ for (underlying i = 0; i < 6; i++) {
+ std::stringstream ss;
+ ss << i;
+ env.Set(ss.str().c_str());
+
+ auto level = trimLogLevel(static_cast<AbstractLogProvider::LogLevel>(i));
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == level);
+ }
+}
+
+NEGATIVE_TEST_CASE(env_log_level)
+{
+ Env env("CKM_LOG_LEVEL");
+
+ env.Unset();
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
+
+ env.Set("");
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
+
+ env.Set("-1");
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::None));
+
+ env.Set("6");
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == trimLogLevel(AbstractLogProvider::LogLevel::Pedantic));
+
+ env.Set("WRONG_LEVEL");
+ BOOST_REQUIRE(LogSystem().GetLogLevel() == defaultLogLevel());
+}
+
+
BOOST_AUTO_TEST_SUITE_END()