You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

236 lines
6.3 KiB

  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)," - %s (%i %s)",Function,errno,strerror(errno));
  98. // Put it on the list
  99. Item *Itm = new Item;
  100. Itm->Text = S;
  101. Itm->Error = false;
  102. Insert(Itm);
  103. return false;
  104. }
  105. /*}}}*/
  106. // GlobalError::Error - Add an error to the list /*{{{*/
  107. // ---------------------------------------------------------------------
  108. /* Just vsprintfs and pushes */
  109. bool GlobalError::Error(const char *Description,...)
  110. {
  111. va_list args;
  112. va_start(args,Description);
  113. // sprintf the description
  114. char S[400];
  115. vsnprintf(S,sizeof(S),Description,args);
  116. // Put it on the list
  117. Item *Itm = new Item;
  118. Itm->Text = S;
  119. Itm->Error = true;
  120. Insert(Itm);
  121. PendingFlag = true;
  122. return false;
  123. }
  124. /*}}}*/
  125. // GlobalError::Warning - Add a warning to the list /*{{{*/
  126. // ---------------------------------------------------------------------
  127. /* This doesn't set the pending error flag */
  128. bool GlobalError::Warning(const char *Description,...)
  129. {
  130. va_list args;
  131. va_start(args,Description);
  132. // sprintf the description
  133. char S[400];
  134. vsnprintf(S,sizeof(S),Description,args);
  135. // Put it on the list
  136. Item *Itm = new Item;
  137. Itm->Text = S;
  138. Itm->Error = false;
  139. Insert(Itm);
  140. return false;
  141. }
  142. /*}}}*/
  143. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  144. // ---------------------------------------------------------------------
  145. /* This should be used in a loop checking empty() each cycle. It returns
  146. true if the message is an error. */
  147. bool GlobalError::PopMessage(string &Text)
  148. {
  149. if (List == 0)
  150. return false;
  151. bool Ret = List->Error;
  152. Text = List->Text;
  153. Item *Old = List;
  154. List = List->Next;
  155. delete Old;
  156. // This really should check the list to see if only warnings are left..
  157. if (List == 0)
  158. PendingFlag = false;
  159. return Ret;
  160. }
  161. /*}}}*/
  162. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  163. // ---------------------------------------------------------------------
  164. /* */
  165. void GlobalError::DumpErrors()
  166. {
  167. // Print any errors or warnings found
  168. string Err;
  169. while (empty() == false)
  170. {
  171. bool Type = PopMessage(Err);
  172. if (Type == true)
  173. cerr << "E: " << Err << endl;
  174. else
  175. cerr << "W: " << Err << endl;
  176. }
  177. }
  178. /*}}}*/
  179. // GlobalError::Discard - Discard /*{{{*/
  180. // ---------------------------------------------------------------------
  181. /* */
  182. void GlobalError::Discard()
  183. {
  184. while (List != 0)
  185. {
  186. Item *Old = List;
  187. List = List->Next;
  188. delete Old;
  189. }
  190. PendingFlag = false;
  191. };
  192. /*}}}*/
  193. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  194. // ---------------------------------------------------------------------
  195. /* */
  196. void GlobalError::Insert(Item *Itm)
  197. {
  198. Item **End = &List;
  199. for (Item *I = List; I != 0; I = I->Next)
  200. End = &I->Next;
  201. Itm->Next = *End;
  202. *End = Itm;
  203. }
  204. /*}}}*/