File: rpmlog.c

package info (click to toggle)
rpm 4.14.2.1+dfsg1-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 17,592 kB
  • sloc: ansic: 68,414; sh: 31,370; makefile: 884; cpp: 569; python: 485; perl: 222; sed: 16
file content (440 lines) | stat: -rw-r--r-- 9,535 bytes parent folder | download | duplicates (2)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
/** \ingroup rpmio
 * \file rpmio/rpmlog.c
 */

#include "system.h"
#include <stdarg.h>
#include <stdlib.h>
#include <pthread.h>
#include <errno.h>
#include <rpm/rpmlog.h>
#include <rpm/rpmmacro.h>
#include "debug.h"

typedef struct rpmlogCtx_s * rpmlogCtx;
struct rpmlogCtx_s {
    pthread_rwlock_t lock;
    unsigned mask;
    int nrecs;
    rpmlogRec recs;
    rpmlogCallback cbfunc;
    rpmlogCallbackData cbdata;
    FILE *stdlog;
};

struct rpmlogRec_s {
    int		code;		/* unused */
    rpmlogLvl	pri;		/* priority */ 
    char * message;		/* log message string */
};

/* Force log context acquisition through a function */
static rpmlogCtx rpmlogCtxAcquire(int write)
{
    static struct rpmlogCtx_s _globalCtx = { PTHREAD_RWLOCK_INITIALIZER,
					     RPMLOG_UPTO(RPMLOG_NOTICE),
					     0, NULL, NULL, NULL, NULL };
    rpmlogCtx ctx = &_globalCtx;
    int xx;

    /* XXX Silently failing is bad, but we can't very well use log here... */
    if (write)
	xx = pthread_rwlock_wrlock(&ctx->lock);
    else
	xx = pthread_rwlock_rdlock(&ctx->lock);

    return (xx == 0) ? ctx : NULL;
}

/* Release log context */
static rpmlogCtx rpmlogCtxRelease(rpmlogCtx ctx)
{
    if (ctx)
	pthread_rwlock_unlock(&ctx->lock);
    return NULL;
}

int rpmlogGetNrecs(void)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(0);
    int nrecs = -1;
    if (ctx)
	nrecs = ctx->nrecs;
    rpmlogCtxRelease(ctx);
    return nrecs;
}

int rpmlogCode(void)
{
    int code = -1;
    rpmlogCtx ctx = rpmlogCtxAcquire(0);
    
    if (ctx && ctx->recs != NULL && ctx->nrecs > 0)
	code = ctx->recs[ctx->nrecs-1].code;

    rpmlogCtxRelease(ctx);
    return code;
}

const char * rpmlogMessage(void)
{
    const char *msg = _("(no error)");
    rpmlogCtx ctx = rpmlogCtxAcquire(0);

    if (ctx && ctx->recs != NULL && ctx->nrecs > 0)
	msg = ctx->recs[ctx->nrecs-1].message;

    rpmlogCtxRelease(ctx);
    return msg;
}

const char * rpmlogRecMessage(rpmlogRec rec)
{
    return (rec != NULL) ? rec->message : NULL;
}

rpmlogLvl rpmlogRecPriority(rpmlogRec rec)
{
    return (rec != NULL) ? rec->pri : (rpmlogLvl)-1;
}

void rpmlogPrint(FILE *f)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(0);

    if (ctx == NULL)
	return;

    if (f == NULL)
	f = stderr;

    for (int i = 0; i < ctx->nrecs; i++) {
	rpmlogRec rec = ctx->recs + i;
	if (rec->message && *rec->message)
	    fprintf(f, "    %s", rec->message);
    }

    rpmlogCtxRelease(ctx);
}

void rpmlogClose (void)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(1);

    if (ctx == NULL)
	return;

    for (int i = 0; i < ctx->nrecs; i++) {
	rpmlogRec rec = ctx->recs + i;
	rec->message = _free(rec->message);
    }
    ctx->recs = _free(ctx->recs);
    ctx->nrecs = 0;

    rpmlogCtxRelease(ctx);
}

void rpmlogOpen (const char *ident, int option,
		int facility)
{
}

#ifdef NOTYET
static unsigned rpmlogFacility = RPMLOG_USER;
#endif

