Explicit branchstate annotations.
[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         /*@globals internalState @*/
146         /*@modifies internalState @*/
147 {
148     int pri = RPMLOG_PRI(code);
149     int mask = RPMLOG_MASK(pri);
150     /*@unused@*/ int fac = RPMLOG_FAC(code);
151     char *msgbuf, *msg;
152     int msgnb = BUFSIZ, nb;
153     FILE * msgout = stderr;
154
155     if ((mask & rpmlogMask) == 0)
156         return;
157
158     msgbuf = xmalloc(msgnb);
159     *msgbuf = '\0';
160
161     /* Allocate a sufficently large buffer for output. */
162     while (1) {
163         va_list apc;
164         /*@-sysunrecog -usedef@*/ va_copy(apc, ap); /*@=sysunrecog =usedef@*/
165         nb = vsnprintf(msgbuf, msgnb, fmt, apc);
166         if (nb > -1 && nb < msgnb)
167             break;
168         if (nb > -1)            /* glibc 2.1 (and later) */
169             msgnb = nb+1;
170         else                    /* glibc 2.0 */
171             msgnb *= 2;
172         msgbuf = xrealloc(msgbuf, msgnb);
173     }
174     msgbuf[msgnb - 1] = '\0';
175     msg = msgbuf;
176
177     /* Save copy of all messages at warning (or below == "more important"). */
178     /*@-branchstate@*/
179     if (pri <= RPMLOG_WARNING) {
180
181         if (recs == NULL)
182             recs = xmalloc((nrecs+2) * sizeof(*recs));
183         else
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;
190         ++nrecs;
191
192         if (_rpmlogCallback) {
193             _rpmlogCallback();
194             return;     /* XXX Preserve legacy rpmError behavior. */
195         }
196     }
197     /*@=branchstate@*/
198
199     /* rpmMessage behavior */
200
201     switch (pri) {
202     case RPMLOG_INFO:
203     case RPMLOG_NOTICE:
204         msgout = stdout;
205         break;
206
207     case RPMLOG_EMERG:
208     case RPMLOG_ALERT:
209     case RPMLOG_CRIT:
210     case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
211     case RPMLOG_WARNING:
212     case RPMLOG_DEBUG:
213         break;
214     }
215
216     if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
217         (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
218
219     (void) fputs(msg, msgout);
220     (void) fflush(msgout);
221     msgbuf = _free(msgbuf);
222     if (pri <= RPMLOG_CRIT)
223         exit(EXIT_FAILURE);
224 }
225 /*@=compmempass =nullstate@*/
226 /*@=modfilesys@*/
227
228 void rpmlog (int code, const char *fmt, ...)
229 {
230     va_list ap;
231
232     va_start(ap, fmt);
233     /*@-internalglobs@*/ /* FIX: shrug */
234     vrpmlog(code, fmt, ap);
235     /*@=internalglobs@*/
236     va_end(ap);
237 }
238
239 int rpmErrorCode(void)
240 {
241     return rpmlogCode();
242 }
243
244 const char * rpmErrorString(void)
245 {
246     return rpmlogMessage();
247 }
248
249 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
250 {
251     return rpmlogSetCallback(cb);
252 }