Sync
[ntk/apt.git] / apt-pkg / contrib / error.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
6c139d6e 3// $Id: error.cc,v 1.2 1998/07/07 04:17:10 jgg Exp $
578bfd0a
AL
4/* ######################################################################
5
6 Global Erorr Class - Global error mechanism
7
8 We use a simple STL vector to store each error record. A PendingFlag
9 is kept which indicates when the vector contains a Sever error.
10
11 This source is placed in the Public Domain, do with it what you will
12 It was originally written by Jason Gunthorpe.
13
14 ##################################################################### */
15 /*}}}*/
16// Include Files /*{{{*/
6c139d6e
AL
17#ifdef __GNUG__
18#pragma implementation "pkglib/error.h"
19#endif
20
578bfd0a
AL
21#include <errno.h>
22#include <stdio.h>
23#include <string.h>
24#include <stdarg.h>
25
26#include <pkglib/error.h>
27 /*}}}*/
28
29GlobalError *_error = new GlobalError;
30
31// GlobalError::GlobalError - Constructor /*{{{*/
32// ---------------------------------------------------------------------
33/* */
6c139d6e 34GlobalError::GlobalError() : List(0), PendingFlag(false)
578bfd0a
AL
35{
36}
37 /*}}}*/
38// GlobalError::Errno - Get part of the error string from errno /*{{{*/
39// ---------------------------------------------------------------------
40/* Function indicates the stdlib function that failed and Description is
41 a user string that leads the text. Form is:
42 Description - Function (errno: strerror)
43 Carefull of the buffer overrun, sprintf.
44 */
45bool GlobalError::Errno(const char *Function,const char *Description,...)
46{
47 va_list args;
48 va_start(args,Description);
49
50 // sprintf the description
51 char S[400];
52 vsprintf(S,Description,args);
53 sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
54
55 // Put it on the list
6c139d6e
AL
56 Item *Itm = new Item;
57 Itm->Text = S;
58 Itm->Error = true;
59 Insert(Itm);
578bfd0a
AL
60
61 PendingFlag = true;
62
63 return false;
64}
65 /*}}}*/
66// GlobalError::Error - Add an error to the list /*{{{*/
67// ---------------------------------------------------------------------
68/* Just vsprintfs and pushes */
69bool GlobalError::Error(const char *Description,...)
70{
71 va_list args;
72 va_start(args,Description);
73
74 // sprintf the description
75 char S[400];
76 vsprintf(S,Description,args);
77
78 // Put it on the list
6c139d6e
AL
79 Item *Itm = new Item;
80 Itm->Text = S;
81 Itm->Error = true;
82 Insert(Itm);
578bfd0a
AL
83
84 PendingFlag = true;
85
86 return false;
87}
88 /*}}}*/
89// GlobalError::Warning - Add a warning to the list /*{{{*/
90// ---------------------------------------------------------------------
91/* This doesn't set the pending error flag */
92bool GlobalError::Warning(const char *Description,...)
93{
94 va_list args;
95 va_start(args,Description);
96
97 // sprintf the description
98 char S[400];
99 vsprintf(S,Description,args);
100
101 // Put it on the list
6c139d6e
AL
102 Item *Itm = new Item;
103 Itm->Text = S;
104 Itm->Error = false;
105 Insert(Itm);
578bfd0a
AL
106
107 return false;
108}
109 /*}}}*/
110// GlobalError::PopMessage - Pulls a single message out /*{{{*/
111// ---------------------------------------------------------------------
112/* This should be used in a loop checking empty() each cycle. It returns
113 true if the message is an error. */
114bool GlobalError::PopMessage(string &Text)
115{
6c139d6e
AL
116 if (List == 0)
117 return false;
118
119 bool Ret = List->Error;
120 Text = List->Text;
121 Item *Old = List;
122 List = List->Next;
123 delete Old;
124
578bfd0a 125 // This really should check the list to see if only warnings are left..
6c139d6e 126 if (List == 0)
578bfd0a
AL
127 PendingFlag = false;
128
129 return Ret;
130}
131 /*}}}*/
132// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
133// ---------------------------------------------------------------------
134/* */
135void GlobalError::DumpErrors()
136{
137 // Print any errors or warnings found
138 string Err;
139 while (empty() == false)
140 {
141 bool Type = PopMessage(Err);
142 if (Type == true)
143 cerr << "E: " << Err << endl;
144 else
145 cerr << "W: " << Err << endl;
146 }
147}
148 /*}}}*/
6c139d6e
AL
149// GlobalError::Discard - Discard /*{{{*/
150// ---------------------------------------------------------------------
151/* */
152void GlobalError::Discard()
153{
154 while (List != 0)
155 {
156 Item *Old = List;
157 List = List->Next;
158 delete Old;
159 }
160
161 PendingFlag = false;
162};
163 /*}}}*/
164// GlobalError::Insert - Insert a new item at the end /*{{{*/
165// ---------------------------------------------------------------------
166/* */
167void GlobalError::Insert(Item *Itm)
168{
169 Item **End = &List;
170 for (Item *I = List; I != 0; I = I->Next)
171 End = &I->Next;
172 Itm->Next = *End;
173 *End = Itm;
174}
175 /*}}}*/