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"
18 struct stream_config {
22 char *syslog_facility;
24 clist substreams; // simple_list of names
25 int microseconds; // Enable logging of precise timestamps
28 struct log_stream *ls;
29 int mark; // Used temporarily in log_config_commit()
33 stream_init(void *ptr)
35 struct stream_config *c = ptr;
42 stream_commit(void *ptr)
44 struct stream_config *c = ptr;
46 if (c->file_name && c->syslog_facility)
47 return "Both FileName and SyslogFacility selected";
48 if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
49 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
50 if (c->syslog_facility && c->microseconds)
51 return "Syslog streams do not support microsecond precision";
55 static const char * const level_names[] = {
62 static struct cf_section stream_config = {
63 CF_TYPE(struct stream_config),
65 CF_COMMIT(stream_commit),
67 #define P(x) PTR_TO(struct stream_config, x)
68 CF_STRING("Name", P(name)),
69 CF_STRING("FileName", P(file_name)),
70 CF_STRING("SyslogFacility", P(syslog_facility)),
71 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
72 CF_LIST("Substream", P(substreams), &cf_string_list_config),
73 CF_INT("Microseconds", P(microseconds)),
74 CF_INT("SyslogPID", P(syslog_pids)),
75 CF_INT("ErrorsFatal", P(errors_fatal)),
81 static clist log_stream_confs;
83 static struct stream_config *
84 stream_find(const char *name)
86 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
87 if (!strcmp(c->name, name))
93 stream_resolve(struct stream_config *c)
98 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
102 CLIST_FOR_EACH(simp_node *, s, c->substreams)
104 struct stream_config *d = stream_find(s->s);
106 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
107 if (err = stream_resolve(d))
115 log_config_commit(void *ptr UNUSED)
117 // Verify uniqueness of names
118 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
119 if (stream_find(c->name) != c)
120 return cf_printf("Log stream `%s' defined twice", c->name);
122 // Check that all substreams resolve and that there are no cycles
124 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
125 if (err = stream_resolve(c))
131 static struct cf_section log_config = {
132 CF_COMMIT(log_config_commit),
134 CF_LIST("Stream", &log_stream_confs, &stream_config),
139 static void CONSTRUCTOR
140 log_config_init(void)
142 cf_declare_section("Logging", &log_config, 0);
146 log_check_configured(const char *name)
148 if (stream_find(name))
151 return cf_printf("Log stream `%s' not found", name);
154 static struct log_stream *
155 do_new_configured(struct stream_config *c)
157 struct log_stream *ls;
164 ls = log_new_file(c->file_name);
165 else if (c->syslog_facility)
166 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
168 ls = log_new_stream(sizeof(*ls));
170 CLIST_FOR_EACH(simp_node *, s, c->substreams)
171 log_add_substream(ls, do_new_configured(stream_find(s->s)));
173 ls->levels = c->levels;
175 ls->msgfmt |= LSFMT_USEC;
177 ls->stream_flags |= LSFLAG_ERR_IS_FATAL;
184 log_new_configured(const char *name)
186 struct stream_config *c = stream_find(name);
188 die("Unable to find log stream %s", name);
190 return log_ref_stream(c->ls);
191 return do_new_configured(c);
195 log_configured(const char *name)
197 struct log_stream *ls = log_new_configured(name);
198 struct log_stream *def = log_stream_by_flags(0);
199 log_rm_substream(def, NULL);
200 log_add_substream(def, ls);
201 log_close_stream(ls);
206 #include "ucw/getopt.h"
208 int main(int argc, char **argv)
212 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
213 die("No options here.");
215 struct log_stream *ls = log_new_configured("combined");
216 msg(L_INFO | ls->regnum, "Hello, universe!");