]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 /* ######################################################################
5 Global Error Class - Global error mechanism
7 We use a simple STL vector to store each error record. A PendingFlag
8 is kept which indicates when the vector contains a Sever error.
10 This source is placed in the Public Domain, do with it what you will
11 It was originally written by Jason Gunthorpe.
13 ##################################################################### */
15 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
29 // Global Error Object /*{{{*/
30 /* If the implementation supports posix threads then the accessor function
31 is compiled to be thread safe otherwise a non-safe version is used. A
32 Per-Thread error object is maintained in much the same manner as libc
34 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
37 static pthread_key_t ErrorKey
;
38 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
39 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
41 GlobalError
*_GetErrorObj() {
42 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
43 pthread_once(&Once
,KeyAlloc
);
45 void *Res
= pthread_getspecific(ErrorKey
);
47 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
48 return (GlobalError
*)Res
;
51 GlobalError
*_GetErrorObj() {
52 static GlobalError
*Obj
= new GlobalError
;
57 // GlobalError::GlobalError - Constructor /*{{{*/
58 GlobalError::GlobalError() : PendingFlag(false) {}
60 // GlobalError::FatalE - Get part of the error string from errno /*{{{*/
61 bool GlobalError::FatalE(const char *Function
,const char *Description
,...) {
63 va_start(args
,Description
);
64 return InsertErrno(FATAL
, Function
, Description
, args
);
67 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
68 bool GlobalError::Errno(const char *Function
,const char *Description
,...) {
70 va_start(args
,Description
);
71 return InsertErrno(ERROR
, Function
, Description
, args
);
74 // GlobalError::WarningE - Get part of the warning string from errno /*{{{*/
75 bool GlobalError::WarningE(const char *Function
,const char *Description
,...) {
77 va_start(args
,Description
);
78 return InsertErrno(WARNING
, Function
, Description
, args
);
81 // GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/
82 bool GlobalError::NoticeE(const char *Function
,const char *Description
,...) {
84 va_start(args
,Description
);
85 return InsertErrno(NOTICE
, Function
, Description
, args
);
88 // GlobalError::DebugE - Get part of the debug string from errno /*{{{*/
89 bool GlobalError::DebugE(const char *Function
,const char *Description
,...) {
91 va_start(args
,Description
);
92 return InsertErrno(DEBUG
, Function
, Description
, args
);
95 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
96 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
97 const char* Description
, va_list &args
) {
99 snprintf(S
, sizeof(S
), "%s - %s (%i: %s)", Description
,
100 Function
, errno
, strerror(errno
));
101 return Insert(type
, S
, args
);
104 // GlobalError::Fatal - Add a fatal error to the list /*{{{*/
105 bool GlobalError::Fatal(const char *Description
,...) {
107 va_start(args
,Description
);
108 return Insert(FATAL
, Description
, args
);
111 // GlobalError::Error - Add an error to the list /*{{{*/
112 bool GlobalError::Error(const char *Description
,...) {
114 va_start(args
,Description
);
115 return Insert(ERROR
, Description
, args
);
118 // GlobalError::Warning - Add a warning to the list /*{{{*/
119 bool GlobalError::Warning(const char *Description
,...) {
121 va_start(args
,Description
);
122 return Insert(WARNING
, Description
, args
);
125 // GlobalError::Notice - Add a notice to the list /*{{{*/
126 bool GlobalError::Notice(const char *Description
,...)
129 va_start(args
,Description
);
130 return Insert(NOTICE
, Description
, args
);
133 // GlobalError::Debug - Add a debug to the list /*{{{*/
134 bool GlobalError::Debug(const char *Description
,...)
137 va_start(args
,Description
);
138 return Insert(DEBUG
, Description
, args
);
141 // GlobalError::Insert - Insert a new item at the end /*{{{*/
142 bool GlobalError::Insert(MsgType type
, const char* Description
,
145 vsnprintf(S
,sizeof(S
),Description
,args
);
147 Item
const m(S
, type
);
148 Messages
.push_back(m
);
150 if (type
== ERROR
|| type
== FATAL
)
153 if (type
== FATAL
|| type
== DEBUG
)
154 std::clog
<< m
<< std::endl
;
159 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
160 bool GlobalError::PopMessage(std::string
&Text
) {
161 if (Messages
.empty() == true)
164 Item
const msg
= Messages
.front();
165 Messages
.pop_front();
167 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
169 if (PendingFlag
== false || Ret
== false)
172 // check if another error message is pending
173 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
174 m
!= Messages
.end(); m
++)
175 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
182 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
183 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
184 bool const &mergeStack
) {
185 if (mergeStack
== true)
186 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
187 s
!= Stacks
.rend(); ++s
)
188 Messages
.insert(Messages
.begin(), s
->Messages
.begin(), s
->Messages
.end());
190 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
191 m
!= Messages
.end(); m
++)
192 if (m
->Type
>= threshold
)
193 out
<< (*m
) << std::endl
;
197 // GlobalError::Discard - Discard /*{{{*/
198 void GlobalError::Discard() {
203 // GlobalError::empty - does our error list include anything? /*{{{*/
204 bool GlobalError::empty(MsgType
const &trashhold
) const {
205 if (PendingFlag
== true)
208 if (Messages
.empty() == true)
211 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
212 m
!= Messages
.end(); m
++)
213 if (m
->Type
>= trashhold
)
219 // GlobalError::PushToStack /*{{{*/
220 void GlobalError::PushToStack() {
221 MsgStack
pack(Messages
, PendingFlag
);
222 Stacks
.push_back(pack
);
226 // GlobalError::RevertToStack /*{{{*/
227 void GlobalError::RevertToStack() {
229 MsgStack pack
= Stacks
.back();
230 Messages
= pack
.Messages
;
231 PendingFlag
= pack
.PendingFlag
;
235 // GlobalError::MergeWithStack /*{{{*/
236 void GlobalError::MergeWithStack() {
237 MsgStack pack
= Stacks
.back();
238 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
239 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;