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.
13 #include "ucw/simple-lists.h"
15 #include "ucw/threads.h"
21 /*** Configuration of streams ***/
23 struct stream_config {
27 char *syslog_facility;
29 clist types; // simple_list of names
30 clist substreams; // simple_list of names
31 clist limits; // of struct limit_config's
32 int microseconds; // Enable logging of precise timestamps
36 struct log_stream *ls;
37 int mark; // Used temporarily in log_config_commit()
42 clist types; // simple_list of names
48 stream_init(void *ptr)
50 struct stream_config *c = ptr;
57 stream_commit(void *ptr)
59 struct stream_config *c = ptr;
61 if (c->file_name && c->syslog_facility)
62 return "Both FileName and SyslogFacility selected";
63 if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
64 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
65 if (c->syslog_facility && c->microseconds)
66 return "Syslog streams do not support microsecond precision";
70 static const char * const level_names[] = {
77 static struct cf_section limit_config = {
78 CF_TYPE(struct limit_config),
80 #define P(x) PTR_TO(struct limit_config, x)
81 CF_LIST("Types", P(types), &cf_string_list_config),
82 CF_DOUBLE("Rate", P(rate)),
83 CF_UNS("Burst", P(burst)),
89 static struct cf_section stream_config = {
90 CF_TYPE(struct stream_config),
92 CF_COMMIT(stream_commit),
94 #define P(x) PTR_TO(struct stream_config, x)
95 CF_STRING("Name", P(name)),
96 CF_STRING("FileName", P(file_name)),
97 CF_STRING("SyslogFacility", P(syslog_facility)),
98 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
99 CF_LIST("Types", P(types), &cf_string_list_config),
100 CF_LIST("Substream", P(substreams), &cf_string_list_config),
101 CF_LIST("Limit", P(limits), &limit_config),
102 CF_INT("Microseconds", P(microseconds)),
103 CF_INT("ShowTypes", P(show_types)),
104 CF_INT("SyslogPID", P(syslog_pids)),
105 CF_INT("ErrorsFatal", P(errors_fatal)),
111 static clist log_stream_confs;
113 static struct stream_config *
114 stream_find(const char *name)
116 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
117 if (!strcmp(c->name, name))
123 stream_resolve(struct stream_config *c)
128 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
132 CLIST_FOR_EACH(simp_node *, s, c->substreams)
134 struct stream_config *d = stream_find(s->s);
136 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
137 if (err = stream_resolve(d))
145 log_config_commit(void *ptr UNUSED)
147 // Verify uniqueness of names
148 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
149 if (stream_find(c->name) != c)
150 return cf_printf("Log stream `%s' defined twice", c->name);
152 // Check that all substreams resolve and that there are no cycles
154 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
155 if (err = stream_resolve(c))
161 static struct cf_section log_config = {
162 CF_COMMIT(log_config_commit),
164 CF_LIST("Stream", &log_stream_confs, &stream_config),
169 static void CONSTRUCTOR
170 log_config_init(void)
172 cf_declare_section("Logging", &log_config, 0);
178 log_type_mask(clist *l)
184 CLIST_FOR_EACH(simp_node *, s, *l)
185 if (!strcmp(s->s, "all"))
190 * We intentionally ignore unknown types as not all types are known
191 * to all programs sharing a common configuration file. This is also
192 * the reason why Types is a list and not a bitmap.
194 int type = log_find_type(s->s);
196 types |= 1 << LS_GET_TYPE(type);
201 /*** Generating limiters ***/
204 * When limiting is enabled, we let log_stream->filter point to this function
205 * and log_stream->user_data point to an array of pointers to token bucket
206 * filters for individual message types.
209 log_limiter(struct log_stream *ls, struct log_msg *m)
211 struct token_bucket_filter **limits = ls->user_data;
214 struct token_bucket_filter *tbf = limits[LS_GET_TYPE(m->flags)];
218 ASSERT(!(m->flags & L_SIGHANDLER));
219 timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
222 int res = tbf_limit(tbf, now);
228 log_apply_limits(struct log_stream *ls, struct limit_config *lim)
232 ls->user_data = cf_malloc_zero(LS_NUM_TYPES * sizeof(struct token_bucket_filter *));
233 ls->filter = log_limiter;
235 struct token_bucket_filter **limits = ls->user_data;
236 struct token_bucket_filter *tbf = cf_malloc_zero(sizeof(*lim));
237 tbf->rate = lim->rate;
238 tbf->burst = lim->burst;
241 uns mask = log_type_mask(&lim->types);
242 for (uns i=0; i < LS_NUM_TYPES; i++)
247 /*** Generating streams ***/
250 log_check_configured(const char *name)
252 if (stream_find(name))
255 return cf_printf("Log stream `%s' not found", name);
258 static struct log_stream *
259 do_new_configured(struct stream_config *c)
261 struct log_stream *ls;
268 ls = log_new_file(c->file_name);
269 else if (c->syslog_facility)
270 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
272 ls = log_new_stream(sizeof(*ls));
274 CLIST_FOR_EACH(simp_node *, s, c->substreams)
275 log_add_substream(ls, do_new_configured(stream_find(s->s)));
277 ls->levels = c->levels;
279 ls->msgfmt |= LSFMT_USEC;
281 ls->msgfmt |= LSFMT_TYPE;
283 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
284 ls->types = log_type_mask(&c->types);
286 CLIST_FOR_EACH(struct limit_config *, lim, c->limits)
287 log_apply_limits(ls, lim);
294 log_new_configured(const char *name)
296 struct stream_config *c = stream_find(name);
298 die("Unable to find log stream %s", name);
300 return log_ref_stream(c->ls);
301 return do_new_configured(c);
305 log_configured(const char *name)
307 struct log_stream *ls = log_new_configured(name);
308 struct log_stream *def = log_stream_by_flags(0);
309 log_rm_substream(def, NULL);
310 log_add_substream(def, ls);
311 log_close_stream(ls);
316 #include "ucw/getopt.h"
318 int main(int argc, char **argv)
322 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
323 die("No options here.");
325 int type = log_register_type("foo");
326 struct log_stream *ls = log_new_configured("combined");
327 msg(L_INFO | ls->regnum | type, "Hello, universe!");