Remove bunch of deprecated and unused rpmlog functions
[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
23 static int nrecs = 0;
24 static rpmlogRec recs = NULL;
25
26 int rpmlogGetNrecs(void)
27 {
28     return nrecs;
29 }
30
31 int rpmlogCode(void)
32 {
33     if (recs != NULL && nrecs > 0)
34         return recs[nrecs-1].code;
35     return -1;
36 }
37
38
39 const char * rpmlogMessage(void)
40 {
41     if (recs != NULL && nrecs > 0)
42         return recs[nrecs-1].message;
43     return _("(no error)");
44 }
45
46 void rpmlogPrint(FILE *f)
47 {
48     int i;
49
50     if (f == NULL)
51         f = stderr;
52
53     if (recs)
54     for (i = 0; i < nrecs; i++) {
55         rpmlogRec rec = recs + i;
56         if (rec->message && *rec->message)
57             fprintf(f, "    %s", rec->message);
58     }
59 }
60
61 void rpmlogClose (void)
62 {
63     int i;
64
65     if (recs)
66     for (i = 0; i < nrecs; i++) {
67         rpmlogRec rec = recs + i;
68         rec->message = _free(rec->message);
69     }
70     recs = _free(recs);
71     nrecs = 0;
72 }
73
74 void rpmlogOpen (const char *ident, int option,
75                 int facility)
76 {
77 }
78
79 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
80
81 #ifdef NOTYET
82 static unsigned rpmlogFacility = RPMLOG_USER;
83 #endif
84
85 int rpmlogSetMask (int mask)
86 {
87     int omask = rpmlogMask;
88     if (mask)
89         rpmlogMask = mask;
90     return omask;
91 }
92
93 static rpmlogCallback _rpmlogCallback = NULL;
94
95 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb)
96 {
97     rpmlogCallback ocb = _rpmlogCallback;
98     _rpmlogCallback = cb;
99     return ocb;
100 }
101
102 static FILE * _stdlog = NULL;
103
104 FILE * rpmlogSetFile(FILE * fp)
105 {
106     FILE * ofp = _stdlog;
107     _stdlog = fp;
108     return ofp;
109 }
110
111 static char *rpmlogMsgPrefix[] = {
112     N_("fatal error: "),/*!< RPMLOG_EMERG */
113     N_("fatal error: "),/*!< RPMLOG_ALERT */
114     N_("fatal error: "),/*!< RPMLOG_CRIT */
115     N_("error: "),      /*!< RPMLOG_ERR */
116     N_("warning: "),    /*!< RPMLOG_WARNING */
117     "",                 /*!< RPMLOG_NOTICE */
118     "",                 /*!< RPMLOG_INFO */
119     "D: ",              /*!< RPMLOG_DEBUG */
120 };
121
122 #if !defined(HAVE_VSNPRINTF)
123 static inline int vsnprintf(char * buf, int nb,
124         const char * fmt, va_list ap)
125 {
126     return vsprintf(buf, fmt, ap);
127 }
128 #endif
129
130 /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
131 /* FIX: rpmlogMsgPrefix[] may be NULL */
132 static void vrpmlog (unsigned code, const char *fmt, va_list ap)
133 {
134     unsigned pri = RPMLOG_PRI(code);
135     unsigned mask = RPMLOG_MASK(pri);
136 #ifdef NOTYET
137     unsigned fac = RPMLOG_FAC(code);
138 #endif
139     char *msgbuf, *msg;
140     int msgnb = BUFSIZ, nb;
141     FILE * msgout = (_stdlog ? _stdlog : 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         va_copy(apc, ap);
153         nb = vsnprintf(msgbuf, msgnb, fmt, apc);
154         if (nb > -1 && nb < msgnb)
155             break;
156         if (nb > -1)            /* glibc 2.1 (and later) */
157             msgnb = nb+1;
158         else                    /* glibc 2.0 */
159             msgnb *= 2;
160         msgbuf = xrealloc(msgbuf, msgnb);
161         va_end(apc);
162     }
163     msgbuf[msgnb - 1] = '\0';
164     msg = msgbuf;
165
166     /* Save copy of all messages at warning (or below == "more important"). */
167     if (pri <= RPMLOG_WARNING) {
168
169         if (recs == NULL)
170             recs = xmalloc((nrecs+2) * sizeof(*recs));
171         else
172             recs = xrealloc(recs, (nrecs+2) * sizeof(*recs));
173         recs[nrecs].code = code;
174         recs[nrecs].message = msg = xrealloc(msgbuf, strlen(msgbuf)+1);
175         msgbuf = NULL;          /* XXX don't free at exit. */
176         recs[nrecs+1].code = 0;
177         recs[nrecs+1].message = NULL;
178         ++nrecs;
179
180         if (_rpmlogCallback) {
181             /* FIX: useless callback */
182             _rpmlogCallback();
183             return;     /* XXX Preserve legacy rpmError behavior. */
184         }
185     }
186
187     /* rpmMessage behavior */
188
189     switch (pri) {
190     case RPMLOG_INFO:
191     case RPMLOG_NOTICE:
192         msgout = (_stdlog ? _stdlog : stdout);
193         break;
194
195     case RPMLOG_EMERG:
196     case RPMLOG_ALERT:
197     case RPMLOG_CRIT:
198     case RPMLOG_ERR: /* XXX Legacy rpmError behavior used stdout w/o prefix. */
199     case RPMLOG_WARNING:
200     case RPMLOG_DEBUG:
201         break;
202     }
203
204     if (rpmlogMsgPrefix[pri] && *rpmlogMsgPrefix[pri])
205         (void) fputs(_(rpmlogMsgPrefix[pri]), msgout);
206
207     (void) fputs(msg, msgout);
208     (void) fflush(msgout);
209     msgbuf = _free(msgbuf);
210     if (pri <= RPMLOG_CRIT)
211         exit(EXIT_FAILURE);
212 }
213
214 void rpmlog (int code, const char *fmt, ...)
215 {
216     va_list ap;
217
218     va_start(ap, fmt);
219     /* FIX: shrug */
220     vrpmlog(code, fmt, ap);
221     va_end(ap);
222 }
223