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