Merging tizen into ckm. Stage 1.
[platform/core/test/security-tests.git] / tests / framework / 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
25 #include <dpl/log/log.h>
26 #include <dpl/singleton_impl.h>
27
28 IMPLEMENT_SINGLETON(DPL::Log::LogSystem)
29
30 namespace DPL {
31 namespace Log {
32 namespace // anonymous
33 {
34 const char *OLD_STYLE_LOGS_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS";
35 const char *OLD_STYLE_PEDANTIC_LOGS_ENV_NAME =
36     "DPL_USE_OLD_STYLE_PEDANTIC_LOGS";
37 const char *OLD_STYLE_LOGS_MASK_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS_MASK";
38 const char *DPL_LOG_OFF = "DPL_LOG_OFF";
39 } // namespace anonymous
40
41 bool LogSystem::IsLoggingEnabled() const
42 {
43     return m_isLoggingEnabled;
44 }
45
46 LogSystem::LogSystem() :
47     m_dlogProvider(nullptr),
48     m_oldStyleProvider(nullptr),
49     m_isLoggingEnabled(!getenv(DPL_LOG_OFF))
50 {
51     bool oldStyleLogs = false;
52     bool oldStyleDebugLogs = true;
53     bool oldStyleInfoLogs = true;
54     bool oldStyleWarningLogs = true;
55     bool oldStyleErrorLogs = true;
56     bool oldStylePedanticLogs = false;
57
58     // Check environment settings about pedantic logs
59     const char *value = getenv(OLD_STYLE_LOGS_ENV_NAME);
60
61     if (value != nullptr && !strcmp(value, "1")) {
62         oldStyleLogs = true;
63     }
64
65     value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
66
67     if (value != nullptr && !strcmp(value, "1")) {
68         oldStylePedanticLogs = true;
69     }
70
71     value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
72
73     if (value != nullptr) {
74         size_t len = strlen(value);
75
76         if (len >= 1) {
77             if (value[0] == '0') {
78                 oldStyleDebugLogs = false;
79             } else if (value[0] == '1') {
80                 oldStyleDebugLogs = true;
81             }
82         }
83
84         if (len >= 2) {
85             if (value[1] == '0') {
86                 oldStyleInfoLogs = false;
87             } else if (value[1] == '1') {
88                 oldStyleInfoLogs = true;
89             }
90         }
91
92         if (len >= 3) {
93             if (value[2] == '0') {
94                 oldStyleWarningLogs = false;
95             } else if (value[2] == '1') {
96                 oldStyleWarningLogs = true;
97             }
98         }
99
100         if (len >= 4) {
101             if (value[3] == '0') {
102                 oldStyleErrorLogs = false;
103             } else if (value[3] == '1') {
104                 oldStyleErrorLogs = true;
105             }
106         }
107     }
108
109     // Setup default DLOG and old style logging
110     if (oldStyleLogs) {
111         // Old style
112         m_oldStyleProvider = new OldStyleLogProvider(oldStyleDebugLogs,
113                                                      oldStyleInfoLogs,
114                                                      oldStyleWarningLogs,
115                                                      oldStyleErrorLogs,
116                                                      oldStylePedanticLogs);
117         AddProvider(m_oldStyleProvider);
118     } else {
119         // DLOG
120         m_dlogProvider = new DLOGLogProvider();
121         AddProvider(m_dlogProvider);
122     }
123 }
124
125 LogSystem::~LogSystem()
126 {
127     // Delete all providers
128     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
129          iterator != m_providers.end();
130          ++iterator)
131     {
132         delete *iterator;
133     }
134
135     m_providers.clear();
136
137     // And even default providers
138     m_dlogProvider = nullptr;
139     m_oldStyleProvider = nullptr;
140 }
141
142 void LogSystem::SetTag(const char* tag)
143 {
144     if (m_dlogProvider != nullptr) {
145         m_dlogProvider->SetTag(tag);
146     }
147 }
148
149 void LogSystem::AddProvider(AbstractLogProvider *provider)
150 {
151     m_providers.push_back(provider);
152 }
153
154 void LogSystem::RemoveProvider(AbstractLogProvider *provider)
155 {
156     m_providers.remove(provider);
157 }
158
159 void LogSystem::Debug(const char *message,
160                       const char *filename,
161                       int line,
162                       const char *function)
163 {
164     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
165          iterator != m_providers.end();
166          ++iterator)
167     {
168         (*iterator)->Debug(message, filename, line, function);
169     }
170 }
171
172 void LogSystem::Info(const char *message,
173                      const char *filename,
174                      int line,
175                      const char *function)
176 {
177     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
178          iterator != m_providers.end();
179          ++iterator)
180     {
181         (*iterator)->Info(message, filename, line, function);
182     }
183 }
184
185 void LogSystem::Warning(const char *message,
186                         const char *filename,
187                         int line,
188                         const char *function)
189 {
190     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
191          iterator != m_providers.end();
192          ++iterator)
193     {
194         (*iterator)->Warning(message, filename, line, function);
195     }
196 }
197
198 void LogSystem::Error(const char *message,
199                       const char *filename,
200                       int line,
201                       const char *function)
202 {
203     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
204          iterator != m_providers.end();
205          ++iterator)
206     {
207         (*iterator)->Error(message, filename, line, function);
208     }
209 }
210
211 void LogSystem::Pedantic(const char *message,
212                          const char *filename,
213                          int line,
214                          const char *function)
215 {
216     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
217          iterator != m_providers.end();
218          ++iterator)
219     {
220         (*iterator)->Pedantic(message, filename, line, function);
221     }
222 }
223 }
224 } // namespace DPL