Fix syntax errors
[vlc.git] / src / control / log.c
1 /*****************************************************************************
2  * log.c: libvlc new API log functions
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  *
6  * $Id: core.c 14187 2006-02-07 16:37:40Z courmisch $
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 <vlc/libvlc.h>
27
28 struct libvlc_log_t
29 {
30     libvlc_instance_t  *p_instance;
31     msg_subscription_t *p_messages;
32 };
33
34 struct libvlc_log_iterator_t
35 {
36     msg_subscription_t *p_messages;
37     int i_start;
38     int i_pos;
39     int i_end;
40 };
41
42 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
43 {
44     if( p_instance )
45     {
46         return p_instance->p_libvlc_int->i_verbose;
47     }
48     RAISEZERO("Invalid VLC instance!");
49 }
50
51 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level, libvlc_exception_t *p_e )
52 {
53     if( p_instance )
54     {
55         p_instance->p_libvlc_int->i_verbose = level;
56     }
57     else
58         RAISEVOID("Invalid VLC instance!");
59 }
60
61 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
62 {
63     struct libvlc_log_t *p_log =
64         (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));
65
66     if( !p_log ) RAISENULL( "Out of memory" );
67
68     p_log->p_instance = p_instance;
69     p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, MSG_QUEUE_NORMAL);
70
71     if( !p_log->p_messages )
72     {
73         free( p_log );
74         RAISENULL( "Out of memory" );
75     }
76
77     libvlc_retain( p_instance );
78     return p_log;
79 }
80
81 void libvlc_log_close( libvlc_log_t *p_log, libvlc_exception_t *p_e )
82 {
83     if( p_log && p_log->p_messages )
84     {
85         msg_Unsubscribe(p_log->p_instance->p_libvlc_int, p_log->p_messages);
86         libvlc_release( p_log->p_instance, p_e );
87         free(p_log);
88     }
89     else
90         RAISEVOID("Invalid log object!");
91 }
92
93 unsigned libvlc_log_count( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
94 {
95     if( p_log && p_log->p_messages )
96     {
97         int i_start = p_log->p_messages->i_start;
98         int i_stop  = *(p_log->p_messages->pi_stop);
99
100         if( i_stop >= i_start )
101             return i_stop-i_start;
102         else
103             return VLC_MSG_QSIZE-(i_start-i_stop);
104     }
105     RAISEZERO("Invalid log object!");
106 }
107
108 void libvlc_log_clear( libvlc_log_t *p_log, libvlc_exception_t *p_e )
109 {
110     if( p_log && p_log->p_messages )
111     {
112         vlc_mutex_lock(p_log->p_messages->p_lock);
113         p_log->p_messages->i_start = *(p_log->p_messages->pi_stop);
114         vlc_mutex_unlock(p_log->p_messages->p_lock);
115     }
116     else
117         RAISEVOID("Invalid log object!");
118 }
119
120 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
121 {
122     if( p_log && p_log->p_messages )
123     {
124         struct libvlc_log_iterator_t *p_iter =
125             (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));
126
127         if( !p_iter ) RAISENULL( "Out of memory" );
128
129         vlc_mutex_lock(p_log->p_messages->p_lock);
130         p_iter->p_messages = p_log->p_messages;
131         p_iter->i_start    = p_log->p_messages->i_start;
132         p_iter->i_pos      = p_log->p_messages->i_start;
133         p_iter->i_end      = *(p_log->p_messages->pi_stop);
134         vlc_mutex_unlock(p_log->p_messages->p_lock);
135
136         return p_iter;
137     }
138     RAISENULL("Invalid log object!");
139 }
140
141 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
142 {
143     if( p_iter )
144     {
145         free(p_iter);
146     }
147     else
148         RAISEVOID("Invalid log iterator!");
149 }
150
151 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
152 {
153     if( p_iter )
154     {
155         return p_iter->i_pos != p_iter->i_end;
156     }
157     RAISEZERO("Invalid log iterator!");
158 }
159
160 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
161                                                 struct libvlc_log_message_t *buffer,
162                                                 libvlc_exception_t *p_e )
163 {
164     if( p_iter )
165     {
166         if( buffer && (sizeof(struct libvlc_log_message_t) == buffer->sizeof_msg) )
167         {
168             int i_pos = p_iter->i_pos;
169             if( i_pos != p_iter->i_end )
170             {
171                 msg_item_t *msg;
172                 vlc_mutex_lock(p_iter->p_messages->p_lock);
173                 msg = p_iter->p_messages->p_msg+i_pos;
174                 buffer->i_severity  = msg->i_type;
175                 buffer->psz_type    = msg_GetObjectTypeName(msg->i_object_type);
176                 buffer->psz_name    = msg->psz_module;
177                 buffer->psz_header  = msg->psz_header;
178                 buffer->psz_message = msg->psz_msg;
179                 p_iter->i_pos = ++i_pos % VLC_MSG_QSIZE;
180                 vlc_mutex_unlock(p_iter->p_messages->p_lock);
181
182                 return buffer;
183             }
184             RAISENULL("No more messages");
185         }
186         RAISENULL("Invalid message buffer!");
187     }
188     RAISENULL("Invalid log iterator!");
189 }
190