]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: error.cc,v 1.8 1999/08/08 07:24:54 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>
32 // Global Error Object /*{{{*/
33 /* If the implementation supports posix threads then the accessor function
34 is compiled to be thread safe otherwise a non-safe version is used. A
35 Per-Thread error object is maintained in much the same manner as libc
37 #if _POSIX_THREADS == 1 && defined(HAVE_PTHREAD)
40 static pthread_key_t ErrorKey
;
41 static void ErrorDestroy(void *Obj
) {delete (GlobalError
*)Obj
;};
42 static void KeyAlloc() {pthread_key_create(&ErrorKey
,ErrorDestroy
);};
44 GlobalError
*_GetErrorObj()
46 static pthread_once_t Once
= PTHREAD_ONCE_INIT
;
47 pthread_once(&Once
,KeyAlloc
);
49 void *Res
= pthread_getspecific(ErrorKey
);
51 pthread_setspecific(ErrorKey
,Res
= new GlobalError
);
52 return (GlobalError
*)Res
;
55 GlobalError
*_GetErrorObj()
57 static GlobalError
*Obj
= new GlobalError
;
63 // GlobalError::GlobalError - Constructor /*{{{*/
64 // ---------------------------------------------------------------------
66 GlobalError::GlobalError() : List(0), PendingFlag(false)
70 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
71 // ---------------------------------------------------------------------
72 /* Function indicates the stdlib function that failed and Description is
73 a user string that leads the text. Form is:
74 Description - Function (errno: strerror)
75 Carefull of the buffer overrun, sprintf.
77 bool GlobalError::Errno(const char *Function
,const char *Description
,...)
80 va_start(args
,Description
);
82 // sprintf the description
84 vsnprintf(S
,sizeof(S
),Description
,args
);
85 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
),
86 " - %s (%i %s)",Function
,errno
,strerror(errno
));
99 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
100 // ---------------------------------------------------------------------
101 /* Function indicates the stdlib function that failed and Description is
102 a user string that leads the text. Form is:
103 Description - Function (errno: strerror)
104 Carefull of the buffer overrun, sprintf.
106 bool GlobalError::WarningE(const char *Function
,const char *Description
,...)
109 va_start(args
,Description
);
111 // sprintf the description
113 vsnprintf(S
,sizeof(S
),Description
,args
);
114 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
116 // Put it on the list
117 Item
*Itm
= new Item
;
125 // GlobalError::Error - Add an error to the list /*{{{*/
126 // ---------------------------------------------------------------------
127 /* Just vsprintfs and pushes */
128 bool GlobalError::Error(const char *Description
,...)
131 va_start(args
,Description
);
133 // sprintf the description
135 vsnprintf(S
,sizeof(S
),Description
,args
);
137 // Put it on the list
138 Item
*Itm
= new Item
;
148 // GlobalError::Warning - Add a warning to the list /*{{{*/
149 // ---------------------------------------------------------------------
150 /* This doesn't set the pending error flag */
151 bool GlobalError::Warning(const char *Description
,...)
154 va_start(args
,Description
);
156 // sprintf the description
158 vsnprintf(S
,sizeof(S
),Description
,args
);
160 // Put it on the list
161 Item
*Itm
= new Item
;
169 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
170 // ---------------------------------------------------------------------
171 /* This should be used in a loop checking empty() each cycle. It returns
172 true if the message is an error. */
173 bool GlobalError::PopMessage(string
&Text
)
178 bool Ret
= List
->Error
;
184 // This really should check the list to see if only warnings are left..
191 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
192 // ---------------------------------------------------------------------
194 void GlobalError::DumpErrors()
196 // Print any errors or warnings found
198 while (empty() == false)
200 bool Type
= PopMessage(Err
);
202 cerr
<< "E: " << Err
<< endl
;
204 cerr
<< "W: " << Err
<< endl
;
208 // GlobalError::Discard - Discard /*{{{*/
209 // ---------------------------------------------------------------------
211 void GlobalError::Discard()
223 // GlobalError::Insert - Insert a new item at the end /*{{{*/
224 // ---------------------------------------------------------------------
226 void GlobalError::Insert(Item
*Itm
)
229 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)