2 Copyright (C) 2012 Intel Corporation
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with this library; if not, write to the Free Software
16 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27 #include <sys/types.h>
29 // Comment this line if you don't need multithread support
30 #define LOGGER_MULTITHREAD
31 #ifdef LOGGER_MULTITHREAD
35 /** \addtogroup libamb-plugins-common
42 * \brief Simple logger to log messages on file and console.
44 * This is the implementation of a simple logger in C++. It is implemented
45 * as a Singleton, so it can be easily called through two DEBUG macros.
47 * It allows to log on both file and screen, and to specify a verbosity
48 * threshold for both of them.
55 * Type used for the configuration
57 enum loggerConf_ {L_nofile_ = 1 << 0,
62 #ifdef LOGGER_MULTITHREAD
64 * \brief Lock for mutual exclusion between different threads
66 static pthread_mutex_t lock_;
72 * \brief Pointer to the unique Logger (i.e., Singleton)
77 * \brief Initial part of the name of the file used for Logging.
78 * Date and time are automatically appended.
83 * \brief Initial part of the name of the file used for Logging.
84 * Date and time are automatically appended.
86 std::string logFileName_;
89 * \brief Current configuration of the logger.
90 * Variable to know if logging on file and on screen are enabled.
91 * Note that if the log on file is enabled, it means that the
92 * logger has been already configured, therefore the stream is
95 loggerConf_ configuration_;
98 * \brief Stream used when logging on a file
103 * \brief Initial time (used to print relative times)
105 int64_t initialTime_usec_;
108 * \brief Verbosity threshold for files
110 unsigned int fileVerbosityLevel_;
113 * \brief Verbosity threshold for screen
115 unsigned int screenVerbosityLevel_;
121 * \brief Method to lock in case of multithreading
123 inline static void lock();
126 * \brief Method to unlock in case of multithreading
128 inline static void unlock();
147 * Macro to create and configure logger.
150 * Example of configuration of the Logger:
152 * DEBUG_CONF("outputfile", Logger::file_on|Logger::screen_on, DBG_DEBUG, DBG_ERROR);
155 #define DEBUG_CONF(outputFile, \
157 fileVerbosityLevel, \
158 screenVerbosityLevel) { \
159 CUtil::Logger::getInstance().configure(outputFile, \
161 fileVerbosityLevel, \
162 screenVerbosityLevel); \
166 * \brief Macro to print log messages.
169 * Example of usage of the Logger:
171 * LOGGER(DBG_DEBUG, "hello " << "world");
174 #define LOGGER(priority, msg) { \
175 std::ostringstream __debug_stream__; \
176 __debug_stream__ << msg; \
177 CUtil::Logger::getInstance().print(priority, __FILE__, __LINE__, \
178 __debug_stream__.str()); \
181 #ifndef _LOGGER_NO_LOG
184 * Macro to log errors.
187 #define LOG_ERROR(M) LOGGER(CUtil::Logger::EError, M)
189 * Macro to log warnings.
192 #define LOG_WARNING(M) LOGGER(CUtil::Logger::EWarning, M)
194 * Macro to log messages.
197 #define LOG_MESSAGE(M) LOGGER(CUtil::Logger::EMessage, M)
199 * Macro to log info messages.
202 #define LOG_INFO(M) LOGGER(CUtil::Logger::EInfo, M)
204 * Macro to log trace messages.
207 #define LOG_TRACE(M) LOGGER(CUtil::Logger::ETrace, M)
209 * Macro to log debug messages.
212 #define LOG_DEBUG(M) LOGGER(CUtil::Logger::EDebug, M)
216 #define LOG_ERROR(M) {}
217 #define LOG_WARNING(M) {}
218 #define LOG_MESSAGE(M) {}
219 #define LOG_INFO(M) {}
220 #define LOG_TRACE(M) {}
221 #define LOG_DEBUG(M) {}
226 * Type used for the configuration
228 typedef loggerConf_ loggerConf;
230 * Disable logging to file
232 static const loggerConf file_on= L_nofile_;
234 * Enable logging to file
236 static const loggerConf file_off= L_file_;
238 * Enable logging to screen
240 static const loggerConf screen_on= L_noscreen_;
242 * Disable logging to screen
244 static const loggerConf screen_off= L_screen_;
246 static Logger& getInstance();
248 void print(const unsigned int verbosityLevel,
249 const std::string& sourceFile,
251 const std::string& message);
253 void configure (const std::string& outputFile,
254 const loggerConf configuration,
255 const int fileVerbosityLevel,
256 const int screenVerbosityLevel);
259 * Flush output buffer
268 * operator| - Can be used in Logger::configure
269 * \param __a Configuration (i.e., log on file and on screen on or off).
270 * \param __b Configuration (i.e., log on file and on screen on or off).
271 * \return Configuration (i.e., log on file and on screen on or off).
273 inline Logger::loggerConf operator|
274 (Logger::loggerConf __a, Logger::loggerConf __b)
276 return Logger::loggerConf(static_cast<int>(__a) |
277 static_cast<int>(__b));
281 * operator& - Can be used in Logger::configure
282 * \param __a Configuration (i.e., log on file and on screen on or off).
283 * \param __b Configuration (i.e., log on file and on screen on or off).
284 * \return Configuration (i.e., log on file and on screen on or off).
286 inline Logger::loggerConf operator&
287 (Logger::loggerConf __a, Logger::loggerConf __b)
289 return Logger::loggerConf(static_cast<int>(__a) &
290 static_cast<int>(__b)); }
294 #endif /* LOGGER_H */