]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
rename ReOwn to ReMap in the cacheiterators
[apt.git] / apt-pkg / contrib / error.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
578bfd0a 3/* ######################################################################
98ee7cd3
DK
4
5 Global Error Class - Global error mechanism
578bfd0a
AL
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.
98ee7cd3 9
578bfd0a
AL
10 This source is placed in the Public Domain, do with it what you will
11 It was originally written by Jason Gunthorpe.
98ee7cd3 12
578bfd0a
AL
13 ##################################################################### */
14 /*}}}*/
15// Include Files /*{{{*/
6f27a7fc
AL
16#include <apt-pkg/error.h>
17
90f057fd 18#include <iostream>
578bfd0a
AL
19#include <errno.h>
20#include <stdio.h>
6f27a7fc 21#include <unistd.h>
578bfd0a 22
4f333a8b
MV
23#include <string>
24#include <cstring>
25
afd9aaf3 26#include "config.h"
578bfd0a
AL
27 /*}}}*/
28
6f27a7fc
AL
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 */
b2e465d6 34#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
98ee7cd3
DK
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 }
6f27a7fc 50#else
98ee7cd3
DK
51 GlobalError *_GetErrorObj() {
52 static GlobalError *Obj = new GlobalError;
53 return Obj;
54 }
6f27a7fc
AL
55#endif
56 /*}}}*/
578bfd0a 57// GlobalError::GlobalError - Constructor /*{{{*/
98ee7cd3
DK
58GlobalError::GlobalError() : PendingFlag(false) {}
59 /*}}}*/
60// GlobalError::FatalE - Get part of the error string from errno /*{{{*/
61bool GlobalError::FatalE(const char *Function,const char *Description,...) {
62 va_list args;
63 va_start(args,Description);
64 return InsertErrno(FATAL, Function, Description, args);
578bfd0a
AL
65}
66 /*}}}*/
67// GlobalError::Errno - Get part of the error string from errno /*{{{*/
98ee7cd3
DK
68bool GlobalError::Errno(const char *Function,const char *Description,...) {
69 va_list args;
70 va_start(args,Description);
71 return InsertErrno(ERROR, Function, Description, args);
c5162d56
AL
72}
73 /*}}}*/
98ee7cd3
DK
74// GlobalError::WarningE - Get part of the warning string from errno /*{{{*/
75bool 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 /*{{{*/
82bool 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 /*{{{*/
89bool 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 - formats an error message with the errno /*{{{*/
96bool GlobalError::InsertErrno(MsgType type, const char* Function,
3c0929ec 97 const char* Description, va_list &args) {
98ee7cd3 98 char S[400];
3c0929ec
DK
99 snprintf(S, sizeof(S), "%s - %s (%i: %s)", Description,
100 Function, errno, strerror(errno));
98ee7cd3
DK
101 return Insert(type, S, args);
102}
103 /*}}}*/
104// GlobalError::Fatal - Add a fatal error to the list /*{{{*/
105bool GlobalError::Fatal(const char *Description,...) {
106 va_list args;
107 va_start(args,Description);
108 return Insert(FATAL, Description, args);
578bfd0a
AL
109}
110 /*}}}*/
111// GlobalError::Error - Add an error to the list /*{{{*/
98ee7cd3
DK
112bool GlobalError::Error(const char *Description,...) {
113 va_list args;
114 va_start(args,Description);
115 return Insert(ERROR, Description, args);
578bfd0a
AL
116}
117 /*}}}*/
118// GlobalError::Warning - Add a warning to the list /*{{{*/
98ee7cd3
DK
119bool GlobalError::Warning(const char *Description,...) {
120 va_list args;
121 va_start(args,Description);
122 return Insert(WARNING, Description, args);
123}
124 /*}}}*/
125// GlobalError::Notice - Add a notice to the list /*{{{*/
126bool GlobalError::Notice(const char *Description,...)
578bfd0a 127{
98ee7cd3
DK
128 va_list args;
129 va_start(args,Description);
130 return Insert(NOTICE, Description, args);
578bfd0a
AL
131}
132 /*}}}*/
98ee7cd3
DK
133// GlobalError::Debug - Add a debug to the list /*{{{*/
134bool GlobalError::Debug(const char *Description,...)
578bfd0a 135{
98ee7cd3
DK
136 va_list args;
137 va_start(args,Description);
138 return Insert(DEBUG, Description, args);
139}
140 /*}}}*/
141// GlobalError::Insert - Insert a new item at the end /*{{{*/
142bool GlobalError::Insert(MsgType type, const char* Description,
3c0929ec 143 va_list &args) {
98ee7cd3
DK
144 char S[400];
145 vsnprintf(S,sizeof(S),Description,args);
146
147 Item const m(S, type);
148 Messages.push_back(m);
149
150 if (type == ERROR || type == FATAL)
151 PendingFlag = true;
152
153 if (type == FATAL || type == DEBUG)
154 std::clog << m << std::endl;
155
156 return false;
157}
158 /*}}}*/
159// GlobalError::PopMessage - Pulls a single message out /*{{{*/
160bool GlobalError::PopMessage(std::string &Text) {
161 if (Messages.empty() == true)
162 return false;
163
164 Item const msg = Messages.front();
165 Messages.pop_front();
166
167 bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
168 Text = msg.Text;
169 if (PendingFlag == false || Ret == false)
170 return Ret;
171
172 // check if another error message is pending
173 for (std::list<Item>::const_iterator m = Messages.begin();
174 m != Messages.end(); m++)
175 if (m->Type == ERROR || m->Type == FATAL)
176 return Ret;
177
178 PendingFlag = false;
179 return Ret;
578bfd0a
AL
180}
181 /*}}}*/
182// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
c4ba7c44
DK
183void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold,
184 bool const &mergeStack) {
185 if (mergeStack == true)
186 for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
187 s != Stacks.rend(); ++s)
188 Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
189
98ee7cd3
DK
190 for (std::list<Item>::const_iterator m = Messages.begin();
191 m != Messages.end(); m++)
192 if (m->Type >= trashhold)
193 out << (*m) << std::endl;
194 Discard();
578bfd0a
AL
195}
196 /*}}}*/
98ee7cd3
DK
197// GlobalError::Discard - Discard /*{{{*/
198void GlobalError::Discard() {
199 Messages.clear();
200 PendingFlag = false;
6c139d6e
AL
201};
202 /*}}}*/
98ee7cd3
DK
203// GlobalError::empty - does our error list include anything? /*{{{*/
204bool GlobalError::empty(MsgType const &trashhold) const {
205 if (PendingFlag == true)
206 return false;
207
208 if (Messages.empty() == true)
209 return true;
210
211 for (std::list<Item>::const_iterator m = Messages.begin();
212 m != Messages.end(); m++)
213 if (m->Type >= trashhold)
214 return false;
215
216 return true;
6c139d6e
AL
217}
218 /*}}}*/
c4ba7c44
DK
219// GlobalError::PushToStack /*{{{*/
220void GlobalError::PushToStack() {
221 MsgStack pack(Messages, PendingFlag);
222 Stacks.push_back(pack);
223 Discard();
224}
225 /*}}}*/
226// GlobalError::RevertToStack /*{{{*/
227void GlobalError::RevertToStack() {
228 Discard();
229 MsgStack pack = Stacks.back();
230 Messages = pack.Messages;
231 PendingFlag = pack.PendingFlag;
232 Stacks.pop_back();
233}
234 /*}}}*/
235// GlobalError::MergeWithStack /*{{{*/
236void GlobalError::MergeWithStack() {
237 MsgStack pack = Stacks.back();
238 Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
239 PendingFlag = PendingFlag || pack.PendingFlag;
240 Stacks.pop_back();
241}
242 /*}}}*/