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