error.cc 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 <config.h>
  13. #include <apt-pkg/error.h>
  14. #include <stdarg.h>
  15. #include <stddef.h>
  16. #include <list>
  17. #include <iostream>
  18. #include <errno.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <unistd.h>
  22. #include <string>
  23. #include <cstring>
  24. /*}}}*/
  25. // Global Error Object /*{{{*/
  26. /* If the implementation supports posix threads then the accessor function
  27. is compiled to be thread safe otherwise a non-safe version is used. A
  28. Per-Thread error object is maintained in much the same manner as libc
  29. manages errno */
  30. #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
  31. #include <pthread.h>
  32. static pthread_key_t ErrorKey;
  33. static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
  34. static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
  35. GlobalError *_GetErrorObj() {
  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. static GlobalError *Obj = new GlobalError;
  46. return Obj;
  47. }
  48. #endif
  49. /*}}}*/
  50. // GlobalError::GlobalError - Constructor /*{{{*/
  51. GlobalError::GlobalError() : PendingFlag(false) {}
  52. /*}}}*/
  53. // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
  54. #define GEMessage(NAME, TYPE) \
  55. bool GlobalError::NAME (const char *Function, const char *Description,...) { \
  56. va_list args; \
  57. size_t msgSize = 400; \
  58. int const errsv = errno; \
  59. while (true) { \
  60. va_start(args,Description); \
  61. bool const retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
  62. va_end(args); \
  63. if (retry == false) \
  64. break; \
  65. } \
  66. return false; \
  67. }
  68. GEMessage(FatalE, FATAL)
  69. GEMessage(Errno, ERROR)
  70. GEMessage(WarningE, WARNING)
  71. GEMessage(NoticeE, NOTICE)
  72. GEMessage(DebugE, DEBUG)
  73. #undef GEMessage
  74. /*}}}*/
  75. // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
  76. bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
  77. const char *Description,...) {
  78. va_list args;
  79. size_t msgSize = 400;
  80. int const errsv = errno;
  81. while (true) {
  82. va_start(args,Description);
  83. bool const retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
  84. va_end(args);
  85. if (retry == false)
  86. break;
  87. }
  88. return false;
  89. }
  90. /*}}}*/
  91. // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
  92. bool GlobalError::InsertErrno(MsgType type, const char* Function,
  93. const char* Description, va_list &args,
  94. int const errsv, size_t &msgSize) {
  95. char* S = (char*) malloc(msgSize);
  96. int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description,
  97. Function, errsv, strerror(errsv));
  98. if (n > -1 && ((unsigned int) n) < msgSize);
  99. else {
  100. if (n > -1)
  101. msgSize = n + 1;
  102. else
  103. msgSize *= 2;
  104. free(S);
  105. return true;
  106. }
  107. bool const geins = Insert(type, S, args, msgSize);
  108. free(S);
  109. return geins;
  110. }
  111. /*}}}*/
  112. // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
  113. #define GEMessage(NAME, TYPE) \
  114. bool GlobalError::NAME (const char *Description,...) { \
  115. va_list args; \
  116. size_t msgSize = 400; \
  117. while (true) { \
  118. va_start(args,Description); \
  119. if (Insert(TYPE, Description, args, msgSize) == false) \
  120. break; \
  121. va_end(args); \
  122. } \
  123. return false; \
  124. }
  125. GEMessage(Fatal, FATAL)
  126. GEMessage(Error, ERROR)
  127. GEMessage(Warning, WARNING)
  128. GEMessage(Notice, NOTICE)
  129. GEMessage(Debug, DEBUG)
  130. #undef GEMessage
  131. /*}}}*/
  132. // GlobalError::Insert - Add a errotype message to the list /*{{{*/
  133. bool GlobalError::Insert(MsgType const &type, const char *Description,...)
  134. {
  135. va_list args;
  136. size_t msgSize = 400;
  137. while (true) {
  138. va_start(args,Description);
  139. if (Insert(type, Description, args, msgSize) == false)
  140. break;
  141. va_end(args);
  142. }
  143. return false;
  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, size_t &msgSize) {
  149. char* S = (char*) malloc(msgSize);
  150. int const n = vsnprintf(S, msgSize, Description, args);
  151. if (n > -1 && ((unsigned int) n) < msgSize);
  152. else {
  153. if (n > -1)
  154. msgSize = n + 1;
  155. else
  156. msgSize *= 2;
  157. free(S);
  158. return true;
  159. }
  160. Item const m(S, type);
  161. Messages.push_back(m);
  162. if (type == ERROR || type == FATAL)
  163. PendingFlag = true;
  164. if (type == FATAL || type == DEBUG)
  165. std::clog << m << std::endl;
  166. free(S);
  167. return false;
  168. }
  169. /*}}}*/
  170. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  171. bool GlobalError::PopMessage(std::string &Text) {
  172. if (Messages.empty() == true)
  173. return false;
  174. Item const msg = Messages.front();
  175. Messages.pop_front();
  176. bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
  177. Text = msg.Text;
  178. if (PendingFlag == false || Ret == false)
  179. return Ret;
  180. // check if another error message is pending
  181. for (std::list<Item>::const_iterator m = Messages.begin();
  182. m != Messages.end(); ++m)
  183. if (m->Type == ERROR || m->Type == FATAL)
  184. return Ret;
  185. PendingFlag = false;
  186. return Ret;
  187. }
  188. /*}}}*/
  189. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  190. void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
  191. bool const &mergeStack) {
  192. if (mergeStack == true)
  193. for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
  194. s != Stacks.rend(); ++s)
  195. Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
  196. for (std::list<Item>::const_iterator m = Messages.begin();
  197. m != Messages.end(); ++m)
  198. if (m->Type >= threshold)
  199. out << (*m) << std::endl;
  200. Discard();
  201. }
  202. /*}}}*/
  203. // GlobalError::Discard - Discard /*{{{*/
  204. void GlobalError::Discard() {
  205. Messages.clear();
  206. PendingFlag = false;
  207. }
  208. /*}}}*/
  209. // GlobalError::empty - does our error list include anything? /*{{{*/
  210. bool GlobalError::empty(MsgType const &trashhold) const {
  211. if (PendingFlag == true)
  212. return false;
  213. if (Messages.empty() == true)
  214. return true;
  215. for (std::list<Item>::const_iterator m = Messages.begin();
  216. m != Messages.end(); ++m)
  217. if (m->Type >= trashhold)
  218. return false;
  219. return true;
  220. }
  221. /*}}}*/
  222. // GlobalError::PushToStack /*{{{*/
  223. void GlobalError::PushToStack() {
  224. MsgStack pack(Messages, PendingFlag);
  225. Stacks.push_back(pack);
  226. Discard();
  227. }
  228. /*}}}*/
  229. // GlobalError::RevertToStack /*{{{*/
  230. void GlobalError::RevertToStack() {
  231. Discard();
  232. MsgStack pack = Stacks.back();
  233. Messages = pack.Messages;
  234. PendingFlag = pack.PendingFlag;
  235. Stacks.pop_back();
  236. }
  237. /*}}}*/
  238. // GlobalError::MergeWithStack /*{{{*/
  239. void GlobalError::MergeWithStack() {
  240. MsgStack pack = Stacks.back();
  241. Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
  242. PendingFlag = PendingFlag || pack.PendingFlag;
  243. Stacks.pop_back();
  244. }
  245. /*}}}*/