11 static rpmlogRec recs = NULL;
14 int code; /* unused */
15 rpmlogLvl pri; /* priority */
16 const 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 void rpmlogPrint(FILE *f)
47 for (i = 0; i < nrecs; i++) {
48 rpmlogRec rec = recs + i;
49 if (rec->message && *rec->message)
50 fprintf(f, " %s", rec->message);
54 void rpmlogClose (void)
59 for (i = 0; i < nrecs; i++) {
60 rpmlogRec rec = recs + i;
61 rec->message = _free(rec->message);
67 void rpmlogOpen (const char *ident, int option,
72 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
75 static unsigned rpmlogFacility = RPMLOG_USER;
78 int rpmlogSetMask (int mask)
80 int omask = rpmlogMask;
86 static rpmlogCallback _rpmlogCallback = NULL;
87 static rpmlogCallbackData _rpmlogCallbackData = NULL;
89 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
91 rpmlogCallback ocb = _rpmlogCallback;
93 _rpmlogCallbackData = data;
97 static FILE * _stdlog = NULL;
99 static int rpmlogDefault(rpmlogRec rec)
101 FILE *msgout = (_stdlog ? _stdlog : stderr);
106 msgout = (_stdlog ? _stdlog : stdout);
118 (void) fputs(rpmlogLevelPrefix(rec->pri), msgout);
120 (void) fputs(rec->message, msgout);
121 (void) fflush(msgout);
123 return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
127 FILE * rpmlogSetFile(FILE * fp)
129 FILE * ofp = _stdlog;
134 static const char *rpmlogMsgPrefix[] = {
135 N_("fatal error: "),/*!< RPMLOG_EMERG */
136 N_("fatal error: "),/*!< RPMLOG_ALERT */
137 N_("fatal error: "),/*!< RPMLOG_CRIT */
138 N_("error: "), /*!< RPMLOG_ERR */
139 N_("warning: "), /*!< RPMLOG_WARNING */
140 "", /*!< RPMLOG_NOTICE */
141 "", /*!< RPMLOG_INFO */
142 "D: ", /*!< RPMLOG_DEBUG */
145 const char * rpmlogLevelPrefix(rpmlogLvl pri)
147 const char * prefix = "";
148 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
149 prefix = _(rpmlogMsgPrefix[pri]);
153 #if !defined(HAVE_VSNPRINTF)
154 static inline int vsnprintf(char * buf, int nb,
155 const char * fmt, va_list ap)
157 return vsprintf(buf, fmt, ap);
161 /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
162 /* FIX: rpmlogMsgPrefix[] may be NULL */
163 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
165 unsigned pri = RPMLOG_PRI(code);
166 unsigned mask = RPMLOG_MASK(pri);
168 unsigned fac = RPMLOG_FAC(code);
171 int msgnb = BUFSIZ, nb;
172 int cbrc = RPMLOG_DEFAULT;
174 struct rpmlogRec_s rec;
176 if ((mask & rpmlogMask) == 0)
179 msgbuf = xmalloc(msgnb);
182 /* Allocate a sufficently large buffer for output. */
186 nb = vsnprintf(msgbuf, msgnb, fmt, apc);
187 if (nb > -1 && nb < msgnb)
189 if (nb > -1) /* glibc 2.1 (and later) */
193 msgbuf = xrealloc(msgbuf, msgnb);
196 msgbuf[msgnb - 1] = '\0';
203 /* Save copy of all messages at warning (or below == "more important"). */
204 if (pri <= RPMLOG_WARNING) {
207 recs = xmalloc((nrecs+2) * sizeof(*recs));
209 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
210 recs[nrecs].code = rec.code;
211 recs[nrecs].pri = rec.pri;
212 recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
213 msgbuf = NULL; /* XXX don't free at exit. */
214 recs[nrecs+1].code = 0;
215 recs[nrecs+1].message = NULL;
219 if (_rpmlogCallback) {
220 cbrc = _rpmlogCallback(&rec, _rpmlogCallbackData);
221 needexit += cbrc & RPMLOG_EXIT;
224 if (cbrc & RPMLOG_DEFAULT) {
225 cbrc = rpmlogDefault(&rec);
226 needexit += cbrc & RPMLOG_EXIT;
229 msgbuf = _free(msgbuf);
234 void rpmlog (int code, const char *fmt, ...)
240 vrpmlog(code, fmt, ap);