]> git.sesse.net Git - vlc/blob - lib/log.c
Rename msg_item_* to vlc_log_* (cosmetic)
[vlc] / lib / log.c
1 /*****************************************************************************
2  * log.c: libvlc new API log functions
3  *****************************************************************************
4  * Copyright (C) 2005 VLC authors and VideoLAN
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 it
11  * under the terms of the GNU Lesser General Public License as published by
12  * the Free Software Foundation; either version 2.1 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 Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public License
21  * along with this program; if not, write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <assert.h>
30 #include <vlc/libvlc.h>
31 #include "libvlc_internal.h"
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34
35 /*** Logging core dispatcher ***/
36
37 static vlc_rwlock_t log_lock = VLC_STATIC_RWLOCK;
38 static libvlc_log_subscriber_t *log_first = NULL;
39 static msg_subscription_t sub;
40
41 VLC_FORMAT(2,3)
42 static void libvlc_log (int level, const char *fmt, ...)
43 {
44     libvlc_log_subscriber_t *sub;
45     va_list ap;
46
47     switch (level)
48     {
49         case VLC_MSG_INFO: level = LIBVLC_NOTICE;  break;
50         case VLC_MSG_ERR:  level = LIBVLC_ERROR;   break;
51         case VLC_MSG_WARN: level = LIBVLC_WARNING; break;
52         case VLC_MSG_DBG:  level = LIBVLC_DEBUG;   break;
53     }
54
55     va_start (ap, fmt);
56     vlc_rwlock_rdlock (&log_lock);
57     for (sub = log_first; sub != NULL; sub = sub->next)
58         sub->func (sub->opaque, level, fmt, ap);
59     vlc_rwlock_unlock (&log_lock);
60     va_end (ap);
61 }
62
63 static void libvlc_logf (void *dummy, int level, const vlc_log_t *item,
64                          const char *fmt, va_list ap)
65 {
66     char *msg;
67
68     if (unlikely(vasprintf (&msg, fmt, ap) == -1))
69         msg = NULL;
70     if (item->psz_header != NULL)
71         libvlc_log (level, "[%p] [%s]: %s %s %s", (void *)item->i_object_id,
72                     item->psz_header, item->psz_module, item->psz_object_type,
73                     msg ? msg : "Not enough memory");
74     else
75         libvlc_log (level, "[%p]: %s %s %s", (void *)item->i_object_id,
76                     item->psz_module, item->psz_object_type,
77                     msg ? msg : "Not enough memory");
78     free (msg);
79     (void) dummy;
80 }
81
82 void libvlc_log_init (void)
83 {
84     vlc_Subscribe (&sub, libvlc_logf, NULL);
85 }
86
87 void libvlc_log_deinit (void)
88 {
89     vlc_Unsubscribe (&sub);
90 }
91
92 void libvlc_log_subscribe (libvlc_log_subscriber_t *sub,
93                            libvlc_log_cb cb, void *data)
94 {
95     sub->prev = NULL;
96     sub->func = cb;
97     sub->opaque = data;
98     vlc_rwlock_wrlock (&log_lock);
99     sub->next = log_first;
100     log_first = sub;
101     vlc_rwlock_unlock (&log_lock);
102 }
103
104 void libvlc_log_unsubscribe( libvlc_log_subscriber_t *sub )
105 {
106     vlc_rwlock_wrlock (&log_lock);
107     if (sub->next != NULL)
108         sub->next->prev = sub->prev;
109     if (sub->prev != NULL)
110         sub->prev->next = sub->next;
111     else
112         log_first = sub->next;
113     vlc_rwlock_unlock (&log_lock);
114 }
115
116 /*** Helpers for logging to files ***/
117 static void libvlc_log_file (void *data, int level, const char *fmt,
118                              va_list ap)
119 {
120     FILE *stream = data;
121
122     flockfile (stream);
123     vfprintf (stream, fmt, ap);
124     fputc ('\n', stream);
125     funlockfile (stream);
126     (void) level;
127 }
128
129 void libvlc_log_subscribe_file (libvlc_log_subscriber_t *sub, FILE *stream)
130 {
131     libvlc_log_subscribe (sub, libvlc_log_file, stream);
132 }
133
134 /*** Stubs for the old interface ***/
135 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
136 {
137     (void) p_instance;
138     return -1;
139 }
140
141 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
142 {
143     (void) p_instance;
144     (void) level;
145 }
146
147 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
148 {
149     (void) p_instance;
150     return malloc(1);
151 }
152
153 void libvlc_log_close( libvlc_log_t *p_log )
154 {
155     free(p_log);
156 }
157
158 unsigned libvlc_log_count( const libvlc_log_t *p_log )
159 {
160     (void) p_log;
161     return 0;
162 }
163
164 void libvlc_log_clear( libvlc_log_t *p_log )
165 {
166     (void) p_log;
167 }
168
169 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
170 {
171     return (p_log != NULL) ? malloc(1) : NULL;
172 }
173
174 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
175 {
176     free( p_iter );
177 }
178
179 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
180 {
181     (void) p_iter;
182     return 0;
183 }
184
185 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
186                                                 libvlc_log_message_t *buffer )
187 {
188     (void) p_iter; (void) buffer;
189     return NULL;
190 }