Thrill  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
logger.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * tlx/logger.hpp
3  *
4  * Simple logging methods using ostream output.
5  *
6  * Part of tlx - http://panthema.net/tlx
7  *
8  * Copyright (C) 2015-2017 Timo Bingmann <[email protected]>
9  *
10  * All rights reserved. Published under the Boost Software License, Version 1.0
11  ******************************************************************************/
12 
13 #ifndef TLX_LOGGER_HEADER
14 #define TLX_LOGGER_HEADER
15 
16 #include <sstream>
17 #include <string>
18 
19 namespace tlx {
20 
21 /*!
22 
23 \brief LOG and sLOG for development and debugging
24 
25 This is a short description of how to use \ref LOG and \ref sLOG for rapid
26 development of modules with debug output, and how to **keep it afterwards**.
27 
28 There are two classes Logger and SpacingLogger, but one does not use these
29 directly.
30 
31 Instead there are the macros: \ref LOG and \ref sLOG that can be used as such:
32 \code
33 LOG << "This will be printed with a newline";
34 sLOG << "Print variables a" << a << "b" << b << "c" << c;
35 \endcode
36 
37 There macros only print the lines if the boolean variable **debug** is
38 true. This variable is searched for in the scope of the LOG, which means it can
39 be set or overridden in the function scope, the class scope, from **inherited
40 classes**, or even the global scope.
41 
42 \code
43 class MyClass
44 {
45  static constexpr bool debug = true;
46 
47  void func1()
48  {
49  LOG << "Hello World";
50 
51  LOG0 << "This is temporarily disabled.";
52  }
53 
54  void func2()
55  {
56  static constexpr bool debug = false;
57  LOG << "This is not printed any more.";
58 
59  LOG1 << "But this is forced.";
60  }
61 };
62 \endcode
63 
64 There are two variation of \ref LOG and \ref sLOG : append 0 or 1 for
65 temporarily disabled or enabled debug lines. These macros are then \ref LOG0,
66 \ref LOG1, \ref sLOG0, and \ref sLOG1. The suffix overrides the debug variable's
67 setting.
68 
69 After a module works as intended, one can just set `debug = false`, and all
70 debug output will disappear and be optimized out.
71  */
72 class Logger
73 {
74 private:
75  //! collector stream
76  std::ostringstream oss_;
77 
78 public:
79  //! mutex synchronized output to std::cout
80  static void Output(const char* str);
81  //! mutex synchronized output to std::cout
82  static void Output(const std::string& str);
83 
84  //! output any type, including io manipulators
85  template <typename AnyType>
86  Logger& operator << (const AnyType& at) {
87  oss_ << at;
88  return *this;
89  }
90 
91  //! destructor: output a newline
92  ~Logger();
93 };
94 
95 /*!
96  * A logging class which outputs spaces between elements pushed via
97  * operator<<. Depending on the real parameter the output may be suppressed.
98  */
100 {
101 private:
102  //! true until the first element it outputted.
103  bool first_ = true;
104 
105  //! collector stream
106  std::ostringstream oss_;
107 
108 public:
109  //! output any type, including io manipulators
110  template <typename AnyType>
111  SpacingLogger& operator << (const AnyType& at) {
112  if (!first_) oss_ << ' ';
113  else first_ = false;
114 
115  oss_ << at;
116 
117  return *this;
118  }
119 
120  //! destructor: output a newline
121  ~SpacingLogger();
122 };
123 
125 {
126 public:
127  void operator & (Logger&) { }
129 };
130 
131 //! Explicitly specify the condition for logging
132 #define LOGC(cond) \
133  !(cond) ? (void)0 : ::tlx::LoggerVoidify() & ::tlx::Logger()
134 
135 //! Default logging method: output if the local debug variable is true.
136 #define LOG LOGC(debug)
137 
138 //! Override default output: never or always output log.
139 #define LOG0 LOGC(false)
140 #define LOG1 LOGC(true)
141 
142 //! Explicitly specify the condition for logging
143 #define sLOGC(cond) \
144  !(cond) ? (void)0 : ::tlx::LoggerVoidify() & ::tlx::SpacingLogger()
145 
146 //! Default logging method: output if the local debug variable is true.
147 #define sLOG sLOGC(debug)
148 
149 //! Override default output: never or always output log.
150 #define sLOG0 sLOGC(false)
151 #define sLOG1 sLOGC(true)
152 
153 } // namespace tlx
154 
155 #endif // !TLX_LOGGER_HEADER
156 
157 /******************************************************************************/
LOG and sLOG for development and debugging.
Definition: logger.hpp:72
A logging class which outputs spaces between elements pushed via operator<<.
Definition: logger.hpp:99
bool first_
true until the first element it outputted.
Definition: logger.hpp:103
~SpacingLogger()
destructor: output a newline
Definition: logger.cpp:45
static void Output(const char *str)
mutex synchronized output to std::cout
Definition: logger.cpp:26
std::ostringstream oss_
collector stream
Definition: logger.hpp:76
SpacingLogger & operator<<(const AnyType &at)
output any type, including io manipulators
Definition: logger.hpp:111
std::basic_string< char, std::char_traits< char >, Allocator< char > > string
string with Manager tracking
Definition: allocator.hpp:220
~Logger()
destructor: output a newline
Definition: logger.cpp:40
void operator&(Logger &)
Definition: logger.hpp:127
std::ostringstream oss_
collector stream
Definition: logger.hpp:106
Logger & operator<<(const AnyType &at)
output any type, including io manipulators
Definition: logger.hpp:86