error.cc 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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. bool retry; \
  61. do { \
  62. va_start(args,Description); \
  63. retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
  64. va_end(args); \
  65. } while (retry); \
  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. bool retry;
  82. do {
  83. va_start(args,Description);
  84. retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
  85. va_end(args);
  86. } while (retry);
  87. return false;
  88. }
  89. /*}}}*/
  90. // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
  91. bool GlobalError::InsertErrno(MsgType type, const char* Function,
  92. const char* Description, va_list &args,
  93. int const errsv, size_t &msgSize) {
  94. char* S = (char*) malloc(msgSize);
  95. int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description,
  96. Function, errsv, strerror(errsv));
  97. if (n > -1 && ((unsigned int) n) < msgSize);
  98. else {
  99. if (n > -1)
  100. msgSize = n + 1;
  101. else
  102. msgSize *= 2;
  103. free(S);
  104. return true;
  105. }
  106. bool const geins = Insert(type, S, args, msgSize);
  107. free(S);
  108. return geins;
  109. }
  110. /*}}}*/
  111. // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
  112. #define GEMessage(NAME, TYPE) \
  113. bool GlobalError::NAME (const char *Description,...) { \
  114. va_list args; \
  115. size_t msgSize = 400; \
  116. bool retry; \
  117. do { \
  118. va_start(args,Description); \
  119. retry = Insert(TYPE, Description, args, msgSize); \
  120. va_end(args); \
  121. } while (retry); \
  122. return false; \
  123. }
  124. GEMessage(Fatal, FATAL)
  125. GEMessage(Error, ERROR)
  126. GEMessage(Warning, WARNING)
  127. GEMessage(Notice, NOTICE)
  128. GEMessage(Debug, DEBUG)
  129. #undef GEMessage
  130. /*}}}*/
  131. // GlobalError::Insert - Add a errotype message to the list /*{{{*/
  132. bool GlobalError::Insert(MsgType const &type, const char *Description,...)
  133. {
  134. va_list args;
  135. size_t msgSize = 400;
  136. bool retry;
  137. do {
  138. va_start(args,Description);
  139. retry = Insert(type, Description, args, msgSize);
  140. va_end(args);
  141. } while (retry);
  142. return false;
  143. }
  144. /*}}}*/
  145. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  146. bool GlobalError::Insert(MsgType type, const char* Description,
  147. va_list &args, size_t &msgSize) {
  148. char* S = (char*) malloc(msgSize);
  149. int const n = vsnprintf(S, msgSize, Description, args);
  150. if (n > -1 && ((unsigned int) n) < msgSize);
  151. else {
  152. if (n > -1)
  153. msgSize = n + 1;
  154. else
  155. msgSize *= 2;
  156. free(S);
  157. return true;
  158. }
  159. Item const m(S, type);
  160. Messages.push_back(m);
  161. if (type == ERROR || type == FATAL)
  162. PendingFlag = true;
  163. if (type == FATAL || type == DEBUG)
  164. std::clog << m << std::endl;
  165. free(S);
  166. return false;
  167. }
  168. /*}}}*/
  169. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  170. bool GlobalError::PopMessage(std::string &Text) {
  171. if (Messages.empty() == true)
  172. return false;
  173. Item const msg = Messages.front();
  174. Messages.pop_front();
  175. bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
  176. Text = msg.Text;
  177. if (PendingFlag == false || Ret == false)
  178. return Ret;
  179. // check if another error message is pending
  180. for (std::list<Item>::const_iterator m = Messages.begin();
  181. m != Messages.end(); ++m)
  182. if (m->Type == ERROR || m->Type == FATAL)
  183. return Ret;
  184. PendingFlag = false;
  185. return Ret;
  186. }
  187. /*}}}*/
  188. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  189. void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
  190. bool const &mergeStack) {
  191. if (mergeStack == true)
  192. for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
  193. s != Stacks.rend(); ++s)
  194. std::copy(s->Messages.begin(), s->Messages.end(), std::front_inserter(Messages));
  195. std::for_each(Messages.begin(), Messages.end(), [&threshold, &out](Item const &m) {
  196. if (m.Type >= threshold)
  197. out << m << std::endl;
  198. });
  199. Discard();
  200. }
  201. /*}}}*/
  202. // GlobalError::Discard - Discard /*{{{*/
  203. void GlobalError::Discard() {
  204. Messages.clear();
  205. PendingFlag = false;
  206. }
  207. /*}}}*/
  208. // GlobalError::ReturnError - convert a stored error to a return code /*{{{*/
  209. bool GlobalError::ReturnError() {
  210. for (auto &message : Messages)
  211. if (message.Type == ERROR)
  212. message.Type = WARNING;
  213. PendingFlag = false;
  214. return false;
  215. }
  216. /*}}}*/
  217. // GlobalError::empty - does our error list include anything? /*{{{*/
  218. bool GlobalError::empty(MsgType const &threshold) const {
  219. if (PendingFlag == true)
  220. return false;
  221. if (Messages.empty() == true)
  222. return true;
  223. return std::find_if(Messages.begin(), Messages.end(), [&threshold](Item const &m) {
  224. return m.Type >= threshold;
  225. }) == Messages.end();
  226. }
  227. /*}}}*/
  228. // GlobalError::PushToStack /*{{{*/
  229. void GlobalError::PushToStack() {
  230. Stacks.emplace_back(Messages, PendingFlag);
  231. Discard();
  232. }
  233. /*}}}*/
  234. // GlobalError::RevertToStack /*{{{*/
  235. void GlobalError::RevertToStack() {
  236. Discard();
  237. MsgStack pack = Stacks.back();
  238. Messages = pack.Messages;
  239. PendingFlag = pack.PendingFlag;
  240. Stacks.pop_back();
  241. }
  242. /*}}}*/
  243. // GlobalError::MergeWithStack /*{{{*/
  244. void GlobalError::MergeWithStack() {
  245. MsgStack pack = Stacks.back();
  246. Messages.splice(Messages.begin(), pack.Messages);
  247. PendingFlag = PendingFlag || pack.PendingFlag;
  248. Stacks.pop_back();
  249. }
  250. /*}}}*/