]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: error.cc,v 1.5 1998/09/18 02:42:40 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 /*{{{*/
18 #pragma implementation "apt-pkg/error.h"
21 #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 _POSIX_THREADS == 1
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()
45 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
46 pthread_once(&Once
,KeyAlloc
);
48 void *Res
= pthread_getspecific(ErrorKey
);
50 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
51 return (GlobalError
*)Res
;
54 GlobalError
*_GetErrorObj()
56 static GlobalError
*Obj
= new GlobalError
;
62 // GlobalError::GlobalError - Constructor /*{{{*/
63 // ---------------------------------------------------------------------
65 GlobalError::GlobalError() : List(0), PendingFlag(false)
69 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
70 // ---------------------------------------------------------------------
71 /* Function indicates the stdlib function that failed and Description is
72 a user string that leads the text. Form is:
73 Description - Function (errno: strerror)
74 Carefull of the buffer overrun, sprintf.
76 bool GlobalError::Errno(const char *Function
,const char *Description
,...)
79 va_start(args
,Description
);
81 // sprintf the description
83 vsprintf(S
,Description
,args
);
84 sprintf(S
+ strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
97 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
98 // ---------------------------------------------------------------------
99 /* Function indicates the stdlib function that failed and Description is
100 a user string that leads the text. Form is:
101 Description - Function (errno: strerror)
102 Carefull of the buffer overrun, sprintf.
104 bool GlobalError::WarningE(const char *Function
,const char *Description
,...)
107 va_start(args
,Description
);
109 // sprintf the description
111 vsprintf(S
,Description
,args
);
112 sprintf(S
+ strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
114 // Put it on the list
115 Item
*Itm
= new Item
;
123 // GlobalError::Error - Add an error to the list /*{{{*/
124 // ---------------------------------------------------------------------
125 /* Just vsprintfs and pushes */
126 bool GlobalError::Error(const char *Description
,...)
129 va_start(args
,Description
);
131 // sprintf the description
133 vsprintf(S
,Description
,args
);
135 // Put it on the list
136 Item
*Itm
= new Item
;
146 // GlobalError::Warning - Add a warning to the list /*{{{*/
147 // ---------------------------------------------------------------------
148 /* This doesn't set the pending error flag */
149 bool GlobalError::Warning(const char *Description
,...)
152 va_start(args
,Description
);
154 // sprintf the description
156 vsprintf(S
,Description
,args
);
158 // Put it on the list
159 Item
*Itm
= new Item
;
167 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
168 // ---------------------------------------------------------------------
169 /* This should be used in a loop checking empty() each cycle. It returns
170 true if the message is an error. */
171 bool GlobalError::PopMessage(string
&Text
)
176 bool Ret
= List
->Error
;
182 // This really should check the list to see if only warnings are left..
189 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
190 // ---------------------------------------------------------------------
192 void GlobalError::DumpErrors()
194 // Print any errors or warnings found
196 while (empty() == false)
198 bool Type
= PopMessage(Err
);
200 cerr
<< "E: " << Err
<< endl
;
202 cerr
<< "W: " << Err
<< endl
;
206 // GlobalError::Discard - Discard /*{{{*/
207 // ---------------------------------------------------------------------
209 void GlobalError::Discard()
221 // GlobalError::Insert - Insert a new item at the end /*{{{*/
222 // ---------------------------------------------------------------------
224 void GlobalError::Insert(Item
*Itm
)
227 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)