12 # define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
14 # ifdef HAVE_VA_LIST_AS_ARRAY
15 # define va_copy(DEST,SRC) (*(DEST) = *(SRC))
17 # define va_copy(DEST,SRC) ((DEST) = (SRC))
22 /*@access rpmlogRec @*/
27 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
30 * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
31 * @param p memory to free
34 /*@unused@*/ static inline /*@null@*/ void *
35 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p) /*@modifies p@*/
37 if (p != NULL) free((void *)p);
41 int rpmlogGetNrecs(void)
48 if (recs != NULL && nrecs > 0)
49 return recs[nrecs-1].code;
54 const char * rpmlogMessage(void)
56 if (recs != NULL && nrecs > 0)
57 return recs[nrecs-1].message;
58 return _("(no error)");
62 void rpmlogPrint(FILE *f)
70 for (i = 0; i < nrecs; i++) {
71 rpmlogRec rec = recs + i;
72 if (rec->message && *rec->message)
73 fprintf(f, " %s", rec->message);
78 void rpmlogClose (void)
83 for (i = 0; i < nrecs; i++) {
84 rpmlogRec rec = recs + i;
85 rec->message = _free(rec->message);
91 void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
92 /*@unused@*/ int facility)
97 static int rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
99 static /*@unused@*/ int rpmlogFacility = RPMLOG_USER;
101 int rpmlogSetMask (int mask)
103 int omask = rpmlogMask;
110 static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
112 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
114 rpmlogCallback ocb = _rpmlogCallback;
115 _rpmlogCallback = cb;
119 /*@-readonlytrans@*/ /* FIX: double indirection. */
120 /*@observer@*/ /*@unchecked@*/
121 static char *rpmlogMsgPrefix[] = {
122 N_("fatal error: "),/*!< RPMLOG_EMERG */
123 N_("fatal error: "),/*!< RPMLOG_ALERT */
124 N_("fatal error: "),/*!< RPMLOG_CRIT */
125 N_("error: "), /*!< RPMLOG_ERR */
126 N_("warning: "), /*!< RPMLOG_WARNING */
127 "", /*!< RPMLOG_NOTICE */
128 "", /*!< RPMLOG_INFO */
129 "D: ", /*!< RPMLOG_DEBUG */
133 #if !defined(HAVE_VSNPRINTF)
134 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
135 const char * fmt, va_list ap)
137 return vsprintf(buf, fmt, ap);
142 /*@-compmempass@*/ /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
143 /*@-nullstate@*/ /* FIX: rpmlogMsgPrefix[] may be NULL */
144 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
145 /*@globals internalState @*/
146 /*@modifies internalState @*/
148 int pri = RPMLOG_PRI(code);
149 int mask = RPMLOG_MASK(pri);
150 /*@unused@*/ int fac = RPMLOG_FAC(code);
152 int msgnb = BUFSIZ, nb;
153 FILE * msgout = stderr;
155 if ((mask & rpmlogMask) == 0)
158 msgbuf = xmalloc(msgnb);
161 /* Allocate a sufficently large buffer for output. */
164 /*@-sysunrecog -usedef@*/ va_copy(apc, ap); /*@=sysunrecog =usedef@*/
165 nb = vsnprintf(msgbuf, msgnb, fmt, apc);
166 if (nb > -1 && nb < msgnb)
168 if (nb > -1) /* glibc 2.1 (and later) */
172 msgbuf = xrealloc(msgbuf, msgnb);
174 msgbuf[msgnb - 1] = '\0';
177 /* Save copy of all messages at warning (or below == "more important"). */
179 if (pri <= RPMLOG_WARNING) {
182 recs = xmalloc((nrecs+2) * sizeof(*recs));
184 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
185 recs[nrecs].code = code;
186 recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
187 msgbuf = NULL; /* XXX don't free at exit. */
188 recs[nrecs+1].code = 0;
189 recs[nrecs+1].message = NULL;
192 if (_rpmlogCallback) {
193 /*@-noeffectuncon@*/ /* FIX: useless callback */
196 return; /* XXX Preserve legacy rpmError behavior. */
201 /* rpmMessage behavior */
212 case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
218 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
219 (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
221 (void) fputs(msg, msgout);
222 (void) fflush(msgout);
223 msgbuf = _free(msgbuf);
224 if (pri <= RPMLOG_CRIT)
227 /*@=compmempass =nullstate@*/
230 void rpmlog (int code, const char *fmt, ...)
235 /*@-internalglobs@*/ /* FIX: shrug */
236 vrpmlog(code, fmt, ap);
241 int rpmErrorCode(void)
246 const char * rpmErrorString(void)
248 return rpmlogMessage();
251 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
253 return rpmlogSetCallback(cb);