PageRenderTime 41ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/tools/pymol_asphere/src/error.h

http://github.com/browndeer/lammps-ocl
C Header | 218 lines | 80 code | 25 blank | 113 comment | 0 complexity | b6cd9dda5a7b84c028f520c2c47d3d26 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0
  1. /***************************************************************************
  2. error.h
  3. -------------------
  4. Class for error handling
  5. __________________________________________________________________________
  6. begin : Thu Oct 9 2003
  7. copyright : (C) 2003 by W. Michael Brown
  8. email : wmbrown@sandia.gov
  9. ***************************************************************************/
  10. #ifndef ERRORCLASS
  11. #define ERRORCLASS
  12. #include <string>
  13. #include <vector>
  14. #include <map>
  15. #include <iostream>
  16. #include <sstream>
  17. #include <iomanip>
  18. #include <math.h>
  19. #ifdef MUSE_MPI
  20. #include <mpi.h>
  21. #endif
  22. using namespace std;
  23. // forward declarations
  24. namespace a {
  25. string itoa(unsigned int);
  26. void format_fit(unsigned, const string &, vector<string> &);
  27. }
  28. /// Notice Class for Handling Object Output
  29. /** A notice object stores an ostream for output and a notice_level.
  30. * All output is sent along with a level. Any output whose level is
  31. * greater than notice_level is sent to a null stream. The C++ output
  32. * operator '<<' can be used with the Notice operator '[]' which passes
  33. * the level:
  34. * \verbatim notice_object[29] << "This notice has level 29" << endl;
  35. * \endverbatim
  36. *
  37. * The guidelines for output notice levels are:
  38. * - \e 0: Information that must be output
  39. * - \e 1 - 9: Normal program output
  40. * - \e 10-19: Parameters useful for storing how the program was run
  41. * - \e 20-29: Extraneous information useful that may be useful to user
  42. * - \e 30- : Debugging information */
  43. class Notice {
  44. public:
  45. /// Standard output (cout) is the default notice output ostream
  46. /** The default maximum notice level is 9 \e (notice_level=10) */
  47. Notice();
  48. ~Notice();
  49. /// Set the output stream for notice output
  50. void setostream(ostream &out);
  51. /// Returns a null stream if level is two high, else returns notice stream
  52. ostream & operator[] (const unsigned level);
  53. /// Set the degree of program output
  54. void set_notice_level(unsigned l);
  55. /// Get the degree of program output
  56. unsigned get_notice_level();
  57. /// Generate a notice with a specified calling class
  58. void notice(unsigned level, const string calling_class, const string note);
  59. /// Generate a notice with a specified calling class
  60. void notice(unsigned level, const string calling_class,
  61. vector<string> &notes);
  62. /// Generate a notice
  63. void notice(unsigned level, const string note);
  64. /// Generate a notice
  65. void notice(unsigned level, vector<string> &note);
  66. private:
  67. unsigned notice_level;
  68. ostream *nullout; // Null stream for redirecting output to nowhere
  69. ostream *noteout; // Output for notices
  70. };
  71. /// Error and Notice Handling
  72. /** This class is intended to handle all output to the user. Output is
  73. * divided into notices and warnings/errors. Output of any message is
  74. * associated with a level. For notices, if level is greater than or equal to
  75. * max_notice_level, no output is generated. For warnings, if level is less
  76. * than min_warning_level, it is dismissed with no output. If the level is
  77. * greater than max_warning_level, the program is terminated and all warnings
  78. * and errors are output.
  79. *
  80. * \note By default, on destruction of an Error object, all unhandled
  81. * warnings and errors are output
  82. *
  83. * A log file can be specified for each object. In this case, all notices
  84. * are output to the log file only and errors are output to both stderr
  85. * and the log file
  86. *
  87. * Errors can be generated with a string or using the internal message buffer:
  88. \verbatim
  89. Error error;
  90. error.buffer() << "Incorrect file format for file: " << filename;
  91. error.addbuf(512,19,"FooClass";
  92. // --- OR
  93. string message = "Incorrect file format for file: "+filename;
  94. error.addwarning(512,19,"FooClass",message);
  95. \endverbatim
  96. *
  97. * Newlines will be inserted into the error message automatically in order
  98. * to format it for the string. Forced newlines can be specified with \n
  99. *
  100. * Programs can check whether or not errors have been generated using the []
  101. * operator and can 'handle' them by outputing the message or dismissing
  102. * them without any output
  103. *
  104. * Notices are generated using the public Notice class (see Notice())
  105. *
  106. * \b Error \b Levels:
  107. * - \e 0 - 1: Errors expected to happen during normal execution
  108. * - \e 2 - 9: Errors that a non-interactive program can handle and continue
  109. * - \e 10-19: Errors that interactive program can handle (file not found,etc.)
  110. * - \e 20- : Serious errors that should terminate execution
  111. **/
  112. class Error {
  113. public:
  114. /// Default constructor (use cerr for output and no log file)
  115. /** Default max notice level is 9, min warning level is 2, and max warning
  116. * level is 9 */
  117. Error();
  118. ~Error();
  119. /// Set a log file for error AND notice output
  120. void set_logfile(ostream &out);
  121. /// Returns the number of errors (if any) generated with id
  122. unsigned operator[](unsigned id);
  123. /// Add warning, terminate if level is greater than max level
  124. /** Newlines will be inserted into the message automatically when the
  125. * message is formatted for output. However, forced newlines can also
  126. * be inserted. **/
  127. void addwarning(unsigned ID, unsigned level, const string calling_class,
  128. const string warning);
  129. /// Add serious error (terminates execution)
  130. void generate_error(unsigned ID, const string calling_class,
  131. const string error);
  132. /// Add an message to the error buffer. Warning generated with addbuf()
  133. /** Newlines will be inserted into the message automatically when the
  134. * message is formatted for output. However, forced newlines can also
  135. * be inserted.
  136. *
  137. \verbatim
  138. Error error;
  139. error.buffer() << "Choice not supported: " << choice;
  140. error.addbuf(512,9,"FooClass");
  141. \endverbatim **/
  142. ostringstream & buffer();
  143. /// Generate warning with message in buffer
  144. /** \sa buffer() **/
  145. void addbuf(unsigned ID, unsigned level, const string calling_class);
  146. /// Generate serious error with message in buffer
  147. /** \sa buffer() **/
  148. void addbuf(unsigned ID, const string calling_class);
  149. /// Number of Unhandled Warnings
  150. unsigned warnings();
  151. /// Total number of warnings
  152. unsigned total_warnings();
  153. /// Handle all warnings with this ID by writing them to out
  154. void writewarning(unsigned ID);
  155. /// Handle LAST warning with this ID WITHOUT writing it
  156. void dismiss_warning(unsigned ID);
  157. /// Handle ALL warnings with this ID WITHOUT writing it
  158. void dismiss_all_warnings(unsigned ID);
  159. /// Handle all warnings by writing them out
  160. void writewarnings();
  161. /// Handle all warnings without writing
  162. void dismiss_warnings();
  163. /// Write out the total warnings (write errorcount errors)
  164. void writetotals(unsigned errorcount);
  165. /// For generating notices
  166. /** \sa Notice **/
  167. Notice note;
  168. private:
  169. struct ErrCom;
  170. map<unsigned,vector<ErrCom> > warning_list;
  171. typedef multimap<unsigned, vector<ErrCom> >::iterator warning_iter;
  172. unsigned handled_warnings;
  173. unsigned unhandled_warnings;
  174. bool handleatend; // Write any unhandled errors on destruct
  175. bool writetotalatend; // Write totals on destruct if not 0
  176. unsigned min_level; // Don't output warnings less than min_level
  177. unsigned max_level; // if a warning has a level>max_level error!
  178. ostream *errout, *logout; // Output for errors and warnings!
  179. ostream *nullout; // No output
  180. ostringstream buffer_stream; // For creating messages for warnings
  181. unsigned column_width;
  182. void write_err(unsigned ID, ErrCom &err);
  183. void writeline();
  184. };
  185. struct Error::ErrCom {
  186. unsigned level;
  187. string calling_class;
  188. string message;
  189. };
  190. #endif