3a3dc7b60a9315bc28b64a98b6bfee0f868a1abd
[platform/upstream/rpm.git] / rpmio / rpmlog.h
1 #ifndef H_RPMLOG
2 #define H_RPMLOG 1
3
4 /** \ingroup rpmio
5  * \file rpmio/rpmlog.h
6  * Yet Another syslog(3) API clone.
7  * Used to unify rpmError() and rpmMessage() interfaces in rpm.
8  */
9
10 #include <stdarg.h>
11
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15
16 /** \ingroup rpmlog
17  * RPM Log levels.
18  * priorities/facilities are encoded into a single 32-bit quantity, where the
19  * bottom 3 bits are the priority (0-7) and the top 28 bits are the facility
20  * (0-big number).  Both the priorities and the facilities map roughly
21  * one-to-one to strings in the syslogd(8) source code.  This mapping is
22  * included in this file.
23  *
24  * priorities (these are ordered)
25  */
26 typedef enum rpmlogLvl_e {
27     RPMLOG_EMERG        = 0,    /*!< system is unusable */
28     RPMLOG_ALERT        = 1,    /*!< action must be taken immediately */
29     RPMLOG_CRIT         = 2,    /*!< critical conditions */
30     RPMLOG_ERR          = 3,    /*!< error conditions */
31     RPMLOG_WARNING      = 4,    /*!< warning conditions */
32     RPMLOG_NOTICE       = 5,    /*!< normal but significant condition */
33     RPMLOG_INFO         = 6,    /*!< informational */
34     RPMLOG_DEBUG        = 7     /*!< debug-level messages */
35 } rpmlogLvl;
36
37 #define RPMLOG_PRIMASK  0x07    /* mask to extract priority part (internal) */
38                                 /* extract priority */
39 #define RPMLOG_PRI(p)   ((p) & RPMLOG_PRIMASK)
40 #define RPMLOG_MAKEPRI(fac, pri)        ((((unsigned)(fac)) << 3) | (pri))
41
42 #ifdef RPMLOG_NAMES
43 #define _RPMLOG_NOPRI   0x10    /* the "no priority" priority */
44                                 /* mark "facility" */
45 #define _RPMLOG_MARK    RPMLOG_MAKEPRI(RPMLOG_NFACILITIES, 0)
46 typedef struct _rpmcode {
47         const char      *c_name;
48         int             c_val;
49 } RPMCODE;
50
51 RPMCODE rpmprioritynames[] =
52   {
53     { "alert",  RPMLOG_ALERT },
54     { "crit",   RPMLOG_CRIT },
55     { "debug",  RPMLOG_DEBUG },
56     { "emerg",  RPMLOG_EMERG },
57     { "err",    RPMLOG_ERR },
58     { "error",  RPMLOG_ERR },           /* DEPRECATED */
59     { "info",   RPMLOG_INFO },
60     { "none",   _RPMLOG_NOPRI },        /* INTERNAL */
61     { "notice", RPMLOG_NOTICE },
62     { "panic",  RPMLOG_EMERG },         /* DEPRECATED */
63     { "warn",   RPMLOG_WARNING },       /* DEPRECATED */
64     { "warning",RPMLOG_WARNING },
65     { NULL, -1 }
66   };
67 #endif
68
69 /** \ingroup rpmlog
70  * facility codes
71  */
72 typedef enum rpmlogFac_e {
73     RPMLOG_KERN         = (0<<3),       /*!< kernel messages */
74     RPMLOG_USER         = (1<<3),       /*!< random user-level messages */
75     RPMLOG_MAIL         = (2<<3),       /*!< mail system */
76     RPMLOG_DAEMON       = (3<<3),       /*!< system daemons */
77     RPMLOG_AUTH         = (4<<3),       /*!< security/authorization messages */
78     RPMLOG_SYSLOG       = (5<<3),       /*!< messages generated internally by syslogd */
79     RPMLOG_LPR          = (6<<3),       /*!< line printer subsystem */
80     RPMLOG_NEWS         = (7<<3),       /*!< network news subsystem */
81     RPMLOG_UUCP         = (8<<3),       /*!< UUCP subsystem */
82     RPMLOG_CRON         = (9<<3),       /*!< clock daemon */
83     RPMLOG_AUTHPRIV     = (10<<3),      /*!< security/authorization messages (private) */
84     RPMLOG_FTP          = (11<<3),      /*!< ftp daemon */
85
86         /* other codes through 15 reserved for system use */
87     RPMLOG_LOCAL0       = (16<<3),      /*!< reserved for local use */
88     RPMLOG_LOCAL1       = (17<<3),      /*!< reserved for local use */
89     RPMLOG_LOCAL2       = (18<<3),      /*!< reserved for local use */
90     RPMLOG_LOCAL3       = (19<<3),      /*!< reserved for local use */
91     RPMLOG_LOCAL4       = (20<<3),      /*!< reserved for local use */
92     RPMLOG_LOCAL5       = (21<<3),      /*!< reserved for local use */
93     RPMLOG_LOCAL6       = (22<<3),      /*!< reserved for local use */
94     RPMLOG_LOCAL7       = (23<<3),      /*!< reserved for local use */
95
96 #define RPMLOG_NFACILITIES 24   /*!< current number of facilities */
97     RPMLOG_ERRMSG       = (((unsigned)(RPMLOG_NFACILITIES+0))<<3)
98 } rpmlogFac;
99
100 #define RPMLOG_FACMASK  0x03f8  /*!< mask to extract facility part */
101 #define RPMLOG_FAC(p)   (((p) & RPMLOG_FACMASK) >> 3)
102
103
104 #ifdef RPMLOG_NAMES
105 RPMCODE facilitynames[] =
106   {
107     { "auth",   RPMLOG_AUTH },
108     { "authpriv",RPMLOG_AUTHPRIV },
109     { "cron",   RPMLOG_CRON },
110     { "daemon", RPMLOG_DAEMON },
111     { "ftp",    RPMLOG_FTP },
112     { "kern",   RPMLOG_KERN },
113     { "lpr",    RPMLOG_LPR },
114     { "mail",   RPMLOG_MAIL },
115     { "mark",   _RPMLOG_MARK },         /* INTERNAL */
116     { "news",   RPMLOG_NEWS },
117     { "security",RPMLOG_AUTH },         /* DEPRECATED */
118     { "syslog", RPMLOG_SYSLOG },
119     { "user",   RPMLOG_USER },
120     { "uucp",   RPMLOG_UUCP },
121     { "local0", RPMLOG_LOCAL0 },
122     { "local1", RPMLOG_LOCAL1 },
123     { "local2", RPMLOG_LOCAL2 },
124     { "local3", RPMLOG_LOCAL3 },
125     { "local4", RPMLOG_LOCAL4 },
126     { "local5", RPMLOG_LOCAL5 },
127     { "local6", RPMLOG_LOCAL6 },
128     { "local7", RPMLOG_LOCAL7 },
129     { NULL, -1 }
130   };
131 #endif
132
133 /*
134  * arguments to setlogmask.
135  */
136 #define RPMLOG_MASK(pri) (1 << ((unsigned)(pri)))       /*!< mask for one priority */
137 #define RPMLOG_UPTO(pri) ((1 << (((unsigned)(pri))+1)) - 1)     /*!< all priorities through pri */
138
139 /*
140  * Option flags for openlog.
141  *
142  * RPMLOG_ODELAY no longer does anything.
143  * RPMLOG_NDELAY is the inverse of what it used to be.
144  */
145 #define RPMLOG_PID      0x01    /*!< log the pid with each message */
146 #define RPMLOG_CONS     0x02    /*!< log on the console if errors in sending */
147 #define RPMLOG_ODELAY   0x04    /*!< delay open until first syslog() (default) */
148 #define RPMLOG_NDELAY   0x08    /*!< don't delay open */
149 #define RPMLOG_NOWAIT   0x10    /*!< don't wait for console forks: DEPRECATED */
150 #define RPMLOG_PERROR   0x20    /*!< log to stderr as well */
151
152 /* \ingroup rpmlog
153  * Option flags for callback return value.
154  */
155 #define RPMLOG_DEFAULT  0x01    /*!< perform default logging */ 
156 #define RPMLOG_EXIT     0x02    /*!< exit after logging */
157
158 /** \ingroup rpmlog
159  */
160 typedef struct rpmlogRec_s * rpmlogRec;
161
162 /** \ingroup rpmlog
163  * Retrieve log message string from rpmlog record
164  * @param rec           rpmlog record
165  * @return              log message
166  */
167 const char * rpmlogRecMessage(rpmlogRec rec);
168
169 /** \ingroup rpmlog
170  * Retrieve log priority from rpmlog record
171  * @param rec           rpmlog record
172  * @return              log priority
173  */
174 rpmlogLvl rpmlogRecPriority(rpmlogRec rec);
175
176 typedef void * rpmlogCallbackData;
177
178 /** \ingroup rpmlog
179   * @param rec          rpmlog record
180   * @param data         private callback data
181   * @return             flags to define further behavior:
182   *                     RPMLOG_DEFAULT to perform default logging,
183   *                     RPMLOG_EXIT to exit after processing, 
184   *                     0 to return after callback
185   */
186 typedef int (*rpmlogCallback) (rpmlogRec rec, rpmlogCallbackData data);
187
188 /** \ingroup rpmlog
189  * Return number of rpmError() ressages.
190  * @return              number of messages
191  */
192 int rpmlogGetNrecs(void)        ;
193
194 /** \ingroup rpmlog
195  * Print all rpmError() messages.
196  * @param f             file handle (NULL uses stderr)
197  */
198 void rpmlogPrint(FILE *f);
199
200 /** \ingroup rpmlog
201  * Close desriptor used to write to system logger.
202  * @todo Implement.
203  */
204 void rpmlogClose (void);
205
206 /** \ingroup rpmlog
207  * Open connection to system logger.
208  * @todo Implement.
209  */
210 void rpmlogOpen (const char * ident, int option, int facility);
211
212 /** \ingroup rpmlog
213  * Set the log mask level.
214  * @param mask          log mask (0 is no operation)
215  * @return              previous log mask
216  */
217 int rpmlogSetMask (int mask);
218
219 /** \ingroup rpmlog
220  * Generate a log message using FMT string and option arguments.
221  */
222 void rpmlog (int code, const char *fmt, ...);
223
224 /** \ingroup rpmlog
225  * Return text of last rpmError() message.
226  * @return              text of last message
227  */
228 const char * rpmlogMessage(void);
229
230 /** \ingroup rpmlog
231  * Return error code from last rpmError() message.
232  * @deprecated Perl-RPM needs, what's really needed is predictable, non-i18n
233  *      encumbered, error text that can be retrieved through rpmlogMessage()
234  *      and parsed IMHO.
235  * @return              code from last message
236  */
237 int rpmlogCode(void);
238
239 /** \ingroup rpmlog
240  * Return translated prefix string (if any) given log level.
241  * @param pri           log priority
242  * @return              message prefix (or "" for none)
243  */
244 const char * rpmlogLevelPrefix(rpmlogLvl pri);
245
246 /** \ingroup rpmlog
247  * Set rpmlog callback function.
248  * @param cb            rpmlog callback function
249  * @param data          callback private (user) data
250  * @return              previous rpmlog callback function
251  */
252 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data);
253
254 /** \ingroup rpmlog
255  * Set rpmlog file handle.
256  * @param fp            rpmlog file handle (NULL uses stdout/stderr)
257  * @return              previous rpmlog file handle
258  */
259 FILE * rpmlogSetFile(FILE * fp);
260
261 #define rpmSetVerbosity(_lvl)   \
262         ((void)rpmlogSetMask( RPMLOG_UPTO( RPMLOG_PRI(_lvl))))
263 #define rpmIncreaseVerbosity()  \
264     ((void)rpmlogSetMask(((((unsigned)(rpmlogSetMask(0) & 0xff)) << 1) | 1)))
265 #define rpmDecreaseVerbosity()  \
266         ((void)rpmlogSetMask((((int)(rpmlogSetMask(0) & 0xff)) >> 1)))
267 #define rpmIsNormal()           \
268         (rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_NOTICE ))
269 #define rpmIsVerbose()          \
270         (rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_INFO ))
271 #define rpmIsDebug()            \
272         (rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_DEBUG ))
273
274 #ifdef __cplusplus
275 }
276 #endif
277
278 #endif /* H_RPMLOG */