automotive-message-broker  0.14.803
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Groups Pages
logger.h
1 /*
2 Copyright (C) 2012 Intel Corporation
3 
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.
8 
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.
13 
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
17 */
18 
19 #ifndef LOGGER_H
20 #define LOGGER_H
21 
22 #include <fstream>
23 #include <ostream>
24 #include <string>
25 #include <sstream>
26 #include <sys/time.h>
27 #include <sys/types.h>
28 
29 // Comment this line if you don't need multithread support
30 #define LOGGER_MULTITHREAD
31 #ifdef LOGGER_MULTITHREAD
32 #include <pthread.h>
33 #endif
34 
39 namespace CUtil {
40 
52 class Logger
53 {
57  enum loggerConf_ {L_nofile_ = 1 << 0,
58  L_file_ = 1 << 1,
59  L_noscreen_ = 1 << 2,
60  L_screen_ = 1 << 3};
61 
62 #ifdef LOGGER_MULTITHREAD
63 
66  static pthread_mutex_t lock_;
67 #endif
68 
69  bool configured_;
70 
74  static Logger* m_;
75 
80  std::string logFile_;
81 
86  std::string logFileName_;
87 
95  loggerConf_ configuration_;
96 
100  std::ofstream out_;
101 
105  int64_t initialTime_usec_;
106 
110  unsigned int fileVerbosityLevel_;
111 
115  unsigned int screenVerbosityLevel_;
116 
117  Logger();
118  ~Logger();
119 
123  inline static void lock();
124 
128  inline static void unlock();
129 
130 public:
131 
137  enum Level {
138  EError = 0,
139  EWarning,
140  EMessage,
141  EInfo,
142  ETrace,
143  EDebug
144  };
145 
155  #define DEBUG_CONF(outputFile, \
156  configuration, \
157  fileVerbosityLevel, \
158  screenVerbosityLevel) { \
159  CUtil::Logger::getInstance().configure(outputFile, \
160  configuration, \
161  fileVerbosityLevel, \
162  screenVerbosityLevel); \
163  }
164 
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()); \
179  }
180 
181  #ifndef _LOGGER_NO_LOG
182 
187  #define LOG_ERROR(M) LOGGER(CUtil::Logger::EError, M)
188 
192  #define LOG_WARNING(M) LOGGER(CUtil::Logger::EWarning, M)
193 
197  #define LOG_MESSAGE(M) LOGGER(CUtil::Logger::EMessage, M)
198 
202  #define LOG_INFO(M) LOGGER(CUtil::Logger::EInfo, M)
203 
207  #define LOG_TRACE(M) LOGGER(CUtil::Logger::ETrace, M)
208 
212  #define LOG_DEBUG(M) LOGGER(CUtil::Logger::EDebug, M)
213 
214  #else
215 
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) {}
222 
223  #endif
224 
228  typedef loggerConf_ loggerConf;
232  static const loggerConf file_on= L_nofile_;
236  static const loggerConf file_off= L_file_;
240  static const loggerConf screen_on= L_noscreen_;
244  static const loggerConf screen_off= L_screen_;
245 
246  static Logger& getInstance();
247 
248  void print(const unsigned int verbosityLevel,
249  const std::string& sourceFile,
250  const int codeLine,
251  const std::string& message);
252 
253  void configure (const std::string& outputFile,
254  const loggerConf configuration,
255  const int fileVerbosityLevel,
256  const int screenVerbosityLevel);
257 
261  inline void flush()
262  {
263  out_.flush();
264  }
265 };
266 
273 inline Logger::loggerConf operator|
274  (Logger::loggerConf __a, Logger::loggerConf __b)
275 {
276  return Logger::loggerConf(static_cast<int>(__a) |
277  static_cast<int>(__b));
278 }
279 
286 inline Logger::loggerConf operator&
287  (Logger::loggerConf __a, Logger::loggerConf __b)
288 {
289  return Logger::loggerConf(static_cast<int>(__a) &
290  static_cast<int>(__b)); }
291 
292 }
293 
294 #endif /* LOGGER_H */
295