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