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