]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
Fixes for file opening
[apt.git] / apt-pkg / contrib / error.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: error.cc,v 1.5 1998/09/18 02:42:40 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 "apt-pkg/error.h"
19 #endif
20
21 #include <apt-pkg/error.h>
22
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdarg.h>
27 #include <unistd.h>
28
29 /*}}}*/
30
31 // Global Error Object /*{{{*/
32 /* If the implementation supports posix threads then the accessor function
33 is compiled to be thread safe otherwise a non-safe version is used. A
34 Per-Thread error object is maintained in much the same manner as libc
35 manages errno */
36 #if _POSIX_THREADS == 1
37 #include <pthread.h>
38
39 static pthread_key_t ErrorKey;
40 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
41 static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
42
43 GlobalError *_GetErrorObj()
44 {
45 static pthread_once_t Once = PTHREAD_ONCE_INIT;
46 pthread_once(&Once,KeyAlloc);
47
48 void *Res = pthread_getspecific(ErrorKey);
49 if (Res == 0)
50 pthread_setspecific(ErrorKey,Res = new GlobalError);
51 return (GlobalError *)Res;
52 }
53 #else
54 GlobalError *_GetErrorObj()
55 {
56 static GlobalError *Obj = new GlobalError;
57 return Obj;
58 }
59 #endif
60 /*}}}*/
61
62 // GlobalError::GlobalError - Constructor /*{{{*/
63 // ---------------------------------------------------------------------
64 /* */
65 GlobalError::GlobalError() : List(0), PendingFlag(false)
66 {
67 }
68 /*}}}*/
69 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
70 // ---------------------------------------------------------------------
71 /* Function indicates the stdlib function that failed and Description is
72 a user string that leads the text. Form is:
73 Description - Function (errno: strerror)
74 Carefull of the buffer overrun, sprintf.
75 */
76 bool GlobalError::Errno(const char *Function,const char *Description,...)
77 {
78 va_list args;
79 va_start(args,Description);
80
81 // sprintf the description
82 char S[400];
83 vsprintf(S,Description,args);
84 sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
85
86 // Put it on the list
87 Item *Itm = new Item;
88 Itm->Text = S;
89 Itm->Error = true;
90 Insert(Itm);
91
92 PendingFlag = true;
93
94 return false;
95 }
96 /*}}}*/
97 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
98 // ---------------------------------------------------------------------
99 /* Function indicates the stdlib function that failed and Description is
100 a user string that leads the text. Form is:
101 Description - Function (errno: strerror)
102 Carefull of the buffer overrun, sprintf.
103 */
104 bool GlobalError::WarningE(const char *Function,const char *Description,...)
105 {
106 va_list args;
107 va_start(args,Description);
108
109 // sprintf the description
110 char S[400];
111 vsprintf(S,Description,args);
112 sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
113
114 // Put it on the list
115 Item *Itm = new Item;
116 Itm->Text = S;
117 Itm->Error = false;
118 Insert(Itm);
119
120 return false;
121 }
122 /*}}}*/
123 // GlobalError::Error - Add an error to the list /*{{{*/
124 // ---------------------------------------------------------------------
125 /* Just vsprintfs and pushes */
126 bool GlobalError::Error(const char *Description,...)
127 {
128 va_list args;
129 va_start(args,Description);
130
131 // sprintf the description
132 char S[400];
133 vsprintf(S,Description,args);
134
135 // Put it on the list
136 Item *Itm = new Item;
137 Itm->Text = S;
138 Itm->Error = true;
139 Insert(Itm);
140
141 PendingFlag = true;
142
143 return false;
144 }
145 /*}}}*/
146 // GlobalError::Warning - Add a warning to the list /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This doesn't set the pending error flag */
149 bool GlobalError::Warning(const char *Description,...)
150 {
151 va_list args;
152 va_start(args,Description);
153
154 // sprintf the description
155 char S[400];
156 vsprintf(S,Description,args);
157
158 // Put it on the list
159 Item *Itm = new Item;
160 Itm->Text = S;
161 Itm->Error = false;
162 Insert(Itm);
163
164 return false;
165 }
166 /*}}}*/
167 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This should be used in a loop checking empty() each cycle. It returns
170 true if the message is an error. */
171 bool GlobalError::PopMessage(string &Text)
172 {
173 if (List == 0)
174 return false;
175
176 bool Ret = List->Error;
177 Text = List->Text;
178 Item *Old = List;
179 List = List->Next;
180 delete Old;
181
182 // This really should check the list to see if only warnings are left..
183 if (List == 0)
184 PendingFlag = false;
185
186 return Ret;
187 }
188 /*}}}*/
189 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
190 // ---------------------------------------------------------------------
191 /* */
192 void GlobalError::DumpErrors()
193 {
194 // Print any errors or warnings found
195 string Err;
196 while (empty() == false)
197 {
198 bool Type = PopMessage(Err);
199 if (Type == true)
200 cerr << "E: " << Err << endl;
201 else
202 cerr << "W: " << Err << endl;
203 }
204 }
205 /*}}}*/
206 // GlobalError::Discard - Discard /*{{{*/
207 // ---------------------------------------------------------------------
208 /* */
209 void GlobalError::Discard()
210 {
211 while (List != 0)
212 {
213 Item *Old = List;
214 List = List->Next;
215 delete Old;
216 }
217
218 PendingFlag = false;
219 };
220 /*}}}*/
221 // GlobalError::Insert - Insert a new item at the end /*{{{*/
222 // ---------------------------------------------------------------------
223 /* */
224 void GlobalError::Insert(Item *Itm)
225 {
226 Item **End = &List;
227 for (Item *I = List; I != 0; I = I->Next)
228 End = &I->Next;
229 Itm->Next = *End;
230 *End = Itm;
231 }
232 /*}}}*/