1 // -*- mode: cpp; mode: fold -*-
3 // $Id: error.cc,v 1.4 1999/01/19 04:41:43 jgg Exp $
4 /* ######################################################################
6 Global Erorr Class - Global error mechanism
8 We use a simple STL vector to store each error record. A PendingFlag
9 is kept which indicates when the vector contains a Sever error.
11 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe.
14 ##################################################################### */
16 // Include Files /*{{{*/
18 #pragma implementation "dsync/error.h"
21 #include <dsync/error.h>
33 // Global Error Object /*{{{*/
34 /* If the implementation supports posix threads then the accessor function
35 is compiled to be thread safe otherwise a non-safe version is used. A
36 Per-Thread error object is maintained in much the same manner as libc
38 #if _POSIX_THREADS == 1
41 static pthread_key_t ErrorKey;
42 static bool GotKey = false;
43 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
44 static void KeyAlloc() {GotKey = true;
45 pthread_key_create(&ErrorKey,ErrorDestroy);};
47 GlobalError *_GetErrorObj()
49 static pthread_once_t Once = PTHREAD_ONCE_INIT;
50 pthread_once(&Once,KeyAlloc);
52 /* Solaris has broken pthread_once support, isn't that nice? Thus
53 we create a race condition for such defective systems here. */
57 void *Res = pthread_getspecific(ErrorKey);
59 pthread_setspecific(ErrorKey,Res = new GlobalError);
60 return (GlobalError *)Res;
63 GlobalError *_GetErrorObj()
65 static GlobalError *Obj = new GlobalError;
71 // GlobalError::GlobalError - Constructor /*{{{*/
72 // ---------------------------------------------------------------------
74 GlobalError::GlobalError() : List(0), PendingFlag(false)
78 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
79 // ---------------------------------------------------------------------
80 /* Function indicates the stdlib function that failed and Description is
81 a user string that leads the text. Form is:
82 Description - Function (errno: strerror)
83 Carefull of the buffer overrun, sprintf.
85 bool GlobalError::Errno(const char *Function,const char *Description,...)
88 va_start(args,Description);
90 // sprintf the description
92 vsnprintf(S,sizeof(S),Description,args);
93 snprintf(S + strlen(S),sizeof(S) - strlen(S),
94 " - %s (%i %s)",Function,errno,strerror(errno));
107 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
108 // ---------------------------------------------------------------------
109 /* Function indicates the stdlib function that failed and Description is
110 a user string that leads the text. Form is:
111 Description - Function (errno: strerror)
112 Carefull of the buffer overrun, sprintf.
114 bool GlobalError::WarningE(const char *Function,const char *Description,...)
117 va_start(args,Description);
119 // sprintf the description
121 vsnprintf(S,sizeof(S),Description,args);
122 snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
124 // Put it on the list
125 Item *Itm = new Item;
133 // GlobalError::Error - Add an error to the list /*{{{*/
134 // ---------------------------------------------------------------------
135 /* Just vsprintfs and pushes */
136 bool GlobalError::Error(const char *Description,...)
139 va_start(args,Description);
141 // sprintf the description
143 vsnprintf(S,sizeof(S),Description,args);
145 // Put it on the list
146 Item *Itm = new Item;
156 // GlobalError::Warning - Add a warning to the list /*{{{*/
157 // ---------------------------------------------------------------------
158 /* This doesn't set the pending error flag */
159 bool GlobalError::Warning(const char *Description,...)
162 va_start(args,Description);
164 // sprintf the description
166 vsnprintf(S,sizeof(S),Description,args);
168 // Put it on the list
169 Item *Itm = new Item;
177 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
178 // ---------------------------------------------------------------------
179 /* This should be used in a loop checking empty() each cycle. It returns
180 true if the message is an error. */
181 bool GlobalError::PopMessage(string &Text)
186 bool Ret = List->Error;
192 // This really should check the list to see if only warnings are left..
199 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
200 // ---------------------------------------------------------------------
202 void GlobalError::DumpErrors()
204 // Print any errors or warnings found
206 while (empty() == false)
208 bool Type = PopMessage(Err);
210 std::cerr << "E: " << Err << endl;
212 cerr << "W: " << Err << endl;
216 // GlobalError::Discard - Discard /*{{{*/
217 // ---------------------------------------------------------------------
219 void GlobalError::Discard()
231 // GlobalError::Insert - Insert a new item at the end /*{{{*/
232 // ---------------------------------------------------------------------
234 void GlobalError::Insert(Item *Itm)
237 for (Item *I = List; I != 0; I = I->Next)