2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * @author Przemyslaw Dobrowolski (p.dobrowolsk@samsung.com)
20 * @brief This file is the implementation file of log system
22 #include <dpl/log/log.h>
23 #include <dpl/singleton_impl.h>
25 IMPLEMENT_SINGLETON(DPL::Log::LogSystem)
31 namespace // anonymous
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
40 bool LogSystem::IsLoggingEnabled() const
42 return m_isLoggingEnabled;
45 LogSystem::LogSystem()
46 : m_dlogProvider(NULL),
47 m_oldStyleProvider(NULL),
48 m_isLoggingEnabled(!getenv(DPL_LOG_OFF))
50 bool oldStyleLogs = false;
51 bool oldStyleDebugLogs = true;
52 bool oldStyleInfoLogs = true;
53 bool oldStyleWarningLogs = true;
54 bool oldStyleErrorLogs = true;
55 bool oldStylePedanticLogs = false;
57 // Check environment settings about pedantic logs
58 const char *value = getenv(OLD_STYLE_LOGS_ENV_NAME);
60 if (value != NULL && !strcmp(value, "1"))
63 value = getenv(OLD_STYLE_PEDANTIC_LOGS_ENV_NAME);
65 if (value != NULL && !strcmp(value, "1"))
66 oldStylePedanticLogs = true;
68 value = getenv(OLD_STYLE_LOGS_MASK_ENV_NAME);
72 size_t len = strlen(value);
77 oldStyleDebugLogs = false;
78 else if (value[0] == '1')
79 oldStyleDebugLogs = true;
85 oldStyleInfoLogs = false;
86 else if (value[1] == '1')
87 oldStyleInfoLogs = true;
93 oldStyleWarningLogs = false;
94 else if (value[2] == '1')
95 oldStyleWarningLogs = true;
101 oldStyleErrorLogs = false;
102 else if (value[3] == '1')
103 oldStyleErrorLogs = true;
107 // Setup default DLOG and old style logging
111 m_oldStyleProvider = new OldStyleLogProvider(oldStyleDebugLogs, oldStyleInfoLogs, oldStyleWarningLogs, oldStyleErrorLogs, oldStylePedanticLogs);
112 AddProvider(m_oldStyleProvider);
117 m_dlogProvider = new DLOGLogProvider();
118 AddProvider(m_dlogProvider);
122 LogSystem::~LogSystem()
124 // Delete all providers
125 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
130 // And even default providers
131 m_dlogProvider = NULL;
132 m_oldStyleProvider = NULL;
135 void LogSystem::SetTag(const char* tag)
137 if (m_dlogProvider != NULL)
138 m_dlogProvider->SetTag(tag);
141 void LogSystem::AddProvider(AbstractLogProvider *provider)
143 ReadWriteMutex::ScopedWriteLock lock(&m_spinLock);
144 m_providers.push_back(provider);
147 void LogSystem::RemoveProvider(AbstractLogProvider *provider)
149 ReadWriteMutex::ScopedWriteLock lock(&m_spinLock);
150 m_providers.remove(provider);
153 void LogSystem::Debug(const char *message, const char *filename, int line, const char *function)
155 ReadWriteMutex::ScopedReadLock lock(&m_spinLock);
157 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
158 (*iterator)->Debug(message, filename, line, function);
161 void LogSystem::Info(const char *message, const char *filename, int line, const char *function)
163 ReadWriteMutex::ScopedReadLock lock(&m_spinLock);
165 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
166 (*iterator)->Info(message, filename, line, function);
169 void LogSystem::Warning(const char *message, const char *filename, int line, const char *function)
171 ReadWriteMutex::ScopedReadLock lock(&m_spinLock);
173 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
174 (*iterator)->Warning(message, filename, line, function);
177 void LogSystem::Error(const char *message, const char *filename, int line, const char *function)
179 ReadWriteMutex::ScopedReadLock lock(&m_spinLock);
181 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
182 (*iterator)->Error(message, filename, line, function);
185 void LogSystem::Pedantic(const char *message, const char *filename, int line, const char *function)
187 ReadWriteMutex::ScopedReadLock lock(&m_spinLock);
189 for (AbstractLogProviderPtrList::iterator iterator = m_providers.begin(); iterator != m_providers.end(); ++iterator)
190 (*iterator)->Pedantic(message, filename, line, function);