]> git.decadent.org.uk Git - dak.git/blob - tools/dsync-0.0/libdsync/contrib/error.cc
Merged from ftpmaster
[dak.git] / tools / dsync-0.0 / libdsync / contrib / error.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description                                                          /*{{{*/
3 // $Id: error.cc,v 1.4 1999/01/19 04:41:43 jgg Exp $
4 /* ######################################################################
5    
6    Global Erorr Class - Global error mechanism
7
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.
10    
11    This source is placed in the Public Domain, do with it what you will
12    It was originally written by Jason Gunthorpe.
13    
14    ##################################################################### */
15                                                                         /*}}}*/
16 // Include Files                                                        /*{{{*/
17 #ifdef __GNUG__
18 #pragma implementation "dsync/error.h"
19 #endif 
20
21 #include <dsync/error.h>
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <unistd.h>
28 #include <iostream>
29
30 using namespace std;
31                                                                         /*}}}*/
32
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
37    manages errno */
38 #if _POSIX_THREADS == 1
39  #include <pthread.h>
40
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);};
46
47  GlobalError *_GetErrorObj()
48  {
49     static pthread_once_t Once = PTHREAD_ONCE_INIT;
50     pthread_once(&Once,KeyAlloc);
51
52     /* Solaris has broken pthread_once support, isn't that nice? Thus
53        we create a race condition for such defective systems here. */
54     if (GotKey == false)
55        KeyAlloc();
56    
57     void *Res = pthread_getspecific(ErrorKey);
58     if (Res == 0)
59        pthread_setspecific(ErrorKey,Res = new GlobalError);
60     return (GlobalError *)Res;
61  }
62 #else
63  GlobalError *_GetErrorObj()
64  {
65     static GlobalError *Obj = new GlobalError;
66     return Obj;
67  }
68 #endif
69                                                                         /*}}}*/
70
71 // GlobalError::GlobalError - Constructor                               /*{{{*/
72 // ---------------------------------------------------------------------
73 /* */
74 GlobalError::GlobalError() : List(0), PendingFlag(false)
75 {
76 }
77                                                                         /*}}}*/
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.
84  */
85 bool GlobalError::Errno(const char *Function,const char *Description,...)
86 {
87    va_list args;
88    va_start(args,Description);
89
90    // sprintf the description
91    char S[400];
92    vsnprintf(S,sizeof(S),Description,args);
93    snprintf(S + strlen(S),sizeof(S) - strlen(S),
94             " - %s (%i %s)",Function,errno,strerror(errno));
95
96    // Put it on the list
97    Item *Itm = new Item;
98    Itm->Text = S;
99    Itm->Error = true;
100    Insert(Itm);
101    
102    PendingFlag = true;
103
104    return false;   
105 }
106                                                                         /*}}}*/
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.
113  */
114 bool GlobalError::WarningE(const char *Function,const char *Description,...)
115 {
116    va_list args;
117    va_start(args,Description);
118
119    // sprintf the description
120    char S[400];
121    vsnprintf(S,sizeof(S),Description,args);
122    snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
123
124    // Put it on the list
125    Item *Itm = new Item;
126    Itm->Text = S;
127    Itm->Error = false;
128    Insert(Itm);
129    
130    return false;   
131 }
132                                                                         /*}}}*/
133 // GlobalError::Error - Add an error to the list                        /*{{{*/
134 // ---------------------------------------------------------------------
135 /* Just vsprintfs and pushes */
136 bool GlobalError::Error(const char *Description,...)
137 {
138    va_list args;
139    va_start(args,Description);
140
141    // sprintf the description
142    char S[400];
143    vsnprintf(S,sizeof(S),Description,args);
144
145    // Put it on the list
146    Item *Itm = new Item;
147    Itm->Text = S;
148    Itm->Error = true;
149    Insert(Itm);
150    
151    PendingFlag = true;
152    
153    return false;
154 }
155                                                                         /*}}}*/
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,...)
160 {
161    va_list args;
162    va_start(args,Description);
163
164    // sprintf the description
165    char S[400];
166    vsnprintf(S,sizeof(S),Description,args);
167
168    // Put it on the list
169    Item *Itm = new Item;
170    Itm->Text = S;
171    Itm->Error = false;
172    Insert(Itm);
173    
174    return false;
175 }
176                                                                         /*}}}*/
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)
182 {
183    if (List == 0)
184       return false;
185       
186    bool Ret = List->Error;
187    Text = List->Text;
188    Item *Old = List;
189    List = List->Next;
190    delete Old;
191    
192    // This really should check the list to see if only warnings are left..
193    if (List == 0)
194       PendingFlag = false;
195    
196    return Ret;
197 }
198                                                                         /*}}}*/
199 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr       /*{{{*/
200 // ---------------------------------------------------------------------
201 /* */
202 void GlobalError::DumpErrors()
203 {
204    // Print any errors or warnings found
205    string Err;
206    while (empty() == false)
207    {
208       bool Type = PopMessage(Err);
209       if (Type == true)
210          std::cerr << "E: " << Err << endl;
211       else
212          cerr << "W: " << Err << endl;
213    }
214 }
215                                                                         /*}}}*/
216 // GlobalError::Discard - Discard                                                                       /*{{{*/
217 // ---------------------------------------------------------------------
218 /* */
219 void GlobalError::Discard()
220 {
221    while (List != 0)
222    {
223       Item *Old = List;
224       List = List->Next;
225       delete Old;
226    }
227    
228    PendingFlag = false;
229 };
230                                                                         /*}}}*/
231 // GlobalError::Insert - Insert a new item at the end                   /*{{{*/
232 // ---------------------------------------------------------------------
233 /* */
234 void GlobalError::Insert(Item *Itm)
235 {
236    Item **End = &List;
237    for (Item *I = List; I != 0; I = I->Next)
238       End = &I->Next;
239    Itm->Next = *End;
240    *End = Itm;
241 }
242                                                                         /*}}}*/