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