]>
Commit | Line | Data |
---|---|---|
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 |
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); | |
578bfd0a AL |
65 | } |
66 | /*}}}*/ | |
67 | // GlobalError::Errno - Get part of the error string from errno /*{{{*/ | |
98ee7cd3 DK |
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); | |
c5162d56 AL |
72 | } |
73 | /*}}}*/ | |
98ee7cd3 DK |
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 - formats an error message with the errno /*{{{*/ | |
96 | bool GlobalError::InsertErrno(MsgType type, const char* Function, | |
97 | const char* Description, va_list const &args) { | |
98 | char S[400]; | |
99 | vsnprintf(S,sizeof(S),Description,args); | |
100 | snprintf(S + strlen(S),sizeof(S) - strlen(S), | |
101 | " - %s (%i: %s)", Function, errno, strerror(errno)); | |
102 | return Insert(type, S, args); | |
103 | } | |
104 | /*}}}*/ | |
105 | // GlobalError::Fatal - Add a fatal error to the list /*{{{*/ | |
106 | bool GlobalError::Fatal(const char *Description,...) { | |
107 | va_list args; | |
108 | va_start(args,Description); | |
109 | return Insert(FATAL, Description, args); | |
578bfd0a AL |
110 | } |
111 | /*}}}*/ | |
112 | // GlobalError::Error - Add an error to the list /*{{{*/ | |
98ee7cd3 DK |
113 | bool GlobalError::Error(const char *Description,...) { |
114 | va_list args; | |
115 | va_start(args,Description); | |
116 | return Insert(ERROR, Description, args); | |
578bfd0a AL |
117 | } |
118 | /*}}}*/ | |
119 | // GlobalError::Warning - Add a warning to the list /*{{{*/ | |
98ee7cd3 DK |
120 | bool GlobalError::Warning(const char *Description,...) { |
121 | va_list args; | |
122 | va_start(args,Description); | |
123 | return Insert(WARNING, Description, args); | |
124 | } | |
125 | /*}}}*/ | |
126 | // GlobalError::Notice - Add a notice to the list /*{{{*/ | |
127 | bool GlobalError::Notice(const char *Description,...) | |
578bfd0a | 128 | { |
98ee7cd3 DK |
129 | va_list args; |
130 | va_start(args,Description); | |
131 | return Insert(NOTICE, Description, args); | |
578bfd0a AL |
132 | } |
133 | /*}}}*/ | |
98ee7cd3 DK |
134 | // GlobalError::Debug - Add a debug to the list /*{{{*/ |
135 | bool GlobalError::Debug(const char *Description,...) | |
578bfd0a | 136 | { |
98ee7cd3 DK |
137 | va_list args; |
138 | va_start(args,Description); | |
139 | return Insert(DEBUG, Description, args); | |
140 | } | |
141 | /*}}}*/ | |
142 | // GlobalError::Insert - Insert a new item at the end /*{{{*/ | |
143 | bool GlobalError::Insert(MsgType type, const char* Description, | |
144 | va_list const &args) { | |
145 | char S[400]; | |
146 | vsnprintf(S,sizeof(S),Description,args); | |
147 | ||
148 | Item const m(S, type); | |
149 | Messages.push_back(m); | |
150 | ||
151 | if (type == ERROR || type == FATAL) | |
152 | PendingFlag = true; | |
153 | ||
154 | if (type == FATAL || type == DEBUG) | |
155 | std::clog << m << std::endl; | |
156 | ||
157 | return false; | |
158 | } | |
159 | /*}}}*/ | |
160 | // GlobalError::PopMessage - Pulls a single message out /*{{{*/ | |
161 | bool GlobalError::PopMessage(std::string &Text) { | |
162 | if (Messages.empty() == true) | |
163 | return false; | |
164 | ||
165 | Item const msg = Messages.front(); | |
166 | Messages.pop_front(); | |
167 | ||
168 | bool const Ret = (msg.Type == ERROR || msg.Type == FATAL); | |
169 | Text = msg.Text; | |
170 | if (PendingFlag == false || Ret == false) | |
171 | return Ret; | |
172 | ||
173 | // check if another error message is pending | |
174 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
175 | m != Messages.end(); m++) | |
176 | if (m->Type == ERROR || m->Type == FATAL) | |
177 | return Ret; | |
178 | ||
179 | PendingFlag = false; | |
180 | return Ret; | |
578bfd0a AL |
181 | } |
182 | /*}}}*/ | |
183 | // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/ | |
c4ba7c44 DK |
184 | void GlobalError::DumpErrors(std::ostream &out, MsgType const &trashhold, |
185 | bool const &mergeStack) { | |
186 | if (mergeStack == true) | |
187 | for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin(); | |
188 | s != Stacks.rend(); ++s) | |
189 | Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end()); | |
190 | ||
98ee7cd3 DK |
191 | for (std::list<Item>::const_iterator m = Messages.begin(); |
192 | m != Messages.end(); m++) | |
193 | if (m->Type >= trashhold) | |
194 | out << (*m) << std::endl; | |
195 | Discard(); | |
578bfd0a AL |
196 | } |
197 | /*}}}*/ | |
98ee7cd3 DK |
198 | // GlobalError::Discard - Discard /*{{{*/ |
199 | void GlobalError::Discard() { | |
200 | Messages.clear(); | |
201 | PendingFlag = false; | |
6c139d6e AL |
202 | }; |
203 | /*}}}*/ | |
98ee7cd3 DK |
204 | // GlobalError::empty - does our error list include anything? /*{{{*/ |
205 | bool GlobalError::empty(MsgType const &trashhold) const { | |
206 | if (PendingFlag == true) | |
207 | return false; | |
208 | ||
209 | if (Messages.empty() == true) | |
210 | return true; | |
211 | ||
212 | for (std::list<Item>::const_iterator m = Messages.begin(); | |
213 | m != Messages.end(); m++) | |
214 | if (m->Type >= trashhold) | |
215 | return false; | |
216 | ||
217 | return true; | |
6c139d6e AL |
218 | } |
219 | /*}}}*/ | |
c4ba7c44 DK |
220 | // GlobalError::PushToStack /*{{{*/ |
221 | void GlobalError::PushToStack() { | |
222 | MsgStack pack(Messages, PendingFlag); | |
223 | Stacks.push_back(pack); | |
224 | Discard(); | |
225 | } | |
226 | /*}}}*/ | |
227 | // GlobalError::RevertToStack /*{{{*/ | |
228 | void GlobalError::RevertToStack() { | |
229 | Discard(); | |
230 | MsgStack pack = Stacks.back(); | |
231 | Messages = pack.Messages; | |
232 | PendingFlag = pack.PendingFlag; | |
233 | Stacks.pop_back(); | |
234 | } | |
235 | /*}}}*/ | |
236 | // GlobalError::MergeWithStack /*{{{*/ | |
237 | void GlobalError::MergeWithStack() { | |
238 | MsgStack pack = Stacks.back(); | |
239 | Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end()); | |
240 | PendingFlag = PendingFlag || pack.PendingFlag; | |
241 | Stacks.pop_back(); | |
242 | } | |
243 | /*}}}*/ |