]> git.sesse.net Git - vlc/blob - src/control/log.c
Do not include <vlc_es.h> from <vlc_fourcc.h>
[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, const msg_item_t *p_item )
52 {
53     if (p_item->i_type > d->verbosity)
54         return;
55
56     msg_item_t *msg = msg_Copy (p_item);
57
58     vlc_spin_lock (&d->lock);
59     if (d->count < VLC_MSG_QSIZE)
60         d->items[d->count++] = msg;
61     vlc_spin_unlock (&d->lock);
62 }
63
64 struct libvlc_log_t
65 {
66     libvlc_instance_t  *p_instance;
67     msg_subscription_t *p_messages;
68     msg_cb_data_t       data;
69 };
70
71 struct libvlc_log_iterator_t
72 {
73     msg_cb_data_t *p_data;
74     unsigned i_pos;
75     unsigned i_end;
76 };
77
78 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
79 {
80     assert( p_instance );
81     return p_instance->verbosity;
82 }
83
84 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
85 {
86     assert( p_instance );
87     p_instance->verbosity = level;
88 }
89
90 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
91 {
92     struct libvlc_log_t *p_log = malloc(sizeof(*p_log));
93     if (unlikely(p_log == NULL))
94     {
95         libvlc_printerr ("Not enough memory");
96         return NULL;
97     }
98
99     p_log->p_instance = p_instance;
100     vlc_spin_init( &p_log->data.lock );
101     p_log->data.count = 0;
102     p_log->data.verbosity = p_instance->verbosity;
103     p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
104
105     if( !p_log->p_messages )
106     {
107         free( p_log );
108         libvlc_printerr ("Not enough memory");
109         return NULL;
110     }
111
112     libvlc_retain( p_instance );
113     return p_log;
114 }
115
116 void libvlc_log_close( libvlc_log_t *p_log )
117 {
118     if( !p_log )
119         return;
120
121     assert( p_log->p_messages );
122     msg_Unsubscribe(p_log->p_messages);
123     libvlc_release( p_log->p_instance );
124     libvlc_log_clear( p_log );
125     vlc_spin_destroy( &p_log->data.lock );
126     free(p_log);
127 }
128
129 unsigned libvlc_log_count( const libvlc_log_t *p_log )
130 {
131     if( !p_log )
132         return 0;
133
134     msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
135     unsigned ret;
136
137     /* We cannot lock due to constant pointer constraints. Break them.
138      * Even then, this si not really thread safe anyway. */
139     vlc_spin_lock (&data->lock);
140     ret = data->count;
141     vlc_spin_unlock (&data->lock);
142     return ret;
143 }
144
145 void libvlc_log_clear( libvlc_log_t *p_log )
146 {
147     if( !p_log )
148         return;
149
150     vlc_spin_lock (&p_log->data.lock);
151     msg_item_t *tab[p_log->data.count];
152     memcpy (tab, p_log->data.items, sizeof (tab));
153     p_log->data.count = 0;
154     vlc_spin_unlock (&p_log->data.lock);
155
156     for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
157          msg_Free (tab[i]);
158 }
159
160 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
161 {
162     if (p_log == NULL)
163         return NULL;
164
165     struct libvlc_log_iterator_t *p_iter = malloc (sizeof (*p_iter));
166     if (unlikely(p_iter == NULL))
167     {
168         libvlc_printerr ("Not enough memory");
169         return NULL;
170     }
171
172     /* FIXME: break constant pointer constraints */
173     msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
174
175     vlc_spin_lock (&data->lock);
176     p_iter->p_data  = data;
177     p_iter->i_pos   = 0;
178     p_iter->i_end   = data->count;
179     vlc_spin_unlock (&data->lock);
180     return p_iter;
181 }
182
183 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
184 {
185     free( p_iter );
186 }
187
188 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
189 {
190     if( !p_iter )
191         return 0;
192     return p_iter->i_pos != p_iter->i_end;
193 }
194
195 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
196                                                 libvlc_log_message_t *buffer )
197 {
198     unsigned i_pos;
199
200     if( !p_iter )
201         return NULL;
202     assert (buffer != NULL);
203
204     i_pos = p_iter->i_pos;
205     if( i_pos != p_iter->i_end )
206     {
207         msg_item_t *msg;
208         vlc_spin_lock (&p_iter->p_data->lock);
209         msg = p_iter->p_data->items[i_pos];
210         buffer->i_severity  = msg->i_type;
211         buffer->psz_type    = msg->psz_object_type;
212         buffer->psz_name    = msg->psz_module;
213         buffer->psz_header  = msg->psz_header;
214         buffer->psz_message = msg->psz_msg;
215         vlc_spin_unlock (&p_iter->p_data->lock);
216         p_iter->i_pos++;
217
218         return buffer;
219     }
220     return NULL;
221 }