13 # define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
15 # ifdef HAVE_VA_LIST_AS_ARRAY
16 # define va_copy(DEST,SRC) (*(DEST) = *(SRC))
18 # define va_copy(DEST,SRC) ((DEST) = (SRC))
23 /*@access rpmlogRec @*/
28 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
31 * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
32 * @param p memory to free
35 /*@unused@*/ static inline /*@null@*/ void *
36 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p) /*@modifies p@*/
38 if (p != NULL) free((void *)p);
42 int rpmlogGetNrecs(void)
49 if (recs != NULL && nrecs > 0)
50 return recs[nrecs-1].code;
55 const char * rpmlogMessage(void)
57 if (recs != NULL && nrecs > 0)
58 return recs[nrecs-1].message;
59 return _("(no error)");
63 void rpmlogPrint(FILE *f)
71 for (i = 0; i < nrecs; i++) {
72 rpmlogRec rec = recs + i;
73 if (rec->message && *rec->message)
74 fprintf(f, " %s", rec->message);
79 void rpmlogClose (void)
80 /*@globals recs, nrecs @*/
81 /*@modifies recs, nrecs @*/
86 for (i = 0; i < nrecs; i++) {
87 rpmlogRec rec = recs + i;
88 rec->message = _free(rec->message);
94 void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
95 /*@unused@*/ int facility)
100 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
103 static /*@unused@*/ unsigned rpmlogFacility = RPMLOG_USER;
105 int rpmlogSetMask (int mask)
106 /*@globals rpmlogMask @*/
107 /*@modifies rpmlogMask @*/
109 int omask = rpmlogMask;
116 static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
118 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
119 /*@globals _rpmlogCallback @*/
120 /*@modifies _rpmlogCallback @*/
122 rpmlogCallback ocb = _rpmlogCallback;
123 _rpmlogCallback = cb;
127 /*@-readonlytrans@*/ /* FIX: double indirection. */
128 /*@observer@*/ /*@unchecked@*/
129 static char *rpmlogMsgPrefix[] = {
130 N_("fatal error: "),/*!< RPMLOG_EMERG */
131 N_("fatal error: "),/*!< RPMLOG_ALERT */
132 N_("fatal error: "),/*!< RPMLOG_CRIT */
133 N_("error: "), /*!< RPMLOG_ERR */
134 N_("warning: "), /*!< RPMLOG_WARNING */
135 "", /*!< RPMLOG_NOTICE */
136 "", /*!< RPMLOG_INFO */
137 "D: ", /*!< RPMLOG_DEBUG */
141 #if !defined(HAVE_VSNPRINTF)
142 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
143 const char * fmt, va_list ap)
145 return vsprintf(buf, fmt, ap);
150 /*@-compmempass@*/ /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
151 /*@-nullstate@*/ /* FIX: rpmlogMsgPrefix[] may be NULL */
152 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
153 /*@globals nrecs, recs, internalState @*/
154 /*@modifies nrecs, recs, internalState @*/
156 unsigned pri = RPMLOG_PRI(code);
157 unsigned mask = RPMLOG_MASK(pri);
158 /*@unused@*/ unsigned fac = RPMLOG_FAC(code);
160 int msgnb = BUFSIZ, nb;
161 FILE * msgout = stderr;
163 if ((mask & rpmlogMask) == 0)
167 msgbuf = xmalloc(msgnb);
170 /* Allocate a sufficently large buffer for output. */
173 /*@-sysunrecog -usedef@*/ va_copy(apc, ap); /*@=sysunrecog =usedef@*/
174 nb = vsnprintf(msgbuf, msgnb, fmt, apc);
175 if (nb > -1 && nb < msgnb)
177 if (nb > -1) /* glibc 2.1 (and later) */
181 msgbuf = xrealloc(msgbuf, msgnb);
184 msgbuf[msgnb - 1] = '\0';
187 /* Save copy of all messages at warning (or below == "more important"). */
189 if (pri <= RPMLOG_WARNING) {
192 recs = xmalloc((nrecs+2) * sizeof(*recs));
194 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
195 recs[nrecs].code = code;
196 recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
197 msgbuf = NULL; /* XXX don't free at exit. */
198 recs[nrecs+1].code = 0;
199 recs[nrecs+1].message = NULL;
202 if (_rpmlogCallback) {
203 /*@-noeffectuncon@*/ /* FIX: useless callback */
206 return; /* XXX Preserve legacy rpmError behavior. */
212 /* rpmMessage behavior */
223 case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
229 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
230 (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
232 (void) fputs(msg, msgout);
233 (void) fflush(msgout);
234 msgbuf = _free(msgbuf);
235 if (pri <= RPMLOG_CRIT)
238 /*@=compmempass =nullstate@*/
241 void rpmlog (int code, const char *fmt, ...)
246 /*@-internalglobs@*/ /* FIX: shrug */
247 vrpmlog(code, fmt, ap);
252 int rpmErrorCode(void)
257 const char * rpmErrorString(void)
259 return rpmlogMessage();
262 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
264 return rpmlogSetCallback(cb);