]> mj.ucw.cz Git - libucw.git/blob - ucw/log.h
Logging: Use `uns' instead of `u32' to pass flags.
[libucw.git] / ucw / log.h
1 /*
2  *      UCW Library -- Logging
3  *
4  *      (c) 1997--2009 Martin Mares <mj@ucw.cz>
5  *      (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #ifndef _UCW_LOG_H_
12 #define _UCW_LOG_H_
13
14 #include "ucw/clists.h"
15
16 /* user de/allocated program/process name for use in the logsystem */
17 extern char *ls_title;
18
19 struct log_stream
20 {
21   /* optional name, 0-term, de/allocated by constr./destr. or user */
22   char *name;
23   /* number for use with msg parameter (from LS_SET_STRNUM()), -1 for closed log_stream */
24   int regnum;
25   /* arbitrary data for filter/handler */
26   int idata;
27   void *pdata;
28   /* severity levels to accept - bitmask of (1<<LEVEL) */
29   int levels;
30   /* if filter returns nonzero, discard the message */
31   int (*filter)(struct log_stream* ls, const char *m, uns cat);
32   /* pass the message to these streams (simple-list of pointers) */
33   struct clist substreams;
34   /* what kind of string to format to pass to the handler (bitmask of LSFMT_xxx ) */
35   int msgfmt;
36   /* what to do to commit the message (ret 0 on success, nonzero on error)
37    * msg is 0-term string, with desired info, one line, ending with "\n\0". */
38   int (*handler)(struct log_stream* ls, const char *m, uns cat);
39   /* close the log_stream file/connection */
40   void (*close)(struct log_stream* ls);
41 };
42
43 /* the default logger */
44 extern const struct log_stream ls_default_log;
45
46 /* A message is processed as follows:
47  *  1. Discard if message level not in levels
48  *  2. Run filter (if any), discard if ret. nonzero
49  *  3. Pass the message to all log_streams in substreams
50  *  4. Format the message informaion acc. to msgfmt
51  *  5. Run the handler
52  */
53
54 /* log header verbosity specifying message passed to handler */
55 enum ls_fmt
56 {
57   LSFMT_LEVEL=1,       /* log severity level (one letter) */
58   LSFMT_TIME=2,        /* log time (date-seconds) */
59   LSFMT_USEC=4,        /* log also micro-seconds */
60   LSFMT_TITLE=8,       /* log program title (global string) */
61   LSFMT_PID=16,        /* log program PID */
62   LSFMT_LOGNAME=32,    /* log log_stream name */
63   LSFMT_NONE=0,
64   LSFMT_FULL=LSFMT_LEVEL+LSFMT_TIME+LSFMT_USEC+LSFMT_TITLE+LSFMT_PID+LSFMT_LOGNAME,
65   LSFMT_DEFAULT=LSFMT_LEVEL+LSFMT_TIME
66 };
67
68 /* Mask of containing all existing levels. */
69 #define LS_ALL_LEVELS 0xfff
70
71 // return the letter associated with the severity level
72 #define LS_LEVEL_LETTER(level) ("DIiWwEe!###"[( level )])
73
74 ///// Macros for extracting parts of the flags parameter
75 // The division of the flags parameter is decided only here
76 // The current division is (for 32 bit flags):
77 // MSB <5 bits: any internal log flags> <8 bits: "user" flags> <10 bits: stream number>
78 //     <8 bits: severity level> LSB
79
80 // Bits per section
81 enum ls_flagbits {
82   LS_LEVEL_BITS    = 8,
83   LS_STRNUM_BITS   = 16,
84   LS_FLAGS_BITS    = 5,
85   LS_INTERNAL_BITS = 4,
86 };
87
88 // Section shifts
89 enum ls_flagpos {
90   LS_LEVEL_POS     = 0,
91   LS_STRNUM_POS    = LS_LEVEL_POS + LS_LEVEL_BITS,
92   LS_FLAGS_POS     = LS_STRNUM_POS + LS_STRNUM_BITS,
93   LS_INTERNAL_POS  = LS_FLAGS_POS + LS_FLAGS_BITS,
94 };
95
96 // Bitmasks
97 enum ls_flagmasks {
98   LS_LEVEL_MASK    = (( 1 << LS_LEVEL_BITS ) - 1 ) << LS_LEVEL_POS,
99   LS_STRNUM_MASK   = (( 1 << LS_STRNUM_BITS ) - 1 ) << LS_STRNUM_POS,
100   LS_FLAGS_MASK    = (( 1 << LS_FLAGS_BITS ) - 1 ) << LS_FLAGS_POS,
101   LS_INTERNAL_MASK = (( 1 << LS_INTERNAL_BITS ) - 1 ) << LS_INTERNAL_POS,
102 };
103
104 // "Get" macros (break flags to parts)
105 #define LS_GET_LEVEL(flags)     ((( flags ) & LS_LEVEL_MASK ) >> LS_LEVEL_POS )
106 #define LS_GET_STRNUM(flags)    ((( flags ) & LS_STRNUM_MASK ) >> LS_STRNUM_POS )
107 #define LS_GET_FLAGS(flags)     ((( flags ) & LS_FLAGS_MASK ) >> LS_FLAGS_POS )
108 #define LS_GET_INTERNAL(flags)  ((( flags ) & LS_INTERNAL_MASK ) >> LS_INTERNAL_POS )
109
110 // "Set" macros (parts to flags)
111 #define LS_SET_LEVEL(level)     (( level ) << LS_LEVEL_POS )
112 #define LS_SET_STRNUM(strnum)   (( strnum ) << LS_STRNUM_POS )
113 #define LS_SET_FLAGS(flags)     (( flags ) << LS_FLAGS_POS )
114 #define LS_SET_INTERNAL(intern) (( intern ) << LS_INTERNAL_POS )
115
116 // Internal flags of the logsystem
117 // Avoid operations that are unsafe in signal handlers
118 #define LSFLAG_SIGHANDLER LS_SET_INTERNAL(0x001)
119
120 // The module is initialized when a first stream is created.
121 // Before that only the default stream exists.
122
123 // Initial number of streams to allocate (must be >=2)
124 #define LS_INIT_STREAMS 8
125
126 /* Return pointer a new (xmalloc()-ated) stream with no handler and an empty substream list. */
127 struct log_stream *ls_new(void);
128
129 /* Close and xfree() given log_stream */
130 /* Does not affect substreams */
131 void ls_close(struct log_stream *ls);
132
133 /* close all open streams, un-initialize the module, free all memory,
134  * use only ls_default_log */
135 void ls_close_all(void);
136
137 /* add a new substream, xmalloc()-ate a new simp_node */
138 void ls_add_substream(struct log_stream *where, struct log_stream *what);
139
140 /* remove all occurences of a substream, free() the simp_node */
141 /* return number of deleted entries */
142 int ls_rm_substream(struct log_stream *where, struct log_stream *what);
143
144 /* get a stream by its number (regnum) */
145 /* returns NULL for free numbers */
146 /* defaults to ls_default_stream for 0 when stream number 0 not set */
147 struct log_stream *log_stream_by_flags(uns flags);
148
149 /* The proposed alternative to original vmsg() */
150 void ls_vmsg(unsigned int cat, const char *fmt, va_list args);
151
152 /* The proposed alternative to original msg() */
153 void ls_msg(unsigned int cat, const char *fmt, ...);
154
155 /* The proposed alternative to original die() */
156 void ls_die(const char *fmt, ...);
157
158 /* process a message (string) (INTERNAL) */
159 /* depth prevents undetected looping */
160 /* returns 1 in case of loop detection or other fatal error
161  *         0 otherwise */
162 int ls_passmsg(int depth, struct log_stream *ls, const char *stime, const char *sutime,
163     const char *msg, uns cat);
164
165 /* Maximal depth of ls_passmsg recursion */
166 #define LS_MAX_DEPTH 64
167
168 /* Define an array (growing buffer) for pointers to log_streams. */
169 #define GBUF_TYPE struct log_stream*
170 #define GBUF_PREFIX(x) lsbuf_##x
171 #include "ucw/gbuf.h"
172
173 extern struct lsbuf_t log_streams;
174 extern int log_streams_after;
175
176 /********* Individual handler types (constructors, handlers, destructors) */
177
178 /**** standard (filedes) files */
179
180 // NOTE:
181 // under unix, for ordinary files open in append mode, the writes
182 // are atomic (unless you meet the quota or other bad things happen),
183 // so using a single log_stream is thread-safe and the file can be shared
184 // among multiple processes
185
186 /* assign log to a file descriptor */
187 /* initialize with the default formatting, does NOT close the descriptor */
188 struct log_stream *ls_fdfile_new(int fd);
189
190 /* open() a file (append mode) */
191 /* initialize with the default formatting */
192 struct log_stream *ls_file_new(const char *path);
193
194
195 /**** syslog */
196
197 // NOTE:
198 // The syslog uses a bit different severity levels, for details, see
199 // ls_syslog_convert_level().
200 // syslog also prepends it's own time and severity info, so the default
201 // messaging passes only clean message
202
203 /* assign log to a syslog facility */
204 /* initialize with no formatting (syslog adds these inforamtion) */
205 /* name is optional prefix (NULL for none) */
206 struct log_stream *ls_syslog_new(int facility, const char *name);
207
208 #endif