PageRenderTime 57ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/pymol_asphere/src/error.cpp

http://github.com/browndeer/lammps-ocl
C++ | 279 lines | 219 code | 38 blank | 22 comment | 28 complexity | 9e726c80b2fd4aaf384e4e8282238127 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause, LGPL-2.0
  1. /***************************************************************************
  2. error.cpp
  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. #include "error.h"
  11. Notice::Notice() {
  12. nullout=new ostream(NULL);
  13. noteout=&cout;
  14. notice_level=9;
  15. }
  16. Notice::~Notice() {
  17. if (nullout!=NULL)
  18. delete nullout;
  19. }
  20. // Returns a null stream if level is two high, else returns notice stream
  21. ostream & Notice::operator[] (const unsigned level) {
  22. if (level<=notice_level)
  23. return *noteout;
  24. else
  25. return *nullout;
  26. }
  27. void Notice::setostream(ostream &out) {
  28. noteout=&out;
  29. }
  30. void Notice::set_notice_level(unsigned l) {
  31. notice_level=l;
  32. }
  33. unsigned Notice::get_notice_level() {
  34. return notice_level;
  35. }
  36. void Notice::notice(unsigned level, const string calling_class,
  37. const string note) {
  38. if (level<notice_level)
  39. *noteout << calling_class << "::" << note;
  40. }
  41. void Notice::notice(unsigned level,const string calling_class,
  42. vector<string> &notes) {
  43. if (level<=notice_level) {
  44. *noteout << calling_class << "::";
  45. for (unsigned i=0; i<notes.size(); i++)
  46. *noteout << notes[i];
  47. }
  48. }
  49. void Notice::notice(unsigned level, const string note) {
  50. if (level<=notice_level)
  51. *noteout << note;
  52. }
  53. void Notice::notice(unsigned level, vector<string> &notes) {
  54. if (level<=notice_level)
  55. for (unsigned i=0; i<notes.size(); i++)
  56. *noteout << notes[i];
  57. }
  58. Error::Error() {
  59. nullout=new ostream(NULL);
  60. unhandled_warnings=0;
  61. handled_warnings=0;
  62. max_level=9;
  63. min_level=2;
  64. handleatend=true;
  65. writetotalatend=true;
  66. errout=&cerr;
  67. logout=nullout;
  68. column_width=70;
  69. }
  70. Error::~Error() {
  71. if (handleatend && unhandled_warnings!=0)
  72. writewarnings();
  73. if (writetotalatend && total_warnings()!=0)
  74. writetotals(0);
  75. if (nullout!=NULL)
  76. delete nullout;
  77. }
  78. // Set a log file for error AND notice output
  79. void Error::set_logfile(ostream &out) {
  80. logout=&out;
  81. note.setostream(out);
  82. }
  83. // Total number of warnings
  84. unsigned Error::total_warnings() {
  85. return unhandled_warnings+handled_warnings;
  86. }
  87. // Returns the number of errors generated with ID
  88. unsigned Error::operator[](unsigned id) {
  89. warning_iter m;
  90. m=warning_list.find(id);
  91. if (m==warning_list.end())
  92. return 0;
  93. return m->second.size();
  94. }
  95. void Error::addwarning(unsigned ID, unsigned level, const string calling_class,
  96. const string warning) {
  97. if (level<min_level)
  98. return;
  99. if (level>max_level)
  100. generate_error(ID,calling_class,warning);
  101. vector<ErrCom> *e=&(warning_list[ID]);
  102. e->push_back(ErrCom());
  103. e->back().level=level;
  104. e->back().calling_class=calling_class;
  105. e->back().message=warning;
  106. unhandled_warnings++;
  107. }
  108. void Error::generate_error(unsigned ID, const string calling_class,
  109. const string error) {
  110. ErrCom err;
  111. err.level=max_level+1;
  112. err.calling_class=calling_class;
  113. err.message=error;
  114. if (warnings()!=0)
  115. writewarnings();
  116. write_err(ID,err);
  117. writetotals(1);
  118. #ifdef MUSE_MPI
  119. MPI_Abort ( MPI_COMM_WORLD, 1 );
  120. #else
  121. exit(1);
  122. #endif
  123. }
  124. // Add an error/warning (Program termination if level >= max level
  125. ostringstream & Error::buffer() {
  126. return buffer_stream;
  127. }
  128. // Generate warning with message in buffer
  129. void Error::addbuf(unsigned ID, unsigned level, const string calling_class) {
  130. addwarning(ID,level,calling_class,buffer_stream.str());
  131. buffer_stream.str("");
  132. }
  133. // Generate serious error with message in buffer
  134. void Error::addbuf(unsigned ID, const string calling_class) {
  135. generate_error(ID,calling_class,buffer_stream.str());
  136. }
  137. unsigned Error::warnings() {
  138. return unhandled_warnings;
  139. }
  140. void Error::writeline() {
  141. *errout << "+";
  142. *logout << "+";
  143. for (unsigned i=0; i<column_width-2; i++) {
  144. *errout << "-";
  145. *logout << "-";
  146. }
  147. *errout << "+\n";
  148. *logout << "+\n";
  149. }
  150. void Error::write_err(unsigned ID, ErrCom &err) {
  151. if (err.level<min_level)
  152. return;
  153. *errout << "\n";
  154. *logout << "\n";
  155. writeline();
  156. (*errout).setf(ios::left);
  157. (*errout).unsetf(ios::right);
  158. // Output the IDs
  159. unsigned width12=(unsigned)floor((double)(column_width-10)/3.0);
  160. unsigned width3=column_width-10-width12-width12;
  161. string et;
  162. unsigned width1;
  163. if (err.level>max_level) {
  164. et="| Error: "; width1=width12-7;
  165. } else {
  166. et="| Warning: "; width1=width12-9;
  167. }
  168. *errout << et << setw(width1) << ID << " | Level: "
  169. << setw(width12-7) << err.level << " | " << setw(width3)
  170. << err.calling_class << " |\n";
  171. *logout << et << setw(width1) << ID << " | Level: "
  172. << setw(width12-7) << err.level << " | " << setw(width3)
  173. << err.calling_class << " |\n";
  174. writeline();
  175. // Output the message
  176. vector <string> messages;
  177. a::format_fit(column_width-3,err.message,messages);
  178. for (unsigned i=0; i<messages.size(); i++) {
  179. *errout << "| " << setw(column_width-3) << messages[i] << "|\n";
  180. *logout << "| " << setw(column_width-3) << messages[i] << "|\n";
  181. }
  182. writeline();
  183. return;
  184. }
  185. void Error::writewarning(unsigned ID) {
  186. for (unsigned i=0; i<warning_list[ID].size(); i++)
  187. write_err(ID,warning_list[ID][i]);
  188. handled_warnings+=warning_list[ID].size();
  189. dismiss_all_warnings(ID);
  190. return;
  191. }
  192. void Error::dismiss_warning(unsigned ID) {
  193. warning_iter m;
  194. m=warning_list.find(ID);
  195. if (m==warning_list.end())
  196. return;
  197. unhandled_warnings--;
  198. if (m->second.size()==1)
  199. warning_list.erase(m);
  200. else
  201. m->second.erase(m->second.end()--);
  202. }
  203. void Error::dismiss_all_warnings(unsigned ID) {
  204. warning_iter m;
  205. m=warning_list.find(ID);
  206. if (m==warning_list.end())
  207. return;
  208. unhandled_warnings-=m->second.size();
  209. warning_list.erase(m);
  210. }
  211. void Error::writewarnings() {
  212. while (warning_list.size()>0)
  213. writewarning(warning_list.begin()->first);
  214. return;
  215. }
  216. void Error::dismiss_warnings() {
  217. while (warning_list.size()>0)
  218. dismiss_warning(warning_list.begin()->first);
  219. return;
  220. }
  221. // Write out the total warnings and errors
  222. void Error::writetotals(unsigned errorcount) {
  223. *errout << "\n";
  224. *logout << "\n";
  225. writeline();
  226. (*errout).setf(ios::left);
  227. (*errout).unsetf(ios::right);
  228. unsigned width1=(unsigned)floor((double)(column_width-7)/2.0);
  229. unsigned width2=column_width-7-width1;
  230. string swarnings="Total Warnings: "+a::itoa(handled_warnings+
  231. unhandled_warnings);
  232. string serrors="Total Errors: "+a::itoa(errorcount);
  233. *errout << "| " << setw(width1) << swarnings << " | " << setw(width2)
  234. << serrors << " |\n";
  235. *logout << "| " << setw(width1) << swarnings << " | " << setw(width2)
  236. << serrors << " |\n";
  237. writeline();
  238. }