9ac65c09ba6d9e86301d606e47faaade12079a78
[platform/core/security/security-manager.git] / src / server / 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
25 #include <dpl/log/log.h>
26 #include <dpl/singleton_impl.h>
27 #include <dpl/log/dlog_log_provider.h>
28 #include <dpl/log/old_style_log_provider.h>
29 #include <dpl/log/audit-smack-log.h>
30
31 IMPLEMENT_SINGLETON(SecurityManager::Log::LogSystem)
32
33 namespace SecurityManager {
34 namespace Log {
35 namespace // anonymous
36 {
37 #ifdef BUILD_TYPE_DEBUG
38 const char *OLD_STYLE_LOGS_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS";
39 const char *OLD_STYLE_PEDANTIC_LOGS_ENV_NAME =
40     "DPL_USE_OLD_STYLE_PEDANTIC_LOGS";
41 const char *OLD_STYLE_LOGS_MASK_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS_MASK";
42 #endif // BUILD_TYPE_DEBUG
43 const char *SECURITY_MANAGER_LOG_OFF = "DPL_LOG_OFF";
44 } // namespace anonymous
45
46 bool LogSystem::IsLoggingEnabled() const
47 {
48     return m_isLoggingEnabled;
49 }
50
51 LogSystem::LogSystem() :
52     m_isLoggingEnabled(!getenv(SECURITY_MANAGER_LOG_OFF))
53 {
54 #ifdef BUILD_TYPE_DEBUG
55     bool oldStyleLogs = false;
56     bool oldStyleDebugLogs = true;
57     bool oldStyleInfoLogs = true;
58     bool oldStyleWarningLogs = true;
59     bool oldStyleErrorLogs = true;
60     bool oldStylePedanticLogs = false;
61
62     // Check environment settings about pedantic logs
63     const char *value = getenv(OLD_STYLE_LOGS_ENV_NAME);
64
65     if (value != NULL && !strcmp(value, "1")) {
66         oldStyleLogs = true;
67     }
68
69     value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
70
71     if (value != NULL && !strcmp(value, "1")) {
72         oldStylePedanticLogs = true;
73     }
74
75     value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
76
77     if (value != NULL) {
78         size_t len = strlen(value);
79
80         if (len >= 1) {
81             if (value[0] == '0') {
82                 oldStyleDebugLogs = false;
83             } else if (value[0] == '1') {
84                 oldStyleDebugLogs = true;
85             }
86         }
87
88         if (len >= 2) {
89             if (value[1] == '0') {
90                 oldStyleInfoLogs = false;
91             } else if (value[1] == '1') {
92                 oldStyleInfoLogs = true;
93             }
94         }
95
96         if (len >= 3) {
97             if (value[2] == '0') {
98                 oldStyleWarningLogs = false;
99             } else if (value[2] == '1') {
100                 oldStyleWarningLogs = true;
101             }
102         }
103
104         if (len >= 4) {
105             if (value[3] == '0') {
106                 oldStyleErrorLogs = false;
107             } else if (value[3] == '1') {
108                 oldStyleErrorLogs = true;
109             }
110         }
111     }
112
113     // Setup default DLOG and old style logging
114     if (oldStyleLogs) {
115         // Old style
116         AddProvider(new OldStyleLogProvider(oldStyleDebugLogs,
117                                             oldStyleInfoLogs,
118                                             oldStyleWarningLogs,
119                                             oldStyleErrorLogs,
120                                             oldStylePedanticLogs));
121     } else {
122         // DLOG
123         AddProvider(new DLOGLogProvider());
124     }
125 #else // BUILD_TYPE_DEBUG
126     AddProvider(new DLOGLogProvider());
127 #endif // BUILD_TYPE_DEBUG
128 }
129
130 LogSystem::~LogSystem()
131 {
132     // Delete all providers
133     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
134          iterator != m_providers.end();
135          ++iterator)
136     {
137         delete *iterator;
138     }
139
140     m_providers.clear();
141 }
142
143 void LogSystem::SetTag(const char* tag)
144 {
145     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
146          iterator != m_providers.end();
147          ++iterator)
148     {
149         (*iterator)->SetTag(tag);
150     }
151 }
152
153 void LogSystem::AddProvider(AbstractLogProvider *provider)
154 {
155     m_providers.push_back(provider);
156 }
157
158 void LogSystem::RemoveProvider(AbstractLogProvider *provider)
159 {
160     m_providers.remove(provider);
161 }
162
163 void LogSystem::Debug(const char *message,
164                       const char *filename,
165                       int line,
166                       const char *function)
167 {
168     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
169          iterator != m_providers.end();
170          ++iterator)
171     {
172         (*iterator)->Debug(message, filename, line, function);
173     }
174 }
175
176 void LogSystem::Info(const char *message,
177                      const char *filename,
178                      int line,
179                      const char *function)
180 {
181     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
182          iterator != m_providers.end();
183          ++iterator)
184     {
185         (*iterator)->Info(message, filename, line, function);
186     }
187 }
188
189 void LogSystem::Warning(const char *message,
190                         const char *filename,
191                         int line,
192                         const char *function)
193 {
194     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
195          iterator != m_providers.end();
196          ++iterator)
197     {
198         (*iterator)->Warning(message, filename, line, function);
199     }
200 }
201
202 void LogSystem::Error(const char *message,
203                       const char *filename,
204                       int line,
205                       const char *function)
206 {
207     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
208          iterator != m_providers.end();
209          ++iterator)
210     {
211         (*iterator)->Error(message, filename, line, function);
212     }
213 }
214
215 void LogSystem::Pedantic(const char *message,
216                          const char *filename,
217                          int line,
218                          const char *function)
219 {
220     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
221          iterator != m_providers.end();
222          ++iterator)
223     {
224         (*iterator)->Pedantic(message, filename, line, function);
225     }
226 }
227
228 void LogSystem::SecureInfo(const char *message,
229                          const char *filename,
230                          int line,
231                          const char *function)
232 {
233     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
234          iterator != m_providers.end();
235          ++iterator)
236     {
237         (*iterator)->SecureInfo(message, filename, line, function);
238     }
239 }
240
241 void LogSystem::SecureDebug(const char *message,
242                          const char *filename,
243                          int line,
244                          const char *function)
245 {
246     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
247          iterator != m_providers.end();
248          ++iterator)
249     {
250         (*iterator)->SecureDebug(message, filename, line, function);
251     }
252 }
253
254 void LogSystem::SecureError(const char *message,
255                          const char *filename,
256                          int line,
257                          const char *function)
258 {
259     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
260          iterator != m_providers.end();
261          ++iterator)
262     {
263         (*iterator)->SecureError(message, filename, line, function);
264     }
265 }
266
267 void LogSystem::SecureWarning(const char *message,
268                          const char *filename,
269                          int line,
270                          const char *function)
271 {
272     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
273          iterator != m_providers.end();
274          ++iterator)
275     {
276         (*iterator)->SecureWarning(message, filename, line, function);
277     }
278 }
279
280 void LogSystem::SmackAudit(const char *message,
281                      const char *fileName,
282                      int line,
283                      const char *function)
284 {
285     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin();
286          iterator != m_providers.end();
287          ++iterator)
288     {
289         (*iterator)->SmackAudit(message, fileName, line, function);
290     }
291 }
292
293 }
294 } // namespace SecurityManager