error.cc 8.2 KB

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