error.cc 7.2 KB

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