tizen 2.3 release
[framework/web/wearable/wrt-security.git] / commons / modules / log / include / dpl / log / log.h
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.h
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 #ifndef DPL_LOG_H
23 #define DPL_LOG_H
24
25 #include <dpl/singleton.h>
26 #include <dpl/noncopyable.h>
27 #include <dpl/log/abstract_log_provider.h>
28 #include <dpl/log/dlog_log_provider.h>
29 #include <dpl/log/old_style_log_provider.h>
30 #include <sstream>
31 #include <list>
32
33 namespace DPL {
34 namespace Log {
35 /**
36  * DPL log system
37  *
38  * To switch logs into old style, export
39  * DPL_USE_OLD_STYLE_LOGS before application start
40  */
41 class LogSystem :
42     private Noncopyable
43 {
44   private:
45     typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
46     AbstractLogProviderPtrList m_providers;
47
48     DLOGLogProvider *m_dlogProvider;
49     OldStyleLogProvider *m_oldStyleProvider;
50
51     bool m_isLoggingEnabled;
52
53   public:
54     bool IsLoggingEnabled() const;
55     LogSystem();
56     virtual ~LogSystem();
57
58     /**
59      * Log debug message
60      */
61     void Debug(const char *message,
62                const char *filename,
63                int line,
64                const char *function);
65
66     /**
67      * Log info message
68      */
69     void Info(const char *message,
70               const char *filename,
71               int line,
72               const char *function);
73
74     /**
75      * Log warning message
76      */
77     void Warning(const char *message,
78                  const char *filename,
79                  int line,
80                  const char *function);
81
82     /**
83      * Log error message
84      */
85     void Error(const char *message,
86                const char *filename,
87                int line,
88                const char *function);
89
90     /**
91      * Log pedantic message
92      */
93     void Pedantic(const char *message,
94                   const char *filename,
95                   int line,
96                   const char *function);
97
98     /**
99      * Set default's DLOG provider Tag
100      */
101     void SetTag(const char *tag);
102
103     /**
104      * Add abstract provider to providers list
105      *
106      * @notice Ownership is transfered to LogSystem and deleted upon exit
107      */
108     void AddProvider(AbstractLogProvider *provider);
109
110     /**
111      * Remove abstract provider from providers list
112      */
113     void RemoveProvider(AbstractLogProvider *provider);
114 };
115
116 /*
117  * Replacement low overhead null logging class
118  */
119 class NullStream
120 {
121   public:
122     NullStream() {}
123
124     template <typename T>
125     NullStream& operator<<(const T&)
126     {
127         return *this;
128     }
129 };
130
131 /**
132  * Log system singleton
133  */
134 typedef Singleton<LogSystem> LogSystemSingleton;
135 }
136 } // namespace DPL
137
138 //
139 // Log support
140 //
141 //
142
143 #ifdef DPL_LOGS_ENABLED
144     #define DPL_MACRO_FOR_LOGGING(message, function)                           \
145     do                                                                     \
146     {                                                                      \
147         if (DPL::Log::LogSystemSingleton::Instance().IsLoggingEnabled())   \
148         {                                                                  \
149             std::ostringstream platformLog;                                \
150             platformLog << message;                                        \
151             DPL::Log::LogSystemSingleton::Instance().function(             \
152                 platformLog.str().c_str(),                                 \
153                 __FILE__, __LINE__, __FUNCTION__);                         \
154         }                                                                  \
155     } while (0)
156 #else
157 /* avoid warnings about unused variables */
158     #define DPL_MACRO_FOR_LOGGING(message, function)                           \
159     do {                                                                   \
160         DPL::Log::NullStream ns;                                           \
161         ns << message;                                                     \
162     } while (0)
163 #endif
164
165 #define  LogDebug(message) DPL_MACRO_FOR_LOGGING(message, Debug)
166 #define  LogInfo(message) DPL_MACRO_FOR_LOGGING(message, Info)
167 #define  LogWarning(message) DPL_MACRO_FOR_LOGGING(message, Warning)
168 #define  LogError(message) DPL_MACRO_FOR_LOGGING(message, Error)
169 #define  LogPedantic(message) DPL_MACRO_FOR_LOGGING(message, Pedantic)
170
171 #endif // DPL_LOG_H