base-logging
logging_iostream_style.h
Go to the documentation of this file.
1 /*
2  * @file logging_iostream_style.h
3  * @author Jan Vogelgesang, jan.vogelgesang@dfki.de
4  *
5  * @brief Wrapper for logging.h, adds iostream-style logging
6  *
7  * @details adds an iostream-style interface to logging.h, appends trailing '_S' for the
8  * iostream interface. Example:
9  *
10  * LOG_WARN_S << "some warning message";
11  *
12  * no trailing endl is required (similar to logging_printf_style.h).
13  * Configuration is done through LOG_CONFIGURE from logging_printf_style.h.
14  *
15  * based on an idea from Petru Marginean, presented at http://drdobbs.com/cpp/201804215
16  */
17 
18 #ifndef _BASE_LOGGING_IOSTREAM_STYLE_H_
19 #define _BASE_LOGGING_IOSTREAM_STYLE_H_
20 
21 #include <base-logging/logging/logging_printf_style.h>
22 #include <sstream>
23 
24 // To allow for the streaming syntax, relying on 'dead code elimination'
25 // of the compiler, i.e. given the if prio > BASE_LOG_PRIORITY then the else branch
26 // is never reachable. Compilers with 'dead code elimination'
27 // will remove the else branch completely (tested on gcc 4.4 with -o0).
28 // Using __PRETTY_FUNCTION__ when using gcc otherwise __func__ to show current function
29 #ifdef __GNUC__
30 #define LOG_STREAM(PRIO) if(PRIO > BASE_LOG_PRIORITY) ; else base::logging::LogStream().get(PRIO,__PRETTY_FUNCTION__, __FILE__, __LINE__, __STRINGIFY(BASE_LOG_NAMESPACE))
31 #else
32 #define LOG_STREAM(PRIO) if(PRIO > BASE_LOG_PRIORITY) ; else base::logging::LogStream().get(PRIO,__func__, __FILE__, __LINE__, __STRINGIFY(BASE_LOG_NAMESPACE))
33 #endif
34 
35 
36 // MACROS for general usage of streaming logging
37 // Example usage:
38 // LOG_WARN_S << "this is a message on level warn";
39 //
40 // NOTE:'std::endl' is inserted automatically at the end of the statement
41 //
42 #define LOG_FATAL_S LOG_STREAM(base::logging::FATAL)
43 #define LOG_ERROR_S LOG_STREAM(base::logging::ERROR)
44 #define LOG_WARN_S LOG_STREAM(base::logging::WARN)
45 #define LOG_INFO_S LOG_STREAM(base::logging::INFO)
46 #define LOG_DEBUG_S LOG_STREAM(base::logging::DEBUG)
47 
48 namespace base {
49 
50 namespace logging {
51 
52 class LogStream
53 {
54 public:
55  LogStream() {};
56  virtual ~LogStream()
57  {
58  // forward it to class Logger
59  Logger::getInstance()->logBuffer(mPrio,mpFuncName,mpFileName,mLineNumber,mNamespace,os.str().c_str());
60  }
61 
62 
63  std::ostringstream& get(Priority prio,const char* pFuncName, const char* pFileName, int lineNumber, const char* name_space)
64  {
65  mPrio = prio;
66  mpFuncName = pFuncName;
67  mpFileName = pFileName;
68  mLineNumber = lineNumber;
69  mNamespace = name_space;
70  return os;
71  }
72 
73 protected:
74  std::ostringstream os;
75 private:
76  LogStream(const LogStream&);
77  LogStream& operator =(const LogStream&);
78 private:
79  Priority mPrio;
80  const char* mpFuncName;
81  const char* mpFileName;
82  int mLineNumber;
83  const char* mNamespace;
84 };
85 
86 
87 } //namespace logging
88 } //namespace base
89 
90 #endif // _BASE_LOGGING_IOSTREAM_STYLE_H_
Definition: logging_iostream_style.h:52
LogStream()
Definition: logging_iostream_style.h:55
std::ostringstream os
Definition: logging_iostream_style.h:74
virtual ~LogStream()
Definition: logging_iostream_style.h:56
Priority
Definition: logging_printf_style.h:173
static Derived * getInstance()
Definition: Singleton.hpp:17