Fixed typo into invmem error message
[vlc.git] / src / control / log.c
blobe6b066d08a67fb325defa7eb3ddd18296933b2ca
1 /*****************************************************************************
2 * log.c: libvlc new API log functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * $Id$
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 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include "libvlc_internal.h"
30 #include <vlc/libvlc.h>
31 #include <assert.h>
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
39 * "If you break your leg, don't run to me for sympathy"
40 * -- some character, Beneath a Steel Sky
43 struct msg_cb_data_t
45 vlc_spinlock_t lock;
46 msg_item_t *items[VLC_MSG_QSIZE];
47 unsigned count;
48 int verbosity;
51 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
53 if (p_item->i_type > d->verbosity)
54 return;
56 vlc_spin_lock (&d->lock);
57 if (d->count < VLC_MSG_QSIZE)
59 d->items[d->count++] = p_item;
60 msg_Hold (p_item);
62 vlc_spin_unlock (&d->lock);
63 (void)i_drop;
66 struct libvlc_log_t
68 libvlc_instance_t *p_instance;
69 msg_subscription_t *p_messages;
70 msg_cb_data_t data;
73 struct libvlc_log_iterator_t
75 msg_cb_data_t *p_data;
76 unsigned i_pos;
77 unsigned i_end;
80 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
82 assert( p_instance );
83 return p_instance->verbosity;
86 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
88 assert( p_instance );
89 p_instance->verbosity = level;
92 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
94 struct libvlc_log_t *p_log =
95 (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
97 if( !p_log ) RAISENULL( "Out of memory" );
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);
105 if( !p_log->p_messages )
107 free( p_log );
108 RAISENULL( "Out of memory" );
111 libvlc_retain( p_instance );
112 return p_log;
115 void libvlc_log_close( libvlc_log_t *p_log )
117 if( !p_log )
118 return;
120 assert( p_log->p_messages );
121 msg_Unsubscribe(p_log->p_messages);
122 libvlc_release( p_log->p_instance );
123 libvlc_log_clear( p_log );
124 vlc_spin_destroy( &p_log->data.lock );
125 free(p_log);
128 unsigned libvlc_log_count( const libvlc_log_t *p_log )
130 if( !p_log )
131 return 0;
133 msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
134 unsigned ret;
136 /* We cannot lock due to constant pointer constraints. Break them.
137 * Even then, this si not really thread safe anyway. */
138 vlc_spin_lock (&data->lock);
139 ret = data->count;
140 vlc_spin_unlock (&data->lock);
141 return ret;
144 void libvlc_log_clear( libvlc_log_t *p_log )
146 if( !p_log )
147 return;
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);
155 for (unsigned i = 0; i < sizeof (tab) / sizeof (tab[0]); i++)
156 msg_Release (tab[i]);
159 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log,
160 libvlc_exception_t *p_e )
162 if( p_log )
164 struct libvlc_log_iterator_t *p_iter =
165 (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
166 /* FIXME: break constant pointer constraints */
167 msg_cb_data_t *data = &((libvlc_log_t *)p_log)->data;
169 if( !p_iter ) RAISENULL( "Out of memory" );
171 vlc_spin_lock (&data->lock);
172 p_iter->p_data = data;
173 p_iter->i_pos = 0;
174 p_iter->i_end = data->count;
175 vlc_spin_unlock (&data->lock);
177 return p_iter;
179 RAISENULL("Invalid log object!");
182 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
184 free( p_iter );
187 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
189 if( !p_iter )
190 return 0;
191 return p_iter->i_pos != p_iter->i_end;
194 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
195 libvlc_log_message_t *buffer,
196 libvlc_exception_t *p_e )
198 unsigned i_pos;
200 if( !p_iter )
201 return NULL;
202 assert( buffer );
204 i_pos = p_iter->i_pos;
205 if( i_pos != p_iter->i_end )
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++;
218 return buffer;
220 RAISENULL("No more messages");