error.cc 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.cc,v 1.1 1998/07/02 02:58:13 jgg Exp $
  4. /* ######################################################################
  5. Global Erorr Class - Global error mechanism
  6. We use a simple STL vector to store each error record. A PendingFlag
  7. is kept which indicates when the vector contains a Sever error.
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #include <errno.h>
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <stdarg.h>
  17. #include <pkglib/error.h>
  18. /*}}}*/
  19. GlobalError *_error = new GlobalError;
  20. // GlobalError::GlobalError - Constructor /*{{{*/
  21. // ---------------------------------------------------------------------
  22. /* */
  23. GlobalError::GlobalError() : PendingFlag(false)
  24. {
  25. }
  26. /*}}}*/
  27. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  28. // ---------------------------------------------------------------------
  29. /* Function indicates the stdlib function that failed and Description is
  30. a user string that leads the text. Form is:
  31. Description - Function (errno: strerror)
  32. Carefull of the buffer overrun, sprintf.
  33. */
  34. bool GlobalError::Errno(const char *Function,const char *Description,...)
  35. {
  36. va_list args;
  37. va_start(args,Description);
  38. // sprintf the description
  39. char S[400];
  40. vsprintf(S,Description,args);
  41. sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  42. // Put it on the list
  43. Item Itm;
  44. Itm.Text = S;
  45. Itm.Error = true;
  46. List.push_back(Itm);
  47. PendingFlag = true;
  48. return false;
  49. }
  50. /*}}}*/
  51. // GlobalError::Error - Add an error to the list /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* Just vsprintfs and pushes */
  54. bool GlobalError::Error(const char *Description,...)
  55. {
  56. va_list args;
  57. va_start(args,Description);
  58. // sprintf the description
  59. char S[400];
  60. vsprintf(S,Description,args);
  61. // Put it on the list
  62. Item Itm;
  63. Itm.Text = S;
  64. Itm.Error = true;
  65. List.push_back(Itm);
  66. PendingFlag = true;
  67. return false;
  68. }
  69. /*}}}*/
  70. // GlobalError::Warning - Add a warning to the list /*{{{*/
  71. // ---------------------------------------------------------------------
  72. /* This doesn't set the pending error flag */
  73. bool GlobalError::Warning(const char *Description,...)
  74. {
  75. va_list args;
  76. va_start(args,Description);
  77. // sprintf the description
  78. char S[400];
  79. vsprintf(S,Description,args);
  80. // Put it on the list
  81. Item Itm;
  82. Itm.Text = S;
  83. Itm.Error = false;
  84. List.push_back(Itm);
  85. return false;
  86. }
  87. /*}}}*/
  88. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  89. // ---------------------------------------------------------------------
  90. /* This should be used in a loop checking empty() each cycle. It returns
  91. true if the message is an error. */
  92. bool GlobalError::PopMessage(string &Text)
  93. {
  94. bool Ret = List.front().Error;
  95. Text = List.front().Text;
  96. List.erase(List.begin());
  97. // This really should check the list to see if only warnings are left..
  98. if (empty())
  99. PendingFlag = false;
  100. return Ret;
  101. }
  102. /*}}}*/
  103. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  104. // ---------------------------------------------------------------------
  105. /* */
  106. void GlobalError::DumpErrors()
  107. {
  108. // Print any errors or warnings found
  109. string Err;
  110. while (empty() == false)
  111. {
  112. bool Type = PopMessage(Err);
  113. if (Type == true)
  114. cerr << "E: " << Err << endl;
  115. else
  116. cerr << "W: " << Err << endl;
  117. }
  118. }
  119. /*}}}*/