fix a bunch of cppcheck warnings/errors based on a patch by
[ntk/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>
8f3853ba 21#include <stdlib.h>
6f27a7fc 22#include <unistd.h>
578bfd0a 23
4f333a8b
MV
24#include <string>
25#include <cstring>
26
afd9aaf3 27#include "config.h"
578bfd0a
AL
28 /*}}}*/
29
6f27a7fc
AL
30// Global Error Object /*{{{*/
31/* If the implementation supports posix threads then the accessor function
32 is compiled to be thread safe otherwise a non-safe version is used. A
33 Per-Thread error object is maintained in much the same manner as libc
34 manages errno */
b2e465d6 35#if defined(_POSIX_THREADS) && defined(HAVE_PTHREAD)
98ee7cd3
DK
36 #include <pthread.h>
37
38 static pthread_key_t ErrorKey;
39 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
40 static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
41
42 GlobalError *_GetErrorObj() {
43 static pthread_once_t Once = PTHREAD_ONCE_INIT;
44 pthread_once(&Once,KeyAlloc);
45
46 void *Res = pthread_getspecific(ErrorKey);
47 if (Res == 0)
48 pthread_setspecific(ErrorKey,Res = new GlobalError);
49 return (GlobalError *)Res;
50 }
6f27a7fc 51#else
98ee7cd3
DK
52 GlobalError *_GetErrorObj() {
53 static GlobalError *Obj = new GlobalError;
54 return Obj;
55 }
6f27a7fc
AL
56#endif
57 /*}}}*/
578bfd0a 58// GlobalError::GlobalError - Constructor /*{{{*/
98ee7cd3
DK
59GlobalError::GlobalError() : PendingFlag(false) {}
60 /*}}}*/
38f29703
DK
61// GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
62#define GEMessage(NAME, TYPE) \
63bool GlobalError::NAME (const char *Function, const char *Description,...) { \
64 va_list args; \
65 size_t msgSize = 400; \
66 int const errsv = errno; \
67 while (true) { \
68 va_start(args,Description); \
69 if (InsertErrno(TYPE, Function, Description, args, errsv, msgSize) == false) \
70 break; \
71 va_end(args); \
72 } \
73 return false; \
98ee7cd3 74}
38f29703
DK
75GEMessage(FatalE, FATAL)
76GEMessage(Errno, ERROR)
77GEMessage(WarningE, WARNING)
78GEMessage(NoticeE, NOTICE)
79GEMessage(DebugE, DEBUG)
80#undef GEMessage
98ee7cd3 81 /*}}}*/
cd7bbc47
DK
82// GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
83bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
84 const char *Description,...) {
85 va_list args;
38f29703
DK
86 size_t msgSize = 400;
87 int const errsv = errno;
88 while (true) {
89 va_start(args,Description);
90 if (InsertErrno(type, Function, Description, args, errsv, msgSize) == false)
91 break;
92 va_end(args);
93 }
94 return false;
cd7bbc47
DK
95}
96 /*}}}*/
98ee7cd3
DK
97// GlobalError::InsertErrno - formats an error message with the errno /*{{{*/
98bool GlobalError::InsertErrno(MsgType type, const char* Function,
38f29703
DK
99 const char* Description, va_list &args,
100 int const errsv, size_t &msgSize) {
101 char* S = (char*) malloc(msgSize);
102 int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description,
103 Function, errsv, strerror(errsv));
104 if (n > -1 && ((unsigned int) n) < msgSize);
105 else {
106 if (n > -1)
107 msgSize = n + 1;
108 else
109 msgSize *= 2;
6070a346 110 free(S);
38f29703 111 return true;
8f3853ba
DK
112 }
113
38f29703 114 bool const geins = Insert(type, S, args, msgSize);
8f3853ba
DK
115 free(S);
116 return geins;
98ee7cd3
DK
117}
118 /*}}}*/
38f29703
DK
119// GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
120#define GEMessage(NAME, TYPE) \
121bool GlobalError::NAME (const char *Description,...) { \
122 va_list args; \
123 size_t msgSize = 400; \
124 while (true) { \
125 va_start(args,Description); \
126 if (Insert(TYPE, Description, args, msgSize) == false) \
127 break; \
128 va_end(args); \
129 } \
130 return false; \
98ee7cd3 131}
38f29703
DK
132GEMessage(Fatal, FATAL)
133GEMessage(Error, ERROR)
134GEMessage(Warning, WARNING)
135GEMessage(Notice, NOTICE)
136GEMessage(Debug, DEBUG)
137#undef GEMessage
98ee7cd3 138 /*}}}*/
cd7bbc47
DK
139// GlobalError::Insert - Add a errotype message to the list /*{{{*/
140bool GlobalError::Insert(MsgType const &type, const char *Description,...)
141{
142 va_list args;
38f29703
DK
143 size_t msgSize = 400;
144 while (true) {
145 va_start(args,Description);
146 if (Insert(type, Description, args, msgSize) == false)
147 break;
148 va_end(args);
149 }
150 return false;
cd7bbc47
DK
151}
152 /*}}}*/
98ee7cd3
DK
153// GlobalError::Insert - Insert a new item at the end /*{{{*/
154bool GlobalError::Insert(MsgType type, const char* Description,
38f29703
DK
155 va_list &args, size_t &msgSize) {
156 char* S = (char*) malloc(msgSize);
157 int const n = vsnprintf(S, msgSize, Description, args);
158 if (n > -1 && ((unsigned int) n) < msgSize);
159 else {
160 if (n > -1)
161 msgSize = n + 1;
162 else
163 msgSize *= 2;
6070a346 164 free(S);
38f29703 165 return true;
8f3853ba 166 }
98ee7cd3
DK
167
168 Item const m(S, type);
169 Messages.push_back(m);
170
171 if (type == ERROR || type == FATAL)
172 PendingFlag = true;
173
174 if (type == FATAL || type == DEBUG)
175 std::clog << m << std::endl;
176
8f3853ba 177 free(S);
98ee7cd3
DK
178 return false;
179}
180 /*}}}*/
181// GlobalError::PopMessage - Pulls a single message out /*{{{*/
182bool GlobalError::PopMessage(std::string &Text) {
183 if (Messages.empty() == true)
184 return false;
185
186 Item const msg = Messages.front();
187 Messages.pop_front();
188
189 bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
190 Text = msg.Text;
191 if (PendingFlag == false || Ret == false)
192 return Ret;
193
194 // check if another error message is pending
195 for (std::list<Item>::const_iterator m = Messages.begin();
196 m != Messages.end(); m++)
197 if (m->Type == ERROR || m->Type == FATAL)
198 return Ret;
199
200 PendingFlag = false;
201 return Ret;
578bfd0a
AL
202}
203 /*}}}*/
204// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
12be8a62 205void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
c4ba7c44
DK
206 bool const &mergeStack) {
207 if (mergeStack == true)
208 for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
209 s != Stacks.rend(); ++s)
210 Messages.insert(Messages.begin(), s->Messages.begin(), s->Messages.end());
211
98ee7cd3
DK
212 for (std::list<Item>::const_iterator m = Messages.begin();
213 m != Messages.end(); m++)
12be8a62 214 if (m->Type >= threshold)
98ee7cd3
DK
215 out << (*m) << std::endl;
216 Discard();
578bfd0a
AL
217}
218 /*}}}*/
98ee7cd3
DK
219// GlobalError::Discard - Discard /*{{{*/
220void GlobalError::Discard() {
221 Messages.clear();
222 PendingFlag = false;
6c139d6e
AL
223};
224 /*}}}*/
98ee7cd3
DK
225// GlobalError::empty - does our error list include anything? /*{{{*/
226bool GlobalError::empty(MsgType const &trashhold) const {
227 if (PendingFlag == true)
228 return false;
229
230 if (Messages.empty() == true)
231 return true;
232
233 for (std::list<Item>::const_iterator m = Messages.begin();
234 m != Messages.end(); m++)
235 if (m->Type >= trashhold)
236 return false;
237
238 return true;
6c139d6e
AL
239}
240 /*}}}*/
c4ba7c44
DK
241// GlobalError::PushToStack /*{{{*/
242void GlobalError::PushToStack() {
243 MsgStack pack(Messages, PendingFlag);
244 Stacks.push_back(pack);
245 Discard();
246}
247 /*}}}*/
248// GlobalError::RevertToStack /*{{{*/
249void GlobalError::RevertToStack() {
250 Discard();
251 MsgStack pack = Stacks.back();
252 Messages = pack.Messages;
253 PendingFlag = pack.PendingFlag;
254 Stacks.pop_back();
255}
256 /*}}}*/
257// GlobalError::MergeWithStack /*{{{*/
258void GlobalError::MergeWithStack() {
259 MsgStack pack = Stacks.back();
260 Messages.insert(Messages.begin(), pack.Messages.begin(), pack.Messages.end());
261 PendingFlag = PendingFlag || pack.PendingFlag;
262 Stacks.pop_back();
263}
264 /*}}}*/