]> git.sesse.net Git - vlc/blob - lib/log.c
ea883e8deed0152816c53e54d308e84a7c1cd8a8
[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 VLC_FORMAT(4,5)
38 static void libvlc_log (libvlc_instance_t *inst, int level,
39                         const libvlc_log_t *ctx, const char *fmt, ...)
40 {
41     va_list ap;
42
43     va_start (ap, fmt);
44     inst->log.cb (inst->log.data, level, ctx, fmt, ap);
45     va_end (ap);
46 }
47
48 static void libvlc_logf (void *data, int level, const vlc_log_t *item,
49                          const char *fmt, va_list ap)
50 {
51     char *msg;
52
53     if (unlikely(vasprintf (&msg, fmt, ap) == -1))
54         return;
55
56     switch (level)
57     {
58         case VLC_MSG_INFO: level = LIBVLC_NOTICE;  break;
59         case VLC_MSG_ERR:  level = LIBVLC_ERROR;   break;
60         case VLC_MSG_WARN: level = LIBVLC_WARNING; break;
61         case VLC_MSG_DBG:  level = LIBVLC_DEBUG;   break;
62     }
63
64     if (item->psz_header != NULL)
65         libvlc_log (data, level, item, "[%p] [%s]: %s %s %s",
66                     (void *)item->i_object_id, item->psz_header,
67                     item->psz_module, item->psz_object_type, msg);
68     else
69         libvlc_log (data, level, item, "[%p]: %s %s %s",
70                     (void *)item->i_object_id, item->psz_module,
71                     item->psz_object_type, msg);
72     free (msg);
73 }
74
75 void libvlc_log_unset (libvlc_instance_t *inst)
76 {
77     vlc_LogSet (inst->p_libvlc_int, NULL, NULL);
78 }
79
80 void libvlc_log_set (libvlc_instance_t *inst, libvlc_log_cb cb, void *data)
81 {
82     libvlc_log_unset (inst); /* <- Barrier before modifying the callback */
83     inst->log.cb = cb;
84     inst->log.data = data;
85     vlc_LogSet (inst->p_libvlc_int, libvlc_logf, inst);
86 }
87
88 /*** Helpers for logging to files ***/
89 static void libvlc_log_file (void *data, int level, const libvlc_log_t *log,
90                              const char *fmt, va_list ap)
91 {
92     FILE *stream = data;
93
94     flockfile (stream);
95     vfprintf (stream, fmt, ap);
96     fputc ('\n', stream);
97     funlockfile (stream);
98     (void) level; (void) log;
99 }
100
101 void libvlc_log_set_file (libvlc_instance_t *inst, FILE *stream)
102 {
103     libvlc_log_set (inst, libvlc_log_file, stream);
104 }
105
106 /*** Stubs for the old interface ***/
107 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
108 {
109     (void) p_instance;
110     return -1;
111 }
112
113 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
114 {
115     (void) p_instance;
116     (void) level;
117 }
118
119 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
120 {
121     (void) p_instance;
122     return malloc(1);
123 }
124
125 void libvlc_log_close( libvlc_log_t *p_log )
126 {
127     free(p_log);
128 }
129
130 unsigned libvlc_log_count( const libvlc_log_t *p_log )
131 {
132     (void) p_log;
133     return 0;
134 }
135
136 void libvlc_log_clear( libvlc_log_t *p_log )
137 {
138     (void) p_log;
139 }
140
141 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
142 {
143     return (p_log != NULL) ? malloc(1) : NULL;
144 }
145
146 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
147 {
148     free( p_iter );
149 }
150
151 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
152 {
153     (void) p_iter;
154     return 0;
155 }
156
157 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
158                                                 libvlc_log_message_t *buffer )
159 {
160     (void) p_iter; (void) buffer;
161     return NULL;
162 }