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