1 /** 2 * @file cverb.h 3 * verbose output stream 4 * 5 * @remark Copyright 2002, 2004 OProfile authors 6 * @remark Read the file COPYING 7 * 8 * @author Philippe Elie 9 * @author John Levon 10 */ 11 12 #ifndef CVERB_H 13 #define CVERB_H 14 15 #include <iosfwd> 16 #include <string> 17 #include <vector> 18 19 struct cverb_object { }; 20 21 /** 22 * verbose object, all output through this stream are made only 23 * if a verbose object with a true state is injected in the stream. 24 */ 25 extern cverb_object cverb; 26 27 /** 28 * typical use: 29 * declare some verbose global object: 30 * verbose debug("debug"); 31 * verbose stats("stats"); 32 * verbose level2("level2"); 33 * 34 * setup from command line the state of these objects 35 * 36 * verbose::setup(command_line_args_to'--verbose='); 37 * 38 * cverb << stats << "stats\n"; 39 * cverb << (stats&level2) << "very verbose stats\n" 40 * cverb << (stats|debug) << "bar\n"; 41 * these will give a compile time error 42 * cverb << stats << "foo" << debug << "bar"; 43 * cout << stats << "foo"; 44 * 45 * In critical code path cverb can be used in the more efficient way: 46 * if (cverb << vdebug) 47 * cverb << vdebug << "foo" << "bar"; 48 * the condition test the fails bit for the returned stream while the later 49 * build a sentry object for each << (more efficient even with one level of <<) 50 */ 51 class verbose { 52 /// The returned stream is either a null stream or cout. 53 friend std::ostream & operator<<(cverb_object &, verbose const &); 54 public: 55 /** 56 * create a verbose object named name, the ctor auto-register name 57 * as a verbose object, the set state can be intialized through 58 * verbose::setup(name) 59 */ 60 verbose(char const * name); 61 62 verbose operator|(verbose const &); 63 verbose operator&(verbose const &); 64 65 /// Return false if this named verbose object has not be registred. 66 static bool setup(std::string const &); 67 /// convenient interface calling the above for string in args 68 static bool setup(std::vector<std::string> const & args); 69 private: 70 bool set; 71 }; 72 73 /** 74 * predefined general purpose verbose object, comment give their names 75 */ 76 extern verbose vlevel1; /**< named "level1" */ 77 extern verbose vdebug; /**< named "debug" */ 78 extern verbose vstats; /**< named "stats" */ 79 // all sample filename manipulation. 80 extern verbose vsfile; /**< named "sfile" */ 81 extern verbose vxml; /**< named "xml" */ 82 83 #endif /* !CVERB_H */ 84