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
27 struct log_stream *ls;
28 int mark; // Used temporarily in log_config_commit()
32 stream_init(void *ptr)
34 struct stream_config *c = ptr;
41 stream_commit(void *ptr)
43 struct stream_config *c = ptr;
45 if (c->file_name && c->syslog_facility)
46 return "Both FileName and SyslogFacility selected";
47 if (c->syslog_facility && !log_syslog_facility_exists(c->syslog_facility))
48 return cf_printf("SyslogFacility `%s' is not recognized", c->syslog_facility);
49 if (c->syslog_facility && c->microseconds)
50 return "Syslog streams do not support microsecond precision";
54 static const char * const level_names[] = {
61 static struct cf_section stream_config = {
62 CF_TYPE(struct stream_config),
64 CF_COMMIT(stream_commit),
66 #define P(x) PTR_TO(struct stream_config, x)
67 CF_STRING("Name", P(name)),
68 CF_STRING("FileName", P(file_name)),
69 CF_STRING("SyslogFacility", P(syslog_facility)),
70 CF_BITMAP_LOOKUP("Levels", P(levels), level_names),
71 CF_LIST("Substream", P(substreams), &cf_string_list_config),
72 CF_INT("Microseconds", P(microseconds)),
73 CF_INT("SyslogPID", P(syslog_pids)),
79 static clist log_stream_confs;
81 static struct stream_config *
82 stream_find(const char *name)
84 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
85 if (!strcmp(c->name, name))
91 stream_resolve(struct stream_config *c)
96 return cf_printf("Log stream `%s' has substreams which refer to itself", c->name);
100 CLIST_FOR_EACH(simp_node *, s, c->substreams)
102 struct stream_config *d = stream_find(s->s);
104 return cf_printf("Log stream `%s' refers to unknown substream `%s'", c->name, s->s);
105 if (err = stream_resolve(d))
113 log_config_commit(void *ptr UNUSED)
115 // Verify uniqueness of names
116 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
117 if (stream_find(c->name) != c)
118 return cf_printf("Log stream `%s' defined twice", c->name);
120 // Check that all substreams resolve and that there are no cycles
122 CLIST_FOR_EACH(struct stream_config *, c, log_stream_confs)
123 if (err = stream_resolve(c))
129 static struct cf_section log_config = {
130 CF_COMMIT(log_config_commit),
132 CF_LIST("Stream", &log_stream_confs, &stream_config),
137 static void CONSTRUCTOR
138 log_config_init(void)
140 cf_declare_section("Logging", &log_config, 0);
144 log_check_configured(const char *name)
146 if (stream_find(name))
149 return cf_printf("Log stream `%s' not found", name);
152 static struct log_stream *
153 do_new_configured(struct stream_config *c)
155 struct log_stream *ls;
162 ls = log_new_file(c->file_name);
163 else if (c->syslog_facility)
164 ls = log_new_syslog(c->syslog_facility, (c->syslog_pids ? LOG_PID : 0));
166 ls = log_new_stream(sizeof(*ls));
168 CLIST_FOR_EACH(simp_node *, s, c->substreams)
169 log_add_substream(ls, do_new_configured(stream_find(s->s)));
171 ls->levels = c->levels;
173 ls->msgfmt |= LSFMT_USEC;
180 log_new_configured(const char *name)
182 struct stream_config *c = stream_find(name);
184 die("Unable to find log stream %s", name);
186 return log_ref_stream(c->ls);
187 return do_new_configured(c);
191 log_configured(const char *name)
193 struct log_stream *ls = log_new_configured(name);
194 struct log_stream *def = log_stream_by_flags(0);
195 log_rm_substream(def, NULL);
196 log_add_substream(def, ls);
197 log_close_stream(ls);
202 #include "ucw/getopt.h"
204 int main(int argc, char **argv)
208 while ((c = cf_getopt(argc, argv, CF_SHORT_OPTS, CF_NO_LONG_OPTS, NULL)) >= 0)
209 die("No options here.");
211 struct log_stream *ls = log_new_configured("combined");
212 msg(L_INFO | ls->regnum, "Hello, universe!");