]> git.sesse.net Git - vlc/blob - src/control/log.c
96ef45278af980668ed8797984cb1ba9470e673e
[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 #include <assert.h>
29
30 /* This API is terminally broken.
31  * First, it does not implement any kind of notification.
32  * Second, the iterating scheme is hermetic to any kind of thread-safety
33  * owing to its constant pointer constraints.
34  *  -- Courmisch
35  *
36  * "If you break your leg, don't run to me for sympathy"
37  *   -- some character, Beneath a Steel Sky
38  */
39
40 struct msg_cb_data_t
41 {
42     vlc_spinlock_t lock;
43     msg_item_t *items[VLC_MSG_QSIZE];
44     unsigned    count;
45 };
46
47 static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
48 {
49     vlc_spin_lock (&d->lock);
50     if (d->count < VLC_MSG_QSIZE)
51     {
52         d->items[d->count++] = p_item;
53         /* FIXME FIXME: yield the message item */
54     }
55     vlc_spin_unlock (&d->lock);
56     (void)i_drop;
57 }
58
59 struct libvlc_log_t
60 {
61     libvlc_instance_t  *p_instance;
62     msg_subscription_t *p_messages;
63     msg_cb_data_t       data;
64 };
65
66 struct libvlc_log_iterator_t
67 {
68     const msg_cb_data_t *p_data;
69     unsigned i_pos;
70     unsigned i_end;
71 };
72
73 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
74 {
75     if( p_instance )
76     {
77         libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
78         return p_priv->i_verbose;
79     }
80     RAISEZERO("Invalid VLC instance!");
81 }
82
83 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level, libvlc_exception_t *p_e )
84 {
85     if( p_instance )
86     {
87         libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
88         p_priv->i_verbose = level;
89     }
90     else
91         RAISEVOID("Invalid VLC instance!");
92 }
93
94 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
95 {
96     struct libvlc_log_t *p_log =
97         (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
98
99     if( !p_log ) RAISENULL( "Out of memory" );
100
101     p_log->p_instance = p_instance;
102     p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
103
104     if( !p_log->p_messages )
105     {
106         free( p_log );
107         RAISENULL( "Out of memory" );
108     }
109
110     libvlc_retain( p_instance );
111     return p_log;
112 }
113
114 void libvlc_log_close( libvlc_log_t *p_log, libvlc_exception_t *p_e )
115 {
116     if( p_log )
117     {
118         assert( p_log->p_messages );
119         msg_Unsubscribe(p_log->p_messages);
120         libvlc_release( p_log->p_instance );
121         free(p_log);
122     }
123     else
124         RAISEVOID("Invalid log object!");
125 }
126
127 unsigned libvlc_log_count( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
128 {
129     if( p_log )
130     {
131         unsigned ret;
132
133         /* We cannot lock due to pointer constraints.
134          * Even then, this would not be thread safe anyway. */
135         /*vlc_spin_lock (&p_log->data.lock);*/
136         ret = p_log->data.count;
137         /*vlc_spin_unlock (&p_log->data.lock);*/
138         return ret;
139     }
140     RAISEZERO("Invalid log object!");
141 }
142
143 void libvlc_log_clear( libvlc_log_t *p_log, libvlc_exception_t *p_e )
144 {
145     if( p_log )
146     {
147         /*vlc_spin_lock (&p_log->data.lock);*/
148         p_log->data.count = 0;
149         /* FIXME: release items */
150         /*vlc_spin_unlock (&p_log->data.lock);*/
151     }
152     else
153         RAISEVOID("Invalid log object!");
154 }
155
156 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
157 {
158     if( p_log )
159     {
160         struct libvlc_log_iterator_t *p_iter =
161             (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
162
163         if( !p_iter ) RAISENULL( "Out of memory" );
164
165         /*vlc_spin_lock (&p_log->data.lock);*/
166         p_iter->p_data  = &p_log->data;
167         p_iter->i_pos   = 0;
168         p_iter->i_end   = p_log->data.count;
169         /*vlc_spin_unlock (&p_log->data.lock);*/
170
171         return p_iter;
172     }
173     RAISENULL("Invalid log object!");
174 }
175
176 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
177 {
178     if( p_iter )
179     {
180         free(p_iter);
181     }
182     else
183         RAISEVOID("Invalid log iterator!");
184 }
185
186 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
187 {
188     if( p_iter )
189     {
190         return p_iter->i_pos != p_iter->i_end;
191     }
192     RAISEZERO("Invalid log iterator!");
193 }
194
195 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
196                                                 libvlc_log_message_t *buffer,
197                                                 libvlc_exception_t *p_e )
198 {
199     unsigned i_pos;
200
201     if( !p_iter )
202         RAISENULL("Invalid log iterator!");
203     if( !buffer )
204         RAISENULL("Invalid message buffer!");
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     RAISENULL("No more messages");
223 }