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 @*/
25 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
28 * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
29 * @param p memory to free
32 /*@unused@*/ static inline /*@null@*/ void *
33 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p) /*@modifies p@*/
35 if (p != NULL) free((void *)p);
39 int rpmlogGetNrecs(void)
46 if (recs != NULL && nrecs > 0)
47 return recs[nrecs-1].code;
52 const char * rpmlogMessage(void)
54 if (recs != NULL && nrecs > 0)
55 return recs[nrecs-1].message;
56 return _("(no error)");
59 void rpmlogPrint(FILE *f)
67 for (i = 0; i < nrecs; i++) {
68 rpmlogRec rec = recs + i;
69 if (rec->message && *rec->message)
70 fprintf(f, " %s", rec->message);
74 void rpmlogClose (void)
79 for (i = 0; i < nrecs; i++) {
80 rpmlogRec rec = recs + i;
81 rec->message = _free(rec->message);
87 void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
88 /*@unused@*/ int facility)
92 static int rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
93 static /*@unused@*/ int rpmlogFacility = RPMLOG_USER;
95 int rpmlogSetMask (int mask)
97 int omask = rpmlogMask;
103 static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
105 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
107 rpmlogCallback ocb = _rpmlogCallback;
108 _rpmlogCallback = cb;
112 /*@-readonlytrans@*/ /* FIX: double indirection. */
113 /*@observer@*/ static char *rpmlogMsgPrefix[] = {
114 N_("fatal error: "),/*!< RPMLOG_EMERG */
115 N_("fatal error: "),/*!< RPMLOG_ALERT */
116 N_("fatal error: "),/*!< RPMLOG_CRIT */
117 N_("error: "), /*!< RPMLOG_ERR */
118 N_("warning: "), /*!< RPMLOG_WARNING */
119 "", /*!< RPMLOG_NOTICE */
120 "", /*!< RPMLOG_INFO */
121 "D: ", /*!< RPMLOG_DEBUG */
125 #if !defined(HAVE_VSNPRINTF)
126 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
127 const char * fmt, va_list ap)
129 return vsprintf(buf, fmt, ap);
133 /*@-compmempass@*/ /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
134 /*@-nullstate@*/ /* FIX: rpmlogMsgPrefix[] may be NULL */
135 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
136 /*@modifies internalState @*/
138 int pri = RPMLOG_PRI(code);
139 int mask = RPMLOG_MASK(pri);
140 /*@unused@*/ int fac = RPMLOG_FAC(code);
142 int msgnb = BUFSIZ, nb;
143 FILE * msgout = stderr;
145 if ((mask & rpmlogMask) == 0)
148 msgbuf = xmalloc(msgnb);
151 /* Allocate a sufficently large buffer for output. */
154 /*@-sysunrecog -usedef@*/ va_copy(apc, ap); /*@=sysunrecog =usedef@*/
155 nb = vsnprintf(msgbuf, msgnb, fmt, apc);
156 if (nb > -1 && nb < msgnb)
158 if (nb > -1) /* glibc 2.1 (and later) */
162 msgbuf = xrealloc(msgbuf, msgnb);
164 msgbuf[msgnb - 1] = '\0';
167 /* Save copy of all messages at warning (or below == "more important"). */
168 if (pri <= RPMLOG_WARNING) {
171 recs = xmalloc((nrecs+2) * sizeof(*recs));
173 recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
174 recs[nrecs].code = code;
175 recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
176 msgbuf = NULL; /* XXX don't free at exit. */
177 recs[nrecs+1].code = 0;
178 recs[nrecs+1].message = NULL;
181 if (_rpmlogCallback) {
183 return; /* XXX Preserve legacy rpmError behavior. */
187 /* rpmMessage behavior */
198 case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
204 if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
205 (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
207 (void) fputs(msg, msgout);
208 (void) fflush(msgout);
209 msgbuf = _free(msgbuf);
210 if (pri <= RPMLOG_CRIT)
213 /*@=compmempass =nullstate@*/
215 void rpmlog (int code, const char *fmt, ...)
220 vrpmlog(code, fmt, ap);
224 int rpmErrorCode(void)
229 const char * rpmErrorString(void)
231 return rpmlogMessage();
234 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
236 return rpmlogSetCallback(cb);