error.cc 6.3 KB

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