2 * UCW Library -- Logging: Configuration of Log Streams
4 * (c) 2009 Martin Mares <mj@ucw.cz>
6 * This software may be freely distributed and used according to the terms
7 * of the GNU Lesser General Public License.
12 #include <ucw/log-internal.h>
14 #include <ucw/simple-lists.h>
16 #include <ucw/threads.h>
25 /*** Configuration of streams ***/
27 struct stream_config {
32 char *syslog_facility;
34 clist types; // simple_list of names
35 clist substreams; // simple_list of names
36 clist limits; // of struct limit_config's
37 int microseconds; // Enable logging of precise timestamps
42 struct log_stream *ls;
43 int mark; // Used temporarily in log_config_commit()
48 clist types; // simple_list of names
54 stream_init(void *ptr)
56 struct stream_config *c = ptr;
64 stream_commit(void *ptr)
66 struct stream_config *c = ptr;
68 if (c->syslog_facility)
70 if (!log_syslog_facility_exists(c->syslog_facility))
71 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
73 return "Both FileName and SyslogFacility selected";
75 return "Syslog streams do not support microsecond precision";
77 if (c->stderr_follows && !c->file_name)
78 return "StdErrFollows requires a file-based stream";
82 static const char * const level_names[] = {
89 static struct cf_section limit_config = {
90 CF_TYPE(struct limit_config),
92 #define P(x) PTR_TO(struct limit_config, x)
93 CF_LIST("Types", P(types), &cf_string_list_config),
94 CF_DOUBLE("Rate", P(rate)),
95 CF_UINT("Burst", P(burst)),
101 static struct cf_section stream_config = {
102 CF_TYPE(struct stream_config),
103 CF_INIT(stream_init),
104 CF_COMMIT(stream_commit),
106 #define P(x) PTR_TO(struct stream_config, x)
107 CF_STRING("Name", P(name)),
108 CF_STRING("FileName", P(file_name)),
109 CF_INT("FileDesc", P(file_desc)),
110 CF_STRING("SyslogFacility", P(syslog_facility)),
111 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
112 CF_LIST("Types", P(types), &cf_string_list_config),
113 CF_LIST("Substream", P(substreams), &cf_string_list_config),
114 CF_LIST("Limit", P(limits), &limit_config),
115 CF_INT("Microseconds", P(microseconds)),
116 CF_INT("ShowTypes", P(show_types)),
117 CF_INT("SyslogPID", P(syslog_pids)),
118 CF_INT("ErrorsFatal", P(errors_fatal)),
119 CF_INT("StdErrFollows", P(stderr_follows)),
125 static clist log_stream_confs;
127 static struct stream_config *
128 stream_find(const char *name)
130 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
131 if (!strcmp(c->name, name))
137 stream_resolve(struct stream_config *c)
142 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
146 CLIST_FOR_EACH(simp_node *, s, c->substreams)
148 struct stream_config *d = stream_find(s->s);
150 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
151 if (err = stream_resolve(d))
159 log_config_commit(void *ptr UNUSED)
161 // Verify uniqueness of names
162 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
163 if (stream_find(c->name) != c)
164 return cf_printf("Log stream `%s' defined twice", c->name);
166 // Check that all substreams resolve and that there are no cycles
168 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
169 if (err = stream_resolve(c))
175 static struct cf_section log_config = {
176 CF_COMMIT(log_config_commit),
178 CF_LIST("Stream", &log_stream_confs, &stream_config),
183 static void CONSTRUCTOR
184 log_config_init(void)
186 cf_declare_section("Logging", &log_config, 0);
192 log_type_mask(clist *l)
198 CLIST_FOR_EACH(simp_node *, s, *l)
199 if (!strcmp(s->s, "all"))
204 * We intentionally ignore unknown types as not all types are known
205 * to all programs sharing a common configuration file. This is also
206 * the reason why Types is a list and not a bitmap.
208 int type = log_find_type(s->s);
210 types |= 1 << LS_GET_TYPE(type);
215 /*** Generating limiters ***/
218 * When limiting is enabled, we let log_stream->filter point to this function
219 * and log_stream->user_data point to an array of pointers to token bucket
220 * filters for individual message types.
223 log_limiter(struct log_stream *ls, struct log_msg *m)
225 struct token_bucket_filter **limits = ls->user_data;
228 struct token_bucket_filter *tbf = limits[LS_GET_TYPE(m->flags)];
232 ASSERT(!(m->flags & L_SIGHANDLER));
233 if (m->flags & L_LOGGER_ERR)
236 timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
238 int res = tbf_limit(tbf, now);
245 struct log_msg mm = *m;
246 mm.flags |= L_LOGGER_ERR;
247 mm.raw_msg = "(maximum logging rate exceeded, some messages will be suppressed)";
248 log_pass_msg(ls, &mm);
257 log_apply_limits(struct log_stream *ls, struct limit_config *lim)
259 uint mask = log_type_mask(&lim->types);
265 ls->user_data = cf_malloc_zero(LS_NUM_TYPES * sizeof(struct token_bucket_filter *));
266 ls->filter = log_limiter;
268 struct token_bucket_filter **limits = ls->user_data;
269 struct token_bucket_filter *tbf = cf_malloc_zero(sizeof(*lim));
270 tbf->rate = lim->rate;
271 tbf->burst = lim->burst;
274 for (uint i=0; i < LS_NUM_TYPES; i++)
279 /*** Generating streams ***/
282 log_check_configured(const char *name)
284 if (stream_find(name))
287 return cf_printf("Log stream `%s' not found", name);
290 static struct log_stream *
291 do_new_configured(struct stream_config *c)
293 struct log_stream *ls;
300 ls = log_new_file(c->file_name, (c->stderr_follows ? FF_FD2_FOLLOWS : 0));
301 else if (c->file_desc >= 0)
302 ls = log_new_fd(c->file_desc, (c->stderr_follows ? FF_FD2_FOLLOWS : 0));
303 else if (c->syslog_facility)
304 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
306 ls = log_new_stream(sizeof(*ls));
308 CLIST_FOR_EACH(simp_node *, s, c->substreams)
309 log_add_substream(ls, do_new_configured(stream_find(s->s)));
311 ls->levels = c->levels;
313 ls->msgfmt |= LSFMT_USEC;
315 ls->msgfmt |= LSFMT_TYPE;
317 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
318 ls->types = log_type_mask(&c->types);
320 CLIST_FOR_EACH(struct limit_config *, lim, c->limits)
321 log_apply_limits(ls, lim);
328 log_new_configured(const char *name)
330 struct stream_config *c = stream_find(name);
332 die("Unable to find log stream %s", name);
334 return log_ref_stream(c->ls);
335 return do_new_configured(c);
339 log_configured(const char *name)
341 log_set_default_stream(log_new_configured(name));
347 #include <ucw/getopt.h>
349 int main(int argc, char **argv)
353 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
354 die("No options here.");
356 int type = log_register_type("foo");
357 struct log_stream *ls = log_new_configured("combined");
359 for (uint i=0; i<10; i++)
361 msg(L_INFO | ls->regnum | type, "Hello, universe!");
364 fprintf(stderr, "Alas, this was printed to stderr.\n");