]>
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>
34 // Global Error Object /*{{{*/
35 /* If the implementation supports posix threads then the accessor function
36 is compiled to be thread safe otherwise a non-safe version is used. A
37 Per-Thread error object is maintained in much the same manner as libc
39 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
42 static pthread_key_t ErrorKey
;
43 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
44 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
46 GlobalError
*_GetErrorObj() {
47 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
48 pthread_once(&Once
,KeyAlloc
);
50 void *Res
= pthread_getspecific(ErrorKey
);
52 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
53 return (GlobalError
*)Res
;
56 GlobalError
*_GetErrorObj() {
57 static GlobalError
*Obj
= new GlobalError
;
62 // GlobalError::GlobalError - Constructor /*{{{*/
63 GlobalError::GlobalError() : PendingFlag(false) {}
65 // GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
66 #define GEMessage(NAME, TYPE) \
67 bool GlobalError::NAME (const char *Function, const char *Description,...) { \
69 size_t msgSize = 400; \
70 int const errsv = errno; \
73 va_start(args,Description); \
74 retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
79 GEMessage(FatalE
, FATAL
)
80 GEMessage(Errno
, ERROR
)
81 GEMessage(WarningE
, WARNING
)
82 GEMessage(NoticeE
, NOTICE
)
83 GEMessage(DebugE
, DEBUG
)
86 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
87 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
88 const char *Description
,...) {
91 int const errsv
= errno
;
94 va_start(args
,Description
);
95 retry
= InsertErrno(type
, Function
, Description
, args
, errsv
, msgSize
);
101 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
102 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
103 const char* Description
, va_list &args
,
104 int const errsv
, size_t &msgSize
) {
105 char* S
= (char*) malloc(msgSize
);
106 int const n
= snprintf(S
, msgSize
, "%s - %s (%i: %s)", Description
,
107 Function
, errsv
, strerror(errsv
));
108 if (n
> -1 && ((unsigned int) n
) < msgSize
);
118 bool const geins
= Insert(type
, S
, args
, msgSize
);
123 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
124 #define GEMessage(NAME, TYPE) \
125 bool GlobalError::NAME (const char *Description,...) { \
127 size_t msgSize = 400; \
130 va_start(args,Description); \
131 retry = Insert(TYPE, Description, args, msgSize); \
136 GEMessage(Fatal
, FATAL
)
137 GEMessage(Error
, ERROR
)
138 GEMessage(Warning
, WARNING
)
139 GEMessage(Notice
, NOTICE
)
140 GEMessage(Debug
, DEBUG
)
143 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
144 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
147 size_t msgSize
= 400;
150 va_start(args
,Description
);
151 retry
= Insert(type
, Description
, args
, msgSize
);
157 // GlobalError::Insert - Insert a new item at the end /*{{{*/
158 bool GlobalError::Insert(MsgType type
, const char* Description
,
159 va_list &args
, size_t &msgSize
) {
160 char* S
= (char*) malloc(msgSize
);
161 int const n
= vsnprintf(S
, msgSize
, Description
, args
);
162 if (n
> -1 && ((unsigned int) n
) < msgSize
);
172 Item
const m(S
, type
);
173 Messages
.push_back(m
);
175 if (type
== ERROR
|| type
== FATAL
)
178 if (type
== FATAL
|| type
== DEBUG
)
179 std::clog
<< m
<< std::endl
;
185 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
186 bool GlobalError::PopMessage(std::string
&Text
) {
187 if (Messages
.empty() == true)
190 Item
const msg
= Messages
.front();
191 Messages
.pop_front();
193 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
195 if (PendingFlag
== false || Ret
== false)
198 // check if another error message is pending
199 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
200 m
!= Messages
.end(); ++m
)
201 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
208 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
209 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
210 bool const &mergeStack
) {
211 if (mergeStack
== true)
212 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
213 s
!= Stacks
.rend(); ++s
)
214 std::copy(s
->Messages
.begin(), s
->Messages
.end(), std::front_inserter(Messages
));
216 std::for_each(Messages
.begin(), Messages
.end(), [&threshold
, &out
](Item
const &m
) {
217 if (m
.Type
>= threshold
)
218 out
<< m
<< std::endl
;
224 // GlobalError::Discard - Discard /*{{{*/
225 void GlobalError::Discard() {
230 // GlobalError::ReturnError - convert a stored error to a return code /*{{{*/
231 bool GlobalError::ReturnError() {
232 for (auto &message
: Messages
)
233 if (message
.Type
== ERROR
)
234 message
.Type
= WARNING
;
239 // GlobalError::empty - does our error list include anything? /*{{{*/
240 bool GlobalError::empty(MsgType
const &threshold
) const {
241 if (PendingFlag
== true)
244 if (Messages
.empty() == true)
247 return std::find_if(Messages
.begin(), Messages
.end(), [&threshold
](Item
const &m
) {
248 return m
.Type
>= threshold
;
249 }) == Messages
.end();
252 // GlobalError::PushToStack /*{{{*/
253 void GlobalError::PushToStack() {
254 Stacks
.emplace_back(Messages
, PendingFlag
);
258 // GlobalError::RevertToStack /*{{{*/
259 void GlobalError::RevertToStack() {
261 MsgStack pack
= Stacks
.back();
262 Messages
= pack
.Messages
;
263 PendingFlag
= pack
.PendingFlag
;
267 // GlobalError::MergeWithStack /*{{{*/
268 void GlobalError::MergeWithStack() {
269 MsgStack pack
= Stacks
.back();
270 Messages
.splice(Messages
.begin(), pack
.Messages
);
271 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;