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