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 /** \ingroup rpmlog
46  * facility codes
47  */
48 typedef	enum rpmlogFac_e {
49     RPMLOG_KERN		= (0<<3),	/*!< kernel messages */
50     RPMLOG_USER		= (1<<3),	/*!< random user-level messages */
51     RPMLOG_MAIL		= (2<<3),	/*!< mail system */
52     RPMLOG_DAEMON	= (3<<3),	/*!< system daemons */
53     RPMLOG_AUTH		= (4<<3),	/*!< security/authorization messages */
54     RPMLOG_SYSLOG	= (5<<3),	/*!< messages generated internally by syslogd */
55     RPMLOG_LPR		= (6<<3),	/*!< line printer subsystem */
56     RPMLOG_NEWS		= (7<<3),	/*!< network news subsystem */
57     RPMLOG_UUCP		= (8<<3),	/*!< UUCP subsystem */
58     RPMLOG_CRON		= (9<<3),	/*!< clock daemon */
59     RPMLOG_AUTHPRIV	= (10<<3),	/*!< security/authorization messages (private) */
60     RPMLOG_FTP		= (11<<3),	/*!< ftp daemon */
61 
62 	/* other codes through 15 reserved for system use */
63     RPMLOG_LOCAL0	= (16<<3),	/*!< reserved for local use */
64     RPMLOG_LOCAL1	= (17<<3),	/*!< reserved for local use */
65     RPMLOG_LOCAL2	= (18<<3),	/*!< reserved for local use */
66     RPMLOG_LOCAL3	= (19<<3),	/*!< reserved for local use */
67     RPMLOG_LOCAL4	= (20<<3),	/*!< reserved for local use */
68     RPMLOG_LOCAL5	= (21<<3),	/*!< reserved for local use */
69     RPMLOG_LOCAL6	= (22<<3),	/*!< reserved for local use */
70     RPMLOG_LOCAL7	= (23<<3),	/*!< reserved for local use */
71 
72 #define	RPMLOG_NFACILITIES 24	/*!< current number of facilities */
73     RPMLOG_ERRMSG	= (((unsigned)(RPMLOG_NFACILITIES+0))<<3)
74 } rpmlogFac;
75 
76 #define	RPMLOG_FACMASK	0x03f8	/*!< mask to extract facility part */
77 #define	RPMLOG_FAC(p)	(((p) & RPMLOG_FACMASK) >> 3)
78 
79 
80 /*
81  * arguments to setlogmask.
82  */
83 #define	RPMLOG_MASK(pri) (1 << ((unsigned)(pri)))	/*!< mask for one priority */
84 #define	RPMLOG_UPTO(pri) ((1 << (((unsigned)(pri))+1)) - 1)	/*!< all priorities through pri */
85 
86 /*
87  * Option flags for openlog.
88  *
89  * RPMLOG_ODELAY no longer does anything.
90  * RPMLOG_NDELAY is the inverse of what it used to be.
91  */
92 #define	RPMLOG_PID	0x01	/*!< log the pid with each message */
93 #define	RPMLOG_CONS	0x02	/*!< log on the console if errors in sending */
94 #define	RPMLOG_ODELAY	0x04	/*!< delay open until first syslog() (default) */
95 #define	RPMLOG_NDELAY	0x08	/*!< don't delay open */
96 #define	RPMLOG_NOWAIT	0x10	/*!< don't wait for console forks: DEPRECATED */
97 #define	RPMLOG_PERROR	0x20	/*!< log to stderr as well */
98 
99 /** \ingroup rpmlog
100  * Option flags for callback return value.
101  */
102 #define RPMLOG_DEFAULT	0x01	/*!< perform default logging */
103 #define RPMLOG_EXIT	0x02	/*!< exit after logging */
104 
105 /** \ingroup rpmlog
106  */
107 typedef struct rpmlogRec_s * rpmlogRec;
108 
109 /** \ingroup rpmlog
110  * Retrieve log message string from rpmlog record
111  * @param rec		rpmlog record
112  * @return		log message
113  */
114 const char * rpmlogRecMessage(rpmlogRec rec);
115 
116 /** \ingroup rpmlog
117  * Retrieve log priority from rpmlog record
118  * @param rec		rpmlog record
119  * @return		log priority
120  */
121 rpmlogLvl rpmlogRecPriority(rpmlogRec rec);
122 
123 typedef void * rpmlogCallbackData;
124 
125 /** \ingroup rpmlog
126   * @param rec		rpmlog record
127   * @param data		private callback data
128   * @return		flags to define further behavior:
129   * 			RPMLOG_DEFAULT to perform default logging,
130   * 			RPMLOG_EXIT to exit after processing,
131   * 			0 to return after callback
132   */
133 typedef int (*rpmlogCallback) (rpmlogRec rec, rpmlogCallbackData data);
134 
135 /** \ingroup rpmlog
136  * Return number of rpmError() ressages.
137  * @return		number of messages
138  */
139 int rpmlogGetNrecs(void)	;
140 
141 /** \ingroup rpmlog
142  * Print all rpmError() messages.
143  * @param f		file handle (NULL uses stderr)
144  */
145 void rpmlogPrint(FILE *f);
146 
147 /** \ingroup rpmlog
148  * Close desriptor used to write to system logger.
149  * @todo Implement.
150  */
151 void rpmlogClose (void);
152 
153 /** \ingroup rpmlog
154  * Open connection to system logger.
155  * @todo Implement.
156  */
157 void rpmlogOpen (const char * ident, int option, int facility);
158 
159 /** \ingroup rpmlog
160  * Set the log mask level.
161  * @param mask		log mask (0 is no operation)
162  * @return		previous log mask
163  */
164 int rpmlogSetMask (int mask);
165 
166 /** \ingroup rpmlog
167  * Generate a log message using FMT string and option arguments.
168  */
169 void rpmlog (int code, const char *fmt, ...) RPM_GNUC_PRINTF(2, 3);
170 
171 /** \ingroup rpmlog
172  * Return text of last rpmError() message.
173  * @return		text of last message
174  */
175 const char * rpmlogMessage(void);
176 
177 /** \ingroup rpmlog
178  * Return error code from last rpmError() message.
179  * @deprecated Perl-RPM needs, what's really needed is predictable, non-i18n
180  *	encumbered, error text that can be retrieved through rpmlogMessage()
181  *	and parsed IMHO.
182  * @return		code from last message
183  */
184 int rpmlogCode(void);
185 
186 /** \ingroup rpmlog
187  * Return translated prefix string (if any) given log level.
188  * @param pri		log priority
189  * @return		message prefix (or "" for none)
190  */
191 const char * rpmlogLevelPrefix(rpmlogLvl pri);
192 
193 /** \ingroup rpmlog
194  * Set rpmlog callback function.
195  * @param cb		rpmlog callback function
196  * @param data		callback private (user) data
197  * @return		previous rpmlog callback function
198  */
199 rpmlogCallback rpmlogSetCallback(rpmlogCallback cb, rpmlogCallbackData data);
200 
201 /** \ingroup rpmlog
202  * Set rpmlog file handle.
203  * @param fp		rpmlog file handle (NULL uses stdout/stderr)
204  * @return		previous rpmlog file handle
205  */
206 FILE * rpmlogSetFile(FILE * fp);
207 
208 #define	rpmSetVerbosity(_lvl)	\
209 	((void)rpmlogSetMask( RPMLOG_UPTO( RPMLOG_PRI(_lvl))))
210 #define	rpmIncreaseVerbosity()	\
211     ((void)rpmlogSetMask(((((unsigned)(rpmlogSetMask(0) & 0xff)) << 1) | 1)))
212 #define	rpmDecreaseVerbosity()	\
213 	((void)rpmlogSetMask((((int)(rpmlogSetMask(0) & 0xff)) >> 1)))
214 #define	rpmIsNormal()		\
215 	(rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_NOTICE ))
216 #define	rpmIsVerbose()		\
217 	(rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_INFO ))
218 #define	rpmIsDebug()		\
219 	(rpmlogSetMask(0) >= RPMLOG_MASK( RPMLOG_DEBUG ))
220 
221 #ifdef __cplusplus
222 }
223 #endif
224 
225 #endif /* H_RPMLOG */
226