Fix internal tests
[platform/core/security/cert-svc.git] / vcore / 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 DPL_LOG_H
23 #define DPL_LOG_H
24
25 #include <sstream>
26 #include <list>
27 #include <unordered_map>
28 #include <string>
29
30 #include <dpl/singleton.h>
31 #include <dpl/noncopyable.h>
32
33 #include <dpl/log/abstract_log_provider.h>
34
35 namespace VcoreDPL {
36 namespace Log {
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 : private Noncopyable
44 {
45 public:
46     LogSystem();
47     virtual ~LogSystem();
48
49     AbstractLogProvider::LogLevel GetLogLevel() const { return m_level; }
50
51     void Log(AbstractLogProvider::LogLevel level,
52              const char *message,
53              const char *filename,
54              int line,
55              const char *function) const;
56
57
58     /**
59      * Set default's DLOG provider Tag
60      */
61     void SetTag(const char *tag);
62
63     /**
64      * Add abstract provider to providers list
65      *
66      * @notice Ownership is transfered to LogSystem and deleted upon exit
67      */
68     void AddProvider(AbstractLogProvider *provider);
69
70     /**
71      * Remove abstract provider from providers list
72      */
73     void RemoveProvider(AbstractLogProvider *provider);
74
75     /**
76      * Selects given provider by name (overwrites environment setting)
77      *
78      * Throws std::out_of_range exception if not found.
79      */
80     void SelectProvider(const std::string& name);
81
82     /**
83      * Sets log level (overwrites environment settings)
84      */
85     void SetLogLevel(const char* level);
86
87 private:
88     void RemoveProviders();
89
90     typedef std::list<AbstractLogProvider *> AbstractLogProviderPtrList;
91     AbstractLogProviderPtrList m_providers;
92     AbstractLogProvider::LogLevel m_level;
93
94     typedef AbstractLogProvider *(*ProviderFn)();
95     /*
96      * It cannot be global as it is used in library constructor and we can't be sure which
97      * constructor is called first: library's or new_provider's.
98      */
99     std::unordered_map<std::string, ProviderFn> m_providerCtor;
100 };
101
102 /*
103  * Replacement low overhead null logging class
104  */
105 class NullStream
106 {
107   public:
108     NullStream() {}
109
110     template <typename T>
111     NullStream& operator<<(const T&)
112     {
113         return *this;
114     }
115 };
116
117 /**
118  * Log system singleton
119  */
120 typedef Singleton<LogSystem> LogSystemSingleton;
121 }
122 } // namespace VcoreDPL
123
124
125 /* avoid warnings about unused variables */
126 #define DPL_MACRO_DUMMY_LOGGING(message, level)                                 \
127     do {                                                                        \
128         VcoreDPL::Log::NullStream ns;                                           \
129         ns << message;                                                          \
130     } while (0)
131
132 #define DPL_MACRO_FOR_LOGGING(message, level)                                   \
133 do                                                                              \
134 {                                                                               \
135     if (level > VcoreDPL::Log::AbstractLogProvider::LogLevel::None &&           \
136         VcoreDPL::Log::LogSystemSingleton::Instance().GetLogLevel() >= level)   \
137     {                                                                           \
138         std::ostringstream platformLog;                                         \
139         platformLog << message;                                                 \
140         VcoreDPL::Log::LogSystemSingleton::Instance().Log(level,                \
141                                                      platformLog.str().c_str(), \
142                                                      __FILE__,                  \
143                                                      __LINE__,                  \
144                                                      __FUNCTION__);             \
145     }                                                                           \
146 } while (0)
147
148 #define  LogError(message) DPL_MACRO_FOR_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Error)
149
150 #ifdef BUILD_TYPE_DEBUG
151     #define LogDebug(message)    DPL_MACRO_FOR_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Debug)
152     #define LogInfo(message)     DPL_MACRO_FOR_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Info)
153     #define LogWarning(message)  DPL_MACRO_FOR_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Warning)
154     #define LogPedantic(message) DPL_MACRO_FOR_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Pedantic)
155 #else
156     #define LogDebug(message)    DPL_MACRO_DUMMY_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Debug)
157     #define LogInfo(message)     DPL_MACRO_DUMMY_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Info)
158     #define LogWarning(message)  DPL_MACRO_DUMMY_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Warning)
159     #define LogPedantic(message) DPL_MACRO_DUMMY_LOGGING(message, VcoreDPL::Log::AbstractLogProvider::LogLevel::Pedantic)
160 #endif // BUILD_TYPE_DEBUG
161
162 #endif // DPL_LOG_H