]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
Simplified CHinese translation update
[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),
9d800312 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);
9d800312 94
578bfd0a
AL
95 PendingFlag = true;
96
9d800312 97 return false;
c5162d56
AL
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 114 vsnprintf(S,sizeof(S),Description,args);
9d800312
DK
115 snprintf(S + strlen(S),sizeof(S) - strlen(S),
116 " - %s (%i: %s)",Function,errno,strerror(errno));
c5162d56
AL
117
118 // Put it on the list
119 Item *Itm = new Item;
120 Itm->Text = S;
121 Itm->Error = false;
122 Insert(Itm);
9d800312
DK
123
124 return false;
578bfd0a
AL
125}
126 /*}}}*/
127// GlobalError::Error - Add an error to the list /*{{{*/
128// ---------------------------------------------------------------------
129/* Just vsprintfs and pushes */
130bool GlobalError::Error(const char *Description,...)
131{
132 va_list args;
133 va_start(args,Description);
134
135 // sprintf the description
136 char S[400];
7f25bdff 137 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
138
139 // Put it on the list
6c139d6e
AL
140 Item *Itm = new Item;
141 Itm->Text = S;
142 Itm->Error = true;
143 Insert(Itm);
578bfd0a
AL
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 */
153bool GlobalError::Warning(const char *Description,...)
154{
155 va_list args;
156 va_start(args,Description);
157
158 // sprintf the description
159 char S[400];
7f25bdff 160 vsnprintf(S,sizeof(S),Description,args);
578bfd0a
AL
161
162 // Put it on the list
6c139d6e
AL
163 Item *Itm = new Item;
164 Itm->Text = S;
165 Itm->Error = false;
166 Insert(Itm);
578bfd0a
AL
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. */
175bool GlobalError::PopMessage(string &Text)
176{
6c139d6e
AL
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
578bfd0a 186 // This really should check the list to see if only warnings are left..
6c139d6e 187 if (List == 0)
578bfd0a
AL
188 PendingFlag = false;
189
190 return Ret;
191}
192 /*}}}*/
193// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
194// ---------------------------------------------------------------------
195/* */
196void 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 /*}}}*/
6c139d6e
AL
210// GlobalError::Discard - Discard /*{{{*/
211// ---------------------------------------------------------------------
212/* */
213void 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/* */
228void 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 /*}}}*/