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