error.cc 7.3 KB

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