error.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.cc,v 1.4 1998/09/12 02:46:26 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. /*}}}*/
  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 _POSIX_THREADS == 1
  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. vsprintf(S,Description,args);
  71. sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  72. // Put it on the list
  73. Item *Itm = new Item;
  74. Itm->Text = S;
  75. Itm->Error = true;
  76. Insert(Itm);
  77. PendingFlag = true;
  78. return false;
  79. }
  80. /*}}}*/
  81. // GlobalError::Error - Add an error to the list /*{{{*/
  82. // ---------------------------------------------------------------------
  83. /* Just vsprintfs and pushes */
  84. bool GlobalError::Error(const char *Description,...)
  85. {
  86. va_list args;
  87. va_start(args,Description);
  88. // sprintf the description
  89. char S[400];
  90. vsprintf(S,Description,args);
  91. // Put it on the list
  92. Item *Itm = new Item;
  93. Itm->Text = S;
  94. Itm->Error = true;
  95. Insert(Itm);
  96. PendingFlag = true;
  97. return false;
  98. }
  99. /*}}}*/
  100. // GlobalError::Warning - Add a warning to the list /*{{{*/
  101. // ---------------------------------------------------------------------
  102. /* This doesn't set the pending error flag */
  103. bool GlobalError::Warning(const char *Description,...)
  104. {
  105. va_list args;
  106. va_start(args,Description);
  107. // sprintf the description
  108. char S[400];
  109. vsprintf(S,Description,args);
  110. // Put it on the list
  111. Item *Itm = new Item;
  112. Itm->Text = S;
  113. Itm->Error = false;
  114. Insert(Itm);
  115. return false;
  116. }
  117. /*}}}*/
  118. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  119. // ---------------------------------------------------------------------
  120. /* This should be used in a loop checking empty() each cycle. It returns
  121. true if the message is an error. */
  122. bool GlobalError::PopMessage(string &Text)
  123. {
  124. if (List == 0)
  125. return false;
  126. bool Ret = List->Error;
  127. Text = List->Text;
  128. Item *Old = List;
  129. List = List->Next;
  130. delete Old;
  131. // This really should check the list to see if only warnings are left..
  132. if (List == 0)
  133. PendingFlag = false;
  134. return Ret;
  135. }
  136. /*}}}*/
  137. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  138. // ---------------------------------------------------------------------
  139. /* */
  140. void GlobalError::DumpErrors()
  141. {
  142. // Print any errors or warnings found
  143. string Err;
  144. while (empty() == false)
  145. {
  146. bool Type = PopMessage(Err);
  147. if (Type == true)
  148. cerr << "E: " << Err << endl;
  149. else
  150. cerr << "W: " << Err << endl;
  151. }
  152. }
  153. /*}}}*/
  154. // GlobalError::Discard - Discard /*{{{*/
  155. // ---------------------------------------------------------------------
  156. /* */
  157. void GlobalError::Discard()
  158. {
  159. while (List != 0)
  160. {
  161. Item *Old = List;
  162. List = List->Next;
  163. delete Old;
  164. }
  165. PendingFlag = false;
  166. };
  167. /*}}}*/
  168. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  169. // ---------------------------------------------------------------------
  170. /* */
  171. void GlobalError::Insert(Item *Itm)
  172. {
  173. Item **End = &List;
  174. for (Item *I = List; I != 0; I = I->Next)
  175. End = &I->Next;
  176. Itm->Next = *End;
  177. *End = Itm;
  178. }
  179. /*}}}*/