7 #include <rpm/rpmlog.h>
11 static rpmlogRec recs = NULL;
14 int code; /* unused */
15 rpmlogLvl pri; /* priority */
16 char * message; /* log message string */
19 int rpmlogGetNrecs(void)
26 if (recs != NULL && nrecs > 0)
27 return recs[nrecs-1].code;
32 const char * rpmlogMessage(void)
34 if (recs != NULL && nrecs > 0)
35 return recs[nrecs-1].message;
36 return _("(no error)");
39 const char * rpmlogRecMessage(rpmlogRec rec)
42 return (rec->message);
45 rpmlogLvl rpmlogRecPriority(rpmlogRec rec)
51 void rpmlogPrint(FILE *f)
59 for (i = 0; i < nrecs; i++) {
60 rpmlogRec rec = recs + i;
61 if (rec->message && *rec->message)
62 fprintf(f, " %s", rec->message);
66 void rpmlogClose (void)
71 for (i = 0; i < nrecs; i++) {
72 rpmlogRec rec = recs + i;
73 rec->message = _free(rec->message);
79 void rpmlogOpen (const char *ident, int option,
84 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
87 static unsigned rpmlogFacility = RPMLOG_USER;
90 int rpmlogSetMask (int mask)
92 int omask = rpmlogMask;
98 static rpmlogCallback _rpmlogCallback = NULL;
99 static rpmlogCallbackData _rpmlogCallbackData = NULL;
101 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
103 rpmlogCallback ocb = _rpmlogCallback;
104 _rpmlogCallback = cb;
105 _rpmlogCallbackData = data;
109 static FILE * _stdlog = NULL;
111 static int rpmlogDefault(rpmlogRec rec)
113 FILE *msgout = (_stdlog ? _stdlog : stderr);
118 msgout = (_stdlog ? _stdlog : stdout);
130 (void) fputs(rpmlogLevelPrefix(rec->pri), msgout);
132 (void) fputs(rec->message, msgout);
133 (void) fflush(msgout);
135 return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
139 FILE * rpmlogSetFile(FILE * fp)
141 FILE * ofp = _stdlog;
146 static const char *rpmlogMsgPrefix[] = {
147 N_("fatal error: "),/*!< RPMLOG_EMERG */
148 N_("fatal error: "),/*!< RPMLOG_ALERT */
149 N_("fatal error: "),/*!< RPMLOG_CRIT */
150 N_("error: "), /*!< RPMLOG_ERR */
151 N_("warning: "), /*!< RPMLOG_WARNING */
152 "", /*!< RPMLOG_NOTICE */
153 "", /*!< RPMLOG_INFO */
154 "D: ", /*!< RPMLOG_DEBUG */
157 const char * rpmlogLevelPrefix(rpmlogLvl pri)
159 const char * prefix = "";
160 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
161 prefix = _(rpmlogMsgPrefix[pri]);
165 #if !defined(HAVE_VSNPRINTF)
166 static inline int vsnprintf(char * buf, int nb,
167 const char * fmt, va_list ap)
169 return vsprintf(buf, fmt, ap);
173 /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
174 /* FIX: rpmlogMsgPrefix[] may be NULL */
175 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
177 unsigned pri = RPMLOG_PRI(code);
178 unsigned mask = RPMLOG_MASK(pri);
180 unsigned fac = RPMLOG_FAC(code);
183 int msgnb = BUFSIZ, nb;
184 int cbrc = RPMLOG_DEFAULT;
187 struct rpmlogRec_s rec;
189 if ((mask & rpmlogMask) == 0)
192 msgbuf = xmalloc(msgnb);
195 /* Allocate a sufficently large buffer for output. */
199 nb = vsnprintf(msgbuf, msgnb, fmt, apc);
200 if (nb > -1 && nb < msgnb)
202 if (nb > -1) /* glibc 2.1 (and later) */
206 msgbuf = xrealloc(msgbuf, msgnb);
209 msgbuf[msgnb - 1] = '\0';
216 /* Save copy of all messages at warning (or below == "more important"). */
217 if (pri <= RPMLOG_WARNING) {
220 recs = xmalloc((nrecs+2) * sizeof(*recs));
222 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
223 recs[nrecs].code = rec.code;
224 recs[nrecs].pri = rec.pri;
225 recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
226 msgbuf = NULL; /* XXX don't free at exit. */
227 recs[nrecs+1].code = 0;
228 recs[nrecs+1].message = NULL;
232 if (_rpmlogCallback) {
233 cbrc = _rpmlogCallback(&rec, _rpmlogCallbackData);
234 needexit += cbrc & RPMLOG_EXIT;
237 if (cbrc & RPMLOG_DEFAULT) {
238 cbrc = rpmlogDefault(&rec);
239 needexit += cbrc & RPMLOG_EXIT;
242 msgbuf = _free(msgbuf);
247 void rpmlog (int code, const char *fmt, ...)
253 vrpmlog(code, fmt, ap);