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