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 struct stream_config {
25 char *syslog_facility;
27 clist types; // simple_list of names
28 clist substreams; // simple_list of names
29 clist limits; // of struct limit_config's
30 int microseconds; // Enable logging of precise timestamps
34 struct log_stream *ls;
35 int mark; // Used temporarily in log_config_commit()
40 clist types; // simple_list of names
46 stream_init(void *ptr)
48 struct stream_config *c = ptr;
55 stream_commit(void *ptr)
57 struct stream_config *c = ptr;
59 if (c->file_name && c->syslog_facility)
60 return "Both FileName and SyslogFacility selected";
61 if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
62 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
63 if (c->syslog_facility && c->microseconds)
64 return "Syslog streams do not support microsecond precision";
68 static const char * const level_names[] = {
75 static struct cf_section limit_config = {
76 CF_TYPE(struct limit_config),
78 #define P(x) PTR_TO(struct limit_config, x)
79 CF_LIST("Types", P(types), &cf_string_list_config),
80 CF_DOUBLE("Rate", P(rate)),
81 CF_UNS("Burst", P(burst)),
87 static struct cf_section stream_config = {
88 CF_TYPE(struct stream_config),
90 CF_COMMIT(stream_commit),
92 #define P(x) PTR_TO(struct stream_config, x)
93 CF_STRING("Name", P(name)),
94 CF_STRING("FileName", P(file_name)),
95 CF_STRING("SyslogFacility", P(syslog_facility)),
96 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
97 CF_LIST("Types", P(types), &cf_string_list_config),
98 CF_LIST("Substream", P(substreams), &cf_string_list_config),
99 CF_LIST("Limit", P(limits), &limit_config),
100 CF_INT("Microseconds", P(microseconds)),
101 CF_INT("ShowTypes", P(show_types)),
102 CF_INT("SyslogPID", P(syslog_pids)),
103 CF_INT("ErrorsFatal", P(errors_fatal)),
109 static clist log_stream_confs;
111 static struct stream_config *
112 stream_find(const char *name)
114 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
115 if (!strcmp(c->name, name))
121 stream_resolve(struct stream_config *c)
126 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
130 CLIST_FOR_EACH(simp_node *, s, c->substreams)
132 struct stream_config *d = stream_find(s->s);
134 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
135 if (err = stream_resolve(d))
143 log_config_commit(void *ptr UNUSED)
145 // Verify uniqueness of names
146 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
147 if (stream_find(c->name) != c)
148 return cf_printf("Log stream `%s' defined twice", c->name);
150 // Check that all substreams resolve and that there are no cycles
152 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
153 if (err = stream_resolve(c))
159 static struct cf_section log_config = {
160 CF_COMMIT(log_config_commit),
162 CF_LIST("Stream", &log_stream_confs, &stream_config),
167 static void CONSTRUCTOR
168 log_config_init(void)
170 cf_declare_section("Logging", &log_config, 0);
174 log_check_configured(const char *name)
176 if (stream_find(name))
179 return cf_printf("Log stream `%s' not found", name);
183 log_type_mask(clist *l)
189 CLIST_FOR_EACH(simp_node *, s, *l)
190 if (!strcmp(s->s, "all"))
195 * We intentionally ignore unknown types as not all types are known
196 * to all programs sharing a common configuration file. This is also
197 * the reason why Types is a list and not a bitmap.
199 int type = log_find_type(s->s);
201 types |= 1 << LS_GET_TYPE(type);
207 * When limiting is enabled, we let log_stream->filter point to this function
208 * and log_stream->user_data point to an array of pointers to token bucket
209 * filters for individual message types.
212 log_limiter(struct log_stream *ls, struct log_msg *m)
214 struct token_bucket_filter **limits = ls->user_data;
217 struct token_bucket_filter *tbf = limits[LS_GET_TYPE(m->flags)];
221 ASSERT(!(m->flags & L_SIGHANDLER));
222 timestamp_t now = ((timestamp_t) m->tv->tv_sec * 1000) + (m->tv->tv_usec / 1000);
225 int res = tbf_limit(tbf, now);
231 log_apply_limits(struct log_stream *ls, struct limit_config *lim)
235 ls->user_data = cf_malloc_zero(LS_NUM_TYPES * sizeof(struct token_bucket_filter *));
236 ls->filter = log_limiter;
238 struct token_bucket_filter **limits = ls->user_data;
239 struct token_bucket_filter *tbf = cf_malloc_zero(sizeof(*lim));
240 tbf->rate = lim->rate;
241 tbf->burst = lim->burst;
244 uns mask = log_type_mask(&lim->types);
245 for (uns i=0; i < LS_NUM_TYPES; i++)
250 static struct log_stream *
251 do_new_configured(struct stream_config *c)
253 struct log_stream *ls;
260 ls = log_new_file(c->file_name);
261 else if (c->syslog_facility)
262 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
264 ls = log_new_stream(sizeof(*ls));
266 CLIST_FOR_EACH(simp_node *, s, c->substreams)
267 log_add_substream(ls, do_new_configured(stream_find(s->s)));
269 ls->levels = c->levels;
271 ls->msgfmt |= LSFMT_USEC;
273 ls->msgfmt |= LSFMT_TYPE;
275 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
276 ls->types = log_type_mask(&c->types);
278 CLIST_FOR_EACH(struct limit_config *, lim, c->limits)
279 log_apply_limits(ls, lim);
286 log_new_configured(const char *name)
288 struct stream_config *c = stream_find(name);
290 die("Unable to find log stream %s", name);
292 return log_ref_stream(c->ls);
293 return do_new_configured(c);
297 log_configured(const char *name)
299 struct log_stream *ls = log_new_configured(name);
300 struct log_stream *def = log_stream_by_flags(0);
301 log_rm_substream(def, NULL);
302 log_add_substream(def, ls);
303 log_close_stream(ls);
308 #include "ucw/getopt.h"
310 int main(int argc, char **argv)
314 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
315 die("No options here.");
317 int type = log_register_type("foo");
318 struct log_stream *ls = log_new_configured("combined");
319 msg(L_INFO | ls->regnum | type, "Hello, universe!");