]> git.sesse.net Git - vlc/blob - src/control/log.c
Port the libvlc log APIs to the new internal messages API
[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 "../libvlc.h"
27 #include <vlc/libvlc.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 };
45
46 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
47 {
48     vlc_spin_lock (&d->lock);
49     if (d->count < VLC_MSG_QSIZE)
50     {
51         d->items[d->count++] = p_item;
52         /* FIXME FIXME: yield the message item */
53     }
54     vlc_spin_unlock (&d->lock);
55     (void)i_drop;
56 }
57
58 struct libvlc_log_t
59 {
60     libvlc_instance_t  *p_instance;
61     msg_subscription_t *p_messages;
62     msg_cb_data_t       data;
63 };
64
65 struct libvlc_log_iterator_t
66 {
67     const msg_cb_data_t *p_data;
68     unsigned i_pos;
69     unsigned i_end;
70 };
71
72 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
73 {
74     if( p_instance )
75     {
76         libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
77         return p_priv->i_verbose;
78     }
79     RAISEZERO("Invalid VLC instance!");
80 }
81
82 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level, libvlc_exception_t *p_e )
83 {
84     if( p_instance )
85     {
86         libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
87         p_priv->i_verbose = level;
88     }
89     else
90         RAISEVOID("Invalid VLC instance!");
91 }
92
93 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
94 {
95     struct libvlc_log_t *p_log =
96         (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
97
98     if( !p_log ) RAISENULL( "Out of memory" );
99
100     p_log->p_instance = p_instance;
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         free(p_log);
121     }
122     else
123         RAISEVOID("Invalid log object!");
124 }
125
126 unsigned libvlc_log_count( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
127 {
128     if( p_log )
129     {
130         unsigned ret;
131
132         /* We cannot lock due to pointer constraints.
133          * Even then, this would not be thread safe anyway. */
134         /*vlc_spin_lock (&p_log->data.lock);*/
135         ret = p_log->data.count;
136         /*vlc_spin_unlock (&p_log->data.lock);*/
137         return ret;
138     }
139     RAISEZERO("Invalid log object!");
140 }
141
142 void libvlc_log_clear( libvlc_log_t *p_log, libvlc_exception_t *p_e )
143 {
144     if( p_log )
145     {
146         vlc_spin_lock (&p_log->data.lock);
147         p_log->data.count = 0;
148         /* FIXME: release items */
149         vlc_spin_unlock (&p_log->data.lock);
150     }
151     else
152         RAISEVOID("Invalid log object!");
153 }
154
155 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
156 {
157     if( p_log )
158     {
159         struct libvlc_log_iterator_t *p_iter =
160             (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
161
162         if( !p_iter ) RAISENULL( "Out of memory" );
163
164         /*vlc_spin_lock (&p_log->data.lock);*/
165         p_iter->p_data  = &p_log->data;
166         p_iter->i_pos   = 0;
167         p_iter->i_end   = p_log->data.count;
168         /*vlc_spin_unlock (&p_log->data.lock);*/
169
170         return p_iter;
171     }
172     RAISENULL("Invalid log object!");
173 }
174
175 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
176 {
177     if( p_iter )
178     {
179         free(p_iter);
180     }
181     else
182         RAISEVOID("Invalid log iterator!");
183 }
184
185 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
186 {
187     if( p_iter )
188     {
189         return p_iter->i_pos != p_iter->i_end;
190     }
191     RAISEZERO("Invalid log iterator!");
192 }
193
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 )
197 {
198     unsigned i_pos;
199
200     if( !p_iter )
201         RAISENULL("Invalid log iterator!");
202     if( !buffer )
203         RAISENULL("Invalid message buffer!");
204
205     i_pos = p_iter->i_pos;
206     if( i_pos != p_iter->i_end )
207     {
208         msg_item_t *msg;
209         /*vlc_spin_lock (&p_iter->p_data->lock);*/
210         msg = p_iter->p_data->items[i_pos];
211         buffer->i_severity  = msg->i_type;
212         buffer->psz_type    = msg->psz_object_type;
213         buffer->psz_name    = msg->psz_module;
214         buffer->psz_header  = msg->psz_header;
215         buffer->psz_message = msg->psz_msg;
216         /*vlc_spin_unlock (&p_iter->p_data->lock);*/
217         p_iter->i_pos++;
218
219         return buffer;
220     }
221     RAISENULL("No more messages");
222 }