- [ABI BREAK] add an ErrorType option to CacheSetHelper
[ntk/apt.git] / apt-pkg / contrib / error.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 /* ######################################################################
4
5 Global Error Class - Global error mechanism
6
7 We use a simple STL vector to store each error record. A PendingFlag
8 is kept which indicates when the vector contains a Sever error.
9
10 This source is placed in the Public Domain, do with it what you will
11 It was originally written by Jason Gunthorpe.
12
13 ##################################################################### */
14 /*}}}*/
15 // Include Files /*{{{*/
16 #include <apt-pkg/error.h>
17
18 #include <iostream>
19 #include <errno.h>
20 #include <stdio.h>
21 #include <unistd.h>
22
23 #include <string>
24 #include <cstring>
25
26 #include "config.h"
27 /*}}}*/
28
29 // Global Error Object /*{{{*/
30 /* If the implementation supports posix threads then the accessor function
31 is compiled to be thread safe otherwise a non-safe version is used. A
32 Per-Thread error object is maintained in much the same manner as libc
33 manages errno */
34 #if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
35 #include <pthread.h>
36
37 static pthread_key_t ErrorKey;
38 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
39 static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
40
41 GlobalError *_GetErrorObj() {
42 static pthread_once_t Once = PTHREAD_ONCE_INIT;
43 pthread_once(&Once,KeyAlloc);
44
45 void *Res = pthread_getspecific(ErrorKey);
46 if (Res == 0)
47 pthread_setspecific(ErrorKey,Res = new GlobalError);
48 return (GlobalError *)Res;
49 }
50 #else
51 GlobalError *_GetErrorObj() {
52 static GlobalError *Obj = new GlobalError;
53 return Obj;
54 }
55 #endif
56 /*}}}*/
57 // GlobalError::GlobalError - Constructor /*{{{*/
58 GlobalError::GlobalError() : PendingFlag(false) {}
59 /*}}}*/
60 // GlobalError::FatalE - Get part of the error string from errno /*{{{*/
61 bool GlobalError::FatalE(const char *Function,const char *Description,...) {
62 va_list args;
63 va_start(args,Description);
64 return InsertErrno(FATAL, Function, Description, args);
65 }
66 /*}}}*/
67 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
68 bool GlobalError::Errno(const char *Function,const char *Description,...) {
69 va_list args;
70 va_start(args,Description);
71 return InsertErrno(ERROR, Function, Description, args);
72 }
73 /*}}}*/
74 // GlobalError::WarningE - Get part of the warning string from errno /*{{{*/
75 bool GlobalError::WarningE(const char *Function,const char *Description,...) {
76 va_list args;
77 va_start(args,Description);
78 return InsertErrno(WARNING, Function, Description, args);
79 }
80 /*}}}*/
81 // GlobalError::NoticeE - Get part of the notice string from errno /*{{{*/
82 bool GlobalError::NoticeE(const char *Function,const char *Description,...) {
83 va_list args;
84 va_start(args,Description);
85 return InsertErrno(NOTICE, Function, Description, args);
86 }
87 /*}}}*/
88 // GlobalError::DebugE - Get part of the debug string from errno /*{{{*/
89 bool GlobalError::DebugE(const char *Function,const char *Description,...) {
90 va_list args;
91 va_start(args,Description);
92 return InsertErrno(DEBUG, Function, Description, args);
93 }
94 /*}}}*/
95 // GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
96 bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
97 const char *Description,...) {
98 va_list args;
99 va_start(args,Description);
100 return InsertErrno(type, Function, Description, args);
101 }
102 /*}}}*/
103 // GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
104 bool GlobalError::InsertErrno(MsgType type, const char* Function,
105 const char* Description, va_list &args) {
106 char S[400];
107 snprintf(S, sizeof(S), "%s - %s (%i: %s)", Description,
108 Function, errno, strerror(errno));
109 return Insert(type, S, args);
110 }
111 /*}}}*/
112 // GlobalError::Fatal - Add a fatal error to the list /*{{{*/
113 bool GlobalError::Fatal(const char *Description,...) {
114 va_list args;
115 va_start(args,Description);
116 return Insert(FATAL, Description, args);
117 }
118 /*}}}*/
119 // GlobalError::Error - Add an error to the list /*{{{*/
120 bool GlobalError::Error(const char *Description,...) {
121 va_list args;
122 va_start(args,Description);
123 return Insert(ERROR, Description, args);
124 }
125 /*}}}*/
126 // GlobalError::Warning - Add a warning to the list /*{{{*/
127 bool GlobalError::Warning(const char *Description,...) {
128 va_list args;
129 va_start(args,Description);
130 return Insert(WARNING, Description, args);
131 }
132 /*}}}*/
133 // GlobalError::Notice - Add a notice to the list /*{{{*/
134 bool GlobalError::Notice(const char *Description,...)
135 {
136 va_list args;
137 va_start(args,Description);
138 return Insert(NOTICE, Description, args);
139 }
140 /*}}}*/
141 // GlobalError::Debug - Add a debug to the list /*{{{*/
142 bool GlobalError::Debug(const char *Description,...)
143 {
144 va_list args;
145 va_start(args,Description);
146 return Insert(DEBUG, Description, args);
147 }
148 /*}}}*/
149 // GlobalError::Insert - Add a errotype message to the list /*{{{*/
150 bool GlobalError::Insert(MsgType const &type, const char *Description,...)
151 {
152 va_list args;
153 va_start(args,Description);
154 return Insert(type, Description, args);
155 }
156 /*}}}*/
157 // GlobalError::Insert - Insert a new item at the end /*{{{*/
158 bool GlobalError::Insert(MsgType type, const char* Description,
159 va_list &args) {
160 char S[400];
161 vsnprintf(S,sizeof(S),Description,args);
162
163 Item const m(S, type);
164 Messages.push_back(m);
165
166 if (type == ERROR || type == FATAL)
167 PendingFlag = true;
168
169 if (type == FATAL || type == DEBUG)
170 std::clog << m << std::endl;
171
172 return false;
173 }
174 /*}}}*/
175 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
176 bool GlobalError::PopMessage(std::string &Text) {
177 if (Messages.empty() == true)
178 return false;
179
180 Item const msg = Messages.front();
181 Messages.pop_front();
182
183 bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
184 Text = msg.Text;
185 if (PendingFlag == false || Ret == false)
186 return Ret;
187
188 // check if another error message is pending
189 for (std::list<Item>::const_iterator m = Messages.begin();
190 m != Messages.end(); m++)
191 if (m->Type == ERROR || m->Type == FATAL)
192 return Ret;
193
194 PendingFlag = false;
195 return Ret;
196 }
197 /*}}}*/
198 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
199 void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
200 bool const &mergeStack) {
201 if (mergeStack == true)
202 for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
203 s != Stacks.rend(); ++s)
204 Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
205
206 for (std::list<Item>::const_iterator m = Messages.begin();
207 m != Messages.end(); m++)
208 if (m->Type >= threshold)
209 out << (*m) << std::endl;
210 Discard();
211 }
212 /*}}}*/
213 // GlobalError::Discard - Discard /*{{{*/
214 void GlobalError::Discard() {
215 Messages.clear();
216 PendingFlag = false;
217 };
218 /*}}}*/
219 // GlobalError::empty - does our error list include anything? /*{{{*/
220 bool GlobalError::empty(MsgType const &trashhold) const {
221 if (PendingFlag == true)
222 return false;
223
224 if (Messages.empty() == true)
225 return true;
226
227 for (std::list<Item>::const_iterator m = Messages.begin();
228 m != Messages.end(); m++)
229 if (m->Type >= trashhold)
230 return false;
231
232 return true;
233 }
234 /*}}}*/
235 // GlobalError::PushToStack /*{{{*/
236 void GlobalError::PushToStack() {
237 MsgStack pack(Messages, PendingFlag);
238 Stacks.push_back(pack);
239 Discard();
240 }
241 /*}}}*/
242 // GlobalError::RevertToStack /*{{{*/
243 void GlobalError::RevertToStack() {
244 Discard();
245 MsgStack pack = Stacks.back();
246 Messages = pack.Messages;
247 PendingFlag = pack.PendingFlag;
248 Stacks.pop_back();
249 }
250 /*}}}*/
251 // GlobalError::MergeWithStack /*{{{*/
252 void GlobalError::MergeWithStack() {
253 MsgStack pack = Stacks.back();
254 Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
255 PendingFlag = PendingFlag || pack.PendingFlag;
256 Stacks.pop_back();
257 }
258 /*}}}*/