]> git.saurik.com Git - apt.git/blame - apt-pkg/contrib/error.cc
Sync
[apt.git] / apt-pkg / contrib / error.cc
CommitLineData
578bfd0a
AL
1// -*- mode: cpp; mode: fold -*-
2// Description /*{{{*/
6f27a7fc 3// $Id: error.cc,v 1.4 1998/09/12 02:46:26 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 17#ifdef __GNUG__
094a497d 18#pragma implementation "apt-pkg/error.h"
6c139d6e
AL
19#endif
20
6f27a7fc
AL
21#include <apt-pkg/error.h>
22
578bfd0a
AL
23#include <errno.h>
24#include <stdio.h>
25#include <string.h>
26#include <stdarg.h>
6f27a7fc 27#include <unistd.h>
578bfd0a 28
578bfd0a
AL
29 /*}}}*/
30
6f27a7fc
AL
31// Global Error Object /*{{{*/
32/* If the implementation supports posix threads then the accessor function
33 is compiled to be thread safe otherwise a non-safe version is used. A
34 Per-Thread error object is maintained in much the same manner as libc
35 manages errno */
36#if _POSIX_THREADS == 1
37 #include <pthread.h>
38
39 static pthread_key_t ErrorKey;
40 static void ErrorDestroy(void *Obj) {delete (GlobalError *)Obj;};
41 static void KeyAlloc() {pthread_key_create(&ErrorKey,ErrorDestroy);};
42
43 GlobalError *_GetErrorObj()
44 {
45 static pthread_once_t Once = PTHREAD_ONCE_INIT;
46 pthread_once(&Once,KeyAlloc);
47
48 void *Res = pthread_getspecific(ErrorKey);
49 if (Res == 0)
50 pthread_setspecific(ErrorKey,Res = new GlobalError);
51 return (GlobalError *)Res;
52 }
53#else
54 GlobalError *_GetErrorObj()
55 {
56 static GlobalError *Obj = new GlobalError;
57 return Obj;
58 }
59#endif
60 /*}}}*/
578bfd0a
AL
61
62// GlobalError::GlobalError - Constructor /*{{{*/
63// ---------------------------------------------------------------------
64/* */
6c139d6e 65GlobalError::GlobalError() : List(0), PendingFlag(false)
578bfd0a
AL
66{
67}
68 /*}}}*/
69// GlobalError::Errno - Get part of the error string from errno /*{{{*/
70// ---------------------------------------------------------------------
71/* Function indicates the stdlib function that failed and Description is
72 a user string that leads the text. Form is:
73 Description - Function (errno: strerror)
74 Carefull of the buffer overrun, sprintf.
75 */
76bool GlobalError::Errno(const char *Function,const char *Description,...)
77{
78 va_list args;
79 va_start(args,Description);
80
81 // sprintf the description
82 char S[400];
83 vsprintf(S,Description,args);
84 sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
85
86 // Put it on the list
6c139d6e
AL
87 Item *Itm = new Item;
88 Itm->Text = S;
89 Itm->Error = true;
90 Insert(Itm);
578bfd0a
AL
91
92 PendingFlag = true;
93
94 return false;
95}
96 /*}}}*/
97// GlobalError::Error - Add an error to the list /*{{{*/
98// ---------------------------------------------------------------------
99/* Just vsprintfs and pushes */
100bool GlobalError::Error(const char *Description,...)
101{
102 va_list args;
103 va_start(args,Description);
104
105 // sprintf the description
106 char S[400];
107 vsprintf(S,Description,args);
108
109 // Put it on the list
6c139d6e
AL
110 Item *Itm = new Item;
111 Itm->Text = S;
112 Itm->Error = true;
113 Insert(Itm);
578bfd0a
AL
114
115 PendingFlag = true;
116
117 return false;
118}
119 /*}}}*/
120// GlobalError::Warning - Add a warning to the list /*{{{*/
121// ---------------------------------------------------------------------
122/* This doesn't set the pending error flag */
123bool GlobalError::Warning(const char *Description,...)
124{
125 va_list args;
126 va_start(args,Description);
127
128 // sprintf the description
129 char S[400];
130 vsprintf(S,Description,args);
131
132 // Put it on the list
6c139d6e
AL
133 Item *Itm = new Item;
134 Itm->Text = S;
135 Itm->Error = false;
136 Insert(Itm);
578bfd0a
AL
137
138 return false;
139}
140 /*}}}*/
141// GlobalError::PopMessage - Pulls a single message out /*{{{*/
142// ---------------------------------------------------------------------
143/* This should be used in a loop checking empty() each cycle. It returns
144 true if the message is an error. */
145bool GlobalError::PopMessage(string &Text)
146{
6c139d6e
AL
147 if (List == 0)
148 return false;
149
150 bool Ret = List->Error;
151 Text = List->Text;
152 Item *Old = List;
153 List = List->Next;
154 delete Old;
155
578bfd0a 156 // This really should check the list to see if only warnings are left..
6c139d6e 157 if (List == 0)
578bfd0a
AL
158 PendingFlag = false;
159
160 return Ret;
161}
162 /*}}}*/
163// GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
164// ---------------------------------------------------------------------
165/* */
166void GlobalError::DumpErrors()
167{
168 // Print any errors or warnings found
169 string Err;
170 while (empty() == false)
171 {
172 bool Type = PopMessage(Err);
173 if (Type == true)
174 cerr << "E: " << Err << endl;
175 else
176 cerr << "W: " << Err << endl;
177 }
178}
179 /*}}}*/
6c139d6e
AL
180// GlobalError::Discard - Discard /*{{{*/
181// ---------------------------------------------------------------------
182/* */
183void GlobalError::Discard()
184{
185 while (List != 0)
186 {
187 Item *Old = List;
188 List = List->Next;
189 delete Old;
190 }
191
192 PendingFlag = false;
193};
194 /*}}}*/
195// GlobalError::Insert - Insert a new item at the end /*{{{*/
196// ---------------------------------------------------------------------
197/* */
198void GlobalError::Insert(Item *Itm)
199{
200 Item **End = &List;
201 for (Item *I = List; I != 0; I = I->Next)
202 End = &I->Next;
203 Itm->Next = *End;
204 *End = Itm;
205}
206 /*}}}*/