Qt: ConfigControl: missing Q_OBJECT
[vlc.git] / lib / log.c
blob0999c562096ee1d7619976c4c3e9bcc694383aa8
1 /*****************************************************************************
2 * log.c: libvlc new API log functions
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
6 * $Id$
8 * Authors: Damien Fouilleul <damienf@videolan.org>
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.
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.
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 *****************************************************************************/
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
29 #include <assert.h>
30 #include <vlc/libvlc.h>
31 #include "libvlc_internal.h"
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
35 /*** Logging core dispatcher ***/
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;
41 VLC_FORMAT(2,3)
42 static void libvlc_log (int level, const char *fmt, ...)
44 libvlc_log_subscriber_t *sub;
45 va_list ap;
47 switch (level)
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;
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);
63 static void libvlc_logf (void *dummy, int level, const msg_item_t *item,
64 const char *fmt, va_list ap)
66 char *msg;
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;
82 void libvlc_log_init (void)
84 vlc_Subscribe (&sub, libvlc_logf, NULL);
87 void libvlc_log_deinit (void)
89 vlc_Unsubscribe (&sub);
92 void libvlc_log_subscribe (libvlc_log_subscriber_t *sub,
93 libvlc_log_cb cb, void *data)
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);
104 void libvlc_log_unsubscribe( libvlc_log_subscriber_t *sub )
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);
116 /*** Helpers for logging to files ***/
117 static void libvlc_log_file (void *data, int level, const char *fmt,
118 va_list ap)
120 FILE *stream = data;
122 flockfile (stream);
123 vfprintf (stream, fmt, ap);
124 fputc ('\n', stream);
125 funlockfile (stream);
126 (void) level;
129 void libvlc_log_subscribe_file (libvlc_log_subscriber_t *sub, FILE *stream)
131 libvlc_log_subscribe (sub, libvlc_log_file, stream);
134 /*** Stubs for the old interface ***/
135 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
137 (void) p_instance;
138 return -1;
141 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
143 (void) p_instance;
144 (void) level;
147 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
149 (void) p_instance;
150 return malloc(1);
153 void libvlc_log_close( libvlc_log_t *p_log )
155 free(p_log);
158 unsigned libvlc_log_count( const libvlc_log_t *p_log )
160 (void) p_log;
161 return 0;
164 void libvlc_log_clear( libvlc_log_t *p_log )
166 (void) p_log;
169 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
171 return (p_log != NULL) ? malloc(1) : NULL;
174 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
176 free( p_iter );
179 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
181 (void) p_iter;
182 return 0;
185 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
186 libvlc_log_message_t *buffer )
188 (void) p_iter; (void) buffer;
189 return NULL;