]>
git.saurik.com Git - apt.git/blob - apt-pkg/contrib/error.cc
1 // -*- mode: cpp; mode: fold -*-
3 // $Id: error.cc,v 1.6 1999/01/18 06:20:07 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 vsnprintf(S
,sizeof(S
),Description
,args
);
84 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
),
85 " - %s (%i %s)",Function
,errno
,strerror(errno
));
98 // GlobalError::WarningE - Get part of the warn string from errno /*{{{*/
99 // ---------------------------------------------------------------------
100 /* Function indicates the stdlib function that failed and Description is
101 a user string that leads the text. Form is:
102 Description - Function (errno: strerror)
103 Carefull of the buffer overrun, sprintf.
105 bool GlobalError::WarningE(const char *Function
,const char *Description
,...)
108 va_start(args
,Description
);
110 // sprintf the description
112 vsnprintf(S
,sizeof(S
),Description
,args
);
113 snprintf(S
+ strlen(S
),sizeof(S
) - strlen(S
)," - %s (%i %s)",Function
,errno
,strerror(errno
));
115 // Put it on the list
116 Item
*Itm
= new Item
;
124 // GlobalError::Error - Add an error to the list /*{{{*/
125 // ---------------------------------------------------------------------
126 /* Just vsprintfs and pushes */
127 bool GlobalError::Error(const char *Description
,...)
130 va_start(args
,Description
);
132 // sprintf the description
134 vsnprintf(S
,sizeof(S
),Description
,args
);
136 // Put it on the list
137 Item
*Itm
= new Item
;
147 // GlobalError::Warning - Add a warning to the list /*{{{*/
148 // ---------------------------------------------------------------------
149 /* This doesn't set the pending error flag */
150 bool GlobalError::Warning(const char *Description
,...)
153 va_start(args
,Description
);
155 // sprintf the description
157 vsnprintf(S
,sizeof(S
),Description
,args
);
159 // Put it on the list
160 Item
*Itm
= new Item
;
168 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
169 // ---------------------------------------------------------------------
170 /* This should be used in a loop checking empty() each cycle. It returns
171 true if the message is an error. */
172 bool GlobalError::PopMessage(string
&Text
)
177 bool Ret
= List
->Error
;
183 // This really should check the list to see if only warnings are left..
190 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
191 // ---------------------------------------------------------------------
193 void GlobalError::DumpErrors()
195 // Print any errors or warnings found
197 while (empty() == false)
199 bool Type
= PopMessage(Err
);
201 cerr
<< "E: " << Err
<< endl
;
203 cerr
<< "W: " << Err
<< endl
;
207 // GlobalError::Discard - Discard /*{{{*/
208 // ---------------------------------------------------------------------
210 void GlobalError::Discard()
222 // GlobalError::Insert - Insert a new item at the end /*{{{*/
223 // ---------------------------------------------------------------------
225 void GlobalError::Insert(Item
*Itm
)
228 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)