error.cc 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #include <apt-pkg/error.h>
  14. #include <iostream>
  15. #include <errno.h>
  16. #include <stdio.h>
  17. #include <string>
  18. #include <stdarg.h>
  19. #include <unistd.h>
  20. #include "config.h"
  21. /*}}}*/
  22. using namespace std;
  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 defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
  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. vsnprintf(S,sizeof(S),Description,args);
  71. snprintf(S + strlen(S),sizeof(S) - strlen(S),
  72. " - %s (%i %s)",Function,errno,strerror(errno));
  73. // Put it on the list
  74. Item *Itm = new Item;
  75. Itm->Text = S;
  76. Itm->Error = true;
  77. Insert(Itm);
  78. PendingFlag = true;
  79. return false;
  80. }
  81. /*}}}*/
  82. // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
  83. // ---------------------------------------------------------------------
  84. /* Function indicates the stdlib function that failed and Description is
  85. a user string that leads the text. Form is:
  86. Description - Function (errno: strerror)
  87. Carefull of the buffer overrun, sprintf.
  88. */
  89. bool GlobalError::WarningE(const char *Function,const char *Description,...)
  90. {
  91. va_list args;
  92. va_start(args,Description);
  93. // sprintf the description
  94. char S[400];
  95. vsnprintf(S,sizeof(S),Description,args);
  96. snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  97. // Put it on the list
  98. Item *Itm = new Item;
  99. Itm->Text = S;
  100. Itm->Error = false;
  101. Insert(Itm);
  102. return false;
  103. }
  104. /*}}}*/
  105. // GlobalError::Error - Add an error to the list /*{{{*/
  106. // ---------------------------------------------------------------------
  107. /* Just vsprintfs and pushes */
  108. bool GlobalError::Error(const char *Description,...)
  109. {
  110. va_list args;
  111. va_start(args,Description);
  112. // sprintf the description
  113. char S[400];
  114. vsnprintf(S,sizeof(S),Description,args);
  115. // Put it on the list
  116. Item *Itm = new Item;
  117. Itm->Text = S;
  118. Itm->Error = true;
  119. Insert(Itm);
  120. PendingFlag = true;
  121. return false;
  122. }
  123. /*}}}*/
  124. // GlobalError::Warning - Add a warning to the list /*{{{*/
  125. // ---------------------------------------------------------------------
  126. /* This doesn't set the pending error flag */
  127. bool GlobalError::Warning(const char *Description,...)
  128. {
  129. va_list args;
  130. va_start(args,Description);
  131. // sprintf the description
  132. char S[400];
  133. vsnprintf(S,sizeof(S),Description,args);
  134. // Put it on the list
  135. Item *Itm = new Item;
  136. Itm->Text = S;
  137. Itm->Error = false;
  138. Insert(Itm);
  139. return false;
  140. }
  141. /*}}}*/
  142. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  143. // ---------------------------------------------------------------------
  144. /* This should be used in a loop checking empty() each cycle. It returns
  145. true if the message is an error. */
  146. bool GlobalError::PopMessage(string &Text)
  147. {
  148. if (List == 0)
  149. return false;
  150. bool Ret = List->Error;
  151. Text = List->Text;
  152. Item *Old = List;
  153. List = List->Next;
  154. delete Old;
  155. // This really should check the list to see if only warnings are left..
  156. if (List == 0)
  157. PendingFlag = false;
  158. return Ret;
  159. }
  160. /*}}}*/
  161. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  162. // ---------------------------------------------------------------------
  163. /* */
  164. void GlobalError::DumpErrors()
  165. {
  166. // Print any errors or warnings found
  167. string Err;
  168. while (empty() == false)
  169. {
  170. bool Type = PopMessage(Err);
  171. if (Type == true)
  172. cerr << "E: " << Err << endl;
  173. else
  174. cerr << "W: " << Err << endl;
  175. }
  176. }
  177. /*}}}*/
  178. // GlobalError::Discard - Discard /*{{{*/
  179. // ---------------------------------------------------------------------
  180. /* */
  181. void GlobalError::Discard()
  182. {
  183. while (List != 0)
  184. {
  185. Item *Old = List;
  186. List = List->Next;
  187. delete Old;
  188. }
  189. PendingFlag = false;
  190. };
  191. /*}}}*/
  192. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  193. // ---------------------------------------------------------------------
  194. /* */
  195. void GlobalError::Insert(Item *Itm)
  196. {
  197. Item **End = &List;
  198. for (Item *I = List; I != 0; I = I->Next)
  199. End = &I->Next;
  200. Itm->Next = *End;
  201. *End = Itm;
  202. }
  203. /*}}}*/