error.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. /* ######################################################################
  4. Global Error Class - Global error mechanism
  5. We use a simple STL vector to store each error record. A PendingFlag
  6. is kept which indicates when the vector contains a Sever error.
  7. This source is placed in the Public Domain, do with it what you will
  8. It was originally written by Jason Gunthorpe.
  9. ##################################################################### */
  10. /*}}}*/
  11. // Include Files /*{{{*/
  12. #include <apt-pkg/error.h>
  13. #include <iostream>
  14. #include <errno.h>
  15. #include <stdio.h>
  16. #include <unistd.h>
  17. #include <string>
  18. #include <cstring>
  19. #include "config.h"
  20. /*}}}*/
  21. // Global Error Object /*{{{*/
  22. /* If the implementation supports posix threads then the accessor function
  23. is compiled to be thread safe otherwise a non-safe version is used. A
  24. Per-Thread error object is maintained in much the same manner as libc
  25. manages errno */
  26. #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
  27. #include <pthread.h>
  28. static pthread_key_t ErrorKey;
  29. static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
  30. static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
  31. GlobalError *_GetErrorObj() {
  32. static pthread_once_t Once = PTHREAD_ONCE_INIT;
  33. pthread_once(&Once,KeyAlloc);
  34. void *Res = pthread_getspecific(ErrorKey);
  35. if (Res == 0)
  36. pthread_setspecific(ErrorKey,Res = new GlobalError);
  37. return (GlobalError *)Res;
  38. }
  39. #else
  40. GlobalError *_GetErrorObj() {
  41. static GlobalError *Obj = new GlobalError;
  42. return Obj;
  43. }
  44. #endif
  45. /*}}}*/
  46. // GlobalError::GlobalError - Constructor /*{{{*/
  47. GlobalError::GlobalError() : PendingFlag(false) {}
  48. /*}}}*/
  49. // GlobalError::FatalE - Get part of the error string from errno /*{{{*/
  50. bool GlobalError::FatalE(const char *Function,const char *Description,...) {
  51. va_list args;
  52. va_start(args,Description);
  53. return InsertErrno(FATAL, Function, Description, args);
  54. }
  55. /*}}}*/
  56. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  57. bool GlobalError::Errno(const char *Function,const char *Description,...) {
  58. va_list args;
  59. va_start(args,Description);
  60. return InsertErrno(ERROR, Function, Description, args);
  61. }
  62. /*}}}*/
  63. // GlobalError::WarningE - Get part of the warning string from errno /*{{{*/
  64. bool GlobalError::WarningE(const char *Function,const char *Description,...) {
  65. va_list args;
  66. va_start(args,Description);
  67. return InsertErrno(WARNING, Function, Description, args);
  68. }
  69. /*}}}*/
  70. // GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/
  71. bool GlobalError::NoticeE(const char *Function,const char *Description,...) {
  72. va_list args;
  73. va_start(args,Description);
  74. return InsertErrno(NOTICE, Function, Description, args);
  75. }
  76. /*}}}*/
  77. // GlobalError::DebugE - Get part of the debug string from errno /*{{{*/
  78. bool GlobalError::DebugE(const char *Function,const char *Description,...) {
  79. va_list args;
  80. va_start(args,Description);
  81. return InsertErrno(DEBUG, Function, Description, args);
  82. }
  83. /*}}}*/
  84. // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
  85. bool GlobalError::InsertErrno(MsgType type, const char* Function,
  86. const char* Description, va_list const &args) {
  87. char S[400];
  88. vsnprintf(S,sizeof(S),Description,args);
  89. snprintf(S + strlen(S),sizeof(S) - strlen(S),
  90. " - %s (%i: %s)", Function, errno, strerror(errno));
  91. return Insert(type, S, args);
  92. }
  93. /*}}}*/
  94. // GlobalError::Fatal - Add a fatal error to the list /*{{{*/
  95. bool GlobalError::Fatal(const char *Description,...) {
  96. va_list args;
  97. va_start(args,Description);
  98. return Insert(FATAL, Description, args);
  99. }
  100. /*}}}*/
  101. // GlobalError::Error - Add an error to the list /*{{{*/
  102. bool GlobalError::Error(const char *Description,...) {
  103. va_list args;
  104. va_start(args,Description);
  105. return Insert(ERROR, Description, args);
  106. }
  107. /*}}}*/
  108. // GlobalError::Warning - Add a warning to the list /*{{{*/
  109. bool GlobalError::Warning(const char *Description,...) {
  110. va_list args;
  111. va_start(args,Description);
  112. return Insert(WARNING, Description, args);
  113. }
  114. /*}}}*/
  115. // GlobalError::Notice - Add a notice to the list /*{{{*/
  116. bool GlobalError::Notice(const char *Description,...)
  117. {
  118. va_list args;
  119. va_start(args,Description);
  120. return Insert(NOTICE, Description, args);
  121. }
  122. /*}}}*/
  123. // GlobalError::Debug - Add a debug to the list /*{{{*/
  124. bool GlobalError::Debug(const char *Description,...)
  125. {
  126. va_list args;
  127. va_start(args,Description);
  128. return Insert(DEBUG, Description, args);
  129. }
  130. /*}}}*/
  131. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  132. bool GlobalError::Insert(MsgType type, const char* Description,
  133. va_list const &args) {
  134. char S[400];
  135. vsnprintf(S,sizeof(S),Description,args);
  136. Item const m(S, type);
  137. Messages.push_back(m);
  138. if (type == ERROR || type == FATAL)
  139. PendingFlag = true;
  140. if (type == FATAL || type == DEBUG)
  141. std::clog << m << std::endl;
  142. return false;
  143. }
  144. /*}}}*/
  145. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  146. bool GlobalError::PopMessage(std::string &Text) {
  147. if (Messages.empty() == true)
  148. return false;
  149. Item const msg = Messages.front();
  150. Messages.pop_front();
  151. bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
  152. Text = msg.Text;
  153. if (PendingFlag == false || Ret == false)
  154. return Ret;
  155. // check if another error message is pending
  156. for (std::list<Item>::const_iterator m = Messages.begin();
  157. m != Messages.end(); m++)
  158. if (m->Type == ERROR || m->Type == FATAL)
  159. return Ret;
  160. PendingFlag = false;
  161. return Ret;
  162. }
  163. /*}}}*/
  164. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  165. void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold,
  166. bool const &mergeStack) {
  167. if (mergeStack == true)
  168. for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
  169. s != Stacks.rend(); ++s)
  170. Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
  171. for (std::list<Item>::const_iterator m = Messages.begin();
  172. m != Messages.end(); m++)
  173. if (m->Type >= trashhold)
  174. out << (*m) << std::endl;
  175. Discard();
  176. }
  177. /*}}}*/
  178. // GlobalError::Discard - Discard /*{{{*/
  179. void GlobalError::Discard() {
  180. Messages.clear();
  181. PendingFlag = false;
  182. };
  183. /*}}}*/
  184. // GlobalError::empty - does our error list include anything? /*{{{*/
  185. bool GlobalError::empty(MsgType const &trashhold) const {
  186. if (PendingFlag == true)
  187. return false;
  188. if (Messages.empty() == true)
  189. return true;
  190. for (std::list<Item>::const_iterator m = Messages.begin();
  191. m != Messages.end(); m++)
  192. if (m->Type >= trashhold)
  193. return false;
  194. return true;
  195. }
  196. /*}}}*/
  197. // GlobalError::PushToStack /*{{{*/
  198. void GlobalError::PushToStack() {
  199. MsgStack pack(Messages, PendingFlag);
  200. Stacks.push_back(pack);
  201. Discard();
  202. }
  203. /*}}}*/
  204. // GlobalError::RevertToStack /*{{{*/
  205. void GlobalError::RevertToStack() {
  206. Discard();
  207. MsgStack pack = Stacks.back();
  208. Messages = pack.Messages;
  209. PendingFlag = pack.PendingFlag;
  210. Stacks.pop_back();
  211. }
  212. /*}}}*/
  213. // GlobalError::MergeWithStack /*{{{*/
  214. void GlobalError::MergeWithStack() {
  215. MsgStack pack = Stacks.back();
  216. Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
  217. PendingFlag = PendingFlag || pack.PendingFlag;
  218. Stacks.pop_back();
  219. }
  220. /*}}}*/