2000-09-14 19:42:48 +08:00
|
|
|
/** \ingroup rpmio
|
|
|
|
* \file rpmio/rpmlog.c
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "system.h"
|
|
|
|
#include <stdarg.h>
|
2007-11-23 13:46:19 +08:00
|
|
|
#include <rpmlog.h>
|
2000-12-13 04:03:45 +08:00
|
|
|
#include "debug.h"
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
static int nrecs = 0;
|
2007-09-11 22:48:54 +08:00
|
|
|
static 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)
|
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
if (recs != NULL && nrecs > 0)
|
2001-02-18 01:53:21 +08:00
|
|
|
return recs[nrecs-1].code;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-09-14 19:42:48 +08:00
|
|
|
const char * rpmlogMessage(void)
|
|
|
|
{
|
2001-05-04 05:00:18 +08:00
|
|
|
if (recs != NULL && nrecs > 0)
|
2000-09-14 19:42:48 +08:00
|
|
|
return recs[nrecs-1].message;
|
|
|
|
return _("(no error)");
|
|
|
|
}
|
|
|
|
|
|
|
|
void rpmlogPrint(FILE *f)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (f == NULL)
|
|
|
|
f = stderr;
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (recs)
|
2000-09-14 19:42:48 +08:00
|
|
|
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;
|
|
|
|
|
2001-06-06 03:26:22 +08:00
|
|
|
if (recs)
|
2000-09-14 19:42:48 +08:00
|
|
|
for (i = 0; i < nrecs; i++) {
|
|
|
|
rpmlogRec rec = recs + i;
|
2001-05-04 05:00:18 +08:00
|
|
|
rec->message = _free(rec->message);
|
2000-09-14 19:42:48 +08:00
|
|
|
}
|
2001-05-04 05:00:18 +08:00
|
|
|
recs = _free(recs);
|
2000-09-14 19:42:48 +08:00
|
|
|
nrecs = 0;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:48:54 +08:00
|
|
|
void rpmlogOpen (const char *ident, int option,
|
|
|
|
int facility)
|
2000-09-14 19:42:48 +08:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2002-01-19 06:51:30 +08:00
|
|
|
static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
|
2001-10-18 00:43:36 +08:00
|
|
|
|
2007-07-11 17:34:46 +08:00
|
|
|
#ifdef NOTYET
|
2007-09-11 22:48:54 +08:00
|
|
|
static unsigned rpmlogFacility = RPMLOG_USER;
|
2007-07-11 17:34:46 +08:00
|
|
|
#endif
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
int rpmlogSetMask (int mask)
|
|
|
|
{
|
|
|
|
int omask = rpmlogMask;
|
|
|
|
if (mask)
|
|
|
|
rpmlogMask = mask;
|
|
|
|
return omask;
|
|
|
|
}
|
|
|
|
|
2007-09-11 22:48:54 +08:00
|
|
|
static rpmlogCallback _rpmlogCallback = NULL;
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
|
|
|
|
{
|
|
|
|
rpmlogCallback ocb = _rpmlogCallback;
|
|
|
|
_rpmlogCallback = cb;
|
|
|
|
return ocb;
|
|
|
|
}
|
|
|
|
|
2003-01-29 01:17:26 +08:00
|
|
|
static FILE * _stdlog = NULL;
|
|
|
|
|
|
|
|
FILE * rpmlogSetFile(FILE * fp)
|
|
|
|
{
|
|
|
|
FILE * ofp = _stdlog;
|
|
|
|
_stdlog = fp;
|
|
|
|
return ofp;
|
|
|
|
}
|
|
|
|
|
2001-10-15 11:22:10 +08:00
|
|
|
static char *rpmlogMsgPrefix[] = {
|
2000-09-14 19:42:48 +08:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2001-04-18 03:29:12 +08:00
|
|
|
#if !defined(HAVE_VSNPRINTF)
|
2007-09-11 22:48:54 +08:00
|
|
|
static inline int vsnprintf(char * buf, int nb,
|
2001-04-18 03:29:12 +08:00
|
|
|
const char * fmt, va_list ap)
|
|
|
|
{
|
|
|
|
return vsprintf(buf, fmt, ap);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2007-09-11 22:48:54 +08:00
|
|
|
/* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
|
|
|
|
/* FIX: rpmlogMsgPrefix[] may be NULL */
|
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
|
|
|
{
|
2002-01-19 06:51:30 +08:00
|
|
|
unsigned pri = RPMLOG_PRI(code);
|
|
|
|
unsigned mask = RPMLOG_MASK(pri);
|
2007-07-11 17:34:46 +08:00
|
|
|
#ifdef NOTYET
|
2007-09-11 22:48:54 +08:00
|
|
|
unsigned fac = RPMLOG_FAC(code);
|
2007-07-11 17:34:46 +08:00
|
|
|
#endif
|
2001-02-18 01:53:21 +08:00
|
|
|
char *msgbuf, *msg;
|
|
|
|
int msgnb = BUFSIZ, nb;
|
2003-01-29 01:17:26 +08:00
|
|
|
FILE * msgout = (_stdlog ? _stdlog : stderr);
|
2000-09-14 19:42:48 +08:00
|
|
|
|
|
|
|
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) {
|
2001-04-21 14:02:09 +08:00
|
|
|
va_list apc;
|
2007-09-11 22:48:54 +08:00
|
|
|
va_copy(apc, ap);
|
2001-09-25 05:53:14 +08:00
|
|
|
nb = vsnprintf(msgbuf, msgnb, fmt, apc);
|
2001-02-18 01:53:21 +08:00
|
|
|
if (nb > -1 && nb < msgnb)
|
|
|
|
break;
|
2001-09-25 05:53:14 +08:00
|
|
|
if (nb > -1) /* glibc 2.1 (and later) */
|
2001-02-18 01:53:21 +08:00
|
|
|
msgnb = nb+1;
|
|
|
|
else /* glibc 2.0 */
|
|
|
|
msgnb *= 2;
|
|
|
|
msgbuf = xrealloc(msgbuf, msgnb);
|
2002-09-21 00:34:33 +08:00
|
|
|
va_end(apc);
|
2001-02-18 01:53:21 +08:00
|
|
|
}
|
|
|
|
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));
|
2001-05-04 05:00:18 +08:00
|
|
|
recs[nrecs].code = code;
|
2001-07-20 05:14:05 +08:00
|
|
|
recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
|
|
|
|
msgbuf = NULL; /* XXX don't free at exit. */
|
2000-09-14 19:42:48 +08:00
|
|
|
recs[nrecs+1].code = 0;
|
|
|
|
recs[nrecs+1].message = NULL;
|
|
|
|
++nrecs;
|
|
|
|
|
|
|
|
if (_rpmlogCallback) {
|
2007-09-11 22:48:54 +08:00
|
|
|
/* FIX: useless callback */
|
2000-09-14 19:42:48 +08:00
|
|
|
_rpmlogCallback();
|
|
|
|
return; /* XXX Preserve legacy rpmError behavior. */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rpmMessage behavior */
|
|
|
|
|
|
|
|
switch (pri) {
|
|
|
|
case RPMLOG_INFO:
|
|
|
|
case RPMLOG_NOTICE:
|
2003-01-29 01:17:26 +08:00
|
|
|
msgout = (_stdlog ? _stdlog : stdout);
|
2000-09-14 19:42:48 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2001-04-29 09:05:43 +08:00
|
|
|
if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
|
2000-09-14 19:42:48 +08:00
|
|
|
|
2001-05-01 06:32:22 +08:00
|
|
|
(void) fputs(msg, msgout);
|
|
|
|
(void) fflush(msgout);
|
2001-05-04 05:00:18 +08:00
|
|
|
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);
|
2007-09-11 22:48:54 +08:00
|
|
|
/* FIX: shrug */
|
2000-09-14 19:42:48 +08:00
|
|
|
vrpmlog(code, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
}
|
2000-12-26 21:10:18 +08:00
|
|
|
|