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;
23 clist substreams; // simple_list of names
24 struct log_stream *ls;
25 int mark; // Used temporarily in log_config_commit()
29 stream_commit(void *ptr)
31 struct stream_config *c = ptr;
33 if (c->file_name && c->syslog_facility)
34 return "Both FileName and SyslogFacility selected";
35 if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
36 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
40 static struct cf_section stream_config = {
41 CF_TYPE(struct stream_config),
42 CF_COMMIT(stream_commit),
44 #define P(x) PTR_TO(struct stream_config, x)
45 CF_STRING("Name", P(name)),
46 CF_STRING("FileName", P(file_name)),
47 CF_STRING("SyslogFacility", P(syslog_facility)),
48 CF_LIST("Substream", P(substreams), &cf_string_list_config),
54 static clist log_stream_confs;
56 static struct stream_config *
57 stream_find(const char *name)
59 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
60 if (!strcmp(c->name, name))
66 stream_resolve(struct stream_config *c)
71 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
75 CLIST_FOR_EACH(simp_node *, s, c->substreams)
77 struct stream_config *d = stream_find(s->s);
79 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
80 if (err = stream_resolve(d))
88 log_config_commit(void *ptr UNUSED)
90 // Verify uniqueness of names
91 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
92 if (stream_find(c->name) != c)
93 return cf_printf("Log stream `%s' defined twice", c->name);
95 // Check that all substreams resolve and that there are no cycles
97 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
98 if (err = stream_resolve(c))
104 static struct cf_section log_config = {
105 CF_COMMIT(log_config_commit),
107 CF_LIST("Stream", &log_stream_confs, &stream_config),
112 static void CONSTRUCTOR
113 log_config_init(void)
115 cf_declare_section("Logging", &log_config, 0);
119 log_check_configured(const char *name)
121 if (stream_find(name))
124 return cf_printf("Log stream `%s' not found", name);
127 static struct log_stream *
128 do_new_configured(struct stream_config *c)
130 struct log_stream *ls;
137 ls = log_new_file(c->file_name);
138 else if (c->syslog_facility)
139 ls = log_new_syslog(c->syslog_facility, 0); // FIXME: Logging of PID
141 ls = log_new_stream(sizeof(*ls));
143 CLIST_FOR_EACH(simp_node *, s, c->substreams)
144 log_add_substream(ls, do_new_configured(stream_find(s->s)));
151 log_new_configured(const char *name)
153 struct stream_config *c = stream_find(name);
155 die("Unable to find log stream %s", name);
157 return log_ref_stream(c->ls);
158 return do_new_configured(c);
163 #include "ucw/getopt.h"
165 int main(int argc, char **argv)
169 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
170 die("No options here.");
172 struct log_stream *ls = log_new_configured("combined");
173 msg(L_INFO | ls->regnum, "Hello, universe!");