]>
git.saurik.com Git - apt.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 /*{{{*/
17 #include <apt-pkg/error.h>
33 // Global Error Object /*{{{*/
34 /* If the implementation supports posix threads then the accessor function
35 is compiled to be thread safe otherwise a non-safe version is used. A
36 Per-Thread error object is maintained in much the same manner as libc
38 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
41 static pthread_key_t ErrorKey
;
42 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
43 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
45 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()
58 static GlobalError
*Obj
= new GlobalError
;
64 // GlobalError::GlobalError - Constructor /*{{{*/
65 // ---------------------------------------------------------------------
67 GlobalError::GlobalError() : List(0), PendingFlag(false)
71 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
72 // ---------------------------------------------------------------------
73 /* Function indicates the stdlib function that failed and Description is
74 a user string that leads the text. Form is:
75 Description - Function (errno: strerror)
76 Carefull of the buffer overrun, sprintf.
78 bool GlobalError::Errno(const char *Function
,const char *Description
,...)
81 va_start(args
,Description
);
83 // sprintf the description
85 vsnprintf(S
,sizeof(S
),Description
,args
);
86 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
),
87 " - %s (%i %s)",Function
,errno
,strerror(errno
));
100 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
101 // ---------------------------------------------------------------------
102 /* Function indicates the stdlib function that failed and Description is
103 a user string that leads the text. Form is:
104 Description - Function (errno: strerror)
105 Carefull of the buffer overrun, sprintf.
107 bool GlobalError::WarningE(const char *Function
,const char *Description
,...)
110 va_start(args
,Description
);
112 // sprintf the description
114 vsnprintf(S
,sizeof(S
),Description
,args
);
115 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
117 // Put it on the list
118 Item
*Itm
= new Item
;
126 // GlobalError::Error - Add an error to the list /*{{{*/
127 // ---------------------------------------------------------------------
128 /* Just vsprintfs and pushes */
129 bool GlobalError::Error(const char *Description
,...)
132 va_start(args
,Description
);
134 // sprintf the description
136 vsnprintf(S
,sizeof(S
),Description
,args
);
138 // Put it on the list
139 Item
*Itm
= new Item
;
149 // GlobalError::Warning - Add a warning to the list /*{{{*/
150 // ---------------------------------------------------------------------
151 /* This doesn't set the pending error flag */
152 bool GlobalError::Warning(const char *Description
,...)
155 va_start(args
,Description
);
157 // sprintf the description
159 vsnprintf(S
,sizeof(S
),Description
,args
);
161 // Put it on the list
162 Item
*Itm
= new Item
;
170 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
171 // ---------------------------------------------------------------------
172 /* This should be used in a loop checking empty() each cycle. It returns
173 true if the message is an error. */
174 bool GlobalError::PopMessage(string
&Text
)
179 bool Ret
= List
->Error
;
185 // This really should check the list to see if only warnings are left..
192 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
193 // ---------------------------------------------------------------------
195 void GlobalError::DumpErrors()
197 // Print any errors or warnings found
199 while (empty() == false)
201 bool Type
= PopMessage(Err
);
203 cerr
<< "E: " << Err
<< endl
;
205 cerr
<< "W: " << Err
<< endl
;
209 // GlobalError::Discard - Discard /*{{{*/
210 // ---------------------------------------------------------------------
212 void GlobalError::Discard()
224 // GlobalError::Insert - Insert a new item at the end /*{{{*/
225 // ---------------------------------------------------------------------
227 void GlobalError::Insert(Item
*Itm
)
230 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)