]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
* methods/https.cc:
[apt.git] / apt-pkg / contrib / error.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
90f057fd 3// $Id: error.cc,v 1.11 2002/03/26 07:38:58 jgg Exp $
578bfd0a
AL
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 /*{{{*/
6c139d6e 17#ifdef __GNUG__
094a497d 18#pragma implementation "apt-pkg/error.h"
6c139d6e
AL
19#endif
20
6f27a7fc
AL
21#include <apt-pkg/error.h>
22
90f057fd 23#include <iostream>
578bfd0a
AL
24#include <errno.h>
25#include <stdio.h>
4d055c05 26#include <string>
578bfd0a 27#include <stdarg.h>
6f27a7fc 28#include <unistd.h>
578bfd0a 29
afd9aaf3 30#include "config.h"
578bfd0a
AL
31 /*}}}*/
32
4d055c05
AL
33using namespace std;
34
6f27a7fc
AL
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 */
b2e465d6 40#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
6f27a7fc
AL
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 /*}}}*/
578bfd0a
AL
65
66// GlobalError::GlobalError - Constructor /*{{{*/
67// ---------------------------------------------------------------------
68/* */
6c139d6e 69GlobalError::GlobalError() : List(0), PendingFlag(false)
578bfd0a
AL
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 */
80bool 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];
7f25bdff
AL
87 vsnprintf(S,sizeof(S),Description,args);
88 snprintf(S + strlen(S),sizeof(S) - strlen(S),
89 " - %s (%i %s)",Function,errno,strerror(errno));
578bfd0a
AL
90
91 // Put it on the list
6c139d6e
AL
92 Item *Itm = new Item;
93 Itm->Text = S;
94 Itm->Error = true;
95 Insert(Itm);
578bfd0a
AL
96
97 PendingFlag = true;
98
c5162d56
AL
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 */
109bool 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];
7f25bdff
AL
116 vsnprintf(S,sizeof(S),Description,args);
117 snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
c5162d56
AL
118
119 // Put it on the list
120 Item *Itm = new Item;
121 Itm->Text = S;
122 Itm->Error = false;
123 Insert(Itm);
124
578bfd0a
AL
125 return false;
126}
127 /*}}}*/
128// GlobalError::Error - Add an error to the list /*{{{*/
129// ---------------------------------------------------------------------
130/* Just vsprintfs and pushes */
131bool GlobalError::Error(const char *Description,...)
132{
133 va_list args;
134 va_start(args,Description);
135
136 // sprintf the description
137 char S[400];
7f25bdff 138 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
139
140 // Put it on the list
6c139d6e
AL
141 Item *Itm = new Item;
142 Itm->Text = S;
143 Itm->Error = true;
144 Insert(Itm);
578bfd0a
AL
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 */
154bool GlobalError::Warning(const char *Description,...)
155{
156 va_list args;
157 va_start(args,Description);
158
159 // sprintf the description
160 char S[400];
7f25bdff 161 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
162
163 // Put it on the list
6c139d6e
AL
164 Item *Itm = new Item;
165 Itm->Text = S;
166 Itm->Error = false;
167 Insert(Itm);
578bfd0a
AL
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. */
176bool GlobalError::PopMessage(string &Text)
177{
6c139d6e
AL
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
578bfd0a 187 // This really should check the list to see if only warnings are left..
6c139d6e 188 if (List == 0)
578bfd0a
AL
189 PendingFlag = false;
190
191 return Ret;
192}
193 /*}}}*/
194// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
195// ---------------------------------------------------------------------
196/* */
197void 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 /*}}}*/
6c139d6e
AL
211// GlobalError::Discard - Discard /*{{{*/
212// ---------------------------------------------------------------------
213/* */
214void 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/* */
229void 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 /*}}}*/