summaryrefslogtreecommitdiffstats
path: root/apt-pkg/contrib/error.cc
blob: 054435b738e73ea2a3e1e8da94757e5b6b948d4e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// -*- mode: cpp; mode: fold -*-
// SPDX-License-Identifier: GPL-2.0+
// Description								/*{{{*/
/* ######################################################################

   Global Error Class - Global error mechanism

   We use a simple STL vector to store each error record. A PendingFlag
   is kept which indicates when the vector contains a Sever error.

   This file had this historic note, but now includes further changes
   under the GPL-2.0+:

   This source is placed in the Public Domain, do with it what you will
   It was originally written by Jason Gunthorpe.

   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#include <config.h>

#include <apt-pkg/configuration.h>
#include <apt-pkg/error.h>

#include <algorithm>
#include <cerrno>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <string>
#include <unistd.h>

#include <apti18n.h>
									/*}}}*/

// Global Error Object							/*{{{*/
GlobalError *_GetErrorObj()
{
   static thread_local GlobalError Obj;
   return &Obj;
}
									/*}}}*/
// GlobalError::GlobalError - Constructor				/*{{{*/
GlobalError::GlobalError() : PendingFlag(false) {}
									/*}}}*/
// GlobalError::FatalE, Errno, WarningE, NoticeE and DebugE - Add to the list/*{{{*/
#define GEMessage(NAME, TYPE) \
bool GlobalError::NAME (const char *Function, const char *Description,...) { \
	va_list args; \
	size_t msgSize = 400; \
	int const errsv = errno; \
	bool retry; \
	do { \
		va_start(args,Description); \
		retry = InsertErrno(TYPE, Function, Description, args, errsv, msgSize); \
		va_end(args); \
	} while (retry); \
	return false; \
}
GEMessage(FatalE, FATAL)
GEMessage(Errno, ERROR)
GEMessage(WarningE, WARNING)
GEMessage(NoticeE, NOTICE)
GEMessage(AuditE, AUDIT)
GEMessage(DebugE, DEBUG)
#undef GEMessage
									/*}}}*/
// GlobalError::InsertErrno - Get part of the errortype string from errno/*{{{*/
bool GlobalError::InsertErrno(MsgType const &type, const char *Function,
				const char *Description,...) {
	va_list args;
	size_t msgSize = 400;
	int const errsv = errno;
	bool retry;
	do {
		va_start(args,Description);
		retry = InsertErrno(type, Function, Description, args, errsv, msgSize);
		va_end(args);
	} while (retry);
	return false;
}
									/*}}}*/
// GlobalError::InsertErrno - formats an error message with the errno	/*{{{*/
bool GlobalError::InsertErrno(MsgType type, const char* Function,
			      const char* Description, va_list &args,
			      int const errsv, size_t &msgSize) {
	char* S = (char*) malloc(msgSize);
	int const n = snprintf(S, msgSize, "%s - %s (%i: %s)", Description,
			       Function, errsv, strerror(errsv));
	if (n > -1 && ((unsigned int) n) < msgSize);
	else {
		if (n > -1)
			msgSize = n + 1;
		else
			msgSize *= 2;
		free(S);
		return true;
	}

	bool const geins = Insert(type, S, args, msgSize);
	free(S);
	return geins;
}
									/*}}}*/
// GlobalError::Fatal, Error, Warning, Notice and Debug - Add to the list/*{{{*/
#define GEMessage(NAME, TYPE) \
bool GlobalError::NAME (const char *Description,...) { \
	va_list args; \
	size_t msgSize = 400; \
	bool retry; \
	do { \
		va_start(args,Description); \
		retry = Insert(TYPE, Description, args, msgSize); \
		va_end(args); \
	} while (retry); \
	return false; \
}
GEMessage(Fatal, FATAL)
GEMessage(Error, ERROR)
GEMessage(Warning, WARNING)
GEMessage(Notice, NOTICE)
GEMessage(Audit, AUDIT)
GEMessage(Debug, DEBUG)
#undef GEMessage
									/*}}}*/
// GlobalError::Insert - Add a errotype message to the list		/*{{{*/
bool GlobalError::Insert(MsgType const &type, const char *Description,...)
{
	va_list args;
	size_t msgSize = 400;
	bool retry;
	do {
		va_start(args,Description);
		retry = Insert(type, Description, args, msgSize);
		va_end(args);
	} while (retry);
	return false;
}
									/*}}}*/
// GlobalError::Insert - Insert a new item at the end			/*{{{*/
bool GlobalError::Insert(MsgType type, const char* Description,
			 va_list &args, size_t &msgSize) {
	char* S = (char*) malloc(msgSize);
	int const n = vsnprintf(S, msgSize, Description, args);
	if (n > -1 && ((unsigned int) n) < msgSize);
	else {
		if (n > -1)
			msgSize = n + 1;
		else
			msgSize *= 2;
 		free(S);
		return true;
	}

	Item const m(S, type);
	Messages.push_back(m);

	if (type == ERROR || type == FATAL)
		PendingFlag = true;

	if (type == FATAL || type == DEBUG)
		std::clog << m << std::endl;

	free(S);
	return false;
}
									/*}}}*/
// GlobalError::PopMessage - Pulls a single message out			/*{{{*/
bool GlobalError::PopMessage(std::string &Text) {
	if (Messages.empty() == true)
		return false;

	Item const msg = Messages.front();
	Messages.pop_front();

	bool const Ret = (msg.Type == ERROR || msg.Type == FATAL);
	Text = msg.Text;
	if (PendingFlag == false || Ret == false)
		return Ret;

	// check if another error message is pending
	for (std::list<Item>::const_iterator m = Messages.begin();
	     m != Messages.end(); ++m)
		if (m->Type == ERROR || m->Type == FATAL)
			return Ret;

	PendingFlag = false;
	return Ret;
}
									/*}}}*/
