]>
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
),
116 " - %s (%i: %s)",Function
,errno
,strerror(errno
));
118 // Put it on the list
119 Item
*Itm
= new Item
;
127 // GlobalError::Error - Add an error to the list /*{{{*/
128 // ---------------------------------------------------------------------
129 /* Just vsprintfs and pushes */
130 bool GlobalError::Error(const char *Description
,...)
133 va_start(args
,Description
);
135 // sprintf the description
137 vsnprintf(S
,sizeof(S
),Description
,args
);
139 // Put it on the list
140 Item
*Itm
= new Item
;
150 // GlobalError::Warning - Add a warning to the list /*{{{*/
151 // ---------------------------------------------------------------------
152 /* This doesn't set the pending error flag */
153 bool GlobalError::Warning(const char *Description
,...)
156 va_start(args
,Description
);
158 // sprintf the description
160 vsnprintf(S
,sizeof(S
),Description
,args
);
162 // Put it on the list
163 Item
*Itm
= new Item
;
171 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
172 // ---------------------------------------------------------------------
173 /* This should be used in a loop checking empty() each cycle. It returns
174 true if the message is an error. */
175 bool GlobalError::PopMessage(string
&Text
)
180 bool Ret
= List
->Error
;
186 // This really should check the list to see if only warnings are left..
193 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
194 // ---------------------------------------------------------------------
196 void GlobalError::DumpErrors()
198 // Print any errors or warnings found
200 while (empty() == false)
202 bool Type
= PopMessage(Err
);
204 cerr
<< "E: " << Err
<< endl
;
206 cerr
<< "W: " << Err
<< endl
;
210 // GlobalError::Discard - Discard /*{{{*/
211 // ---------------------------------------------------------------------
213 void GlobalError::Discard()
225 // GlobalError::Insert - Insert a new item at the end /*{{{*/
226 // ---------------------------------------------------------------------
228 void GlobalError::Insert(Item
*Itm
)
231 for (Item
*I
= List
; I
!= 0; I
= I
->Next
)