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