1 /*****************************************************************************
2 * log.c: libvlc new API log functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
8 * Authors: Damien Fouilleul <damienf@videolan.org>
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.
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.
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 *****************************************************************************/
25 #include "libvlc_internal.h"
26 #include <vlc/libvlc.h>
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.
35 * "If you break your leg, don't run to me for sympathy"
36 * -- some character, Beneath a Steel Sky
42 msg_item_t *items[VLC_MSG_QSIZE];
47 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
49 if (p_item->i_type > d->verbosity)
52 vlc_spin_lock (&d->lock);
53 if (d->count < VLC_MSG_QSIZE)
55 d->items[d->count++] = p_item;
58 vlc_spin_unlock (&d->lock);
64 libvlc_instance_t *p_instance;
65 msg_subscription_t *p_messages;
69 struct libvlc_log_iterator_t
71 msg_cb_data_t *p_data;
76 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
79 return p_instance->verbosity;
82 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
85 p_instance->verbosity = level;
88 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
90 struct libvlc_log_t *p_log =
91 (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
93 if( !p_log ) RAISENULL( "Out of memory" );
95 p_log->p_instance = p_instance;
96 vlc_spin_init( &p_log->data.lock );
97 p_log->data.count = 0;
98 p_log->data.verbosity = p_instance->verbosity;
99 p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
101 if( !p_log->p_messages )
104 RAISENULL( "Out of memory" );
107 libvlc_retain( p_instance );
111 void libvlc_log_close( libvlc_log_t *p_log )
116 assert( p_log->p_messages );
117 msg_Unsubscribe(p_log->p_messages);
118 libvlc_release( p_log->p_instance );
119 libvlc_log_clear( p_log );
120 vlc_spin_destroy( &p_log->data.lock );
124 unsigned libvlc_log_count( const libvlc_log_t *p_log )
129 msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
132 /* We cannot lock due to constant pointer constraints. Break them.
133 * Even then, this si not really thread safe anyway. */
134 vlc_spin_lock (&data->lock);
136 vlc_spin_unlock (&data->lock);
140 void libvlc_log_clear( libvlc_log_t *p_log )
145 vlc_spin_lock (&p_log->data.lock);
146 msg_item_t *tab[p_log->data.count];
147 memcpy (tab, p_log->data.items, sizeof (tab));
148 p_log->data.count = 0;
149 vlc_spin_unlock (&p_log->data.lock);
151 for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
152 msg_Release (tab[i]);
155 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log,
156 libvlc_exception_t *p_e )
160 struct libvlc_log_iterator_t *p_iter =
161 (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
162 /* FIXME: break constant pointer constraints */
163 msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
165 if( !p_iter ) RAISENULL( "Out of memory" );
167 vlc_spin_lock (&data->lock);
168 p_iter->p_data = data;
170 p_iter->i_end = data->count;
171 vlc_spin_unlock (&data->lock);
175 RAISENULL("Invalid log object!");
178 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
183 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
187 return p_iter->i_pos != p_iter->i_end;
190 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
191 libvlc_log_message_t *buffer,
192 libvlc_exception_t *p_e )
200 i_pos = p_iter->i_pos;
201 if( i_pos != p_iter->i_end )
204 vlc_spin_lock (&p_iter->p_data->lock);
205 msg = p_iter->p_data->items[i_pos];
206 buffer->i_severity = msg->i_type;
207 buffer->psz_type = msg->psz_object_type;
208 buffer->psz_name = msg->psz_module;
209 buffer->psz_header = msg->psz_header;
210 buffer->psz_message = msg->psz_msg;
211 vlc_spin_unlock (&p_iter->p_data->lock);
216 RAISENULL("No more messages");