]>
git.saurik.com Git - apt-legacy.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: error.cc,v 1.11 2002/03/26 07:38:58 jgg Exp $
4 /* ######################################################################
6 Global Erorr Class - Global error mechanism
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.
11 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe.
14 ##################################################################### */
16 // Include Files /*{{{*/
18 #pragma implementation "apt-pkg/error.h"
21 #include <apt-pkg/error.h>
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
40 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
43 static pthread_key_t ErrorKey
;
44 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
45 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
47 GlobalError
*_GetErrorObj()
49 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
50 pthread_once(&Once
,KeyAlloc
);
52 void *Res
= pthread_getspecific(ErrorKey
);
54 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
55 return (GlobalError
*)Res
;
58 GlobalError
*_GetErrorObj()
60 static GlobalError
*Obj
= new GlobalError
;
66 // GlobalError::GlobalError - Constructor /*{{{*/
67 // ---------------------------------------------------------------------
69 GlobalError::GlobalError() : List(0), PendingFlag(false)
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.
80 bool GlobalError::Errno(const char *Function
,const char *Description
,...)
83 va_start(args
,Description
);
85 // sprintf the description
87 vsnprintf(S
,sizeof(S
),Description
,args
);
88 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
),
89 " - %s (%i %s)",Function
,errno
,strerror(errno
));
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.
109 bool GlobalError::WarningE(const char *Function
,const char *Description
,...)
112 va_start(args
,Description
);
114 // sprintf the description
116 vsnprintf(S
,sizeof(S
),Description
,args
);
117 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
119 // Put it on the list
120 Item
*Itm
= new Item
;
128 // GlobalError::Error - Add an error to the list /*{{{*/
129 // ---------------------------------------------------------------------
130 /* Just vsprintfs and pushes */
131 bool GlobalError::Error(const char *Description
,...)
134 va_start(args
,Description
);
136 // sprintf the description
138 vsnprintf(S
,sizeof(S
),Description
,args
);
140 // Put it on the list
141 Item
*Itm
= new Item
;
151 // GlobalError::Warning - Add a warning to the list /*{{{*/
152 // ---------------------------------------------------------------------
153 /* This doesn't set the pending error flag */
154 bool GlobalError::Warning(const char *Description
,...)
157 va_start(args
,Description
);
159 // sprintf the description
161 vsnprintf(S
,sizeof(S
),Description
,args
);
163 // Put it on the list
164 Item
*Itm
= new Item
;
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. */
176 bool GlobalError::PopMessage(string
&Text
)
181 bool Ret
= List
->Error
;
187 // This really should check the list to see if only warnings are left..
194 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
195 // ---------------------------------------------------------------------
197 void GlobalError::DumpErrors()
199 // Print any errors or warnings found
201 while (empty() == false)
203 bool Type
= PopMessage(Err
);
205 cerr
<< "E: " << Err
<< endl
;
207 cerr
<< "W: " << Err
<< endl
;
211 // GlobalError::Discard - Discard /*{{{*/
212 // ---------------------------------------------------------------------
214 void GlobalError::Discard()
226 // GlobalError::Insert - Insert a new item at the end /*{{{*/
227 // ---------------------------------------------------------------------
229 void GlobalError::Insert(Item
*Itm
)
232 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)