error.cc 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 - Get part of the errortype string from errno/*{{{*/
  85. bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
  86. const char *Description,...) {
  87. va_list args;
  88. va_start(args,Description);
  89. return InsertErrno(type, Function, Description, args);
  90. }
  91. /*}}}*/
  92. // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
  93. bool GlobalError::InsertErrno(MsgType type, const char* Function,
  94. const char* Description, va_list &args) {
  95. char S[400];
  96. snprintf(S, sizeof(S), "%s - %s (%i: %s)", Description,
  97. Function, errno, strerror(errno));
  98. return Insert(type, S, args);
  99. }
  100. /*}}}*/
  101. // GlobalError::Fatal - Add a fatal error to the list /*{{{*/
  102. bool GlobalError::Fatal(const char *Description,...) {
  103. va_list args;
  104. va_start(args,Description);
  105. return Insert(FATAL, Description, args);
  106. }
  107. /*}}}*/
  108. // GlobalError::Error - Add an error to the list /*{{{*/
  109. bool GlobalError::Error(const char *Description,...) {
  110. va_list args;
  111. va_start(args,Description);
  112. return Insert(ERROR, Description, args);
  113. }
  114. /*}}}*/
  115. // GlobalError::Warning - Add a warning to the list /*{{{*/
  116. bool GlobalError::Warning(const char *Description,...) {
  117. va_list args;
  118. va_start(args,Description);
  119. return Insert(WARNING, Description, args);
  120. }
  121. /*}}}*/
  122. // GlobalError::Notice - Add a notice to the list /*{{{*/
  123. bool GlobalError::Notice(const char *Description,...)
  124. {
  125. va_list args;
  126. va_start(args,Description);
  127. return Insert(NOTICE, Description, args);
  128. }
  129. /*}}}*/
  130. // GlobalError::Debug - Add a debug to the list /*{{{*/
  131. bool GlobalError::Debug(const char *Description,...)
  132. {
  133. va_list args;
  134. va_start(args,Description);
  135. return Insert(DEBUG, Description, args);
  136. }
  137. /*}}}*/
  138. // GlobalError::Insert - Add a errotype message to the list /*{{{*/
  139. bool GlobalError::Insert(MsgType const &type, const char *Description,...)
  140. {
  141. va_list args;
  142. va_start(args,Description);
  143. return Insert(type, Description, args);
  144. }
  145. /*}}}*/
  146. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  147. bool GlobalError::Insert(MsgType type, const char* Description,
  148. va_list &args) {
  149. char S[400];
  150. vsnprintf(S,sizeof(S),Description,args);
  151. Item const m(S, type);
  152. Messages.push_back(m);
  153. if (type == ERROR || type == FATAL)
  154. PendingFlag = true;
  155. if (type == FATAL || type == DEBUG)
  156. std::clog << m << std::endl;
  157. return false;
  158. }
  159. /*}}}*/
  160. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  161. bool GlobalError::PopMessage(std::string &Text) {
  162. if (Messages.empty() == true)
  163. return false;
  164. Item const msg = Messages.front();
  165. Messages.pop_front();
  166. bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
  167. Text = msg.Text;
  168. if (PendingFlag == false || Ret == false)
  169. return Ret;
  170. // check if another error message is pending
  171. for (std::list<Item>::const_iterator m = Messages.begin();
  172. m != Messages.end(); m++)
  173. if (m->Type == ERROR || m->Type == FATAL)
  174. return Ret;
  175. PendingFlag = false;
  176. return Ret;
  177. }
  178. /*}}}*/
  179. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  180. void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
  181. bool const &mergeStack) {
  182. if (mergeStack == true)
  183. for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
  184. s != Stacks.rend(); ++s)
  185. Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
  186. for (std::list<Item>::const_iterator m = Messages.begin();
  187. m != Messages.end(); m++)
  188. if (m->Type >= threshold)
  189. out << (*m) << std::endl;
  190. Discard();
  191. }
  192. /*}}}*/
  193. // GlobalError::Discard - Discard /*{{{*/
  194. void GlobalError::Discard() {
  195. Messages.clear();
  196. PendingFlag = false;
  197. };
  198. /*}}}*/
  199. // GlobalError::empty - does our error list include anything? /*{{{*/
  200. bool GlobalError::empty(MsgType const &trashhold) const {
  201. if (PendingFlag == true)
  202. return false;
  203. if (Messages.empty() == true)
  204. return true;
  205. for (std::list<Item>::const_iterator m = Messages.begin();
  206. m != Messages.end(); m++)
  207. if (m->Type >= trashhold)
  208. return false;
  209. return true;
  210. }
  211. /*}}}*/
  212. // GlobalError::PushToStack /*{{{*/
  213. void GlobalError::PushToStack() {
  214. MsgStack pack(Messages, PendingFlag);
  215. Stacks.push_back(pack);
  216. Discard();
  217. }
  218. /*}}}*/
  219. // GlobalError::RevertToStack /*{{{*/
  220. void GlobalError::RevertToStack() {
  221. Discard();
  222. MsgStack pack = Stacks.back();
  223. Messages = pack.Messages;
  224. PendingFlag = pack.PendingFlag;
  225. Stacks.pop_back();
  226. }
  227. /*}}}*/
  228. // GlobalError::MergeWithStack /*{{{*/
  229. void GlobalError::MergeWithStack() {
  230. MsgStack pack = Stacks.back();
  231. Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
  232. PendingFlag = PendingFlag || pack.PendingFlag;
  233. Stacks.pop_back();
  234. }
  235. /*}}}*/