Tizen 2.0 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 <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 {
30 namespace Log
31 {
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 = "DPL_USE_OLD_STYLE_PEDANTIC_LOGS";
36 const char *OLD_STYLE_LOGS_MASK_ENV_NAME = "DPL_USE_OLD_STYLE_LOGS_MASK";
37 const char *DPL_LOG_OFF = "DPL_LOG_OFF";
38 } // namespace anonymous
39
40
41 bool LogSystem::IsLoggingEnabled() const
42 {
43     return m_isLoggingEnabled;
44 }
45
46 LogSystem::LogSystem()
47     : m_dlogProvider(NULL),
48       m_oldStyleProvider(NULL),
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 != NULL && !strcmp(value, "1"))
62         oldStyleLogs = true;
63
64     value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
65
66     if (value != NULL && !strcmp(value, "1"))
67         oldStylePedanticLogs = true;
68
69     value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
70
71     if (value != NULL)
72     {
73         size_t len = strlen(value);
74
75         if (len >= 1)
76         {
77             if (value[0] == '0')
78                 oldStyleDebugLogs = false;
79             else if (value[0] == '1')
80                 oldStyleDebugLogs = true;
81         }
82
83         if (len >= 2)
84         {
85             if (value[1] == '0')
86                 oldStyleInfoLogs = false;
87             else if (value[1] == '1')
88                 oldStyleInfoLogs = true;
89         }
90
91         if (len >= 3)
92         {
93             if (value[2] == '0')
94                 oldStyleWarningLogs = false;
95             else if (value[2] == '1')
96                 oldStyleWarningLogs = true;
97         }
98
99         if (len >= 4)
100         {
101             if (value[3] == '0')
102                 oldStyleErrorLogs = false;
103             else if (value[3] == '1')
104                 oldStyleErrorLogs = true;
105         }
106     }
107
108     // Setup default DLOG and old style logging
109     if (oldStyleLogs)
110     {
111         // Old style
112         m_oldStyleProvider = new OldStyleLogProvider(oldStyleDebugLogs, oldStyleInfoLogs, oldStyleWarningLogs, oldStyleErrorLogs, oldStylePedanticLogs);
113         AddProvider(m_oldStyleProvider);
114     }
115     else
116     {
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(); iterator != m_providers.end(); ++iterator)
127         delete *iterator;
128
129     m_providers.clear();
130
131     // And even default providers
132     m_dlogProvider = NULL;
133     m_oldStyleProvider = NULL;
134 }
135
136 void LogSystem::SetTag(const char* tag)
137 {
138     if (m_dlogProvider != NULL)
139         m_dlogProvider->SetTag(tag);
140 }
141
142 void LogSystem::AddProvider(AbstractLogProvider *provider)
143 {
144     m_providers.push_back(provider);
145 }
146
147 void LogSystem::RemoveProvider(AbstractLogProvider *provider)
148 {
149     m_providers.remove(provider);
150 }
151
152 void LogSystem::Debug(const char *message, const char *filename, int line, const char *function)
153 {
154     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
155         (*iterator)->Debug(message, filename, line, function);
156 }
157
158 void LogSystem::Info(const char *message, const char *filename, int line, const char *function)
159 {
160     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
161         (*iterator)->Info(message, filename, line, function);
162 }
163
164 void LogSystem::Warning(const char *message, const char *filename, int line, const char *function)
165 {
166     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
167         (*iterator)->Warning(message, filename, line, function);
168 }
169
170 void LogSystem::Error(const char *message, const char *filename, int line, const char *function)
171 {
172     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
173         (*iterator)->Error(message, filename, line, function);
174 }
175
176 void LogSystem::Pedantic(const char *message, const char *filename, int line, const char *function)
177 {
178     for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
179         (*iterator)->Pedantic(message, filename, line, function);
180 }
181
182 }
183 } // namespace DPL