int rpmlogSetMask (int mask)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(mask ? 1 : 0);

    int omask = -1;
    if (ctx) {
	omask = ctx->mask;
	if (mask)
	    ctx->mask = mask;
    }

    rpmlogCtxRelease(ctx);
    return omask;
}

rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(1);

    rpmlogCallback ocb = NULL;
    if (ctx) {
	ocb = ctx->cbfunc;
	ctx->cbfunc = cb;
	ctx->cbdata = data;
    }

    rpmlogCtxRelease(ctx);
    return ocb;
}

FILE * rpmlogSetFile(FILE * fp)
{
    rpmlogCtx ctx = rpmlogCtxAcquire(1);

    FILE * ofp = NULL;
    if (ctx) {
	ofp = ctx->stdlog;
	ctx->stdlog = fp;
    }

    rpmlogCtxRelease(ctx);
    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 */
};

#define ANSI_COLOR_BLACK	"\x1b[30m"
#define ANSI_COLOR_RED		"\x1b[31m"
#define ANSI_COLOR_GREEN	"\x1b[32m"
#define ANSI_COLOR_YELLOW	"\x1b[33m"
#define ANSI_COLOR_BLUE		"\x1b[34m"
#define ANSI_COLOR_MAGENTA	"\x1b[35m"
#define ANSI_COLOR_CYAN		"\x1b[36m"
#define ANSI_COLOR_WHITE	"\x1b[37m"

#define ANSI_BRIGHT_BLACK	"\x1b[30;1m"
#define ANSI_BRIGHT_RED		"\x1b[31;1m"
#define ANSI_BRIGHT_GREEN	"\x1b[32;1m"
#define ANSI_BRIGHT_YELLOW	"\x1b[33;1m"
#define ANSI_BRIGHT_BLUE	"\x1b[34;1m"
#define ANSI_BRIGHT_MAGENTA	"\x1b[35;1m"
#define ANSI_BRIGHT_CYAN	"\x1b[36;1m"
#define ANSI_BRIGHT_WHITE	"\x1b[37;1m"

#define ANSI_COLOR_BOLD		"\x1b[1m"
#define ANSI_COLOR_RESET	"\x1b[0m"

static const char *rpmlogMsgPrefixColor[] = {
    ANSI_BRIGHT_RED,	/*!< RPMLOG_EMERG */
    ANSI_BRIGHT_RED,	/*!< RPMLOG_ALERT */
    ANSI_BRIGHT_RED,	/*!< RPMLOG_CRIT */
    ANSI_BRIGHT_RED,	/*!< RPMLOG_ERR */
    ANSI_BRIGHT_MAGENTA,/*!< RPMLOG_WARNING */
    "",			/*!< RPMLOG_NOTICE */
    "",			/*!< RPMLOG_INFO */
    ANSI_BRIGHT_BLUE,	/*!< RPMLOG_DEBUG */
};

const char * rpmlogLevelPrefix(rpmlogLvl pri)
{
    const char * prefix = "";
    if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri]) 
	prefix = _(rpmlogMsgPrefix[pri]);
    return prefix;
}

static const char * rpmlogLevelColor(rpmlogLvl pri)
{
    return rpmlogMsgPrefixColor[pri&0x7];
}

enum {
    COLOR_NO = 0,
    COLOR_AUTO = 1,
    COLOR_ALWAYS = 2,
};

static int getColorConfig(void)
{
    int rc = COLOR_NO;
    char * color = rpmExpand("%{?_color_output}%{!?_color_output:auto}", NULL);
    if (rstreq(color, "auto"))
	rc = COLOR_AUTO;
    else if (rstreq(color, "always"))
	rc = COLOR_ALWAYS;
    free(color);
    return rc;
}

static void logerror(void)
{
    static __thread int lasterr = 0;
    if (errno != EPIPE && errno != lasterr) {
	lasterr = errno;
	perror(_("Error writing to log"));
    }
}

