summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib/error.cc
blob: 8db8bcc3c7044bc994d0a3bdf007c8fdad1628ee (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
// -*- mode: cpp; mode: fold -*-
// Description								/*{{{*/
// $Id: error.cc,v 1.7 1999/08/05 05:55:45 jgg Exp $
/* ######################################################################
   
   Global Erorr 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 source is placed in the Public Domain, do with it what you will
   It was originally written by Jason Gunthorpe.
   
   ##################################################################### */
									/*}}}*/
// Include Files							/*{{{*/
#ifdef __GNUG__
#pragma implementation "apt-pkg/error.h"
#endif 

#include <apt-pkg/error.h>

#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <unistd.h>

#include "config.h"
   									/*}}}*/

// Global Error Object							/*{{{*/
/* If the implementation supports posix threads then the accessor function
   is compiled to be thread safe otherwise a non-safe version is used. A
   Per-Thread error object is maintained in much the same manner as libc
   manages errno */
#if _POSIX_THREADS == 1 && defined(HAVE_PTHREAD)
 #include <pthread.h>
 #error PTHREAD

 static pthread_key_t ErrorKey;
 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
 static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};

 GlobalError *_GetErrorObj()
 {
    static pthread_once_t Once = PTHREAD_ONCE_INIT;
    pthread_once(&Once,KeyAlloc);
    
    void *Res = pthread_getspecific(ErrorKey);
    if (Res == 0)
       pthread_setspecific(ErrorKey,Res = new GlobalError);
    return (GlobalError *)Res;
 }
#else
 GlobalError *_GetErrorObj()
 {
    static GlobalError *Obj = new GlobalError;
    return Obj;
 }
#endif
									/*}}}*/

// GlobalError::GlobalError - Constructor				/*{{{*/
// ---------------------------------------------------------------------
/* */
GlobalError::GlobalError() : List(0), PendingFlag(false)
{
}
									/*}}}*/
// GlobalError::Errno - Get part of the error string from errno		/*{{{*/
// ---------------------------------------------------------------------
/* Function indicates the stdlib function that failed and Description is
   a user string that leads the text. Form is:
     Description - Function (errno: strerror)
   Carefull of the buffer overrun, sprintf.
 */
bool GlobalError::Errno(const char *Function,const char *Description,...)
{
   va_list args;
   va_start(args,Description);

   // sprintf the description
   char S[400];
   vsnprintf(S,sizeof(S),Description,args);
   snprintf(S + strlen(S),sizeof(S) - strlen(S),
	    " - %s (%i %s)",Function,errno,strerror(errno));

   // Put it on the list
   Item *Itm = new Item;
   Itm->Text = S;
   Itm->Error = true;
   Insert(Itm);
   
   PendingFlag = true;

   return false;   
}
									/*}}}*/
// GlobalError::WarningE - Get part of the warn string from errno	/*{{{*/
// ---------------------------------------------------------------------
/* Function indicates the stdlib function that failed and Description is
   a user string that leads the text. Form is:
     Description - Function (errno: strerror)
   Carefull of the buffer overrun, sprintf.
 */
bool GlobalError::WarningE(const char *Function,const char *Description,...)
{
   va_list args;
   va_start(args,Description);

   // sprintf the description
   char S[400];
   vsnprintf(S,sizeof(S),Description,args);
   snprintf(S + strlen(S),sizeof(S) - strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));

   // Put it on the list
   Item *Itm = new Item;
   Itm->Text = S;
   Itm->Error = false;
   Insert(Itm);
   
   return false;   
}
									/*}}}*/
// GlobalError::Error - Add an error to the list			/*{{{*/
// ---------------------------------------------------------------------
/* Just vsprintfs and pushes */
bool GlobalError::Error(const char *Description,...)
{
   va_list args;
   va_start(args,Description);

   // sprintf the description
   char S[400];
   vsnprintf(S,sizeof(S),Description,args);

   // Put it on the list
   Item *Itm = new Item;
   Itm->Text = S;
   Itm->Error = true;
   Insert(Itm);
   
   PendingFlag = true;
   
   return false;
}
									/*}}}*/
// GlobalError::Warning - Add a warning to the list			/*{{{*/
// ---------------------------------------------------------------------
/* This doesn't set the pending error flag */
bool GlobalError::Warning(const char *Description,...)
{
   va_list args;
   va_start(args,Description);

   // sprintf the description
   char S[400];
   vsnprintf(S,sizeof(S),Description,args);

   // Put it on the list
   Item *Itm = new Item;
   Itm->Text = S;
   Itm->Error = false;
   Insert(Itm);
   
   return false;
}
									/*}}}*/
// GlobalError::PopMessage - Pulls a single message out			/*{{{*/
// ---------------------------------------------------------------------
/* This should be used in a loop checking empty() each cycle. It returns
   true if the message is an error. */
bool GlobalError::PopMessage(string &Text)
{
   if (List == 0)
      return false;
      
   bool Ret = List->Error;
   Text = List->Text;
   Item *Old = List;
   List = List->Next;
   delete Old;
   
   // This really should check the list to see if only warnings are left..
   if (List == 0)
      PendingFlag = false;
   
   return Ret;
}
									/*}}}*/
// GlobalError::DumpErrors - Dump all of the errors/warns to cerr	/*{{{*/
// ---------------------------------------------------------------------
/* */
void GlobalError::DumpErrors()
{
   // Print any errors or warnings found
   string Err;
   while (empty() == false)
   {
      bool Type = PopMessage(Err);
      if (Type == true)
	 cerr << "E: " << Err << endl;
      else
	 cerr << "W: " << Err << endl;
   }
}
									/*}}}*/
// GlobalError::Discard - Discard									/*{{{*/
// ---------------------------------------------------------------------
/* */
void GlobalError::Discard()
{
   while (List != 0)
   {
      Item *Old = List;
      List = List->Next;
      delete Old;
   }
   
   PendingFlag = false;
};
									/*}}}*/
// GlobalError::Insert - Insert a new item at the end			/*{{{*/
// ---------------------------------------------------------------------
/* */
void GlobalError::Insert(Item *Itm)
{
   Item **End = &List;
   for (Item *I = List; I != 0; I = I->Next)
      End = &I->Next;
   Itm->Next = *End;
   *End = Itm;
}
									/*}}}*/