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