2000-09-14 19:42:48 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/rpmlog.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include <stdarg.h>
|
|
|
|
#include "rpmlog.h"
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
/*@access rpmlogRec @*/
|
|
|
|
|
|
|
|
static int nrecs = 0;
|
2000-11-01 00:18:34 +08:00
|
|
|
static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
int rpmlogGetNrecs(void)
|
|
|
|
{
|
|
|
|
return nrecs;
|
|
|
|
}
|
|
|
|
|
2001-02-18 01:53:21 +08:00
|
|
|
int rpmlogCode(void)
|
|
|
|
{
|
|
|
|
if (nrecs > 0)
|
|
|
|
return recs[nrecs-1].code;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 19:42:48 +08:00
|
|
|
const char * rpmlogMessage(void)
|
|
|
|
{
|
|
|
|
if (nrecs > 0)
|
|
|
|
return recs[nrecs-1].message;
|
|
|
|
return _("(no error)");
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpmlogPrint(FILE *f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
f = stderr;
|
|
|
|
|
|
|
|
for (i = 0; i < nrecs; i++) {
|
|
|
|
rpmlogRec rec = recs + i;
|
|
|
|
if (rec->message && *rec->message)
|
2001-01-17 01:33:59 +08:00
|
|
|
fprintf(f, " %s", rec->message);
|
2000-09-14 19:42:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpmlogClose (void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nrecs; i++) {
|
|
|
|
rpmlogRec rec = recs + i;
|
|
|
|
if (rec->message) {
|
|
|
|
free((void *)rec->message);
|
|
|
|
rec->message = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(recs);
|
|
|
|
recs = NULL;
|
|
|
|
nrecs = 0;
|
|
|
|
}
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
|
|
|
|
/*@unused@*/ int facility)
|
2000-09-14 19:42:48 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
|
2000-11-01 00:18:34 +08:00
|
|
|
static /*@unused@*/ int rpmlogFacility = RPMLOG_USER;
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
int rpmlogSetMask (int mask)
|
|
|
|
{
|
|
|
|
int omask = rpmlogMask;
|
|
|
|
if (mask)
|
|
|
|
rpmlogMask = mask;
|
|
|
|
return omask;
|
|
|
|
}
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
|
|
|
|
{
|
|
|
|
rpmlogCallback ocb = _rpmlogCallback;
|
|
|
|
_rpmlogCallback = cb;
|
|
|
|
return ocb;
|
|
|
|
}
|
|
|
|
|
|
|
|
static char *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 */
|
|
|
|
};
|
|
|
|
|
2000-11-01 00:18:34 +08:00
|
|
|
static void vrpmlog (unsigned code, const char *fmt, va_list ap)
|
2000-09-14 19:42:48 +08:00
|
|
|
{
|
|
|
|
int pri = RPMLOG_PRI(code);
|
|
|
|
int mask = RPMLOG_MASK(pri);
|
2000-11-01 00:18:34 +08:00
|
|
|
/*@unused@*/ int fac = RPMLOG_FAC(code);
|
2001-02-18 01:53:21 +08:00
|
|
|
char *msgbuf, *msg;
|
|
|
|
int msgnb = BUFSIZ, nb;
|
2000-09-14 19:42:48 +08:00
|
|
|
FILE * msgout = stderr;
|
|
|
|
rpmlogRec rec;
|
|
|
|
|
|
|
|
if ((mask & rpmlogMask) == 0)
|
|
|
|
return;
|
|
|
|
|
2001-02-18 01:53:21 +08:00
|
|
|
msgbuf = xmalloc(msgnb);
|
|
|
|
*msgbuf = '\0';
|
|
|
|
|
|
|
|
/* Allocate a sufficently large buffer for output. */
|
|
|
|
while (1) {
|
|
|
|
/*@-unrecog@*/
|
|
|
|
nb = vsnprintf(msgbuf, msgnb, fmt, ap);
|
|
|
|
/*@=unrecog@*/
|
|
|
|
if (nb > -1 && nb < msgnb)
|
|
|
|
break;
|
|
|
|
if (nb > -1) /* glibc 2.1 */
|
|
|
|
msgnb = nb+1;
|
|
|
|
else /* glibc 2.0 */
|
|
|
|
msgnb *= 2;
|
|
|
|
msgbuf = xrealloc(msgbuf, msgnb);
|
|
|
|
}
|
|
|
|
msgbuf[msgnb - 1] = '\0';
|
2000-09-14 19:42:48 +08:00
|
|
|
msg = msgbuf;
|
|
|
|
|
|
|
|
/* 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+1].code = 0;
|
|
|
|
recs[nrecs+1].message = NULL;
|
|
|
|
rec = recs + nrecs;
|
|
|
|
++nrecs;
|
|
|
|
|
|
|
|
rec->code = code;
|
2001-02-18 01:53:21 +08:00
|
|
|
rec->message = msgbuf;
|
|
|
|
msgbuf = NULL;
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
if (_rpmlogCallback) {
|
|
|
|
_rpmlogCallback();
|
2001-02-18 01:53:21 +08:00
|
|
|
if (msgbuf)
|
|
|
|
free(msgbuf);
|
2000-09-14 19:42:48 +08:00
|
|
|
return; /* XXX Preserve legacy rpmError behavior. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rpmMessage behavior */
|
|
|
|
|
|
|
|
switch (pri) {
|
|
|
|
case RPMLOG_INFO:
|
|
|
|
case RPMLOG_NOTICE:
|
|
|
|
msgout = stdout;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case RPMLOG_EMERG:
|
|
|
|
case RPMLOG_ALERT:
|
|
|
|
case RPMLOG_CRIT:
|
|
|
|
case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
|
|
|
|
case RPMLOG_WARNING:
|
|
|
|
case RPMLOG_DEBUG:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Silly FORTRAN-like carriage control. */
|
|
|
|
if (*msg == '+')
|
|
|
|
msg++;
|
|
|
|
else if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
|
|
|
|
fputs(_(rpmlogMsgPrefix[pri]), msgout);
|
|
|
|
|
|
|
|
fputs(msg, msgout);
|
|
|
|
fflush(msgout);
|
2001-02-18 01:53:21 +08:00
|
|
|
if (msgbuf)
|
|
|
|
free(msgbuf);
|
2000-09-14 19:42:48 +08:00
|
|
|
if (pri <= RPMLOG_CRIT)
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpmlog (int code, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
vrpmlog(code, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2000-12-26 21:10:18 +08:00
|
|
|
|
2001-02-18 01:53:21 +08:00
|
|
|
int rpmErrorCode(void)
|
|
|
|
{
|
|
|
|
return rpmlogCode();
|
|
|
|
}
|
|
|
|
|
2000-12-26 21:10:18 +08:00
|
|
|
const char * rpmErrorString(void)
|
|
|
|
{
|
|
|
|
return rpmlogMessage();
|
|
|
|
}
|
|
|
|
|
|
|
|
rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
|
|
|
|
{
|
|
|
|
return rpmlogSetCallback(cb);
|
|
|
|
}
|