hpp-util  4.14.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 
102  public:
103  explicit Output();
104  virtual ~Output();
105 
106  virtual void write(const Channel& channel, char const* file, int line,
107  char const* function, const std::string& data) = 0;
108 
109  virtual void write(const Channel& channel, char const* file, int line,
110  char const* function, const std::stringstream& data) = 0;
111 
112  protected:
113  std::ostream& writePrefix(std::ostream& stream, const Channel& channel,
114  char const* file, int line, char const* function);
115 };
116 
128  public:
129  typedef std::vector<Output*> subscribers_t;
130 
131  explicit Channel(char const* label, const subscribers_t& subscribers);
132  virtual ~Channel();
133 
134  void write(char const* file, int line, char const* function,
135  const std::string& data);
136 
137  void write(char const* file, int line, char const* function,
138  const std::stringstream& data);
139 
140  const char* label() const;
141 
142  private:
143  const char* label_;
144  subscribers_t subscribers_;
145 };
146 
149  public:
150  explicit JournalOutput(std::string filename);
151  ~JournalOutput();
152 
153  void write(const Channel& channel, char const* file, int line,
154  char const* function, const std::string& data);
155 
156  void write(const Channel& channel, char const* file, int line,
157  char const* function, const std::stringstream& data);
158 
159  std::string getFilename() const;
160 
161  private:
162  std::string filename;
163  std::string lastFunction;
164  std::ofstream stream;
165 };
166 
169  public:
170  explicit ConsoleOutput();
171  ~ConsoleOutput();
172  void write(const Channel& channel, char const* file, int line,
173  char const* function, const std::string& data);
174  void write(const Channel& channel, char const* file, int line,
175  char const* function, const std::stringstream& data);
176 };
177 
180  public:
181  explicit Logging();
182  ~Logging();
183 
190 
193 
196 
199 
202 
205 };
206 } // end of namespace debug
207 } // end of namespace hpp.
208 
209 namespace hpp {
210 namespace debug {
213 } // end of namespace debug
214 } // end of namespace hpp
215 
216 #ifdef HPP_DEBUG
217 
220 
222 #define hppDebug(statement) \
223  do { \
224  using namespace ::hpp::debug; \
225  { statement; } \
226  } while (0)
227 
229 #define hppDebugStatement(statement) statement
230 
232 
235 
240 #define hppDout(channel, data) \
241  do { \
242  using namespace hpp; \
243  using namespace ::hpp::debug; \
244  std::stringstream __ss; \
245  __ss << data << iendl; \
246  logging.channel.write(__FILE__, __LINE__, __PRETTY_FUNCTION__, __ss); \
247  } while (0)
248 
253 #define hppDoutFatal(channel, data) \
254  do { \
255  using namespace hpp; \
256  using namespace ::hpp::debug; \
257  std::stringstream __ss; \
258  __ss << data << iendl; \
259  logging.channel.write(__FILE__, __LINE__, __PRETTY_FUNCTION__, __ss); \
260  ::std::exit(EXIT_FAILURE); \
261  } while (1)
262 
264 
265 #else
266 
267 #define hppDebug(statement) \
268  do { \
269  } while (0)
270 #define hppDebugStatement(statement)
271 #define hppDout(channel, data) \
272  do { \
273  } while (0)
274 #define hppDoutFatal(channel, data) \
275  do { \
276  using namespace hpp; \
277  ::std::cerr << data << iendl; \
278  ::std::exit(EXIT_FAILURE); \
279  } while (1)
280 
281 #endif // HPP_DEBUG
282 
283 #endif
JournalOutput benchmarkJournal
Logs to benchmark journal file (i.e. benchmark.XXX.log).
Definition: debug.hh:189
Definition: assertion.hh:45
Channel info
Technical information and debugging.
Definition: debug.hh:201
Logging in journal file in the logging directory.
Definition: debug.hh:148
std::vector< Output * > subscribers_t
Definition: debug.hh:129
Channel notice
User-oriented information.
Definition: debug.hh:198
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:89
Channel benchmark
Benchmark information.
Definition: debug.hh:204
Debugging output.
Definition: debug.hh:101
#define HPP_UTIL_DLLAPI
Definition: config.hh:64
Channel warning
Non-fatal problems channel.
Definition: debug.hh:195
Channel error
Fatal problems channel.
Definition: debug.hh:192
HPP_UTIL_DLLAPI Logging logging
Benchmark information.
Definition: debug.cc:225
Logging in console (std::cerr).
Definition: debug.hh:168
Receive debugging information.
Definition: debug.hh:127
JournalOutput journal
Logs to main journal file (i.e. journal.XXX.log).
Definition: debug.hh:187
ConsoleOutput console
Logs to console (i.e. stderr).
Definition: debug.hh:185
Logging class owns all channels and outputs.
Definition: debug.hh:179
HPP_UTIL_DLLAPI std::string getPrefix(const std::string &packageName)
Compute the logging prefix.
Definition: debug.cc:72