X-Git-Url: http://mj.ucw.cz/gitweb/?a=blobdiff_plain;ds=inline;f=ucw%2Flog-stream.c;h=342fe098872f6f213e7335826d92bbce930b55a8;hb=156301aa8c7fd24d49ed27cfbd1afc10ccb7bc58;hp=1ca76a5567215da0b4c67179dc1bb8063f22ae38;hpb=2bc61d56ea0e8a70fb3d15a51fbfbce013c9a648;p=libucw.git diff --git a/ucw/log-stream.c b/ucw/log-stream.c index 1ca76a55..342fe098 100644 --- a/ucw/log-stream.c +++ b/ucw/log-stream.c @@ -2,16 +2,16 @@ * UCW Library -- Logging: Management of Log Streams * * (c) 2008 Tomas Gavenciak - * (c) 2009 Martin Mares + * (c) 2009--2012 Martin Mares * * This software may be freely distributed and used according to the terms * of the GNU Lesser General Public License. */ -#include "ucw/lib.h" -#include "ucw/log.h" -#include "ucw/log-internal.h" -#include "ucw/simple-lists.h" +#include +#include +#include +#include #include @@ -24,7 +24,7 @@ static int log_initialized = 0; /* The head of the list of freed log_streams indexes in log_streams.ptr (~0U if none free). * Freed positions in log_streams.ptr are connected into a linked list in the following way: * log_streams.ptr[log_streams_free].levels is the index of next freed position (or ~0U) */ -static uns log_streams_free = ~0U; +static uint log_streams_free = ~0U; /* Initialize the logstream module. * It is not neccessary to call this explicitely as it is called by @@ -58,15 +58,17 @@ log_close_all(void) if (!log_initialized) return; - // Close all open streams + // Remove substreams of all streams for (int i=0; i < log_streams_after; i++) if (log_streams.ptr[i]->regnum >= 0) - log_close_stream(log_streams.ptr[i]); + log_rm_substream(log_streams.ptr[i], NULL); - // Free all cached structures + // Close all streams that remain and free all cached structures for (int i=0; i < log_streams_after; i++) { struct log_stream *ls = log_streams.ptr[i]; + if (ls->regnum >= 0) + log_close_stream(ls); ASSERT(ls->regnum < 0 || !ls->use_count); xfree(ls); } @@ -134,6 +136,7 @@ log_new_stream(size_t size) /* Initialize the stream */ bzero(l, sizeof(*l)); l->levels = ~0U; + l->types = ~0U; l->regnum = LS_SET_STRNUM(index); clist_init(&l->substreams); return log_ref_stream(l); @@ -160,9 +163,50 @@ log_close_stream(struct log_stream *ls) } void -log_set_format(struct log_stream *ls, uns mask, uns data) +log_set_format(struct log_stream *ls, uint mask, uint data) { ls->msgfmt = (ls->msgfmt & mask) | data; CLIST_FOR_EACH(simp_node *, i, ls->substreams) log_set_format(i->p, mask, data); } + +void +log_set_default_stream(struct log_stream *ls) +{ + struct log_stream *def = log_stream_by_flags(0); + log_rm_substream(def, NULL); + log_add_substream(def, ls); + log_close_stream(ls); +} + +/*** Registry of type names ***/ + +int log_register_type(const char *name) +{ + if (!log_type_names) + { + log_type_names = xmalloc_zero(LS_NUM_TYPES * sizeof(char *)); + log_type_names[0] = "default"; + } + uint id; + for (id=0; id < LS_NUM_TYPES && log_type_names[id]; id++) + if (!strcmp(log_type_names[id], name)) + return LS_SET_TYPE(id); + ASSERT(id < LS_NUM_TYPES); + log_type_names[id] = xstrdup(name); + return LS_SET_TYPE(id); +} + +/** Find a message type by name and return its ID encoded by `LS_SET_TYPE`. Returns -1 if no such type found. **/ +int log_find_type(const char *name) +{ + if (!strcmp(name, "default")) + return 0; + if (!log_type_names) + return -1; + + for (uint id=0; id < LS_NUM_TYPES && log_type_names[id]; id++) + if (!strcmp(log_type_names[id], name)) + return LS_SET_TYPE(id); + return -1; +}