hpp-util  4.9.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 // This file is part of the hpp-util.
4 //
5 // This software is provided "as is" without warranty of any kind,
6 // either expressed or implied, including but not limited to the
7 // implied warranties of fitness for a particular purpose.
8 //
9 // See the COPYING file for more information.
10 
11 #ifndef HPP_UTIL_DEBUG_HH
12 # define HPP_UTIL_DEBUG_HH
13 # include <cstdlib>
14 # include <ostream>
15 # include <fstream>
16 # include <sstream>
17 # include <vector>
18 
19 # include <hpp/util/config.hh>
20 # include <hpp/util/indent.hh>
21 
22 namespace hpp
23 {
24  namespace debug
25  {
26  class Output;
27  class JournalOutput;
28  class ConsoleOutput;
29 
30  class Channel;
31  } // end of namespace debug
32 } // end of namespace hpp.
33 
34 
35 namespace hpp
36 {
37  namespace debug
38  {
61  HPP_UTIL_DLLAPI std::string getPrefix (const std::string& packageName);
62 
72  HPP_UTIL_DLLAPI std::string getFilename (const std::string& filename,
73  const std::string& packageName);
74 
84  class HPP_UTIL_DLLAPI Output
85  {
86  public:
87  explicit Output ();
88  virtual ~Output ();
89 
90  virtual void
91  write (const Channel& channel,
92  char const* file,
93  int line,
94  char const* function,
95  const std::string& data) = 0;
96 
97  virtual void
98  write (const Channel& channel,
99  char const* file,
100  int line,
101  char const* function,
102  const std::stringstream& data) = 0;
103 
104  protected:
105  std::ostream&
106  writePrefix (std::ostream& stream,
107  const Channel& channel,
108  char const* file,
109  int line,
110  char const* function);
111  };
112 
123  class HPP_UTIL_DLLAPI Channel
124  {
125  public:
126  typedef std::vector<Output*> subscribers_t;
127 
128  explicit Channel (char const* label,
129  const subscribers_t& subscribers);
130  virtual ~Channel ();
131 
132  void write (char const* file,
133  int line,
134  char const* function,
135  const std::string& data);
136 
137  void write (char const* file,
138  int line,
139  char const* function,
140  const std::stringstream& data);
141 
142  const char* label () const;
143  private:
144  const char* label_;
145  subscribers_t subscribers_;
146  };
147 
149  class HPP_UTIL_DLLAPI JournalOutput : public Output
150  {
151  public:
152  explicit JournalOutput (std::string filename);
153  ~JournalOutput ();
154 
155  void write (const Channel& channel,
156  char const* file,
157  int line,
158  char const* function,
159  const std::string& data);
160 
161  void write (const Channel& channel,
162  char const* file,
163  int line,
164  char const* function,
165  const std::stringstream& data);
166 
167  std::string getFilename () const;
168 
169  private:
170  std::string filename;
171  std::string lastFunction;
172  std::ofstream stream;
173  };
174 
176  class HPP_UTIL_DLLAPI ConsoleOutput : public Output
177  {
178  public:
179  explicit ConsoleOutput ();
180  ~ConsoleOutput ();
181  void write (const Channel& channel,
182  char const* file,
183  int line,
184  char const* function,
185  const std::string& data);
186  void write (const Channel& channel,
187  char const* file,
188  int line,
189  char const* function,
190  const std::stringstream& data);
191  };
192 
194  class HPP_UTIL_DLLAPI Logging
195  {
196  public:
197  explicit Logging ();
198  ~Logging ();
199 
206 
209 
212 
215 
218 
221  };
222  } // end of namespace debug
223 } // end of namespace hpp.
224 
225 
226 namespace hpp
227 {
228  namespace debug
229  {
231  extern HPP_UTIL_DLLAPI Logging logging;
232  } // end of namespace debug
233 } // end of namespace hpp
234 
235 
236 # ifdef HPP_DEBUG
237 
240 
242 # define hppDebug(statement) \
243  do { \
244  using namespace ::hpp::debug; \
245  { statement; } \
246  } while(0)
247 
249 # define hppDebugStatement(statement) \
250  statement
251 
253 
256 
260 # define hppDout(channel, data) \
261  do { \
262  using namespace hpp; \
263  using namespace ::hpp::debug; \
264  std::stringstream __ss; \
265  __ss << data << iendl; \
266  logging.channel.write (__FILE__, __LINE__, __PRETTY_FUNCTION__, \
267  __ss); \
268  } while (0)
269 
273 # define hppDoutFatal(channel, data) \
274  do { \
275  using namespace hpp; \
276  using namespace ::hpp::debug; \
277  std::stringstream __ss; \
278  __ss << data << iendl; \
279  logging.channel.write ( __FILE__, __LINE__, __PRETTY_FUNCTION__, \
280  __ss); \
281  ::std::exit(EXIT_FAILURE); \
282  } while (1)
283 
285 
286 # else
287 
288 # define hppDebug(statement) \
289  do { \
290  } while (0)
291 # define hppDebugStatement(statement)
292 # define hppDout(channel, data) \
293  do { \
294  } while (0)
295 # define hppDoutFatal(channel, data) \
296  do { \
297  using namespace hpp; \
298  ::std::cerr << data << iendl; \
299  ::std::exit (EXIT_FAILURE); \
300  } while (1)
301 
302 # endif // HPP_DEBUG
303 
304 #endif
JournalOutput benchmarkJournal
Logs to benchmark journal file (i.e. benchmark.XXX.log).
Definition: debug.hh:205
Definition: assertion.hh:24
Channel info
Technical information and debugging.
Definition: debug.hh:217
Logging in journal file in the logging directory.
Definition: debug.hh:149
std::vector< Output * > subscribers_t
Definition: debug.hh:126
Channel notice
User-oriented information.
Definition: debug.hh:214
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:96
Channel benchmark
Benchmark information.
Definition: debug.hh:220
Debugging output.
Definition: debug.hh:84
Channel warning
Non-fatal problems channel.
Definition: debug.hh:211
Channel error
Fatal problems channel.
Definition: debug.hh:208
HPP_UTIL_DLLAPI Logging logging
Benchmark information.
Definition: debug.cc:309
Logging in console (std::cerr).
Definition: debug.hh:176
Receive debugging information.
Definition: debug.hh:123
JournalOutput journal
Logs to main journal file (i.e. journal.XXX.log).
Definition: debug.hh:203
ConsoleOutput console
Logs to console (i.e. stderr).
Definition: debug.hh:201
Logging class owns all channels and outputs.
Definition: debug.hh:194
HPP_UTIL_DLLAPI std::string getPrefix(const std::string &packageName)
Compute the logging prefix.
Definition: debug.cc:75