Base revisions
[ntk/apt.git] / apt-pkg / contrib / error.cc
1 // -*- mode: cpp; mode: fold -*-
2 // Description /*{{{*/
3 // $Id: error.cc,v 1.1 1998/07/02 02:58:13 jgg Exp $
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 /*{{{*/
17 #include <errno.h>
18 #include <stdio.h>
19 #include <string.h>
20 #include <stdarg.h>
21
22 #include <pkglib/error.h>
23 /*}}}*/
24
25 GlobalError *_error = new GlobalError;
26
27 // GlobalError::GlobalError - Constructor /*{{{*/
28 // ---------------------------------------------------------------------
29 /* */
30 GlobalError::GlobalError() : PendingFlag(false)
31 {
32 }
33 /*}}}*/
34 // GlobalError::Errno - Get part of the error string from errno /*{{{*/
35 // ---------------------------------------------------------------------
36 /* Function indicates the stdlib function that failed and Description is
37 a user string that leads the text. Form is:
38 Description - Function (errno: strerror)
39 Carefull of the buffer overrun, sprintf.
40 */
41 bool GlobalError::Errno(const char *Function,const char *Description,...)
42 {
43 va_list args;
44 va_start(args,Description);
45
46 // sprintf the description
47 char S[400];
48 vsprintf(S,Description,args);
49 sprintf(S + strlen(S)," - %s (%i %s)",Function,errno,strerror(errno));
50
51 // Put it on the list
52 Item Itm;
53 Itm.Text = S;
54 Itm.Error = true;
55 List.push_back(Itm);
56
57 PendingFlag = true;
58
59 return false;
60 }
61 /*}}}*/
62 // GlobalError::Error - Add an error to the list /*{{{*/
63 // ---------------------------------------------------------------------
64 /* Just vsprintfs and pushes */
65 bool GlobalError::Error(const char *Description,...)
66 {
67 va_list args;
68 va_start(args,Description);
69
70 // sprintf the description
71 char S[400];
72 vsprintf(S,Description,args);
73
74 // Put it on the list
75 Item Itm;
76 Itm.Text = S;
77 Itm.Error = true;
78 List.push_back(Itm);
79
80 PendingFlag = true;
81
82 return false;
83 }
84 /*}}}*/
85 // GlobalError::Warning - Add a warning to the list /*{{{*/
86 // ---------------------------------------------------------------------
87 /* This doesn't set the pending error flag */
88 bool GlobalError::Warning(const char *Description,...)
89 {
90 va_list args;
91 va_start(args,Description);
92
93 // sprintf the description
94 char S[400];
95 vsprintf(S,Description,args);
96
97 // Put it on the list
98 Item Itm;
99 Itm.Text = S;
100 Itm.Error = false;
101 List.push_back(Itm);
102
103 return false;
104 }
105 /*}}}*/
106 // GlobalError::PopMessage - Pulls a single message out /*{{{*/
107 // ---------------------------------------------------------------------
108 /* This should be used in a loop checking empty() each cycle. It returns
109 true if the message is an error. */
110 bool GlobalError::PopMessage(string &Text)
111 {
112 bool Ret = List.front().Error;
113 Text = List.front().Text;
114 List.erase(List.begin());
115
116 // This really should check the list to see if only warnings are left..
117 if (empty())
118 PendingFlag = false;
119
120 return Ret;
121 }
122 /*}}}*/
123 // GlobalError::DumpErrors - Dump all of the errors/warns to cerr /*{{{*/
124 // ---------------------------------------------------------------------
125 /* */
126 void GlobalError::DumpErrors()
127 {
128 // Print any errors or warnings found
129 string Err;
130 while (empty() == false)
131 {
132 bool Type = PopMessage(Err);
133 if (Type == true)
134 cerr << "E: " << Err << endl;
135 else
136 cerr << "W: " << Err << endl;
137 }
138 }
139 /*}}}*/