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