error.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 <stdarg.h>
  18. #include <unistd.h>
  19. #include <string>
  20. #include <cstring>
  21. #include "config.h"
  22. /*}}}*/
  23. using namespace std;
  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 defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
  30. #include <pthread.h>
  31. static pthread_key_t ErrorKey;
  32. static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
  33. static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
  34. GlobalError *_GetErrorObj()
  35. {
  36. static pthread_once_t Once = PTHREAD_ONCE_INIT;
  37. pthread_once(&Once,KeyAlloc);
  38. void *Res = pthread_getspecific(ErrorKey);
  39. if (Res == 0)
  40. pthread_setspecific(ErrorKey,Res = new GlobalError);
  41. return (GlobalError *)Res;
  42. }
  43. #else
  44. GlobalError *_GetErrorObj()
  45. {
  46. static GlobalError *Obj = new GlobalError;
  47. return Obj;
  48. }
  49. #endif
  50. /*}}}*/
  51. // GlobalError::GlobalError - Constructor /*{{{*/
  52. // ---------------------------------------------------------------------
  53. /* */
  54. GlobalError::GlobalError() : List(0), PendingFlag(false)
  55. {
  56. }
  57. /*}}}*/
  58. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* Function indicates the stdlib function that failed and Description is
  61. a user string that leads the text. Form is:
  62. Description - Function (errno: strerror)
  63. Carefull of the buffer overrun, sprintf.
  64. */
  65. bool GlobalError::Errno(const char *Function,const char *Description,...)
  66. {
  67. va_list args;
  68. va_start(args,Description);
  69. // sprintf the description
  70. char S[400];
  71. vsnprintf(S,sizeof(S),Description,args);
  72. snprintf(S + strlen(S),sizeof(S) - strlen(S),
  73. " - %s (%i: %s)",Function,errno,strerror(errno));
  74. // Put it on the list
  75. Item *Itm = new Item;
  76. Itm->Text = S;
  77. Itm->Error = true;
  78. Insert(Itm);
  79. PendingFlag = true;
  80. return false;
  81. }
  82. /*}}}*/
  83. // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
  84. // ---------------------------------------------------------------------
  85. /* Function indicates the stdlib function that failed and Description is
  86. a user string that leads the text. Form is:
  87. Description - Function (errno: strerror)
  88. Carefull of the buffer overrun, sprintf.
  89. */
  90. bool GlobalError::WarningE(const char *Function,const char *Description,...)
  91. {
  92. va_list args;
  93. va_start(args,Description);
  94. // sprintf the description
  95. char S[400];
  96. vsnprintf(S,sizeof(S),Description,args);
  97. snprintf(S + strlen(S),sizeof(S) - strlen(S),
  98. " - %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. /*}}}*/