static int rpmlogDefault(FILE *stdlog, rpmlogRec rec)
{
    FILE *msgout = (stdlog ? stdlog : stderr);
    static __thread int color = -1;
    const char * colorOn = NULL;

    if (color < 0)
	color = getColorConfig();

    if (color == COLOR_ALWAYS ||
	    (color == COLOR_AUTO && isatty(fileno(msgout))))
	colorOn = rpmlogLevelColor(rec->pri);

    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:
	if (colorOn && *colorOn)
	    if (fputs(rpmlogLevelColor(rec->pri), msgout) == EOF)
		logerror();
	break;
    default:
	break;
    }

    if (fputs(rpmlogLevelPrefix(rec->pri), msgout) == EOF)
	logerror();

    switch (rec->pri) {
    case RPMLOG_INFO:
    case RPMLOG_NOTICE:
	break;
    case RPMLOG_EMERG:
    case RPMLOG_ALERT:
    case RPMLOG_CRIT:
    case RPMLOG_ERR:
    case RPMLOG_WARNING:
	if (colorOn && *colorOn) {
	    if (fputs(ANSI_COLOR_RESET, msgout) == EOF)
		logerror();
	    if (fputs(ANSI_COLOR_BOLD, msgout) == EOF)
		logerror();
	}
    case RPMLOG_DEBUG:
    default:
	break;
    }

    if (rec->message)
	if (fputs(rec->message, msgout) == EOF)
	    logerror();

    switch (rec->pri) {
    case RPMLOG_INFO:
    case RPMLOG_NOTICE:
	break;
    case RPMLOG_EMERG:
    case RPMLOG_ALERT:
    case RPMLOG_CRIT:
    case RPMLOG_ERR:
    case RPMLOG_WARNING:
    case RPMLOG_DEBUG:
	if (colorOn && *colorOn)
	    if (fputs(ANSI_COLOR_RESET, msgout) == EOF)
		logerror();
	break;
    default:
	break;
    }

    if (fflush(msgout) == EOF)
	logerror();

    return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
}

/* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
/* FIX: rpmlogMsgPrefix[] may be NULL */
static void dolog(struct rpmlogRec_s *rec, int saverec)
{
    static pthread_mutex_t serialize = PTHREAD_MUTEX_INITIALIZER;

    int cbrc = RPMLOG_DEFAULT;
    int needexit = 0;
    FILE *clog = NULL;
    rpmlogCallbackData *cbdata = NULL;
    rpmlogCallback cbfunc = NULL;
    rpmlogCtx ctx = rpmlogCtxAcquire(saverec);

    if (ctx == NULL)
	return;

    /* Save copy of all messages at warning (or below == "more important"). */
    if (saverec) {
	ctx->recs = xrealloc(ctx->recs, (ctx->nrecs+2) * sizeof(*ctx->recs));
	ctx->recs[ctx->nrecs].code = rec->code;
	ctx->recs[ctx->nrecs].pri = rec->pri;
	ctx->recs[ctx->nrecs].message = xstrdup(rec->message);
	ctx->recs[ctx->nrecs+1].code = 0;
	ctx->recs[ctx->nrecs+1].message = NULL;
	ctx->nrecs++;
    }
    cbfunc = ctx->cbfunc;
    cbdata = ctx->cbdata;
    clog = ctx->stdlog;

    /* Free the context for callback and actual log output */
    ctx = rpmlogCtxRelease(ctx);

    /* Always serialize callback and output to avoid interleaved messages. */
    if (pthread_mutex_lock(&serialize) == 0) {
	if (cbfunc) {
	    cbrc = cbfunc(rec, cbdata);
	    needexit += cbrc & RPMLOG_EXIT;
	}

	if (cbrc & RPMLOG_DEFAULT) {
	    cbrc = rpmlogDefault(clog, rec);
	    needexit += cbrc & RPMLOG_EXIT;
	}
	pthread_mutex_unlock(&serialize);
    }
    
    if (needexit)
	exit(EXIT_FAILURE);

}

void rpmlog (int code, const char *fmt, ...)
{
    unsigned pri = RPMLOG_PRI(code);
    unsigned mask = RPMLOG_MASK(pri);
    int saverec = (pri <= RPMLOG_WARNING);
    va_list ap;
    int n;

    if ((mask & rpmlogSetMask(0)) == 0)
	return;

    va_start(ap, fmt);
    n = vsnprintf(NULL, 0, fmt, ap);
    va_end(ap);

    if (n >= -1) {
	struct rpmlogRec_s rec;
	size_t nb = n + 1;
	char *msg = xmalloc(nb);

	va_start(ap, fmt);
	n = vsnprintf(msg, nb, fmt, ap);
	va_end(ap);

	rec.code = code;
	rec.pri = pri;
	rec.message = msg;

	dolog(&rec, saverec);

	free(msg);
    }
}