- fix typos in linux.{req,prov}.
[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 /*@access rpmlogRec @*/
11
12 static int nrecs = 0;
13 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
14
15 /**
16  * Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
17  * @param p             memory to free
18  * @retval              NULL always
19  */
20 /*@unused@*/ static inline /*@null@*/ void *
21 _free(/*@only@*/ /*@null@*/ const void * p) /*@modifies p@*/
22 {
23     if (p != NULL)      free((void *)p);
24     return NULL;
25 }
26
27 int rpmlogGetNrecs(void)
28 {
29     return nrecs;
30 }
31
32 int rpmlogCode(void)
33 {
34     if (recs != NULL && nrecs > 0)
35         return recs[nrecs-1].code;
36     return -1;
37 }
38
39
40 const char * rpmlogMessage(void)
41 {
42     if (recs != NULL && nrecs > 0)
43         return recs[nrecs-1].message;
44     return _("(no error)");
45 }
46
47 void rpmlogPrint(FILE *f)
48 {
49     int i;
50
51     if (f == NULL)
52         f = stderr;
53
54     if (recs)
55     for (i = 0; i < nrecs; i++) {
56         rpmlogRec rec = recs + i;
57         if (rec->message && *rec->message)
58             fprintf(f, "    %s", rec->message);
59     }
60 }
61
62 void rpmlogClose (void)
63 {
64     int i;
65
66     if (recs)
67     for (i = 0; i < nrecs; i++) {
68         rpmlogRec rec = recs + i;
69         rec->message = _free(rec->message);
70     }
71     recs = _free(recs);
72     nrecs = 0;
73 }
74
75 void rpmlogOpen (/*@unused@*/ const char *ident, /*@unused@*/ int option,
76                 /*@unused@*/ int facility)
77 {
78 }
79
80 static int rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
81 static /*@unused@*/ int rpmlogFacility = RPMLOG_USER;
82
83 int rpmlogSetMask (int mask)
84 {
85     int omask = rpmlogMask;
86     if (mask)
87         rpmlogMask = mask;
88     return omask;
89 }
90
91 static /*@null@*/ rpmlogCallback _rpmlogCallback = NULL;
92
93 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
94 {
95     rpmlogCallback ocb = _rpmlogCallback;
96     _rpmlogCallback = cb;
97     return ocb;
98 }
99
100 /*@-readonlytrans@*/    /* FIX: double indeirection. */
101 /*@observer@*/ static char *rpmlogMsgPrefix[] = {
102     N_("fatal error: "),/*!< RPMLOG_EMERG */
103     N_("fatal error: "),/*!< RPMLOG_ALERT */
104     N_("fatal error: "),/*!< RPMLOG_CRIT */
105     N_("error: "),      /*!< RPMLOG_ERR */
106     N_("warning: "),    /*!< RPMLOG_WARNING */
107     "",                 /*!< RPMLOG_NOTICE */
108     "",                 /*!< RPMLOG_INFO */
109     "D: ",              /*!< RPMLOG_DEBUG */
110 };
111 /*@=readonlytrans@*/
112
113 #if !defined(HAVE_VSNPRINTF)
114 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
115         const char * fmt, va_list ap)
116 {
117     return vsprintf(buf, fmt, ap);
118 }
119 #endif
120
121 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
122         /*@modifies internalState @*/
123 {
124     int pri = RPMLOG_PRI(code);
125     int mask = RPMLOG_MASK(pri);
126     /*@unused@*/ int fac = RPMLOG_FAC(code);
127     char *msgbuf, *msg;
128     int msgnb = BUFSIZ, nb;
129     FILE * msgout = stderr;
130
131     if ((mask & rpmlogMask) == 0)
132         return;
133
134     msgbuf = xmalloc(msgnb);
135     *msgbuf = '\0';
136
137     /* Allocate a sufficently large buffer for output. */
138     while (1) {
139         va_list apc;
140         /*@-sysunrecog -usedef@*/ __va_copy(apc, ap); /*@=sysunrecog =usedef@*/
141         /*@-unrecog@*/ nb = vsnprintf(msgbuf, msgnb, fmt, apc); /*@=unrecog@*/
142         if (nb > -1 && nb < msgnb)
143             break;
144         if (nb > -1)            /* glibc 2.1 */
145             msgnb = nb+1;
146         else                    /* glibc 2.0 */
147             msgnb *= 2;
148         msgbuf = xrealloc(msgbuf, msgnb);
149     }
150     msgbuf[msgnb - 1] = '\0';
151     msg = msgbuf;
152
153     /* Save copy of all messages at warning (or below == "more important"). */
154     if (pri <= RPMLOG_WARNING) {
155
156         if (recs == NULL)
157             recs = xmalloc((nrecs+2) * sizeof(*recs));
158         else
159             recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
160         recs[nrecs].code = code;
161         recs[nrecs].message = msg = msgbuf;
162         msgbuf = NULL;
163         recs[nrecs+1].code = 0;
164         recs[nrecs+1].message = NULL;
165         ++nrecs;
166
167
168         if (_rpmlogCallback) {
169             _rpmlogCallback();
170             return;     /* XXX Preserve legacy rpmError behavior. */
171         }
172     }
173
174     /* rpmMessage behavior */
175
176     switch (pri) {
177     case RPMLOG_INFO:
178     case RPMLOG_NOTICE:
179         msgout = stdout;
180         break;
181
182     case RPMLOG_EMERG:
183     case RPMLOG_ALERT:
184     case RPMLOG_CRIT:
185     case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
186     case RPMLOG_WARNING:
187     case RPMLOG_DEBUG:
188         break;
189     }
190
191     if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
192         (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
193
194     (void) fputs(msg, msgout);
195     (void) fflush(msgout);
196     msgbuf = _free(msgbuf);
197     if (pri <= RPMLOG_CRIT)
198         exit(EXIT_FAILURE);
199 }
200
201 void rpmlog (int code, const char *fmt, ...)
202 {
203     va_list ap;
204
205     va_start(ap, fmt);
206     vrpmlog(code, fmt, ap);
207     va_end(ap);
208 }
209
210 int rpmErrorCode(void)
211 {
212     return rpmlogCode();
213 }
214
215 const char * rpmErrorString(void)
216 {
217     return rpmlogMessage();
218 }
219
220 rpmlogCallback rpmErrorSetCallback(rpmlogCallback cb)
221 {
222     return rpmlogSetCallback(cb);
223 }