// GlobalError::DumpErrors - Dump all of the errors/warns to cerr	/*{{{*/
void GlobalError::DumpErrors(std::ostream &out, MsgType const &threshold,
			     bool const &mergeStack) {
	if (mergeStack == true)
		for (std::list<MsgStack>::const_reverse_iterator s = Stacks.rbegin();
		     s != Stacks.rend(); ++s)
			std::copy(s->Messages.begin(), s->Messages.end(), std::front_inserter(Messages));

	std::for_each(Messages.begin(), Messages.end(), [&threshold, &out](Item const &m) {
		if (m.Type >= threshold)
			out << m << std::endl;
	});

	Discard();
}
									/*}}}*/
// GlobalError::Discard - Discard					/*{{{*/
void GlobalError::Discard() {
	Messages.clear();
	PendingFlag = false;
}
									/*}}}*/
// GlobalError::empty - does our error list include anything?		/*{{{*/
bool GlobalError::empty(MsgType const &threshold) const {
	if (PendingFlag == true)
		return false;

	if (Messages.empty() == true)
		return true;

	return std::find_if(Messages.begin(), Messages.end(), [&threshold](Item const &m) {
		return m.Type >= threshold;
	}) == Messages.end();
}
									/*}}}*/
// GlobalError::PushToStack						/*{{{*/
void GlobalError::PushToStack() {
	Stacks.emplace_back(Messages, PendingFlag);
	Discard();
}
									/*}}}*/
// GlobalError::RevertToStack						/*{{{*/
void GlobalError::RevertToStack() {
	Discard();
	MsgStack pack = Stacks.back();
	Messages = pack.Messages;
	PendingFlag = pack.PendingFlag;
	Stacks.pop_back();
}
									/*}}}*/
// GlobalError::MergeWithStack						/*{{{*/
void GlobalError::MergeWithStack() {
	MsgStack pack = Stacks.back();
	Messages.splice(Messages.begin(), pack.Messages);
	PendingFlag = PendingFlag || pack.PendingFlag;
	Stacks.pop_back();
}
									/*}}}*/

// GlobalError::Item::operator<<						/*{{{*/
APT_HIDDEN std::ostream &operator<<(std::ostream &out, GlobalError::Item i)
{
   static constexpr auto COLOR_RESET = "\033[0m";
   static constexpr auto COLOR_BOLD = "\033[1m";   // bold neutral
   static constexpr auto COLOR_NOTICE = "\033[1m";   // bold neutral
   static constexpr auto COLOR_WARN = "\033[1;33m";  // bold yellow
   static constexpr auto COLOR_ERROR = "\033[1;31m"; // bold red

   bool use_color = _config->FindB("APT::Color", false);
   auto out_ver = _config->FindI("APT::Output-Version");

   if (use_color)
   {
      switch (i.Type)
      {
      case GlobalError::FATAL:
      case GlobalError::ERROR:
	 out << COLOR_ERROR;
	 break;
      case GlobalError::WARNING:
	 out << COLOR_WARN;
	 break;
      case GlobalError::NOTICE:
      case GlobalError::AUDIT:
	 out << COLOR_NOTICE;
	 break;
      default:
	 break;
      }
   }

   switch (i.Type)
   {
   case GlobalError::FATAL:
   case GlobalError::ERROR:
      // TRANSLATOR: This is a warning level displayed before the message
      out << (out_ver < 30 ? "E:" : _("Error:"));
      break;
   case GlobalError::WARNING:
      // TRANSLATOR: This is a warning level displayed before the message
      out << (out_ver < 30 ? "W:" : _("Warning:"));
      break;
   case GlobalError::NOTICE:
      // TRANSLATOR: This is a warning level displayed before the message
      out << (out_ver < 30 ? "N:" : _("Notice:"));
      break;
   case GlobalError::AUDIT:
      out << (out_ver < 30 ? "A:" : _("Audit:"));
      break;
   case GlobalError::DEBUG:
      // TRANSLATOR: This is a warning level displayed before the message
      out << _("Debug:");
      break;
   }
   out << " ";

   if (use_color)
   {
      switch (i.Type)
      {
      case GlobalError::FATAL:
      case GlobalError::ERROR:
      case GlobalError::WARNING:
	 out << COLOR_RESET;
	 if (out_ver >= 30)
	    out << COLOR_BOLD;
	 break;
      case GlobalError::NOTICE:
      case GlobalError::AUDIT:
	 out << COLOR_RESET;
	 break;
      default:
	 break;
      }
   }

   std::string::size_type line_start = 0;
   std::string::size_type line_end;
   while ((line_end = i.Text.find_first_of("\n\r", line_start)) != std::string::npos)
   {
      if (line_start != 0)
	 out << std::endl
	     << "   ";
      out << i.Text.substr(line_start, line_end - line_start);
      line_start = i.Text.find_first_not_of("\n\r", line_end + 1);
      if (line_start == std::string::npos)
	 break;
   }
   if (line_start == 0)
      out << i.Text;
   else if (line_start != std::string::npos)
      out << std::endl
	  << "   " << i.Text.substr(line_start);

   if (use_color)
      out << COLOR_RESET;

   return out;
}
									/*}}}*/