]> git.sesse.net Git - vlc/blob - src/control/log.c
Use var_Inherit* instead of var_CreateGet*.
[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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include "libvlc_internal.h"
30 #include <vlc/libvlc.h>
31 #include <assert.h>
32
33 /* This API is terminally broken.
34  * First, it does not implement any kind of notification.
35  * Second, the iterating scheme is hermetic to any kind of thread-safety
36  * owing to its constant pointer constraints.
37  *  -- Courmisch
38  *
39  * "If you break your leg, don't run to me for sympathy"
40  *   -- some character, Beneath a Steel Sky
41  */
42
43 struct msg_cb_data_t
44 {
45     vlc_spinlock_t lock;
46     msg_item_t *items[VLC_MSG_QSIZE];
47     unsigned    count;
48     int         verbosity;
49 };
50
51 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
52 {
53     if (p_item->i_type > d->verbosity)
54         return;
55
56     vlc_spin_lock (&d->lock);
57     if (d->count < VLC_MSG_QSIZE)
58     {
59         d->items[d->count++] = p_item;
60         msg_Hold (p_item);
61     }
62     vlc_spin_unlock (&d->lock);
63     (void)i_drop;
64 }
65
66 struct libvlc_log_t
67 {
68     libvlc_instance_t  *p_instance;
69     msg_subscription_t *p_messages;
70     msg_cb_data_t       data;
71 };
72
73 struct libvlc_log_iterator_t
74 {
75     msg_cb_data_t *p_data;
76     unsigned i_pos;
77     unsigned i_end;
78 };
79
80 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
81 {
82     assert( p_instance );
83     return p_instance->verbosity;
84 }
85
86 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
87 {
88     assert( p_instance );
89     p_instance->verbosity = level;
90 }
91
92 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
93 {
94     struct libvlc_log_t *p_log = malloc(sizeof(*p_log));
95     if (unlikely(p_log == NULL))
96     {
97         libvlc_printerr ("Not enough memory");
98         return NULL;
99     }
100
101     p_log->p_instance = p_instance;
102     vlc_spin_init( &p_log->data.lock );
103     p_log->data.count = 0;
104     p_log->data.verbosity = p_instance->verbosity;
105     p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
106
107     if( !p_log->p_messages )
108     {
109         free( p_log );
110         libvlc_printerr ("Not enough memory");
111         return NULL;
112     }
113
114     libvlc_retain( p_instance );
115     return p_log;
116 }
117
118 void libvlc_log_close( libvlc_log_t *p_log )
119 {
120     if( !p_log )
121         return;
122
123     assert( p_log->p_messages );
124     msg_Unsubscribe(p_log->p_messages);
125     libvlc_release( p_log->p_instance );
126     libvlc_log_clear( p_log );
127     vlc_spin_destroy( &p_log->data.lock );
128     free(p_log);
129 }
130
131 unsigned libvlc_log_count( const libvlc_log_t *p_log )
132 {
133     if( !p_log )
134         return 0;
135
136     msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
137     unsigned ret;
138
139     /* We cannot lock due to constant pointer constraints. Break them.
140      * Even then, this si not really thread safe anyway. */
141     vlc_spin_lock (&data->lock);
142     ret = data->count;
143     vlc_spin_unlock (&data->lock);
144     return ret;
145 }
146
147 void libvlc_log_clear( libvlc_log_t *p_log )
148 {
149     if( !p_log )
150         return;
151
152     vlc_spin_lock (&p_log->data.lock);
153     msg_item_t *tab[p_log->data.count];
154     memcpy (tab, p_log->data.items, sizeof (tab));
155     p_log->data.count = 0;
156     vlc_spin_unlock (&p_log->data.lock);
157
158     for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
159          msg_Release (tab[i]);
160 }
161
162 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
163 {
164     if (p_log == NULL)
165         return NULL;
166
167     struct libvlc_log_iterator_t *p_iter = malloc (sizeof (*p_iter));
168     if (unlikely(p_iter == NULL))
169     {
170         libvlc_printerr ("Not enough memory");
171         return NULL;
172     }
173
174     /* FIXME: break constant pointer constraints */
175     msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
176
177     vlc_spin_lock (&data->lock);
178     p_iter->p_data  = data;
179     p_iter->i_pos   = 0;
180     p_iter->i_end   = data->count;
181     vlc_spin_unlock (&data->lock);
182     return p_iter;
183 }
184
185 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
186 {
187     free( p_iter );
188 }
189
190 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
191 {
192     if( !p_iter )
193         return 0;
194     return p_iter->i_pos != p_iter->i_end;
195 }
196
197 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
198                                                 libvlc_log_message_t *buffer )
199 {
200     unsigned i_pos;
201
202     if( !p_iter )
203         return NULL;
204     assert (buffer != NULL);
205
206     i_pos = p_iter->i_pos;
207     if( i_pos != p_iter->i_end )
208     {
209         msg_item_t *msg;
210         vlc_spin_lock (&p_iter->p_data->lock);
211         msg = p_iter->p_data->items[i_pos];
212         buffer->i_severity  = msg->i_type;
213         buffer->psz_type    = msg->psz_object_type;
214         buffer->psz_name    = msg->psz_module;
215         buffer->psz_header  = msg->psz_header;
216         buffer->psz_message = msg->psz_msg;
217         vlc_spin_unlock (&p_iter->p_data->lock);
218         p_iter->i_pos++;
219
220         return buffer;
221     }
222     return NULL;
223 }