]> git.sesse.net Git - vlc/blob - src/control/log.c
Implement reference counting in the libvlc logger
[vlc] / src / control / log.c
1 /*****************************************************************************
2  * log.c: libvlc new API log functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * $Id$
7  *
8  * Authors: Damien Fouilleul <damienf@videolan.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
27 #include <assert.h>
28
29 /* This API is terminally broken.
30  * First, it does not implement any kind of notification.
31  * Second, the iterating scheme is hermetic to any kind of thread-safety
32  * owing to its constant pointer constraints.
33  *  -- Courmisch
34  *
35  * "If you break your leg, don't run to me for sympathy"
36  *   -- some character, Beneath a Steel Sky
37  */
38
39 struct msg_cb_data_t
40 {
41     vlc_spinlock_t lock;
42     msg_item_t *items[VLC_MSG_QSIZE];
43     unsigned    count;
44     int         verbosity;
45 };
46
47 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
48 {
49     if (p_item->i_type > d->verbosity)
50         return;
51
52     vlc_spin_lock (&d->lock);
53     if (d->count < VLC_MSG_QSIZE)
54     {
55         d->items[d->count++] = p_item;
56         msg_Hold (p_item);
57     }
58     vlc_spin_unlock (&d->lock);
59     (void)i_drop;
60 }
61
62 struct libvlc_log_t
63 {
64     libvlc_instance_t  *p_instance;
65     msg_subscription_t *p_messages;
66     msg_cb_data_t       data;
67 };
68
69 struct libvlc_log_iterator_t
70 {
71     msg_cb_data_t *p_data;
72     unsigned i_pos;
73     unsigned i_end;
74 };
75
76 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
77 {
78     assert( p_instance );
79     (void)p_e;
80     return p_instance->verbosity;
81 }
82
83 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level, libvlc_exception_t *p_e )
84 {
85     assert( p_instance );
86     (void)p_e;
87     p_instance->verbosity = level;
88 }
89
90 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
91 {
92     struct libvlc_log_t *p_log =
93         (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
94
95     if( !p_log ) RAISENULL( "Out of memory" );
96
97     p_log->p_instance = p_instance;
98     vlc_spin_init( &p_log->data.lock );
99     p_log->data.count = 0;
100     p_log->data.verbosity = p_instance->verbosity;
101     p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
102
103     if( !p_log->p_messages )
104     {
105         free( p_log );
106         RAISENULL( "Out of memory" );
107     }
108
109     libvlc_retain( p_instance );
110     return p_log;
111 }
112
113 void libvlc_log_close( libvlc_log_t *p_log, libvlc_exception_t *p_e )
114 {
115     if( p_log )
116     {
117         assert( p_log->p_messages );
118         msg_Unsubscribe(p_log->p_messages);
119         libvlc_release( p_log->p_instance );
120         libvlc_log_clear( p_log, p_e );
121         vlc_spin_destroy( &p_log->data.lock );
122         free(p_log);
123     }
124     else
125         RAISEVOID("Invalid log object!");
126 }
127
128 unsigned libvlc_log_count( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
129 {
130     if( p_log )
131     {
132         msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
133         unsigned ret;
134
135         /* We cannot lock due to constant pointer constraints. Break them.
136          * Even then, this si not really thread safe anyway. */
137         vlc_spin_lock (&data->lock);
138         ret = data->count;
139         vlc_spin_unlock (&data->lock);
140         return ret;
141     }
142     RAISEZERO("Invalid log object!");
143 }
144
145 void libvlc_log_clear( libvlc_log_t *p_log, libvlc_exception_t *p_e )
146 {
147     if( p_log )
148     {
149         vlc_spin_lock (&p_log->data.lock);
150         msg_item_t *tab[p_log->data.count];
151         memcpy (tab, p_log->data.items, sizeof (tab));
152         p_log->data.count = 0;
153         vlc_spin_unlock (&p_log->data.lock);
154
155         for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
156             msg_Release (tab[i]);
157     }
158     else
159         RAISEVOID("Invalid log object!");
160 }
161
162 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
163 {
164     if( p_log )
165     {
166         struct libvlc_log_iterator_t *p_iter =
167             (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
168         /* FIXME: break constant pointer constraints */
169         msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
170
171         if( !p_iter ) RAISENULL( "Out of memory" );
172
173         vlc_spin_lock (&data->lock);
174         p_iter->p_data  = data;
175         p_iter->i_pos   = 0;
176         p_iter->i_end   = data->count;
177         vlc_spin_unlock (&data->lock);
178
179         return p_iter;
180     }
181     RAISENULL("Invalid log object!");
182 }
183
184 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
185 {
186     if( p_iter )
187     {
188         free(p_iter);
189     }
190     else
191         RAISEVOID("Invalid log iterator!");
192 }
193
194 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
195 {
196     if( p_iter )
197     {
198         return p_iter->i_pos != p_iter->i_end;
199     }
200     RAISEZERO("Invalid log iterator!");
201 }
202
203 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
204                                                 libvlc_log_message_t *buffer,
205                                                 libvlc_exception_t *p_e )
206 {
207     unsigned i_pos;
208
209     if( !p_iter )
210         RAISENULL("Invalid log iterator!");
211     if( !buffer )
212         RAISENULL("Invalid message buffer!");
213
214     i_pos = p_iter->i_pos;
215     if( i_pos != p_iter->i_end )
216     {
217         msg_item_t *msg;
218         vlc_spin_lock (&p_iter->p_data->lock);
219         msg = p_iter->p_data->items[i_pos];
220         buffer->i_severity  = msg->i_type;
221         buffer->psz_type    = msg->psz_object_type;
222         buffer->psz_name    = msg->psz_module;
223         buffer->psz_header  = msg->psz_header;
224         buffer->psz_message = msg->psz_msg;
225         vlc_spin_unlock (&p_iter->p_data->lock);
226         p_iter->i_pos++;
227
228         return buffer;
229     }
230     RAISENULL("No more messages");
231 }