hpp-util  5.0.0
Debugging tools for the HPP project.
debug.hh
Go to the documentation of this file.
1 // Copyright (C) 2008, 2009 by Florent Lamiraux, Thomas Moulard, CNRS.
2 //
3 
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // 1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
18 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
19 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
20 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
26 // DAMAGE.
27 //
28 // This software is provided "as is" without warranty of any kind,
29 // either expressed or implied, including but not limited to the
30 // implied warranties of fitness for a particular purpose.
31 //
32 // See the COPYING file for more information.
33 
34 #ifndef HPP_UTIL_DEBUG_HH
35 #define HPP_UTIL_DEBUG_HH
36 #include <cstdlib>
37 #include <fstream>
38 #include <hpp/util/config.hh>
39 #include <hpp/util/indent.hh>
40 #include <ostream>
41 #include <sstream>
42 #include <vector>
43 
44 namespace hpp {
45 namespace debug {
46 class Output;
47 class JournalOutput;
48 class ConsoleOutput;
49 
50 class Channel;
51 } // end of namespace debug
52 } // end of namespace hpp.
53 
54 namespace hpp {
55 namespace debug {
78 HPP_UTIL_DLLAPI std::string getPrefix(const std::string& packageName);
79 
89 HPP_UTIL_DLLAPI std::string getFilename(const std::string& filename,
90  const std::string& packageName);
91 
92 namespace verbosityLevel {
93 constexpr int none = 0;
94 constexpr int error = 10;
95 constexpr int warning = 20;
96 constexpr int notice = 30;
97 constexpr int info = 40;
98 constexpr int benchmark = -1;
99 } // namespace verbosityLevel
100 
103 
105 HPP_UTIL_DLLAPI void setVerbosityLevel(int level);
106 
108 
109 HPP_UTIL_DLLAPI void enableBenchmark(bool enable);
110 
111 inline bool isChannelEnabled(int channel) {
112  if (channel == verbosityLevel::benchmark) return isBenchmarkEnabled();
113  return getVerbosityLevel() >= channel;
114 }
115 
126  public:
127  explicit Output();
128  virtual ~Output();
129 
130  virtual void write(const Channel& channel, char const* file, int line,
131  char const* function, const std::string& data) = 0;
132 
133  virtual void write(const Channel& channel, char const* file, int line,
134  char const* function, const std::stringstream& data) = 0;
135 
136  protected:
137  std::ostream& writePrefix(std::ostream& stream, const Channel& channel,
138  char const* file, int line, char const* function);
139 };
140 
152  public:
153  typedef std::vector<Output*> subscribers_t;
154 
155  explicit Channel(char const* label, const subscribers_t& subscribers);
156  virtual ~Channel();
157 
158  void write(char const* file, int line, char const* function,
159  const std::string& data);
160 
161  void write(char const* file, int line, char const* function,
162  const std::stringstream& data);
163 
164  const char* label() const;
165 
166  private:
167  const char* label_;
168  subscribers_t subscribers_;
169 };
170 
173  public:
174  explicit JournalOutput(std::string filename);
175  ~JournalOutput();
176 
177  void write(const Channel& channel, char const* file, int line,
178  char const* function, const std::string& data);
179 
180  void write(const Channel& channel, char const* file, int line,
181  char const* function, const std::stringstream& data);
182 
183  std::string getFilename() const;
184 
185  private:
186  std::string filename;
187  std::string lastFunction;
188  std::ofstream stream;
189 };
190 
193  public:
194  explicit ConsoleOutput();
195  ~ConsoleOutput();
196  void write(const Channel& channel, char const* file, int line,
197  char const* function, const std::string& data);
198  void write(const Channel& channel, char const* file, int line,
199  char const* function, const std::stringstream& data);
200 };
201 
204  public:
205  explicit Logging();
206  ~Logging();
207 
214 
217 
220 
223 
226 
229 };
230 } // end of namespace debug
231 } // end of namespace hpp.
232 
233 namespace hpp {
234 namespace debug {
237 } // end of namespace debug
238 } // end of namespace hpp
239 
240 #ifdef HPP_DEBUG
241 
244 
246 #define hppDebug(statement) \
247  do { \
248  using namespace ::hpp::debug; \
249  { statement; } \
250  } while (0)
251 
253 #define hppDebugStatement(statement) statement
254 
256 
259 
264 #define hppDout(channel, data) \
265  do { \
266  using namespace hpp; \
267  using namespace ::hpp::debug; \
268  if (isChannelEnabled(verbosityLevel::channel)) { \
269  std::stringstream __ss; \
270  __ss << data << iendl; \
271  logging.channel.write(__FILE__, __LINE__, __PRETTY_FUNCTION__, __ss); \
272  } \
273  } while (0)
274 
279 #define hppDoutFatal(channel, data) \
280  do { \
281  using namespace hpp; \
282  using namespace ::hpp::debug; \
283  std::stringstream __ss; \
284  __ss << data << iendl; \
285  logging.channel.write(__FILE__, __LINE__, __PRETTY_FUNCTION__, __ss); \
286  ::std::exit(EXIT_FAILURE); \
287  } while (1)
288 
290 
291 #else
292 
293 #define hppDebug(statement) \
294  do { \
295  } while (0)
296 #define hppDebugStatement(statement)
297 #define hppDout(channel, data) \
298  do { \
299  } while (0)
300 #define hppDoutFatal(channel, data) \
301  do { \
302  using namespace hpp; \
303  ::std::cerr << data << iendl; \
304  ::std::exit(EXIT_FAILURE); \
305  } while (1)
306 
307 #endif // HPP_DEBUG
308 
309 #endif
HPP_UTIL_DLLAPI int getVerbosityLevel()
Get the verbosity level.
Definition: debug.cc:104
constexpr int warning
Definition: debug.hh:95
JournalOutput benchmarkJournal
Logs to benchmark journal file (i.e. benchmark.XXX.log).
Definition: debug.hh:213
constexpr int benchmark
Definition: debug.hh:98
Definition: assertion.hh:45
Channel info
Technical information and debugging.
Definition: debug.hh:225
constexpr int error
Definition: debug.hh:94
Logging in journal file in the logging directory.
Definition: debug.hh:172
std::vector< Output * > subscribers_t
Definition: debug.hh:153
Channel notice
User-oriented information.
Definition: debug.hh:222
HPP_UTIL_DLLAPI std::string getFilename(const std::string &filename, const std::string &packageName)
Compute the path of a file in the logging prefix.
Definition: debug.cc:95
Channel benchmark
Benchmark information.
Definition: debug.hh:228
HPP_UTIL_DLLAPI void enableBenchmark(bool enable)
Definition: debug.cc:110
Debugging output.
Definition: debug.hh:125
#define HPP_UTIL_DLLAPI
Definition: config.hh:88
Channel warning
Non-fatal problems channel.
Definition: debug.hh:219
HPP_UTIL_DLLAPI bool isBenchmarkEnabled()
Definition: debug.cc:108
Channel error
Fatal problems channel.
Definition: debug.hh:216
HPP_UTIL_DLLAPI Logging logging
Benchmark information.
Definition: debug.cc:249
Logging in console (std::cerr).
Definition: debug.hh:192
constexpr int notice
Definition: debug.hh:96
Receive debugging information.
Definition: debug.hh:151
JournalOutput journal
Logs to main journal file (i.e. journal.XXX.log).
Definition: debug.hh:211
ConsoleOutput console
Logs to console (i.e. stderr).
Definition: debug.hh:209
constexpr int none
Definition: debug.hh:93
Logging class owns all channels and outputs.
Definition: debug.hh:203
constexpr int info
Definition: debug.hh:97
bool isChannelEnabled(int channel)
Definition: debug.hh:111
HPP_UTIL_DLLAPI std::string getPrefix(const std::string &packageName)
Compute the logging prefix.
Definition: debug.cc:78
HPP_UTIL_DLLAPI void setVerbosityLevel(int level)
Set the verbosity level.
Definition: debug.cc:106