CBMC
message.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_UTIL_MESSAGE_H
11 #define CPROVER_UTIL_MESSAGE_H
12 
13 #include "invariant.h"
14 #include "source_location.h"
15 
16 #include <functional>
17 #include <iosfwd>
18 #include <sstream>
19 #include <string>
20 
21 class json_objectt;
22 class jsont;
23 class structured_datat;
24 class xmlt;
25 
27 {
28 public:
30  {
31  }
32 
33  virtual void print(unsigned level, const std::string &message)=0;
34 
35  virtual void print(unsigned level, const xmlt &xml) = 0;
36 
37  virtual void print(unsigned level, const jsont &json) = 0;
38 
39  virtual void print(unsigned level, const structured_datat &data);
40 
41  virtual void print(
42  unsigned level,
43  const std::string &message,
44  const source_locationt &location);
45 
46  virtual void flush(unsigned) = 0;
47 
49  {
50  }
51 
52  void set_verbosity(unsigned _verbosity) { verbosity=_verbosity; }
53  unsigned get_verbosity() const { return verbosity; }
54 
55  std::size_t get_message_count(unsigned level) const
56  {
57  if(level>=message_count.size())
58  return 0;
59 
60  return message_count[level];
61  }
62 
65  virtual std::string command(unsigned) const
66  {
67  return std::string();
68  }
69 
70 protected:
71  unsigned verbosity;
72  std::vector<std::size_t> message_count;
73 };
74 
76 {
77 public:
79  {
80  verbosity = 0;
81  }
82 
83  void print(unsigned level, const std::string &message) override
84  {
85  message_handlert::print(level, message);
86  }
87 
88  void print(unsigned, const xmlt &) override
89  {
90  }
91 
92  void print(unsigned, const jsont &) override
93  {
94  }
95 
96  void print(
97  unsigned level,
98  const std::string &message,
99  const source_locationt &) override
100  {
101  print(level, message);
102  }
103 
104  void flush(unsigned) override
105  {
106  }
107 };
108 
110 {
111 public:
112  explicit stream_message_handlert(std::ostream &_out):out(_out)
113  {
114  }
115 
116  void print(unsigned level, const std::string &message) override
117  {
118  message_handlert::print(level, message);
119 
120  if(verbosity>=level)
121  out << message << '\n';
122  }
123 
124  void print(unsigned, const xmlt &) override
125  {
126  }
127 
128  void print(unsigned, const jsont &) override
129  {
130  }
131 
132  void flush(unsigned) override
133  {
134  out << std::flush;
135  }
136 
137 protected:
138  std::ostream &out;
139 };
140 
153 class messaget
154 {
155 public:
156  // Standard message levels:
157  //
158  // 0 none
159  // 1 only errors
160  // 2 + warnings
161  // 4 + results
162  // 6 + status/phase information
163  // 8 + statistical information
164  // 9 + progress information
165  // 10 + debug info
166 
168  {
171  };
172 
173  static unsigned eval_verbosity(
174  const std::string &user_input,
175  const message_levelt default_verbosity,
176  message_handlert &dest);
177 
178  virtual void set_message_handler(message_handlert &_message_handler)
179  {
180  message_handler=&_message_handler;
181  }
182 
184  {
185  INVARIANT(
186  message_handler!=nullptr,
187  "message handler should be set before calling get_message_handler");
188  return *message_handler;
189  }
190 
191  // constructors, destructor
192 
193  messaget(const messaget &other):
195  mstream(other.mstream, *this)
196  {
197  }
198 
199  messaget &operator=(const messaget &other)
200  {
202  mstream.assign_from(other.mstream);
203  return *this;
204  }
205 
206  explicit messaget(message_handlert &_message_handler):
207  message_handler(&_message_handler),
208  mstream(M_DEBUG, *this)
209  {
210  }
211 
212  virtual ~messaget();
213 
214  // \brief Class that stores an individual 'message' with a verbosity 'level'.
215  class mstreamt:public std::ostringstream
216  {
217  public:
219  unsigned _message_level,
220  messaget &_message):
221  message_level(_message_level),
222  message(_message)
223  {
224  }
225 
226  mstreamt(const mstreamt &other)=delete;
227 
228  mstreamt(const mstreamt &other, messaget &_message):
230  message(_message),
232  {
233  }
234 
235  mstreamt &operator=(const mstreamt &other)=delete;
236 
237  unsigned message_level;
240 
241  mstreamt &operator << (const xmlt &data)
242  {
243  if(this->tellp() > 0)
244  *this << eom; // force end of previous message
246  {
248  }
249  return *this;
250  }
251 
252  mstreamt &operator<<(const json_objectt &data);
253 
255  {
256  if(this->tellp() > 0)
257  *this << eom; // force end of previous message
259  {
261  }
262  return *this;
263  }
264 
265  template <class T>
266  mstreamt &operator << (const T &x)
267  {
268  static_cast<std::ostream &>(*this) << x;
269  return *this;
270  }
271 
272  private:
273  void assign_from(const mstreamt &other)
274  {
277  // message, the pointer to my enclosing messaget, remains unaltered.
278  }
279 
280  friend class messaget;
281  };
282 
283  // Feeding 'eom' into the stream triggers the printing of the message
284  // This is implemented as an I/O manipulator (compare to STL's endl).
285  class eomt
286  {
287  };
288 
289  static eomt eom;
290 
292  {
294  {
296  m.message_level,
297  m.str(),
298  m.source_location);
300  }
301  m.clear(); // clears error bits
302  m.str(std::string()); // clears the string
304  return m;
305  }
306 
307  // This is an I/O manipulator (compare to STL's set_precision).
308  class commandt
309  {
310  public:
311  explicit commandt(unsigned _command) : command(_command)
312  {
313  }
314 
315  unsigned command;
316  };
317 
319  friend mstreamt &operator<<(mstreamt &m, const commandt &c)
320  {
322  return m << m.message.message_handler->command(c.command);
323  else
324  return m;
325  }
326 
328  static commandt command(unsigned c)
329  {
330  return commandt(c);
331  }
332 
335  static const commandt reset;
336 
338  static const commandt red;
339 
341  static const commandt green;
342 
344  static const commandt yellow;
345 
347  static const commandt blue;
348 
350  static const commandt magenta;
351 
353  static const commandt cyan;
354 
356  static const commandt bright_red;
357 
359  static const commandt bright_green;
360 
362  static const commandt bright_yellow;
363 
365  static const commandt bright_blue;
366 
368  static const commandt bright_magenta;
369 
371  static const commandt bright_cyan;
372 
374  static const commandt bold;
375 
377  static const commandt faint;
378 
380  static const commandt italic;
381 
383  static const commandt underline;
384 
385  mstreamt &get_mstream(unsigned message_level) const
386  {
387  mstream.message_level=message_level;
388  return mstream;
389  }
390 
391  mstreamt &error() const
392  {
393  return get_mstream(M_ERROR);
394  }
395 
396  mstreamt &warning() const
397  {
398  return get_mstream(M_WARNING);
399  }
400 
401  mstreamt &result() const
402  {
403  return get_mstream(M_RESULT);
404  }
405 
406  mstreamt &status() const
407  {
408  return get_mstream(M_STATUS);
409  }
410 
412  {
413  return get_mstream(M_STATISTICS);
414  }
415 
417  {
418  return get_mstream(M_PROGRESS);
419  }
420 
421  mstreamt &debug() const
422  {
423  return get_mstream(M_DEBUG);
424  }
425 
426  void conditional_output(
427  mstreamt &mstream,
428  const std::function<void(mstreamt &)> &output_generator) const;
429 
430 protected:
432  mutable mstreamt mstream;
433 };
434 
435 #endif // CPROVER_UTIL_MESSAGE_H
void clear()
Definition: irep.h:444
Definition: json.h:27
void set_verbosity(unsigned _verbosity)
Definition: message.h:52
virtual void print(unsigned level, const std::string &message)=0
Definition: message.cpp:60
virtual void print(unsigned level, const jsont &json)=0
std::size_t get_message_count(unsigned level) const
Definition: message.h:55
virtual ~message_handlert()
Definition: message.h:48
std::vector< std::size_t > message_count
Definition: message.h:72
unsigned get_verbosity() const
Definition: message.h:53
virtual std::string command(unsigned) const
Create an ECMA-48 SGR (Select Graphic Rendition) command.
Definition: message.h:65
unsigned verbosity
Definition: message.h:71
virtual void flush(unsigned)=0
virtual void print(unsigned level, const xmlt &xml)=0
commandt(unsigned _command)
Definition: message.h:311
unsigned command
Definition: message.h:315
messaget & message
Definition: message.h:238
unsigned message_level
Definition: message.h:237
mstreamt(const mstreamt &other, messaget &_message)
Definition: message.h:228
mstreamt & operator<<(const xmlt &data)
Definition: message.h:241
mstreamt(unsigned _message_level, messaget &_message)
Definition: message.h:218
mstreamt & operator<<(const structured_datat &data)
Definition: message.h:254
mstreamt(const mstreamt &other)=delete
source_locationt source_location
Definition: message.h:239
mstreamt & operator=(const mstreamt &other)=delete
void assign_from(const mstreamt &other)
Definition: message.h:273
Class that provides messages with a built-in verbosity 'level'.
Definition: message.h:154
static const commandt yellow
render text with yellow foreground color
Definition: message.h:344
friend mstreamt & operator<<(mstreamt &m, const commandt &c)
feed a command into an mstreamt
Definition: message.h:319
static const commandt bright_magenta
render text with bright magenta foreground color
Definition: message.h:368
static unsigned eval_verbosity(const std::string &user_input, const message_levelt default_verbosity, message_handlert &dest)
Parse a (user-)provided string as a verbosity level and set it as the verbosity of dest.
Definition: message.cpp:105
mstreamt & error() const
Definition: message.h:391
message_handlert * message_handler
Definition: message.h:431
mstreamt & statistics() const
Definition: message.h:411
static const commandt magenta
render text with magenta foreground color
Definition: message.h:350
mstreamt & warning() const
Definition: message.h:396
static const commandt reset
return to default formatting, as defined by the terminal
Definition: message.h:335
mstreamt & status() const
Definition: message.h:406
static const commandt green
render text with green foreground color
Definition: message.h:341
static const commandt faint
render text with faint font
Definition: message.h:377
static const commandt bold
render text with bold font
Definition: message.h:374
static const commandt bright_red
render text with bright red foreground color
Definition: message.h:356
messaget & operator=(const messaget &other)
Definition: message.h:199
static const commandt underline
render underlined text
Definition: message.h:383
void conditional_output(mstreamt &mstream, const std::function< void(mstreamt &)> &output_generator) const
Generate output to message_stream using output_generator if the configured verbosity is at least as h...
Definition: message.cpp:139
static const commandt bright_cyan
render text with bright cyan foreground color
Definition: message.h:371
static const commandt bright_yellow
render text with bright yellow foreground color
Definition: message.h:362
mstreamt & progress() const
Definition: message.h:416
mstreamt & get_mstream(unsigned message_level) const
Definition: message.h:385
mstreamt & result() const
Definition: message.h:401
mstreamt & debug() const
Definition: message.h:421
message_handlert & get_message_handler()
Definition: message.h:183
virtual ~messaget()
Definition: message.cpp:74
virtual void set_message_handler(message_handlert &_message_handler)
Definition: message.h:178
messaget(const messaget &other)
Definition: message.h:193
message_levelt
Definition: message.h:168
@ M_DEBUG
Definition: message.h:170
@ M_RESULT
Definition: message.h:169
@ M_STATISTICS
Definition: message.h:170
@ M_STATUS
Definition: message.h:169
@ M_ERROR
Definition: message.h:169
@ M_WARNING
Definition: message.h:169
@ M_PROGRESS
Definition: message.h:170
static const commandt bright_blue
render text with bright blue foreground color
Definition: message.h:365
static const commandt italic
render italic text
Definition: message.h:380
static const commandt red
render text with red foreground color
Definition: message.h:338
messaget(message_handlert &_message_handler)
Definition: message.h:206
mstreamt mstream
Definition: message.h:432
friend mstreamt & operator<<(mstreamt &m, eomt)
Definition: message.h:291
static commandt command(unsigned c)
Create an ECMA-48 SGR (Select Graphic Rendition) command.
Definition: message.h:328
static eomt eom
Definition: message.h:289
static const commandt cyan
render text with cyan foreground color
Definition: message.h:353
static const commandt bright_green
render text with bright green foreground color
Definition: message.h:359
static const commandt blue
render text with blue foreground color
Definition: message.h:347
void print(unsigned level, const std::string &message) override
Definition: message.h:83
void print(unsigned, const jsont &) override
Definition: message.h:92
void print(unsigned level, const std::string &message, const source_locationt &) override
Definition: message.h:96
void print(unsigned, const xmlt &) override
Definition: message.h:88
void flush(unsigned) override
Definition: message.h:104
void print(unsigned, const xmlt &) override
Definition: message.h:124
void print(unsigned level, const std::string &message) override
Definition: message.h:116
stream_message_handlert(std::ostream &_out)
Definition: message.h:112
std::ostream & out
Definition: message.h:138
void print(unsigned, const jsont &) override
Definition: message.h:128
void flush(unsigned) override
Definition: message.h:132
A way of representing nested key/value data.
Definition: xml.h:21
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:120
xmlt xml(const irep_idt &property_id, const property_infot &property_info)
Definition: properties.cpp:110