rpm  5.4.14
rpmlog.c
Go to the documentation of this file.
1 
5 #include "system.h"
6 #include <stdarg.h>
7 #include <rpmiotypes.h>
8 #define _RPMLOG_INTERNAL
9 #include <rpmlog.h>
10 #include "debug.h"
11 
12 /*@access rpmlogRec @*/
13 
14 #ifdef __cplusplus
15 GENfree(rpmlogRec)
16 #endif /* __cplusplus */
17 
18 /*@unchecked@*/
19 static int nrecs = 0;
20 /*@unchecked@*/
21 static /*@only@*/ /*@null@*/ rpmlogRec recs = NULL;
22 
23 int rpmlogGetNrecs(void)
24 {
25  return nrecs;
26 }
27 
28 int rpmlogCode(void)
29 {
30  if (recs != NULL && nrecs > 0)
31  return recs[nrecs-1].code;
32  return -1;
33 }
34 
35 const char * rpmlogMessage(void)
36 {
37  if (recs != NULL && nrecs > 0)
38  return recs[nrecs-1].message;
39  return _("(no error)");
40 }
41 
42 const char * rpmlogRecMessage(rpmlogRec rec)
43 {
44  return (rec ? rec->message : NULL);
45 }
46 
48 {
49  return (rec ? rec->pri : RPMLOG_DEBUG);
50 }
51 
52 /*@-modfilesys@*/
53 void rpmlogPrint(FILE *f)
54 {
55  int i;
56 
57  if (f == NULL)
58  f = stderr;
59 
60  if (recs)
61  for (i = 0; i < nrecs; i++) {
62  rpmlogRec rec = recs + i;
63  if (rec->message && *rec->message)
64  fprintf(f, " %s", rec->message);
65  }
66 }
67 /*@=modfilesys@*/
68 
69 void rpmlogClose (void)
70  /*@globals recs, nrecs @*/
71  /*@modifies recs, nrecs @*/
72 {
73  int i;
74 
75  if (recs)
76  for (i = 0; i < nrecs; i++) {
77  rpmlogRec rec = recs + i;
78  rec->message = _free(rec->message);
79  }
80  recs = _free(recs);
81  nrecs = 0;
82 }
83 
84 void rpmlogOpen (/*@unused@*/ const char *ident,
85  /*@unused@*/ int option,
86  /*@unused@*/ int facility)
87 {
88 }
89 
90 /*@unchecked@*/
91 static unsigned rpmlogMask = RPMLOG_UPTO( RPMLOG_NOTICE );
92 
93 #if 0
94 /*@unchecked@*/
95 static /*@unused@*/ unsigned rpmlogFacility = RPMLOG_USER;
96 #endif
97 
98 int rpmlogSetMask (int mask)
99  /*@globals rpmlogMask @*/
100  /*@modifies rpmlogMask @*/
101 {
102  int omask = rpmlogMask;
103  if (mask)
104  rpmlogMask = mask;
105  return omask;
106 }
107 
108 /*@unchecked@*/ /*@null@*/
110 
111 /*@unchecked@*/ /*@null@*/
113 
115  /*@globals _rpmlogCallback, _rpmlogCallbackData @*/
116  /*@modifies _rpmlogCallback, _rpmlogCallbackData @*/
117 {
119  _rpmlogCallback = cb;
120  _rpmlogCallbackData = data;
121  return ocb;
122 }
123 
125  /*@globals _rpmlogCallback, _rpmlogCallbackData @*/
126 {
127  *cb = _rpmlogCallback;
128  *data = _rpmlogCallbackData;
129  return;
130 }
131 
132 /*@unchecked@*/ /*@null@*/
133 static FILE * _stdlog = NULL;
134 
135 static int rpmlogDefault(rpmlogRec rec)
136  /*@globals fileSystem @*/
137  /*@modifies fileSystem @*/
138 {
139  FILE *msgout = (_stdlog ? _stdlog : stderr);
140 
141  switch (rec->pri) {
142  case RPMLOG_INFO:
143  case RPMLOG_NOTICE:
144  msgout = (_stdlog ? _stdlog : stdout);
145  break;
146  case RPMLOG_EMERG:
147  case RPMLOG_ALERT:
148  case RPMLOG_CRIT:
149  case RPMLOG_ERR:
150  case RPMLOG_WARNING:
151  case RPMLOG_DEBUG:
152  default:
153  break;
154  }
155 
156  (void) fputs(rpmlogLevelPrefix(rec->pri), msgout);
157 
158  if (rec->message)
159  (void) fputs(rec->message, msgout);
160  (void) fflush(msgout);
161 
162  return (rec->pri <= RPMLOG_CRIT ? RPMLOG_EXIT : 0);
163 }
164 
165 FILE * rpmlogSetFile(FILE * fp)
166  /*@globals _stdlog @*/
167  /*@modifies _stdlog @*/
168 {
169  FILE * ofp = _stdlog;
170  _stdlog = fp;
171  return ofp;
172 }
173 
174 /*@-readonlytrans@*/ /* FIX: double indirection. */
175 /*@observer@*/ /*@unchecked@*/
176 static const char *rpmlogMsgPrefix[] = {
177  N_("fatal error: "),
178  N_("fatal error: "),
179  N_("fatal error: "),
180  N_("error: "),
181  N_("warning: "),
182  "",
183  "",
184  "D: ",
185 };
186 /*@=readonlytrans@*/
187 
188 const char * rpmlogLevelPrefix(rpmlogLvl pri)
189 {
190  return rpmlogMsgPrefix[pri&0x7];
191 }
192 
193 #if !defined(HAVE_VSNPRINTF)
194 static inline int vsnprintf(char * buf, /*@unused@*/ int nb,
195  const char * fmt, va_list ap)
196 {
197  return vsprintf(buf, fmt, ap);
198 }
199 #endif
200 
201 /*@-modfilesys@*/
202 /*@-compmempass@*/ /* FIX: rpmlogMsgPrefix[] dependent, not unqualified */
203 /*@-nullstate@*/ /* FIX: rpmlogMsgPrefix[] may be NULL */
204 void vrpmlog (unsigned code, const char *fmt, va_list ap)
205  /*@globals nrecs, recs, internalState @*/
206  /*@modifies nrecs, recs, internalState @*/
207 {
208  unsigned pri = RPMLOG_PRI(code);
209  unsigned mask = RPMLOG_MASK(pri);
210 #if 0
211  /*@unused@*/ unsigned fac = RPMLOG_FAC(code);
212 #endif
213  char *msgbuf, *msg;
214  size_t msgnb = BUFSIZ;
215  int nb;
216  int cbrc = RPMLOG_DEFAULT;
217  int needexit = 0;
218  struct rpmlogRec_s rec;
219 
220  if ((mask & rpmlogMask) == 0)
221  return;
222 
223  msgbuf = (char *) xmalloc(msgnb);
224  *msgbuf = '\0';
225 
226  /* Allocate a sufficiently large buffer for output. */
227  while (1) {
228  va_list apc;
229  /*@-unrecog -usedef@*/ va_copy(apc, ap); /*@=unrecog =usedef@*/
230  nb = vsnprintf(msgbuf, msgnb, fmt, apc);
231  if (nb > -1 && (size_t)nb < msgnb)
232  break;
233  if (nb > -1) /* glibc 2.1 (and later) */
234  msgnb = nb+1;
235  else /* glibc 2.0 */
236  msgnb *= 2;
237  msgbuf = (char *) xrealloc(msgbuf, msgnb);
238 /*@-mods@*/
239  va_end(apc);
240 /*@=mods@*/
241  }
242  msgbuf[msgnb - 1] = '\0';
243  msg = msgbuf;
244 
245  rec.code = code;
246  rec.message = msg;
247  rec.pri = (rpmlogLvl) pri;
248 
249  /* Save copy of all messages at warning (or below == "more important"). */
250  if (pri <= RPMLOG_WARNING) {
251  if (recs == NULL)
252  recs = (rpmlogRec) xmalloc((nrecs+2) * sizeof(*recs));
253  else
254  recs = (rpmlogRec) xrealloc(recs, (nrecs+2) * sizeof(*recs));
255  recs[nrecs].code = rec.code;
256  recs[nrecs].pri = rec.pri;
257  recs[nrecs].message = xstrdup(msgbuf);
258  ++nrecs;
259  recs[nrecs].code = 0;
260  recs[nrecs].pri = (rpmlogLvl) 0;
261  recs[nrecs].message = NULL;
262  }
263 
264  if (_rpmlogCallback) {
265  cbrc = _rpmlogCallback(&rec, _rpmlogCallbackData);
266  needexit += cbrc & RPMLOG_EXIT;
267  }
268 
269  if (cbrc & RPMLOG_DEFAULT) {
270 /*@-usereleased@*/
271  cbrc = rpmlogDefault(&rec);
272 /*@=usereleased@*/
273  needexit += cbrc & RPMLOG_EXIT;
274  }
275 
276 /*@-usereleased@*/ /* msgbuf is NULL or needs free'ing */
277  msgbuf = _free(msgbuf);
278 /*@=usereleased@*/
279  if (needexit)
281 }
282 /*@=compmempass =nullstate@*/
283 /*@=modfilesys@*/
284 
285 void _rpmlog (int code, const char *fmt, ...)
286 {
287  va_list ap;
288 
289  va_start(ap, fmt);
290  /*@-internalglobs@*/ /* FIX: shrug */
291  vrpmlog(code, fmt, ap);
292  /*@=internalglobs@*/
293  va_end(ap);
294 }
#define RPMLOG_PRI(p)
Definition: rpmlog.h:37
static FILE * _stdlog
Definition: rpmlog.c:133
enum rpmlogLvl_e rpmlogLvl
RPM Log levels.
#define EXIT_FAILURE
rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data)
Set rpmlog callback function.
Definition: rpmlog.c:114
char * xstrdup(const char *str)
Definition: rpmmalloc.c:321
static char *size_t nb
fgets(3) analogue that reads \ continuations.
Definition: macro.c:409
void * rpmlogCallbackData
Definition: rpmlog.h:158
#define RPMLOG_MASK(pri)
Definition: rpmlog.h:136
void rpmlogClose(void)
Close desriptor used to write to system logger.
Definition: rpmlog.c:69
static rpmlogCallback _rpmlogCallback
Definition: rpmlog.c:109
static rpmlogRec recs
Definition: rpmlog.c:21
rpmlogLvl rpmlogRecPriority(rpmlogRec rec)
Retrieve log priority from rpmlog record.
Definition: rpmlog.c:47
void rpmlogGetCallback(rpmlogCallback *cb, rpmlogCallbackData *data)
Get rpmlog callback function and data.
Definition: rpmlog.c:124
void _rpmlog(int code, const char *fmt,...)
Generate a log message using FMT string and option arguments.
Definition: rpmlog.c:285
FILE * rpmlogSetFile(FILE *fp)
Set rpmlog file handle.
Definition: rpmlog.c:165
const char * rpmlogMessage(void)
Return text of last rpmError() message.
Definition: rpmlog.c:35
Yet Another syslog(3) API clone.
goto exit
Definition: db3.c:1903
static int nrecs
Definition: rpmlog.c:19
void vrpmlog(unsigned code, const char *fmt, va_list ap)
Same as _rpmlog with stdarg argument list.
Definition: rpmlog.c:204
struct rpmlogRec_s * rpmlogRec
Definition: rpmlog.h:154
static unsigned rpmlogMask
Definition: rpmlog.c:91
static int rpmlogDefault(rpmlogRec rec)
Definition: rpmlog.c:135
fprintf(stderr,"--> %s(%p,%p,%p) sig %p sigp %p\n", __FUNCTION__, dig, t, rsactx, sig, sigp)
#define N_(Text)
Definition: system.h:531
int rpmlogGetNrecs(void)
Return number of messages.
Definition: rpmlog.c:23
int(* rpmlogCallback)(rpmlogRec rec, rpmlogCallbackData data)
Definition: rpmlog.h:168
static rpmlogCallbackData _rpmlogCallbackData
Definition: rpmlog.c:112
#define RPMLOG_DEFAULT
Option flags for callback return value.
Definition: rpmlog.h:174
void rpmlogOpen(const char *ident, int option, int facility)
Open connection to system logger.
Definition: rpmlog.c:84
static int vsnprintf(char *buf, int nb, const char *fmt, va_list ap)
Definition: rpmlog.c:194
int rpmlogCode(void)
Return error code from last rpmError() message.
Definition: rpmlog.c:28
const char * rpmlogLevelPrefix(rpmlogLvl pri)
Return translated prefix string (if any) given log level.
Definition: rpmlog.c:188
#define RPMLOG_FAC(p)
Definition: rpmlog.h:101
const char * msg
Definition: rpmts-py.c:976
FILE * f
Definition: macro.c:411
return NULL
Definition: poptALL.c:613
static const char * rpmlogMsgPrefix[]
Definition: rpmlog.c:176
void rpmlogPrint(FILE *f)
Print all rpmError() messages.
Definition: rpmlog.c:53
static void
Print copy of spec file, filling in Group/Description/Summary from specspo.
Definition: spec.c:737
char * buf
Parse (and execute) macro undefinition.
Definition: macro.c:703
#define _(Text)
Definition: system.h:29
#define xmalloc
Definition: system.h:32
#define RPMLOG_EXIT
Definition: rpmlog.h:175
int rpmlogSetMask(int mask)
Set the log mask level.
Definition: rpmlog.c:98
const char * rpmlogRecMessage(rpmlogRec rec)
Retrieve log message string from rpmlog record.
Definition: rpmlog.c:42
static void * _free(const void *p)
Wrapper to free(3), hides const compilation noise, permit NULL, return NULL.
Definition: rpmiotypes.h:647
int i
Definition: spec.c:743
#define xrealloc
Definition: system.h:35
#define RPMLOG_UPTO(pri)
Definition: rpmlog.h:137