2 * UCW Library -- Logging
4 * (c) 1997--2009 Martin Mares <mj@ucw.cz>
5 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
14 #include "ucw/clists.h"
16 /* user de/allocated program/process name for use in the logsystem */
17 extern char *ls_title;
21 char *m; // The formatted message itself, ending with \n\0
22 int m_len; // Length without the \0
23 struct tm *tm; // Current time
24 uns flags; // Category and other flags as passed to msg()
25 char *raw_msg; // Unformatted parts
32 char *name; // Optional name, allocated by the user (or constructor)
33 int regnum; // Stream number, already encoded by LS_SET_STRNUM(); -1 if closed
34 uns levels; // Bitmask of accepted severity levels
35 uns msgfmt; // Formatting flags (LSFMT_xxx)
36 int (*filter)(struct log_stream* ls, struct log_msg *m); // Filter function, return non-zero to discard the message
37 struct clist substreams; // Pass the message to these streams (simple_list of pointers)
38 int (*handler)(struct log_stream *ls, struct log_msg *m); // Called to commit the message
39 void (*close)(struct log_stream* ls); // Called on log_close_stream()
40 int idata; // Private data of the handler
45 /* the default logger */
46 extern const struct log_stream log_stream_default;
47 #define LOG_STREAM_DEFAULT ((struct log_stream *) &log_stream_default)
49 /* A message is processed as follows:
50 * 1. Discard if message level not in levels
51 * 2. Run filter (if any), discard if ret. nonzero
52 * 3. Pass the message to all log_streams in substreams
53 * 4. Format the message informaion acc. to msgfmt
57 /* log header verbosity specifying message passed to handler */
60 LSFMT_LEVEL=1, /* log severity level (one letter) */
61 LSFMT_TIME=2, /* log time (date-seconds) */
62 LSFMT_USEC=4, /* log also micro-seconds */
63 LSFMT_TITLE=8, /* log program title (global string) */
64 LSFMT_PID=16, /* log program PID */
65 LSFMT_LOGNAME=32, /* log log_stream name */
67 LSFMT_FULL=LSFMT_LEVEL+LSFMT_TIME+LSFMT_USEC+LSFMT_TITLE+LSFMT_PID+LSFMT_LOGNAME,
68 LSFMT_DEFAULT=LSFMT_LEVEL+LSFMT_TIME
71 /* Mask of containing all existing levels. */
72 #define LS_ALL_LEVELS 0xfff
74 // return the letter associated with the severity level
75 #define LS_LEVEL_LETTER(level) ("DIiWwEe!###"[( level )])
77 ///// Macros for extracting parts of the flags parameter
78 // The division of the flags parameter is decided only here
79 // The current division is (for 32 bit flags):
80 // MSB <5 bits: any internal log flags> <8 bits: "user" flags> <10 bits: stream number>
81 // <8 bits: severity level> LSB
94 LS_STRNUM_POS = LS_LEVEL_POS + LS_LEVEL_BITS,
95 LS_FLAGS_POS = LS_STRNUM_POS + LS_STRNUM_BITS,
96 LS_INTERNAL_POS = LS_FLAGS_POS + LS_FLAGS_BITS,
101 LS_LEVEL_MASK = (( 1 << LS_LEVEL_BITS ) - 1 ) << LS_LEVEL_POS,
102 LS_STRNUM_MASK = (( 1 << LS_STRNUM_BITS ) - 1 ) << LS_STRNUM_POS,
103 LS_FLAGS_MASK = (( 1 << LS_FLAGS_BITS ) - 1 ) << LS_FLAGS_POS,
104 LS_INTERNAL_MASK = (( 1 << LS_INTERNAL_BITS ) - 1 ) << LS_INTERNAL_POS,
107 // "Get" macros (break flags to parts)
108 #define LS_GET_LEVEL(flags) ((( flags ) & LS_LEVEL_MASK ) >> LS_LEVEL_POS )
109 #define LS_GET_STRNUM(flags) ((( flags ) & LS_STRNUM_MASK ) >> LS_STRNUM_POS )
110 #define LS_GET_FLAGS(flags) ((( flags ) & LS_FLAGS_MASK ) >> LS_FLAGS_POS )
111 #define LS_GET_INTERNAL(flags) ((( flags ) & LS_INTERNAL_MASK ) >> LS_INTERNAL_POS )
113 // "Set" macros (parts to flags)
114 #define LS_SET_LEVEL(level) (( level ) << LS_LEVEL_POS )
115 #define LS_SET_STRNUM(strnum) (( strnum ) << LS_STRNUM_POS )
116 #define LS_SET_FLAGS(flags) (( flags ) << LS_FLAGS_POS )
117 #define LS_SET_INTERNAL(intern) (( intern ) << LS_INTERNAL_POS )
119 // Internal flags of the logsystem
120 // Avoid operations that are unsafe in signal handlers
121 #define LSFLAG_SIGHANDLER LS_SET_INTERNAL(0x001)
123 // The module is initialized when a first stream is created.
124 // Before that only the default stream exists.
126 /* Return pointer a new (xmalloc()-ated) stream with no handler and an empty substream list. */
127 struct log_stream *log_new_stream(void);
129 /* Close and xfree() given log_stream */
130 /* Does not affect substreams */
131 void log_close_stream(struct log_stream *ls);
133 /* close all open streams, un-initialize the module, free all memory,
134 * use only ls_default_log */
135 void log_close_all(void);
137 /* add a new substream, xmalloc()-ate a new simp_node */
138 void log_add_substream(struct log_stream *where, struct log_stream *what);
140 /* remove all occurences of a substream, free() the simp_node */
141 /* return number of deleted entries */
142 int log_rm_substream(struct log_stream *where, struct log_stream *what);
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);
149 /* process a message (string) (INTERNAL) */
150 /* depth prevents undetected looping */
151 /* returns 1 in case of loop detection or other fatal error
153 int log_pass_msg(int depth, struct log_stream *ls, struct log_msg *m);
155 /* Define an array (growing buffer) for pointers to log_streams. */
156 #define GBUF_TYPE struct log_stream*
157 #define GBUF_PREFIX(x) lsbuf_##x
158 #include "ucw/gbuf.h"
160 extern struct lsbuf_t log_streams;
161 extern int log_streams_after;
163 /********* Individual handler types (constructors, handlers, destructors) */
165 /**** standard (filedes) files */
168 // under unix, for ordinary files open in append mode, the writes
169 // are atomic (unless you meet the quota or other bad things happen),
170 // so using a single log_stream is thread-safe and the file can be shared
171 // among multiple processes
173 /* assign log to a file descriptor */
174 /* initialize with the default formatting, does NOT close the descriptor */
175 struct log_stream *log_new_fd(int fd);
177 /* open() a file (append mode) */
178 /* initialize with the default formatting */
179 struct log_stream *log_new_file(const char *path);
185 // The syslog uses a bit different severity levels, for details, see
186 // ls_syslog_convert_level().
187 // syslog also prepends it's own time and severity info, so the default
188 // messaging passes only clean message
190 /* assign log to a syslog facility */
191 /* initialize with no formatting (syslog adds these inforamtion) */
192 /* name is optional prefix (NULL for none) */
193 struct log_stream *log_new_syslog(int facility, const char *name);