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
|
/** \ingroup rpmio
* \file rpmio/rpmlog.c
*/
#include "system.h"
#include <stdarg.h>
#include <rpm/rpmlog.h>
#include "debug.h"
static int nrecs = 0;
static rpmlogRec recs = NULL;
struct rpmlogRec_s {
int code; /* unused */
rpmlogLvl pri; /* priority */
char * message; /* log message string */
};
int rpmlogGetNrecs(void)
{
return nrecs;
}
int rpmlogCode(void)
{
if (recs != NULL && nrecs > 0)
return recs[nrecs-1].code;
return -1;
}
const char * rpmlogMessage(void)
{
if (recs != NULL && nrecs > 0)
return recs[nrecs-1].message;
return _("(no error)");
}
const char * rpmlogRecMessage(rpmlogRec rec)
{
assert(rec != NULL);
return (rec->message);
}
rpmlogLvl rpmlogRecPriority(rpmlogRec rec)
{
assert(rec != NULL);
return (rec->pri);
}
void rpmlogPrint(FILE *f)
{
int i;
if (f == NULL)
f = stderr;
if (recs)
for (i = 0; i < nrecs; i++) {
rpmlogRec rec = recs + i;
if (rec->message && *rec->message)
fprintf(f, " %s", rec->message);
}
}
void rpmlogClose (void)
{
int i;
if (recs)
for (i = 0; i < nrecs; i++) {
rpmlogRec rec = recs + i;
rec->message = _free(rec->message);
}
recs = _free(recs);
nrecs = 0;
}
void rpmlogOpen (const char *ident, int option,
int facility)
{
}
static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
#ifdef NOTYET
static unsigned rpmlogFacility = RPMLOG_USER;
#endif
int rpmlogSetMask (int mask)
{
int omask = rpmlogMask;
if (mask)
rpmlogMask = mask;
return omask;
}
static rpmlogCallback _rpmlogCallback = NULL;
static rpmlogCallbackData _rpmlogCallbackData = NULL;
rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
{
rpmlogCallback ocb = _rpmlogCallback;
_rpmlogCallback = cb;
_rpmlogCallbackData = data;
return ocb;
}
static FILE * _stdlog = NULL;
static int rpmlogDefault(rpmlogRec rec)
{
FILE *msgout = (_stdlog ? _stdlog : stderr);
switch (rec->pri) {
case RPMLOG_INFO:
case RPMLOG_NOTICE:
msgout = (_stdlog ? _stdlog : stdout);
break;
case RPMLOG_EMERG:
case RPMLOG_ALERT:
case RPMLOG_CRIT:
case RPMLOG_ERR:
case RPMLOG_WARNING:
case RPMLOG_DEBUG:
default:
break;
}
(void) fputs(rpmlogLevelPrefix(rec->pri), msgout);
(void) fputs(rec->message, msgout);
(void) fflush(msgout);
return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
}
FILE * rpmlogSetFile(FILE * fp)
{
FILE * ofp = _stdlog;
_stdlog = fp;
return ofp;
}
static const char * const rpmlogMsgPrefix[] = {
N_("fatal error: "),/*!< RPMLOG_EMERG */
N_("fatal error: "),/*!< RPMLOG_ALERT */
N_("fatal error: "),/*!< RPMLOG_CRIT */
N_("error: "), /*!< RPMLOG_ERR */
N_("warning: "), /*!< RPMLOG_WARNING */
"", /*!< RPMLOG_NOTICE */
"", /*!< RPMLOG_INFO */
"D: ", /*!< RPMLOG_DEBUG */
};
const char * rpmlogLevelPrefix(rpmlogLvl pri)
{
const char * prefix = "";
if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
prefix = _(rpmlogMsgPrefix[pri]);
return prefix;
}
/* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
/* FIX: rpmlogMsgPrefix[] may be NULL */
static void vrpmlog (unsigned code, const char *fmt, va_list ap)
{
unsigned pri = RPMLOG_PRI(code);
unsigned mask = RPMLOG_MASK(pri);
#ifdef NOTYET
unsigned fac = RPMLOG_FAC(code);
#endif
char *msgbuf, *msg;
int msgnb = BUFSIZ, nb;
int cbrc = RPMLOG_DEFAULT;
int needexit = 0;
struct rpmlogRec_s rec;
if ((mask & rpmlogMask) == 0)
return;
msgbuf = xmalloc(msgnb);
*msgbuf = '\0';
/* Allocate a sufficently large buffer for output. */
while (1) {
va_list apc;
va_copy(apc, ap);
nb = vsnprintf(msgbuf, msgnb, fmt, apc);
if (nb > -1 && nb < msgnb)
break;
if (nb > -1) /* glibc 2.1 (and later) */
msgnb = nb+1;
else /* glibc 2.0 */
msgnb *= 2;
msgbuf = xrealloc(msgbuf, msgnb);
va_end(apc);
}
msgbuf[msgnb - 1] = '\0';
msg = msgbuf;
rec.code = code;
rec.message = msg;
rec.pri = pri;
/* Save copy of all messages at warning (or below == "more important"). */
if (pri <= RPMLOG_WARNING) {
if (recs == NULL)
recs = xmalloc((nrecs+2) * sizeof(*recs));
else
recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
recs[nrecs].code = rec.code;
recs[nrecs].pri = rec.pri;
recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
msgbuf = NULL; /* XXX don't free at exit. */
recs[nrecs+1].code = 0;
recs[nrecs+1].message = NULL;
++nrecs;
}
if (_rpmlogCallback) {
cbrc = _rpmlogCallback(&rec, _rpmlogCallbackData);
needexit += cbrc & RPMLOG_EXIT;
}
if (cbrc & RPMLOG_DEFAULT) {
cbrc = rpmlogDefault(&rec);
needexit += cbrc & RPMLOG_EXIT;
}
msgbuf = _free(msgbuf);
if (needexit)
exit(EXIT_FAILURE);
}
void rpmlog (int code, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
/* FIX: shrug */
vrpmlog(code, fmt, ap);
va_end(ap);
}
|