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"
23 /*** Configuration of streams ***/
25 struct stream_config {
29 char *syslog_facility;
31 clist types; // simple_list of names
32 clist substreams; // simple_list of names
33 clist limits; // of struct limit_config's
34 int microseconds; // Enable logging of precise timestamps
39 struct log_stream *ls;
40 int mark; // Used temporarily in log_config_commit()
45 clist types; // simple_list of names
51 stream_init(void *ptr)
53 struct stream_config *c = ptr;
60 stream_commit(void *ptr)
62 struct stream_config *c = ptr;
64 if (c->syslog_facility)
66 if (!log_syslog_facility_exists(c->syslog_facility))
67 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
69 return "Both FileName and SyslogFacility selected";
71 return "Syslog streams do not support microsecond precision";
73 if (c->stderr_follows && !c->file_name)
74 return "StdErrFollows requires a file-based stream";
78 static const char * const level_names[] = {
85 static struct cf_section limit_config = {
86 CF_TYPE(struct limit_config),
88 #define P(x) PTR_TO(struct limit_config, x)
89 CF_LIST("Types", P(types), &cf_string_list_config),
90 CF_DOUBLE("Rate", P(rate)),
91 CF_UNS("Burst", P(burst)),
97 static struct cf_section stream_config = {
98 CF_TYPE(struct stream_config),
100 CF_COMMIT(stream_commit),
102 #define P(x) PTR_TO(struct stream_config, x)
103 CF_STRING("Name", P(name)),
104 CF_STRING("FileName", P(file_name)),
105 CF_STRING("SyslogFacility", P(syslog_facility)),
106 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
107 CF_LIST("Types", P(types), &cf_string_list_config),
108 CF_LIST("Substream", P(substreams), &cf_string_list_config),
109 CF_LIST("Limit", P(limits), &limit_config),
110 CF_INT("Microseconds", P(microseconds)),
111 CF_INT("ShowTypes", P(show_types)),
112 CF_INT("SyslogPID", P(syslog_pids)),
113 CF_INT("ErrorsFatal", P(errors_fatal)),
114 CF_INT("StdErrFollows", P(stderr_follows)),
120 static clist log_stream_confs;
122 static struct stream_config *
123 stream_find(const char *name)
125 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
126 if (!strcmp(c->name, name))
132 stream_resolve(struct stream_config *c)
137 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
141 CLIST_FOR_EACH(simp_node *, s, c->substreams)
143 struct stream_config *d = stream_find(s->s);
145 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
146 if (err = stream_resolve(d))
154 log_config_commit(void *ptr UNUSED)
156 // Verify uniqueness of names
157 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
158 if (stream_find(c->name) != c)
159 return cf_printf("Log stream `%s' defined twice", c->name);
161 // Check that all substreams resolve and that there are no cycles
163 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
164 if (err = stream_resolve(c))
170 static struct cf_section log_config = {
171 CF_COMMIT(log_config_commit),
173 CF_LIST("Stream", &log_stream_confs, &stream_config),
178 static void CONSTRUCTOR
179 log_config_init(void)
181 cf_declare_section("Logging", &log_config, 0);
187 log_type_mask(clist *l)
193 CLIST_FOR_EACH(simp_node *, s, *l)
194 if (!strcmp(s->s, "all"))
199 * We intentionally ignore unknown types as not all types are known
200 * to all programs sharing a common configuration file. This is also
201 * the reason why Types is a list and not a bitmap.
203 int type = log_find_type(s->s);
205 types |= 1 << LS_GET_TYPE(type);
210 /*** Generating limiters ***/
213 * When limiting is enabled, we let log_stream->filter point to this function
214 * and log_stream->user_data point to an array of pointers to token bucket
215 * filters for individual message types.
218 log_limiter(struct log_stream *ls, struct log_msg *m)
220 struct token_bucket_filter **limits = ls->user_data;
223 struct token_bucket_filter *tbf = limits[LS_GET_TYPE(m->flags)];
227 ASSERT(!(m->flags & L_SIGHANDLER));
228 if (m->flags & L_LOGGER_ERR)
231 timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
233 int res = tbf_limit(tbf, now);
240 struct log_msg mm = *m;
241 mm.flags |= L_LOGGER_ERR;
242 mm.raw_msg = "(maximum logging rate exceeded, some messages will be suppressed)";
243 log_pass_msg(0, ls, &mm);
252 log_apply_limits(struct log_stream *ls, struct limit_config *lim)
254 uns mask = log_type_mask(&lim->types);
260 ls->user_data = cf_malloc_zero(LS_NUM_TYPES * sizeof(struct token_bucket_filter *));
261 ls->filter = log_limiter;
263 struct token_bucket_filter **limits = ls->user_data;
264 struct token_bucket_filter *tbf = cf_malloc_zero(sizeof(*lim));
265 tbf->rate = lim->rate;
266 tbf->burst = lim->burst;
269 for (uns i=0; i < LS_NUM_TYPES; i++)
274 /*** Generating streams ***/
277 log_check_configured(const char *name)
279 if (stream_find(name))
282 return cf_printf("Log stream `%s' not found", name);
285 static struct log_stream *
286 do_new_configured(struct stream_config *c)
288 struct log_stream *ls;
295 ls = log_new_file(c->file_name, (c->stderr_follows ? FF_FD2_FOLLOWS : 0));
296 else if (c->syslog_facility)
297 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
299 ls = log_new_stream(sizeof(*ls));
301 CLIST_FOR_EACH(simp_node *, s, c->substreams)
302 log_add_substream(ls, do_new_configured(stream_find(s->s)));
304 ls->levels = c->levels;
306 ls->msgfmt |= LSFMT_USEC;
308 ls->msgfmt |= LSFMT_TYPE;
310 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
311 ls->types = log_type_mask(&c->types);
313 CLIST_FOR_EACH(struct limit_config *, lim, c->limits)
314 log_apply_limits(ls, lim);
321 log_new_configured(const char *name)
323 struct stream_config *c = stream_find(name);
325 die("Unable to find log stream %s", name);
327 return log_ref_stream(c->ls);
328 return do_new_configured(c);
332 log_configured(const char *name)
334 struct log_stream *ls = log_new_configured(name);
335 struct log_stream *def = log_stream_by_flags(0);
336 log_rm_substream(def, NULL);
337 log_add_substream(def, ls);
338 log_close_stream(ls);
344 #include "ucw/getopt.h"
346 int main(int argc, char **argv)
350 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
351 die("No options here.");
353 int type = log_register_type("foo");
354 struct log_stream *ls = log_new_configured("combined");
355 for (uns i=0; i<10; i++)
357 msg(L_INFO | ls->regnum | type, "Hello, universe!");
360 fprintf(stderr, "Alas, this was printed to stderr.\n");