]>
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; \
72 va_start(args,Description); \
73 bool const retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
80 GEMessage(FatalE
, FATAL
)
81 GEMessage(Errno
, ERROR
)
82 GEMessage(WarningE
, WARNING
)
83 GEMessage(NoticeE
, NOTICE
)
84 GEMessage(DebugE
, DEBUG
)
87 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
88 bool GlobalError::InsertErrno(MsgType
const &type
, const char *Function
,
89 const char *Description
,...) {
92 int const errsv
= errno
;
94 va_start(args
,Description
);
95 bool const retry
= InsertErrno(type
, Function
, Description
, args
, errsv
, msgSize
);
103 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
104 bool GlobalError::InsertErrno(MsgType type
, const char* Function
,
105 const char* Description
, va_list &args
,
106 int const errsv
, size_t &msgSize
) {
107 char* S
= (char*) malloc(msgSize
);
108 int const n
= snprintf(S
, msgSize
, "%s - %s (%i: %s)", Description
,
109 Function
, errsv
, strerror(errsv
));
110 if (n
> -1 && ((unsigned int) n
) < msgSize
);
120 bool const geins
= Insert(type
, S
, args
, msgSize
);
125 // GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
126 #define GEMessage(NAME, TYPE) \
127 bool GlobalError::NAME (const char *Description,...) { \
129 size_t msgSize = 400; \
131 va_start(args,Description); \
132 if (Insert(TYPE, Description, args, msgSize) == false) \
138 GEMessage(Fatal
, FATAL
)
139 GEMessage(Error
, ERROR
)
140 GEMessage(Warning
, WARNING
)
141 GEMessage(Notice
, NOTICE
)
142 GEMessage(Debug
, DEBUG
)
145 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
146 bool GlobalError::Insert(MsgType
const &type
, const char *Description
,...)
149 size_t msgSize
= 400;
151 va_start(args
,Description
);
152 if (Insert(type
, Description
, args
, msgSize
) == false)
159 // GlobalError::Insert - Insert a new item at the end /*{{{*/
160 bool GlobalError::Insert(MsgType type
, const char* Description
,
161 va_list &args
, size_t &msgSize
) {
162 char* S
= (char*) malloc(msgSize
);
163 int const n
= vsnprintf(S
, msgSize
, Description
, args
);
164 if (n
> -1 && ((unsigned int) n
) < msgSize
);
174 Item
const m(S
, type
);
175 Messages
.push_back(m
);
177 if (type
== ERROR
|| type
== FATAL
)
180 if (type
== FATAL
|| type
== DEBUG
)
181 std::clog
<< m
<< std::endl
;
187 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
188 bool GlobalError::PopMessage(std::string
&Text
) {
189 if (Messages
.empty() == true)
192 Item
const msg
= Messages
.front();
193 Messages
.pop_front();
195 bool const Ret
= (msg
.Type
== ERROR
|| msg
.Type
== FATAL
);
197 if (PendingFlag
== false || Ret
== false)
200 // check if another error message is pending
201 for (std::list
<Item
>::const_iterator m
= Messages
.begin();
202 m
!= Messages
.end(); ++m
)
203 if (m
->Type
== ERROR
|| m
->Type
== FATAL
)
210 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
211 void GlobalError::DumpErrors(std::ostream
&out
, MsgType
const &threshold
,
212 bool const &mergeStack
) {
213 if (mergeStack
== true)
214 for (std::list
<MsgStack
>::const_reverse_iterator s
= Stacks
.rbegin();
215 s
!= Stacks
.rend(); ++s
)
216 std::copy(s
->Messages
.begin(), s
->Messages
.end(), std::front_inserter(Messages
));
218 std::for_each(Messages
.begin(), Messages
.end(), [&threshold
, &out
](Item
const &m
) {
219 if (m
.Type
>= threshold
)
220 out
<< m
<< std::endl
;
226 // GlobalError::Discard - Discard /*{{{*/
227 void GlobalError::Discard() {
232 // GlobalError::empty - does our error list include anything? /*{{{*/
233 bool GlobalError::empty(MsgType
const &threshold
) const {
234 if (PendingFlag
== true)
237 if (Messages
.empty() == true)
240 return std::find_if(Messages
.begin(), Messages
.end(), [&threshold
](Item
const &m
) {
241 return m
.Type
>= threshold
;
242 }) == Messages
.end();
245 // GlobalError::PushToStack /*{{{*/
246 void GlobalError::PushToStack() {
247 Stacks
.emplace_back(Messages
, PendingFlag
);
251 // GlobalError::RevertToStack /*{{{*/
252 void GlobalError::RevertToStack() {
254 MsgStack pack
= Stacks
.back();
255 Messages
= pack
.Messages
;
256 PendingFlag
= pack
.PendingFlag
;
260 // GlobalError::MergeWithStack /*{{{*/
261 void GlobalError::MergeWithStack() {
262 MsgStack pack
= Stacks
.back();
263 Messages
.splice(Messages
.begin(), pack
.Messages
);
264 PendingFlag
= PendingFlag
|| pack
.PendingFlag
;