* make apt build with g++ 4.3
[ntk/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 /*{{{*/
6f27a7fc
AL
17#include <apt-pkg/error.h>
18
90f057fd 19#include <iostream>
578bfd0a
AL
20#include <errno.h>
21#include <stdio.h>
578bfd0a 22#include <stdarg.h>
6f27a7fc 23#include <unistd.h>
578bfd0a 24
4f333a8b
MV
25#include <string>
26#include <cstring>
27
afd9aaf3 28#include "config.h"
578bfd0a
AL
29 /*}}}*/
30
4d055c05
AL
31using namespace std;
32
6f27a7fc
AL
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 */
b2e465d6 38#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
6f27a7fc
AL
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 /*}}}*/
578bfd0a
AL
63
64// GlobalError::GlobalError - Constructor /*{{{*/
65// ---------------------------------------------------------------------
66/* */
6c139d6e 67GlobalError::GlobalError() : List(0), PendingFlag(false)
578bfd0a
AL
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 */
78bool 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];
7f25bdff
AL
85 vsnprintf(S,sizeof(S),Description,args);
86 snprintf(S + strlen(S),sizeof(S) - strlen(S),
87 " - %s (%i %s)",Function,errno,strerror(errno));
578bfd0a
AL
88
89 // Put it on the list
6c139d6e
AL
90 Item *Itm = new Item;
91 Itm->Text = S;
92 Itm->Error = true;
93 Insert(Itm);
578bfd0a
AL
94
95 PendingFlag = true;
96
c5162d56
AL
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 */
107bool 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];
7f25bdff
AL
114 vsnprintf(S,sizeof(S),Description,args);
115 snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
c5162d56
AL
116
117 // Put it on the list
118 Item *Itm = new Item;
119 Itm->Text = S;
120 Itm->Error = false;
121 Insert(Itm);
122
578bfd0a
AL
123 return false;
124}
125 /*}}}*/
126// GlobalError::Error - Add an error to the list /*{{{*/
127// ---------------------------------------------------------------------
128/* Just vsprintfs and pushes */
129bool GlobalError::Error(const char *Description,...)
130{
131 va_list args;
132 va_start(args,Description);
133
134 // sprintf the description
135 char S[400];
7f25bdff 136 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
137
138 // Put it on the list
6c139d6e
AL
139 Item *Itm = new Item;
140 Itm->Text = S;
141 Itm->Error = true;
142 Insert(Itm);
578bfd0a
AL
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 */
152bool GlobalError::Warning(const char *Description,...)
153{
154 va_list args;
155 va_start(args,Description);
156
157 // sprintf the description
158 char S[400];
7f25bdff 159 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
160
161 // Put it on the list
6c139d6e
AL
162 Item *Itm = new Item;
163 Itm->Text = S;
164 Itm->Error = false;
165 Insert(Itm);
578bfd0a
AL
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. */
174bool GlobalError::PopMessage(string &Text)
175{
6c139d6e
AL
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
578bfd0a 185 // This really should check the list to see if only warnings are left..
6c139d6e 186 if (List == 0)
578bfd0a
AL
187 PendingFlag = false;
188
189 return Ret;
190}
191 /*}}}*/
192// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
193// ---------------------------------------------------------------------
194/* */
195void 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 /*}}}*/
6c139d6e
AL
209// GlobalError::Discard - Discard /*{{{*/
210// ---------------------------------------------------------------------
211/* */
212void 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/* */
227void 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 /*}}}*/