error.cc 7.2 KB

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