Fix internal tests
[platform/core/security/cert-svc.git] / vcore / dpl / log / src / log.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
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
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 /*
17  * @file        log.cpp
18  * @author      Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
19  * @version     1.0
20  * @brief       This file is the implementation file of log system
21  */
22 #include <stddef.h>
23 #include <string.h>
24 #include <stdexcept>
25 #include <cassert>
26
27 #include <dpl/singleton_impl.h>
28 #include <dpl/log/old_style_log_provider.h>
29 #include <dpl/log/dlog_log_provider.h>
30 #include <dpl/log/journal_log_provider.h>
31
32 #include <dpl/log/log.h>
33
34 IMPLEMENT_SINGLETON(VcoreDPL::Log::LogSystem)
35
36 namespace VcoreDPL {
37 namespace Log {
38 namespace // anonymous
39 {
40 const char *const CERTSVC_LOG_LEVEL    = "CERTSVC_LOG_LEVEL";
41 const char *const CERTSVC_LOG_PROVIDER = "CERTSVC_LOG_PROVIDER";
42
43 const char *const CONSOLE  = "CONSOLE";
44 const char *const DLOG     = "DLOG";
45 const char *const JOURNALD = "JOURNALD";
46 } // namespace anonymous
47
48 LogSystem::LogSystem()
49   : m_providerCtor({
50 #ifdef BUILD_TYPE_DEBUG
51         { CONSOLE,  []{ return static_cast<AbstractLogProvider *>(new OldStyleLogProvider()); }},
52 #endif
53         { DLOG,     []{ return static_cast<AbstractLogProvider *>(new DLOGLogProvider());     }},
54         { JOURNALD, []{ return static_cast<AbstractLogProvider *>(new JournalLogProvider());  }}
55     })
56 {
57     SetLogLevel(getenv(CERTSVC_LOG_LEVEL));
58
59     AbstractLogProvider *prv = NULL;
60     try {
61         prv = m_providerCtor.at(getenv(CERTSVC_LOG_PROVIDER))();
62     } catch (const std::exception &) {
63         prv = m_providerCtor[JOURNALD]();
64     }
65
66     AddProvider(prv);
67 }
68
69 LogSystem::~LogSystem()
70 {
71     RemoveProviders();
72 }
73
74 void LogSystem::SetTag(const char* tag)
75 {
76     for (auto &it : m_providers)
77         it->SetTag(tag);
78 }
79
80 void LogSystem::AddProvider(AbstractLogProvider *provider)
81 {
82     m_providers.push_back(provider);
83 }
84
85 void LogSystem::RemoveProvider(AbstractLogProvider *provider)
86 {
87     m_providers.remove(provider);
88 }
89
90 void LogSystem::SelectProvider(const std::string &name)
91 {
92     ProviderFn& prv = m_providerCtor.at(name);
93
94     RemoveProviders();
95     AddProvider(prv());
96 }
97
98 void LogSystem::SetLogLevel(const char *level)
99 {
100     try {
101         m_level = static_cast<AbstractLogProvider::LogLevel>(std::stoi(level));
102     } catch(const std::exception&) {
103         m_level = AbstractLogProvider::LogLevel::Debug;
104     }
105
106     if (m_level < AbstractLogProvider::LogLevel::None)
107         m_level = AbstractLogProvider::LogLevel::None;
108     else if (m_level > AbstractLogProvider::LogLevel::Pedantic)
109         m_level = AbstractLogProvider::LogLevel::Pedantic;
110
111 #ifndef BUILD_TYPE_DEBUG
112     if (m_level > AbstractLogProvider::LogLevel::Error)
113         m_level = AbstractLogProvider::LogLevel::Error;
114 #endif // BUILD_TYPE_DEBUG
115 }
116
117 void LogSystem::Log(AbstractLogProvider::LogLevel level,
118                     const char *message,
119                     const char *filename,
120                     int line,
121                     const char *function) const
122 {
123     for (const auto &it : m_providers)
124         it->Log(level, message, filename, line, function);
125 }
126
127 void LogSystem::RemoveProviders()
128 {
129     for (auto &it : m_providers)
130         delete it;
131
132     m_providers.clear();
133 }
134 }
135 } // namespace VcoreDPL