error.cc 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.cc,v 1.5 1998/09/18 02:42:40 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 <apt-pkg/error.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <unistd.h>
  22. /*}}}*/
  23. // Global Error Object /*{{{*/
  24. /* If the implementation supports posix threads then the accessor function
  25. is compiled to be thread safe otherwise a non-safe version is used. A
  26. Per-Thread error object is maintained in much the same manner as libc
  27. manages errno */
  28. #if _POSIX_THREADS == 1
  29. #include <pthread.h>
  30. static pthread_key_t ErrorKey;
  31. static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
  32. static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
  33. GlobalError *_GetErrorObj()
  34. {
  35. static pthread_once_t Once = PTHREAD_ONCE_INIT;
  36. pthread_once(&Once,KeyAlloc);
  37. void *Res = pthread_getspecific(ErrorKey);
  38. if (Res == 0)
  39. pthread_setspecific(ErrorKey,Res = new GlobalError);
  40. return (GlobalError *)Res;
  41. }
  42. #else
  43. GlobalError *_GetErrorObj()
  44. {
  45. static GlobalError *Obj = new GlobalError;
  46. return Obj;
  47. }
  48. #endif
  49. /*}}}*/
  50. // GlobalError::GlobalError - Constructor /*{{{*/
  51. // ---------------------------------------------------------------------
  52. /* */
  53. GlobalError::GlobalError() : List(0), PendingFlag(false)
  54. {
  55. }
  56. /*}}}*/
  57. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  58. // ---------------------------------------------------------------------
  59. /* Function indicates the stdlib function that failed and Description is
  60. a user string that leads the text. Form is:
  61. Description - Function (errno: strerror)
  62. Carefull of the buffer overrun, sprintf.
  63. */
  64. bool GlobalError::Errno(const char *Function,const char *Description,...)
  65. {
  66. va_list args;
  67. va_start(args,Description);
  68. // sprintf the description
  69. char S[400];
  70. vsprintf(S,Description,args);
  71. sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  72. // Put it on the list
  73. Item *Itm = new Item;
  74. Itm->Text = S;
  75. Itm->Error = true;
  76. Insert(Itm);
  77. PendingFlag = true;
  78. return false;
  79. }
  80. /*}}}*/
  81. // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
  82. // ---------------------------------------------------------------------
  83. /* Function indicates the stdlib function that failed and Description is
  84. a user string that leads the text. Form is:
  85. Description - Function (errno: strerror)
  86. Carefull of the buffer overrun, sprintf.
  87. */
  88. bool GlobalError::WarningE(const char *Function,const char *Description,...)
  89. {
  90. va_list args;
  91. va_start(args,Description);
  92. // sprintf the description
  93. char S[400];
  94. vsprintf(S,Description,args);
  95. sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  96. // Put it on the list
  97. Item *Itm = new Item;
  98. Itm->Text = S;
  99. Itm->Error = false;
  100. Insert(Itm);
  101. return false;
  102. }
  103. /*}}}*/
  104. // GlobalError::Error - Add an error to the list /*{{{*/
  105. // ---------------------------------------------------------------------
  106. /* Just vsprintfs and pushes */
  107. bool GlobalError::Error(const char *Description,...)
  108. {
  109. va_list args;
  110. va_start(args,Description);
  111. // sprintf the description
  112. char S[400];
  113. vsprintf(S,Description,args);
  114. // Put it on the list
  115. Item *Itm = new Item;
  116. Itm->Text = S;
  117. Itm->Error = true;
  118. Insert(Itm);
  119. PendingFlag = true;
  120. return false;
  121. }
  122. /*}}}*/
  123. // GlobalError::Warning - Add a warning to the list /*{{{*/
  124. // ---------------------------------------------------------------------
  125. /* This doesn't set the pending error flag */
  126. bool GlobalError::Warning(const char *Description,...)
  127. {
  128. va_list args;
  129. va_start(args,Description);
  130. // sprintf the description
  131. char S[400];
  132. vsprintf(S,Description,args);
  133. // Put it on the list
  134. Item *Itm = new Item;
  135. Itm->Text = S;
  136. Itm->Error = false;
  137. Insert(Itm);
  138. return false;
  139. }
  140. /*}}}*/
  141. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  142. // ---------------------------------------------------------------------
  143. /* This should be used in a loop checking empty() each cycle. It returns
  144. true if the message is an error. */
  145. bool GlobalError::PopMessage(string &Text)
  146. {
  147. if (List == 0)
  148. return false;
  149. bool Ret = List->Error;
  150. Text = List->Text;
  151. Item *Old = List;
  152. List = List->Next;
  153. delete Old;
  154. // This really should check the list to see if only warnings are left..
  155. if (List == 0)
  156. PendingFlag = false;
  157. return Ret;
  158. }
  159. /*}}}*/
  160. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  161. // ---------------------------------------------------------------------
  162. /* */
  163. void GlobalError::DumpErrors()
  164. {
  165. // Print any errors or warnings found
  166. string Err;
  167. while (empty() == false)
  168. {
  169. bool Type = PopMessage(Err);
  170. if (Type == true)
  171. cerr << "E: " << Err << endl;
  172. else
  173. cerr << "W: " << Err << endl;
  174. }
  175. }
  176. /*}}}*/
  177. // GlobalError::Discard - Discard /*{{{*/
  178. // ---------------------------------------------------------------------
  179. /* */
  180. void GlobalError::Discard()
  181. {
  182. while (List != 0)
  183. {
  184. Item *Old = List;
  185. List = List->Next;
  186. delete Old;
  187. }
  188. PendingFlag = false;
  189. };
  190. /*}}}*/
  191. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  192. // ---------------------------------------------------------------------
  193. /* */
  194. void GlobalError::Insert(Item *Itm)
  195. {
  196. Item **End = &List;
  197. for (Item *I = List; I != 0; I = I->Next)
  198. End = &I->Next;
  199. Itm->Next = *End;
  200. *End = Itm;
  201. }
  202. /*}}}*/