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/log-internal.h"
14 #include "ucw/simple-lists.h"
18 /* Initial number of streams to allocate (must be >=2) */
19 #define LS_INIT_STREAMS 8
21 /* Flag indicating initialization of the module */
22 static int log_initialized = 0;
24 /* The head of the list of freed log_streams indexes in log_streams.ptr (~0U if none free).
25 * Freed positions in log_streams.ptr are connected into a linked list in the following way:
26 * log_streams.ptr[log_streams_free].levels is the index of next freed position (or ~0U) */
27 static uns log_streams_free = ~0U;
29 /* Initialize the logstream module.
30 * It is not neccessary to call this explicitely as it is called by
31 * the first log_new_stream() (for backward compatibility and ease of use). */
38 /* Create the growing array */
39 lsbuf_init(&log_streams);
40 lsbuf_set_size(&log_streams, LS_INIT_STREAMS);
42 bzero(log_streams.ptr, sizeof(struct log_stream*) * (log_streams.len));
43 log_streams_free = ~0U;
47 /* init the default stream (0) as forwarder to fd2 */
48 struct log_stream *ls = log_new_stream(sizeof(*ls));
49 ASSERT(ls == log_streams.ptr[0]);
50 ASSERT(ls->regnum == 0);
52 log_add_substream(ls, &log_stream_default);
61 // Close all open streams
62 for (int i=0; i < log_streams_after; i++)
63 if (log_streams.ptr[i]->regnum >= 0)
64 log_close_stream(log_streams.ptr[i]);
66 // Free all cached structures
67 for (int i=0; i < log_streams_after; i++)
69 struct log_stream *ls = log_streams.ptr[i];
70 ASSERT(ls->regnum < 0 || !ls->use_count);
74 /* Back to the default state */
75 lsbuf_done(&log_streams);
76 log_streams_after = 0;
77 log_streams_free = ~0U;
82 log_add_substream(struct log_stream *where, struct log_stream *what)
87 simp_node *n = xmalloc(sizeof(simp_node));
88 n->p = log_ref_stream(what);
89 clist_add_tail(&where->substreams, &n->n);
93 log_rm_substream(struct log_stream *where, struct log_stream *what)
99 CLIST_FOR_EACH_DELSAFE(simp_node *, i, where->substreams, tmp)
100 if (i->p == what || !what)
103 log_close_stream(i->p);
111 log_new_stream(size_t size)
113 struct log_stream *l;
116 /* Initialize the data structures if needed */
119 /* Get a free stream, possibly recycling a closed one */
120 if (log_streams_free == ~0U)
122 lsbuf_grow(&log_streams, log_streams_after+1);
123 index = log_streams_after++;
124 l = log_streams.ptr[index] = xmalloc(size);
128 index = log_streams_free;
129 l = xrealloc(log_streams.ptr[index], size);
130 log_streams.ptr[index] = l;
131 log_streams_free = l->levels;
134 /* Initialize the stream */
135 bzero(l, sizeof(*l));
137 l->regnum = LS_SET_STRNUM(index);
138 clist_init(&l->substreams);
139 return log_ref_stream(l);
143 log_close_stream(struct log_stream *ls)
146 ASSERT(ls->use_count);
150 /* Unlink all subtreams */
151 log_rm_substream(ls, NULL);
153 /* Close the stream and add it to the free-list */
156 ls->levels = log_streams_free;
157 log_streams_free = LS_GET_STRNUM(ls->regnum);
163 log_set_format(struct log_stream *ls, uns mask, uns data)
165 ls->msgfmt = (ls->msgfmt & mask) | data;
166 CLIST_FOR_EACH(simp_node *, i, ls->substreams)
167 log_set_format(i->p, mask, data);