8 #include <rpm/rpmlog.h>
12 static rpmlogRec recs = NULL;
15 int code; /* unused */
16 rpmlogLvl pri; /* priority */
17 char * message; /* log message string */
20 int rpmlogGetNrecs(void)
27 if (recs != NULL && nrecs > 0)
28 return recs[nrecs-1].code;
33 const char * rpmlogMessage(void)
35 if (recs != NULL && nrecs > 0)
36 return recs[nrecs-1].message;
37 return _("(no error)");
40 const char * rpmlogRecMessage(rpmlogRec rec)
43 return (rec->message);
46 rpmlogLvl rpmlogRecPriority(rpmlogRec rec)
52 void rpmlogPrint(FILE *f)
60 for (i = 0; i < nrecs; i++) {
61 rpmlogRec rec = recs + i;
62 if (rec->message && *rec->message)
63 fprintf(f, " %s", rec->message);
67 void rpmlogClose (void)
72 for (i = 0; i < nrecs; i++) {
73 rpmlogRec rec = recs + i;
74 rec->message = _free(rec->message);
80 void rpmlogOpen (const char *ident, int option,
85 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
88 static unsigned rpmlogFacility = RPMLOG_USER;
91 int rpmlogSetMask (int mask)
93 int omask = rpmlogMask;
99 static rpmlogCallback _rpmlogCallback = NULL;
100 static rpmlogCallbackData _rpmlogCallbackData = NULL;
102 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
104 rpmlogCallback ocb = _rpmlogCallback;
105 _rpmlogCallback = cb;
106 _rpmlogCallbackData = data;
110 static FILE * _stdlog = NULL;
112 static int rpmlogDefault(rpmlogRec rec)
114 FILE *msgout = (_stdlog ? _stdlog : stderr);
119 msgout = (_stdlog ? _stdlog : stdout);
131 (void) fputs(rpmlogLevelPrefix(rec->pri), msgout);
133 (void) fputs(rec->message, msgout);
134 (void) fflush(msgout);
136 return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
140 FILE * rpmlogSetFile(FILE * fp)
142 FILE * ofp = _stdlog;
147 static const char * const rpmlogMsgPrefix[] = {
148 N_("fatal error: "),/*!< RPMLOG_EMERG */
149 N_("fatal error: "),/*!< RPMLOG_ALERT */
150 N_("fatal error: "),/*!< RPMLOG_CRIT */
151 N_("error: "), /*!< RPMLOG_ERR */
152 N_("warning: "), /*!< RPMLOG_WARNING */
153 "", /*!< RPMLOG_NOTICE */
154 "", /*!< RPMLOG_INFO */
155 "D: ", /*!< RPMLOG_DEBUG */
158 const char * rpmlogLevelPrefix(rpmlogLvl pri)
160 const char * prefix = "";
161 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
162 prefix = _(rpmlogMsgPrefix[pri]);
166 /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
167 /* FIX: rpmlogMsgPrefix[] may be NULL */
168 static void dolog (struct rpmlogRec_s *rec)
170 int cbrc = RPMLOG_DEFAULT;
173 /* Save copy of all messages at warning (or below == "more important"). */
174 if (rec->pri <= RPMLOG_WARNING) {
175 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
176 recs[nrecs].code = rec->code;
177 recs[nrecs].pri = rec->pri;
178 recs[nrecs].message = xstrdup(rec->message);
179 recs[nrecs+1].code = 0;
180 recs[nrecs+1].message = NULL;
184 if (_rpmlogCallback) {
185 cbrc = _rpmlogCallback(rec, _rpmlogCallbackData);
186 needexit += cbrc & RPMLOG_EXIT;
189 if (cbrc & RPMLOG_DEFAULT) {
190 cbrc = rpmlogDefault(rec);
191 needexit += cbrc & RPMLOG_EXIT;
198 void rpmlog (int code, const char *fmt, ...)
200 unsigned pri = RPMLOG_PRI(code);
201 unsigned mask = RPMLOG_MASK(pri);
205 if ((mask & rpmlogMask) == 0)
209 n = vsnprintf(NULL, 0, fmt, ap);
213 struct rpmlogRec_s rec;
215 char *msg = xmalloc(nb);
218 n = vsnprintf(msg, nb, fmt, ap);