log.c 6.16 KB
Newer Older
1
2
3
4
5
/*****************************************************************************
 * log.c: libvlc new API log functions
 *****************************************************************************
 * Copyright (C) 2005 the VideoLAN team
 *
6
 * $Id$
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 * Authors: Damien Fouilleul <damienf@videolan.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
 *****************************************************************************/

zorglub's avatar
zorglub committed
25
#include "libvlc_internal.h"
Rafaël Carré's avatar
Rafaël Carré committed
26
#include "../libvlc.h"
27
#include <vlc/libvlc.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
28
#include <assert.h>
29

30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/* This API is terminally broken.
 * First, it does not implement any kind of notification.
 * Second, the iterating scheme is hermetic to any kind of thread-safety
 * owing to its constant pointer constraints.
 *  -- Courmisch
 *
 * "If you break your leg, don't run to me for sympathy"
 *   -- some character, Beneath a Steel Sky
 */

struct msg_cb_data_t
{
    vlc_spinlock_t lock;
    msg_item_t *items[VLC_MSG_QSIZE];
    unsigned    count;
};

static void handler( msg_cb_data_t *d, msg_item_t *p_item, unsigned i_drop )
{
    vlc_spin_lock (&d->lock);
    if (d->count < VLC_MSG_QSIZE)
    {
        d->items[d->count++] = p_item;
        /* FIXME FIXME: yield the message item */
    }
    vlc_spin_unlock (&d->lock);
    (void)i_drop;
}

59
60
struct libvlc_log_t
{
61
    libvlc_instance_t  *p_instance;
62
    msg_subscription_t *p_messages;
63
    msg_cb_data_t       data;
64
65
66
67
};

struct libvlc_log_iterator_t
{
68
69
70
    const msg_cb_data_t *p_data;
    unsigned i_pos;
    unsigned i_end;
71
72
73
74
75
76
};

unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
{
    if( p_instance )
    {
Rafaël Carré's avatar
Rafaël Carré committed
77
78
        libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
        return p_priv->i_verbose;
79
80
81
82
83
84
85
86
    }
    RAISEZERO("Invalid VLC instance!");
}

void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level, libvlc_exception_t *p_e )
{
    if( p_instance )
    {
Rafaël Carré's avatar
Rafaël Carré committed
87
88
        libvlc_priv_t *p_priv = libvlc_priv( p_instance->p_libvlc_int );
        p_priv->i_verbose = level;
89
90
91
92
93
    }
    else
        RAISEVOID("Invalid VLC instance!");
}

94
libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
95
96
97
98
99
100
101
{
    struct libvlc_log_t *p_log =
        (struct libvlc_log_t *)malloc(sizeof(struct libvlc_log_t));

    if( !p_log ) RAISENULL( "Out of memory" );

    p_log->p_instance = p_instance;
102
    p_log->p_messages = msg_Subscribe(p_instance->p_libvlc_int, handler, &p_log->data);
103

104
105
106
107
108
    if( !p_log->p_messages )
    {
        free( p_log );
        RAISENULL( "Out of memory" );
    }
109

110
    libvlc_retain( p_instance );
111
112
113
114
115
    return p_log;
}

void libvlc_log_close( libvlc_log_t *p_log, libvlc_exception_t *p_e )
{
116
    if( p_log )
117
    {
118
119
        assert( p_log->p_messages );
        msg_Unsubscribe(p_log->p_messages);
120
        libvlc_release( p_log->p_instance );
121
122
123
124
125
126
127
128
        free(p_log);
    }
    else
        RAISEVOID("Invalid log object!");
}

unsigned libvlc_log_count( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
{
129
    if( p_log )
130
    {
131
132
133
134
135
136
137
138
        unsigned ret;

        /* We cannot lock due to pointer constraints.
         * Even then, this would not be thread safe anyway. */
        /*vlc_spin_lock (&p_log->data.lock);*/
        ret = p_log->data.count;
        /*vlc_spin_unlock (&p_log->data.lock);*/
        return ret;
139
140
141
142
143
144
    }
    RAISEZERO("Invalid log object!");
}

void libvlc_log_clear( libvlc_log_t *p_log, libvlc_exception_t *p_e )
{
145
    if( p_log )
146
    {
147
        /*vlc_spin_lock (&p_log->data.lock);*/
148
149
        p_log->data.count = 0;
        /* FIXME: release items */
150
        /*vlc_spin_unlock (&p_log->data.lock);*/
151
152
153
154
155
156
157
    }
    else
        RAISEVOID("Invalid log object!");
}

libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log, libvlc_exception_t *p_e )
{
158
    if( p_log )
159
160
161
162
163
164
    {
        struct libvlc_log_iterator_t *p_iter =
            (struct libvlc_log_iterator_t *)malloc(sizeof(struct libvlc_log_iterator_t));

        if( !p_iter ) RAISENULL( "Out of memory" );

165
166
167
168
169
        /*vlc_spin_lock (&p_log->data.lock);*/
        p_iter->p_data  = &p_log->data;
        p_iter->i_pos   = 0;
        p_iter->i_end   = p_log->data.count;
        /*vlc_spin_unlock (&p_log->data.lock);*/
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195

        return p_iter;
    }
    RAISENULL("Invalid log object!");
}

void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
{
    if( p_iter )
    {
        free(p_iter);
    }
    else
        RAISEVOID("Invalid log iterator!");
}

int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter, libvlc_exception_t *p_e )
{
    if( p_iter )
    {
        return p_iter->i_pos != p_iter->i_end;
    }
    RAISEZERO("Invalid log iterator!");
}

libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
littlejohn's avatar
littlejohn committed
196
                                                libvlc_log_message_t *buffer,
197
198
                                                libvlc_exception_t *p_e )
{
199
    unsigned i_pos;
littlejohn's avatar
littlejohn committed
200
201
202
203

    if( !p_iter )
        RAISENULL("Invalid log iterator!");
    if( !buffer )
204
        RAISENULL("Invalid message buffer!");
littlejohn's avatar
littlejohn committed
205
206
207
208
209

    i_pos = p_iter->i_pos;
    if( i_pos != p_iter->i_end )
    {
        msg_item_t *msg;
210
211
        /*vlc_spin_lock (&p_iter->p_data->lock);*/
        msg = p_iter->p_data->items[i_pos];
littlejohn's avatar
littlejohn committed
212
        buffer->i_severity  = msg->i_type;
213
        buffer->psz_type    = msg->psz_object_type;
littlejohn's avatar
littlejohn committed
214
215
216
        buffer->psz_name    = msg->psz_module;
        buffer->psz_header  = msg->psz_header;
        buffer->psz_message = msg->psz_msg;
217
218
        /*vlc_spin_unlock (&p_iter->p_data->lock);*/
        p_iter->i_pos++;
littlejohn's avatar
littlejohn committed
219
220

        return buffer;
221
    }
littlejohn's avatar
littlejohn committed
222
    RAISENULL("No more messages");
223
}