Always print warning log messages
[platform/core/security/security-manager.git] / src / dpl / 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 SECURITYMANAGER_LOG_H
23 #define SECURITYMANAGER_LOG_H
24
25 #include <dpl/singleton.h>
26 #include <dpl/noncopyable.h>
27 #include <dpl/log/abstract_log_provider.h>
28 #include <sstream>
29 #include <list>
30
31 namespace SecurityManager {
32 namespace Log {
33 /**
34  * SecurityManager log system
35  *
36  * To switch logs into old style, export
37  * DPL_USE_OLD_STYLE_LOGS before application start
38  */
39 class LogSystem :
40     private Noncopyable
41 {
42   private:
43     typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
44     AbstractLogProviderPtrList m_providers;
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     void SetTag(const char *tag);
94
95     /**
96      * Add abstract provider to providers list
97      *
98      * @notice Ownership is transfered to LogSystem and deleted upon exit
99      */
100     void AddProvider(AbstractLogProvider *provider);
101
102     /**
103      * Remove abstract provider from providers list
104      */
105     void RemoveProvider(AbstractLogProvider *provider);
106 };
107
108 /*
109  * Replacement low overhead null logging class
110  */
111 class NullStream
112 {
113   public:
114     NullStream() {}
115
116     template <typename T>
117     NullStream& operator<<(const T&)
118     {
119         return *this;
120     }
121 };
122
123 /**
124  * Log system singleton
125  */
126 typedef Singleton<LogSystem> LogSystemSingleton;
127 }
128 } // namespace SecurityManager
129
130 //
131 // Log support
132 //
133 //
134
135 /* avoid warnings about unused variables */
136 #define DPL_MACRO_DUMMY_LOGGING(message, function)                         \
137     do {                                                                   \
138         SecurityManager::Log::NullStream ns;                                \
139         ns << message;                                                     \
140     } while (0)
141
142 #define DPL_MACRO_FOR_LOGGING(message, function)                           \
143 do                                                                         \
144 {                                                                          \
145     if (SecurityManager::Log::LogSystemSingleton::Instance().IsLoggingEnabled())   \
146     {                                                                      \
147         std::ostringstream platformLog;                                    \
148         platformLog << message;                                            \
149         SecurityManager::Log::LogSystemSingleton::Instance().function(      \
150             platformLog.str().c_str(),                                     \
151             __FILE__, __LINE__, __FUNCTION__);                             \
152     }                                                                      \
153 } while (0)
154
155 /* Errors and warnings must be always logged. */
156 #define  LogError(message) DPL_MACRO_FOR_LOGGING(message, Error)
157 #define  LogWarning(message) DPL_MACRO_FOR_LOGGING(message, Warning)
158
159 #ifdef BUILD_TYPE_DEBUG
160     #define LogDebug(message) DPL_MACRO_FOR_LOGGING(message, Debug)
161     #define LogInfo(message) DPL_MACRO_FOR_LOGGING(message, Info)
162     #define LogPedantic(message) DPL_MACRO_FOR_LOGGING(message, Pedantic)
163 #else
164     #define LogDebug(message) DPL_MACRO_DUMMY_LOGGING(message, Debug)
165     #define LogInfo(message) DPL_MACRO_DUMMY_LOGGING(message, Info)
166     #define LogPedantic(message) DPL_MACRO_DUMMY_LOGGING(message, Pedantic)
167 #endif // BUILD_TYPE_DEBUG
168
169 #endif // SECURITYMANAGER_LOG_H