error.cc 7.2 KB

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