]> mj.ucw.cz Git - libucw.git/blob - ucw/log-stream.c
Logging: Added log_set_format() and log_default_stream().
[libucw.git] / ucw / log-stream.c
1 /*
2  *      UCW Library -- Logging: Management of Log Streams
3  *
4  *      (c) 2008 Tomas Gavenciak <gavento@ucw.cz>
5  *      (c) 2009 Martin Mares <mj@ucw.cz>
6  *
7  *      This software may be freely distributed and used according to the terms
8  *      of the GNU Lesser General Public License.
9  */
10
11 #include "ucw/lib.h"
12 #include "ucw/log.h"
13 #include "ucw/simple-lists.h"
14
15 #include <string.h>
16
17 /* Initial number of streams to allocate (must be >=2) */
18 #define LS_INIT_STREAMS 8
19
20 /* Flag indicating initialization of the module */
21 static int log_initialized = 0;
22
23 /* The head of the list of freed log_streams indexes in log_streams.ptr (~0U 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].levels is the index of next freed position (or ~0U) */
26 static uns log_streams_free = ~0U;
27
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). */
31 static void
32 log_init_module(void)
33 {
34   if (log_initialized)
35     return;
36
37   /* Create the growing array */
38   lsbuf_init(&log_streams);
39   lsbuf_set_size(&log_streams, LS_INIT_STREAMS);
40
41   bzero(log_streams.ptr, sizeof(struct log_stream*) * (log_streams.len));
42   log_streams_free = ~0U;
43
44   log_initialized = 1;
45
46   /* init the default stream (0) as forwarder to fd2 */
47   struct log_stream *ls = log_new_stream(sizeof(*ls));
48   ASSERT(ls == log_streams.ptr[0]);
49   ASSERT(ls->regnum == 0);
50   ls->name = "default";
51   log_add_substream(ls, &log_stream_default);
52 }
53
54 /* Close all open streams, un-initialize the module, free all memory,
55  * and fall back to using only log_stream_default. */
56 void
57 log_close_all(void)
58 {
59   if (!log_initialized)
60     return;
61
62   for (int i=0; i < log_streams_after; i++)
63     {
64       if (log_streams.ptr[i]->regnum >= 0)
65         log_close_stream(log_streams.ptr[i]);
66       xfree(log_streams.ptr[i]);
67     }
68
69   /* Back to the default state */
70   lsbuf_done(&log_streams);
71   log_streams_after = 0;
72   log_streams_free = ~0U;
73   log_initialized = 0;
74 }
75
76 /* Add a new substream. The parent stream takes a reference on the substream,
77  * preventing it from being closed as long as it is linked. */
78 void
79 log_add_substream(struct log_stream *where, struct log_stream *what)
80 {
81   ASSERT(where);
82   ASSERT(what);
83
84   simp_node *n = xmalloc(sizeof(simp_node));
85   n->p = log_ref_stream(what);
86   clist_add_tail(&where->substreams, &n->n);
87 }
88
89 /* Remove all occurrences of a substream together with the references they
90  * keep. If a substream becomes unreferenced, it is closed. If what is NULL,
91  * all substreams are removed. Returns the number of deleted entries. */
92 int
93 log_rm_substream(struct log_stream *where, struct log_stream *what)
94 {
95   void *tmp;
96   int cnt = 0;
97   ASSERT(where);
98
99   CLIST_FOR_EACH_DELSAFE(simp_node *, i, where->substreams, tmp)
100     if (i->p == what || !what)
101       {
102         clist_remove(&i->n);
103         log_close_stream(i->p);
104         xfree(i);
105         cnt++;
106       }
107   return cnt;
108 }
109
110 /* Return a pointer to a new stream with no handler and an empty substream list. */
111 struct log_stream *
112 log_new_stream(size_t size)
113 {
114   struct log_stream *l;
115   int index;
116
117   /* Initialize the data structures if needed */
118   log_init_module();
119
120   /* Get a free stream, possibly recycling a closed one */
121   if (log_streams_free == ~0U)
122     {
123       lsbuf_grow(&log_streams, log_streams_after+1);
124       index = log_streams_after++;
125       l = log_streams.ptr[index] = xmalloc(size);
126     }
127   else
128     {
129       index = log_streams_free;
130       l = xrealloc(log_streams.ptr[index], size);
131       log_streams.ptr[index] = l;
132       log_streams_free = l->levels;
133     }
134
135   /* Initialize the stream */
136   bzero(l, sizeof(*l));
137   l->levels = LS_ALL_LEVELS;
138   l->regnum = LS_SET_STRNUM(index);
139   clist_init(&l->substreams);
140   return log_ref_stream(l);
141 }
142
143 /* Remove a reference on a stream and close it if it was the last reference.
144  * Closing automatically unlinks all substreams and closes them if they are
145  * no longer referenced. Returns 1 if the stream has been really closed. */
146 int
147 log_close_stream(struct log_stream *ls)
148 {
149   ASSERT(ls);
150   ASSERT(ls->use_count);
151   if (--ls->use_count)
152     return 0;
153
154   /* Unlink all subtreams */
155   log_rm_substream(ls, NULL);
156
157   /* Close the stream and add it to the free-list */
158   if (ls->close)
159     ls->close(ls);
160   ls->levels = log_streams_free;
161   log_streams_free = LS_GET_STRNUM(ls->regnum);
162   ls->regnum = -1;
163   return 1;
164 }
165
166 void
167 log_set_format(struct log_stream *ls, uns mask, uns data)
168 {
169   ls->msgfmt = (ls->msgfmt & mask) | data;
170   CLIST_FOR_EACH(simp_node *, i, ls->substreams)
171     log_set_format(i->p, mask, data);
172 }