]> git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
merge with lp:~mvo/apt/debian-sid
[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)," - %s (%i %s)",Function,errno,strerror(errno));
116
117 // Put it on the list
118 Item *Itm = new Item;
119 Itm->Text = S;
120 Itm->Error = false;
121 Insert(Itm);
122
123 return false;
124 }
125 /*}}}*/
126 // GlobalError::Error - Add an error to the list /*{{{*/
127 // ---------------------------------------------------------------------
128 /* Just vsprintfs and pushes */
129 bool GlobalError::Error(const char *Description,...)
130 {
131 va_list args;
132 va_start(args,Description);
133
134 // sprintf the description
135 char S[400];
136 vsnprintf(S,sizeof(S),Description,args);
137
138 // Put it on the list
139 Item *Itm = new Item;
140 Itm->Text = S;
141 Itm->Error = true;
142 Insert(Itm);
143
144 PendingFlag = true;
145
146 return false;
147 }
148 /*}}}*/
149 // GlobalError::Warning - Add a warning to the list /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This doesn't set the pending error flag */
152 bool GlobalError::Warning(const char *Description,...)
153 {
154 va_list args;
155 va_start(args,Description);
156
157 // sprintf the description
158 char S[400];
159 vsnprintf(S,sizeof(S),Description,args);
160
161 // Put it on the list
162 Item *Itm = new Item;
163 Itm->Text = S;
164 Itm->Error = false;
165 Insert(Itm);
166
167 return false;
168 }
169 /*}}}*/
170 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This should be used in a loop checking empty() each cycle. It returns
173 true if the message is an error. */
174 bool GlobalError::PopMessage(string &Text)
175 {
176 if (List == 0)
177 return false;
178
179 bool Ret = List->Error;
180 Text = List->Text;
181 Item *Old = List;
182 List = List->Next;
183 delete Old;
184
185 // This really should check the list to see if only warnings are left..
186 if (List == 0)
187 PendingFlag = false;
188
189 return Ret;
190 }
191 /*}}}*/
192 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
193 // ---------------------------------------------------------------------
194 /* */
195 void GlobalError::DumpErrors()
196 {
197 // Print any errors or warnings found
198 string Err;
199 while (empty() == false)
200 {
201 bool Type = PopMessage(Err);
202 if (Type == true)
203 cerr << "E: " << Err << endl;
204 else
205 cerr << "W: " << Err << endl;
206 }
207 }
208 /*}}}*/
209 // GlobalError::Discard - Discard /*{{{*/
210 // ---------------------------------------------------------------------
211 /* */
212 void GlobalError::Discard()
213 {
214 while (List != 0)
215 {
216 Item *Old = List;
217 List = List->Next;
218 delete Old;
219 }
220
221 PendingFlag = false;
222 };
223 /*}}}*/
224 // GlobalError::Insert - Insert a new item at the end /*{{{*/
225 // ---------------------------------------------------------------------
226 /* */
227 void GlobalError::Insert(Item *Itm)
228 {
229 Item **End = &List;
230 for (Item *I = List; I != 0; I = I->Next)
231 End = &I->Next;
232 Itm->Next = *End;
233 *End = Itm;
234 }
235 /*}}}*/