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