error.cc 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. // -*- mode: cpp; mode: fold -*-
  2. // Description /*{{{*/
  3. // $Id: error.cc,v 1.4 1999/01/19 04:41:43 jgg Exp $
  4. /* ######################################################################
  5. Global Erorr Class - Global error mechanism
  6. We use a simple STL vector to store each error record. A PendingFlag
  7. is kept which indicates when the vector contains a Sever error.
  8. This source is placed in the Public Domain, do with it what you will
  9. It was originally written by Jason Gunthorpe.
  10. ##################################################################### */
  11. /*}}}*/
  12. // Include Files /*{{{*/
  13. #ifdef __GNUG__
  14. #pragma implementation "dsync/error.h"
  15. #endif
  16. #include <dsync/error.h>
  17. #include <errno.h>
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <stdarg.h>
  21. #include <unistd.h>
  22. #include <iostream>
  23. using namespace std;
  24. /*}}}*/
  25. // Global Error Object /*{{{*/
  26. /* If the implementation supports posix threads then the accessor function
  27. is compiled to be thread safe otherwise a non-safe version is used. A
  28. Per-Thread error object is maintained in much the same manner as libc
  29. manages errno */
  30. #if _POSIX_THREADS == 1
  31. #include <pthread.h>
  32. static pthread_key_t ErrorKey;
  33. static bool GotKey = false;
  34. static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
  35. static void KeyAlloc() {GotKey = true;
  36. pthread_key_create(&ErrorKey,ErrorDestroy);};
  37. GlobalError *_GetErrorObj()
  38. {
  39. static pthread_once_t Once = PTHREAD_ONCE_INIT;
  40. pthread_once(&Once,KeyAlloc);
  41. /* Solaris has broken pthread_once support, isn't that nice? Thus
  42. we create a race condition for such defective systems here. */
  43. if (GotKey == false)
  44. KeyAlloc();
  45. void *Res = pthread_getspecific(ErrorKey);
  46. if (Res == 0)
  47. pthread_setspecific(ErrorKey,Res = new GlobalError);
  48. return (GlobalError *)Res;
  49. }
  50. #else
  51. GlobalError *_GetErrorObj()
  52. {
  53. static GlobalError *Obj = new GlobalError;
  54. return Obj;
  55. }
  56. #endif
  57. /*}}}*/
  58. // GlobalError::GlobalError - Constructor /*{{{*/
  59. // ---------------------------------------------------------------------
  60. /* */
  61. GlobalError::GlobalError() : List(0), PendingFlag(false)
  62. {
  63. }
  64. /*}}}*/
  65. // GlobalError::Errno - Get part of the error string from errno /*{{{*/
  66. // ---------------------------------------------------------------------
  67. /* Function indicates the stdlib function that failed and Description is
  68. a user string that leads the text. Form is:
  69. Description - Function (errno: strerror)
  70. Carefull of the buffer overrun, sprintf.
  71. */
  72. bool GlobalError::Errno(const char *Function,const char *Description,...)
  73. {
  74. va_list args;
  75. va_start(args,Description);
  76. // sprintf the description
  77. char S[400];
  78. vsnprintf(S,sizeof(S),Description,args);
  79. snprintf(S + strlen(S),sizeof(S) - strlen(S),
  80. " - %s (%i %s)",Function,errno,strerror(errno));
  81. // Put it on the list
  82. Item *Itm = new Item;
  83. Itm->Text = S;
  84. Itm->Error = true;
  85. Insert(Itm);
  86. PendingFlag = true;
  87. return false;
  88. }
  89. /*}}}*/
  90. // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
  91. // ---------------------------------------------------------------------
  92. /* Function indicates the stdlib function that failed and Description is
  93. a user string that leads the text. Form is:
  94. Description - Function (errno: strerror)
  95. Carefull of the buffer overrun, sprintf.
  96. */
  97. bool GlobalError::WarningE(const char *Function,const char *Description,...)
  98. {
  99. va_list args;
  100. va_start(args,Description);
  101. // sprintf the description
  102. char S[400];
  103. vsnprintf(S,sizeof(S),Description,args);
  104. snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
  105. // Put it on the list
  106. Item *Itm = new Item;
  107. Itm->Text = S;
  108. Itm->Error = false;
  109. Insert(Itm);
  110. return false;
  111. }
  112. /*}}}*/
  113. // GlobalError::Error - Add an error to the list /*{{{*/
  114. // ---------------------------------------------------------------------
  115. /* Just vsprintfs and pushes */
  116. bool GlobalError::Error(const char *Description,...)
  117. {
  118. va_list args;
  119. va_start(args,Description);
  120. // sprintf the description
  121. char S[400];
  122. vsnprintf(S,sizeof(S),Description,args);
  123. // Put it on the list
  124. Item *Itm = new Item;
  125. Itm->Text = S;
  126. Itm->Error = true;
  127. Insert(Itm);
  128. PendingFlag = true;
  129. return false;
  130. }
  131. /*}}}*/
  132. // GlobalError::Warning - Add a warning to the list /*{{{*/
  133. // ---------------------------------------------------------------------
  134. /* This doesn't set the pending error flag */
  135. bool GlobalError::Warning(const char *Description,...)
  136. {
  137. va_list args;
  138. va_start(args,Description);
  139. // sprintf the description
  140. char S[400];
  141. vsnprintf(S,sizeof(S),Description,args);
  142. // Put it on the list
  143. Item *Itm = new Item;
  144. Itm->Text = S;
  145. Itm->Error = false;
  146. Insert(Itm);
  147. return false;
  148. }
  149. /*}}}*/
  150. // GlobalError::PopMessage - Pulls a single message out /*{{{*/
  151. // ---------------------------------------------------------------------
  152. /* This should be used in a loop checking empty() each cycle. It returns
  153. true if the message is an error. */
  154. bool GlobalError::PopMessage(string &Text)
  155. {
  156. if (List == 0)
  157. return false;
  158. bool Ret = List->Error;
  159. Text = List->Text;
  160. Item *Old = List;
  161. List = List->Next;
  162. delete Old;
  163. // This really should check the list to see if only warnings are left..
  164. if (List == 0)
  165. PendingFlag = false;
  166. return Ret;
  167. }
  168. /*}}}*/
  169. // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
  170. // ---------------------------------------------------------------------
  171. /* */
  172. void GlobalError::DumpErrors()
  173. {
  174. // Print any errors or warnings found
  175. string Err;
  176. while (empty() == false)
  177. {
  178. bool Type = PopMessage(Err);
  179. if (Type == true)
  180. std::cerr << "E: " << Err << endl;
  181. else
  182. cerr << "W: " << Err << endl;
  183. }
  184. }
  185. /*}}}*/
  186. // GlobalError::Discard - Discard /*{{{*/
  187. // ---------------------------------------------------------------------
  188. /* */
  189. void GlobalError::Discard()
  190. {
  191. while (List != 0)
  192. {
  193. Item *Old = List;
  194. List = List->Next;
  195. delete Old;
  196. }
  197. PendingFlag = false;
  198. };
  199. /*}}}*/
  200. // GlobalError::Insert - Insert a new item at the end /*{{{*/
  201. // ---------------------------------------------------------------------
  202. /* */
  203. void GlobalError::Insert(Item *Itm)
  204. {
  205. Item **End = &List;
  206. for (Item *I = List; I != 0; I = I->Next)
  207. End = &I->Next;
  208. Itm->Next = *End;
  209. *End = Itm;
  210. }
  211. /*}}}*/