error.cc 6.3 KB

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