lclint fiddles to annotate globals.
[platform/upstream/rpm.git] / rpmio / rpmlog.c
1 /** \ingroup rpmio
2  * \file rpmio/rpmlog.c
3  */
4
5 #include "system.h"
6 #include <stdarg.h>
7 #include "rpmlog.h"
8 #include "debug.h"
9
10 #ifndef va_copy
11 # ifdef __va_copy
12 #  define va_copy(DEST,SRC) __va_copy((DEST),(SRC))
13 # else
14 #  ifdef HAVE_VA_LIST_AS_ARRAY
15 #   define va_copy(DEST,SRC) (*(DEST) = *(SRC))
16 #  else
17 #   define va_copy(DEST,SRC) ((DEST) = (SRC))
18 #  endif
19 # endif
20 #endif
21
22 /*@access rpmlogRec @*/
23
24 /*@unchecked@*/
25 static int nrecs = 0;
26 /*@unchecked@*/
27 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
28
29 /**
30  * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
31  * @param p             memory to free
32  * @retval              NULL always
33  */
34 /*@unused@*/ static inline /*@null@*/ void *
35 _free(/*@only@*/ /*@null@*/ /*@out@*/ const void * p) /*@modifies p@*/
36 {
37     if (p != NULL)      free((void *)p);
38     return NULL;
39 }
40
41 int rpmlogGetNrecs(void)
42 {
43     return nrecs;
44 }
45
46 int rpmlogCode(void)
47 {
48     if (recs != NULL && nrecs > 0)
49         return recs[nrecs-1].code;
50     return -1;
51 }
52
53
54 const char * rpmlogMessage(void)
55 {
56     if (recs != NULL && nrecs > 0)
57         return recs[nrecs-1].message;
58     return _("(no error)");
59 }
60
61 /*@-modfilesys@*/
62 void rpmlogPrint(FILE *f)
63 {
64     int i;
65
66     if (f == NULL)
67         f = stderr;
68
69     if (recs)
70     for (i = 0; i < nrecs; i++) {
71         rpmlogRec rec = recs + i;
72         if (rec->message && *rec->message)
73             fprintf(f, "    %s", rec->message);
74     }
75 }
76 /*@=modfilesys@*/
77
78 void rpmlogClose (void)
79 {
80     int i;
81
82     if (recs)
83     for (i = 0; i < nrecs; i++) {
84         rpmlogRec rec = recs + i;
85         rec->message = _free(rec->message);
86     }
87     recs = _free(recs);
88     nrecs = 0;
89 }
90
91 void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
92                 /*@unused@*/ int facility)
93 {
94 }
95
96 /*@unchecked@*/
97 static int rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
98 /*@unchecked@*/
99 static /*@unused@*/ int rpmlogFacility = RPMLOG_USER;
100
101 int rpmlogSetMask (int mask)
102 {
103     int omask = rpmlogMask;
104     if (mask)
105         rpmlogMask = mask;
106     return omask;
107 }
108
109 /*@unchecked@*/
110 static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
111
112 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
113 {
114     rpmlogCallback ocb = _rpmlogCallback;
115     _rpmlogCallback = cb;
116     return ocb;
117 }
118
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 */
130 };
131 /*@=readonlytrans@*/
132
133 #if !defined(HAVE_VSNPRINTF)
134 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
135         const char * fmt, va_list ap)
136 {
137     return vsprintf(buf, fmt, ap);
138 }
139 #endif
140
141 /*@-modfilesys@*/
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         /*@modifies internalState @*/
146 {
147     int pri = RPMLOG_PRI(code);
148     int mask = RPMLOG_MASK(pri);
149     /*@unused@*/ int fac = RPMLOG_FAC(code);
150     char *msgbuf, *msg;
151     int msgnb = BUFSIZ, nb;
152     FILE * msgout = stderr;
153
154     if ((mask & rpmlogMask) == 0)
155         return;
156
157     msgbuf = xmalloc(msgnb);
158     *msgbuf = '\0';
159
160     /* Allocate a sufficently large buffer for output. */
161     while (1) {
162         va_list apc;
163         /*@-sysunrecog -usedef@*/ va_copy(apc, ap); /*@=sysunrecog =usedef@*/
164         nb = vsnprintf(msgbuf, msgnb, fmt, apc);
165         if (nb > -1 && nb < msgnb)
166             break;
167         if (nb > -1)            /* glibc 2.1 (and later) */
168             msgnb = nb+1;
169         else                    /* glibc 2.0 */
170             msgnb *= 2;
171         msgbuf = xrealloc(msgbuf, msgnb);
172     }
173     msgbuf[msgnb - 1] = '\0';
174     msg = msgbuf;
175
176     /* Save copy of all messages at warning (or below == "more important"). */
177     if (pri <= RPMLOG_WARNING) {
178
179         if (recs == NULL)
180             recs = xmalloc((nrecs+2) * sizeof(*recs));
181         else
182             recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
183         recs[nrecs].code = code;
184         recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
185         msgbuf = NULL;          /* XXX don't free at exit. */
186         recs[nrecs+1].code = 0;
187         recs[nrecs+1].message = NULL;
188         ++nrecs;
189
190         if (_rpmlogCallback) {
191             _rpmlogCallback();
192             return;     /* XXX Preserve legacy rpmError behavior. */
193         }
194     }
195
196     /* rpmMessage behavior */
197
198     switch (pri) {
199     case RPMLOG_INFO:
200     case RPMLOG_NOTICE:
201         msgout = stdout;
202         break;
203
204     case RPMLOG_EMERG:
205     case RPMLOG_ALERT:
206     case RPMLOG_CRIT:
207     case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
208     case RPMLOG_WARNING:
209     case RPMLOG_DEBUG:
210         break;
211     }
212
213     if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
214         (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
215
216     (void) fputs(msg, msgout);
217     (void) fflush(msgout);
218     msgbuf = _free(msgbuf);
219     if (pri <= RPMLOG_CRIT)
220         exit(EXIT_FAILURE);
221 }
222 /*@=compmempass =nullstate@*/
223 /*@=modfilesys@*/
224
225 void rpmlog (int code, const char *fmt, ...)
226 {
227     va_list ap;
228
229     va_start(ap, fmt);
230     /*@-internalglobs@*/ /* FIX: shrug */
231     vrpmlog(code, fmt, ap);
232     /*@=internalglobs@*/
233     va_end(ap);
234 }
235
236 int rpmErrorCode(void)
237 {
238     return rpmlogCode();
239 }
240
241 const char * rpmErrorString(void)
242 {
243     return rpmlogMessage();
244 }
245
246 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
247 {
248     return rpmlogSetCallback(cb);
249 }