]>
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 /*{{{*/
18 #include <apt-pkg/error.h>
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
36 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
39 static pthread_key_t ErrorKey
;
40 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
41 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
43 GlobalError
*_GetErrorObj() {
44 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
45 pthread_once(&Once
,KeyAlloc
);
47 void *Res
= pthread_getspecific(ErrorKey
);
49 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
50 return (GlobalError
*)Res
;
53 GlobalError
*_GetErrorObj() {
54 static GlobalError
*Obj
= new GlobalError
;
59 // GlobalError::GlobalError - Constructor /*{{{*/
60 GlobalError::GlobalError() : PendingFlag(false) {}
62 // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
63 #define GEMessage(NAME, TYPE) \
64 bool GlobalError::NAME (const char *Function, const char *Description,...) { \
66 size_t msgSize = 400; \
67 int const errsv = errno; \
69 va_start(args,Description); \
70 if (InsertErrno(TYPE, Function, Description, args, errsv, msgSize) == false) \
76 GEMessage(FatalE
, FATAL
)
77 GEMessage(Errno
, ERROR
)
78 GEMessage(WarningE
, WARNING
)
79 GEMessage(NoticeE
, NOTICE
)
80 GEMessage(DebugE
, DEBUG
)
83 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
84 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
85 const char *Description
,...) {
88 int const errsv
= errno
;
90 va_start(args
,Description
);
91 if (InsertErrno(type
, Function
, Description
, args
, errsv
, msgSize
) == false)
98 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
99 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
100 const char* Description
, va_list &args
,
101 int const errsv
, size_t &msgSize
) {
102 char* S
= (char*) malloc(msgSize
);
103 int const n
= snprintf(S
, msgSize
, "%s - %s (%i: %s)", Description
,
104 Function
, errsv
, strerror(errsv
));
105 if (n
> -1 && ((unsigned int) n
) < msgSize
);
115 bool const geins
= Insert(type
, S
, args
, msgSize
);
120 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
121 #define GEMessage(NAME, TYPE) \
122 bool GlobalError::NAME (const char *Description,...) { \
124 size_t msgSize = 400; \
126 va_start(args,Description); \
127 if (Insert(TYPE, Description, args, msgSize) == false) \
133 GEMessage(Fatal
, FATAL
)
134 GEMessage(Error
, ERROR
)
135 GEMessage(Warning
, WARNING
)
136 GEMessage(Notice
, NOTICE
)
137 GEMessage(Debug
, DEBUG
)
140 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
141 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
144 size_t msgSize
= 400;
146 va_start(args
,Description
);
147 if (Insert(type
, Description
, args
, msgSize
) == false)
154 // GlobalError::Insert - Insert a new item at the end /*{{{*/
155 bool GlobalError::Insert(MsgType type
, const char* Description
,
156 va_list &args
, size_t &msgSize
) {
157 char* S
= (char*) malloc(msgSize
);
158 int const n
= vsnprintf(S
, msgSize
, Description
, args
);
159 if (n
> -1 && ((unsigned int) n
) < msgSize
);
169 Item
const m(S
, type
);
170 Messages
.push_back(m
);
172 if (type
== ERROR
|| type
== FATAL
)
175 if (type
== FATAL
|| type
== DEBUG
)
176 std::clog
<< m
<< std::endl
;
182 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
183 bool GlobalError::PopMessage(std::string
&Text
) {
184 if (Messages
.empty() == true)
187 Item
const msg
= Messages
.front();
188 Messages
.pop_front();
190 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
192 if (PendingFlag
== false || Ret
== false)
195 // check if another error message is pending
196 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
197 m
!= Messages
.end(); ++m
)
198 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
205 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
206 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
207 bool const &mergeStack
) {
208 if (mergeStack
== true)
209 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
210 s
!= Stacks
.rend(); ++s
)
211 Messages
.insert(Messages
.begin(), s
->Messages
.begin(), s
->Messages
.end());
213 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
214 m
!= Messages
.end(); ++m
)
215 if (m
->Type
>= threshold
)
216 out
<< (*m
) << std::endl
;
220 // GlobalError::Discard - Discard /*{{{*/
221 void GlobalError::Discard() {
226 // GlobalError::empty - does our error list include anything? /*{{{*/
227 bool GlobalError::empty(MsgType
const &trashhold
) const {
228 if (PendingFlag
== true)
231 if (Messages
.empty() == true)
234 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
235 m
!= Messages
.end(); ++m
)
236 if (m
->Type
>= trashhold
)
242 // GlobalError::PushToStack /*{{{*/
243 void GlobalError::PushToStack() {
244 MsgStack
pack(Messages
, PendingFlag
);
245 Stacks
.push_back(pack
);
249 // GlobalError::RevertToStack /*{{{*/
250 void GlobalError::RevertToStack() {
252 MsgStack pack
= Stacks
.back();
253 Messages
= pack
.Messages
;
254 PendingFlag
= pack
.PendingFlag
;
258 // GlobalError::MergeWithStack /*{{{*/
259 void GlobalError::MergeWithStack() {
260 MsgStack pack
= Stacks
.back();
261 Messages
.insert(Messages
.begin(), pack
.Messages
.begin(), pack
.Messages
.end());
262 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;