error.cc 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.cc,v 1.3 1998/07/12 23:58:46 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. #ifdef __GNUG__
  14. #pragma implementation "apt-pkg/error.h"
  15. #endif
  16. #include <errno.h>
  17. #include <stdio.h>
  18. #include <string.h>
  19. #include <stdarg.h>
  20. #include <apt-pkg/error.h>
  21. /*}}}*/
  22. GlobalError *_error = new GlobalError;
  23. // GlobalError::GlobalError - Constructor /*{{{*/
  24. // ---------------------------------------------------------------------
  25. /* */
  26. GlobalError::GlobalError() : List(0), PendingFlag(false)
  27. {
  28. }
  29. /*}}}*/
  30. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  31. // ---------------------------------------------------------------------
  32. /* Function indicates the stdlib function that failed and Description is
  33. a user string that leads the text. Form is:
  34. Description - Function (errno: strerror)
  35. Carefull of the buffer overrun, sprintf.
  36. */
  37. bool GlobalError::Errno(const char *Function,const char *Description,...)
  38. {
  39. va_list args;
  40. va_start(args,Description);
  41. // sprintf the description
  42. char S[400];
  43. vsprintf(S,Description,args);
  44. sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  45. // Put it on the list
  46. Item *Itm = new Item;
  47. Itm->Text = S;
  48. Itm->Error = true;
  49. Insert(Itm);
  50. PendingFlag = true;
  51. return false;
  52. }
  53. /*}}}*/
  54. // GlobalError::Error - Add an error to the list /*{{{*/
  55. // ---------------------------------------------------------------------
  56. /* Just vsprintfs and pushes */
  57. bool GlobalError::Error(const char *Description,...)
  58. {
  59. va_list args;
  60. va_start(args,Description);
  61. // sprintf the description
  62. char S[400];
  63. vsprintf(S,Description,args);
  64. // Put it on the list
  65. Item *Itm = new Item;
  66. Itm->Text = S;
  67. Itm->Error = true;
  68. Insert(Itm);
  69. PendingFlag = true;
  70. return false;
  71. }
  72. /*}}}*/
  73. // GlobalError::Warning - Add a warning to the list /*{{{*/
  74. // ---------------------------------------------------------------------
  75. /* This doesn't set the pending error flag */
  76. bool GlobalError::Warning(const char *Description,...)
  77. {
  78. va_list args;
  79. va_start(args,Description);
  80. // sprintf the description
  81. char S[400];
  82. vsprintf(S,Description,args);
  83. // Put it on the list
  84. Item *Itm = new Item;
  85. Itm->Text = S;
  86. Itm->Error = false;
  87. Insert(Itm);
  88. return false;
  89. }
  90. /*}}}*/
  91. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  92. // ---------------------------------------------------------------------
  93. /* This should be used in a loop checking empty() each cycle. It returns
  94. true if the message is an error. */
  95. bool GlobalError::PopMessage(string &Text)
  96. {
  97. if (List == 0)
  98. return false;
  99. bool Ret = List->Error;
  100. Text = List->Text;
  101. Item *Old = List;
  102. List = List->Next;
  103. delete Old;
  104. // This really should check the list to see if only warnings are left..
  105. if (List == 0)
  106. PendingFlag = false;
  107. return Ret;
  108. }
  109. /*}}}*/
  110. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  111. // ---------------------------------------------------------------------
  112. /* */
  113. void GlobalError::DumpErrors()
  114. {
  115. // Print any errors or warnings found
  116. string Err;
  117. while (empty() == false)
  118. {
  119. bool Type = PopMessage(Err);
  120. if (Type == true)
  121. cerr << "E: " << Err << endl;
  122. else
  123. cerr << "W: " << Err << endl;
  124. }
  125. }
  126. /*}}}*/
  127. // GlobalError::Discard - Discard /*{{{*/
  128. // ---------------------------------------------------------------------
  129. /* */
  130. void GlobalError::Discard()
  131. {
  132. while (List != 0)
  133. {
  134. Item *Old = List;
  135. List = List->Next;
  136. delete Old;
  137. }
  138. PendingFlag = false;
  139. };
  140. /*}}}*/
  141. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* */
  144. void GlobalError::Insert(Item *Itm)
  145. {
  146. Item **End = &List;
  147. for (Item *I = List; I != 0; I = I->Next)
  148. End = &I->Next;
  149. Itm->Next = *End;
  150. *End = Itm;
  151. }
  152. /*}}}*/