2 * UCW Library -- Logging: Management of Log Streams
4 * (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
5 * (c) 2009 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
13 #include "ucw/simple-lists.h"
17 /* Initial number of streams to allocate (must be >=2) */
18 #define LS_INIT_STREAMS 8
20 /* Flag indicating initialization of the module */
21 static int log_initialized = 0;
23 /* The head of the list of freed log_streams indexes in log_streams.ptr (-1 if none free).
24 * Freed positions in log_streams.ptr are connected into a linked list in the following way:
25 * log_streams.ptr[log_streams_free].idata is the index of next freed position (or -1) */
26 static int log_streams_free = -1;
28 /* Initialize the logstream module.
29 * It is not neccessary to call this explicitely as it is called by
30 * the first log_new_stream() (for backward compatibility and ease of use). */
37 /* Create the growing array */
38 lsbuf_init(&log_streams);
39 lsbuf_set_size(&log_streams, LS_INIT_STREAMS);
41 bzero(log_streams.ptr, sizeof(struct log_stream*) * (log_streams.len));
42 log_streams_free = -1;
46 /* init the default stream (0) as forwarder to fd2 */
47 struct log_stream *ls = log_new_stream();
48 ASSERT(ls == log_streams.ptr[0]);
49 ASSERT(ls->regnum == 0);
51 log_add_substream(ls, (struct log_stream *) &log_stream_default);
54 /* Close all open streams, un-initialize the module, free all memory,
55 * and fall back to using only log_stream_default. */
62 for (int i=0; i < log_streams_after; i++)
64 if (log_streams.ptr[i]->regnum >= 0)
65 log_close_stream(log_streams.ptr[i]);
66 xfree(log_streams.ptr[i]);
69 /* Back to the default state */
70 lsbuf_done(&log_streams);
71 log_streams_after = 0;
72 log_streams_free = -1;
76 /* Add a new substream, xmalloc()-ate a new simp_node. */
78 log_add_substream(struct log_stream *where, struct log_stream *what)
83 simp_node *n = xmalloc(sizeof(simp_node));
85 clist_add_tail(&where->substreams, &n->n);
88 /* Remove all occurences of a substream, xfree() the simp_node. */
89 /* Return the number of deleted entries. */
91 log_rm_substream(struct log_stream *where, struct log_stream *what)
98 CLIST_FOR_EACH_DELSAFE(simp_node *, i, where->substreams, tmp)
108 /* Return a pointer to a new stream with no handler and an empty substream list. */
112 struct log_stream *l;
115 /* Initialize the data structures if needed */
118 /* Get a free stream, possibly recycling a closed one */
119 if (log_streams_free < 0)
121 lsbuf_grow(&log_streams, log_streams_after+1);
122 index = log_streams_after++;
123 l = log_streams.ptr[index] = xmalloc(sizeof(struct log_stream));
127 index = log_streams_free;
128 l = log_streams.ptr[index];
129 log_streams_free = l->idata;
132 /* Initialize the stream */
133 bzero(l, sizeof(*l));
134 l->levels = LS_ALL_LEVELS;
135 l->regnum = LS_SET_STRNUM(index);
136 clist_init(&l->substreams);
140 /* Close a stream, unlink (but do not close) all its substreams */
142 log_close_stream(struct log_stream *ls)
147 /* xfree() all the simp_nodes from substreams */
148 CLIST_FOR_EACH_DELSAFE(simp_node *, i, ls->substreams, tmp)
154 /* Close the stream and add it to the free-list */
157 ls->idata = log_streams_free;
158 log_streams_free = LS_GET_STRNUM(ls->regnum);