1 /*****************************************************************************
2  * log.c: libvlc new API log functions
3  *****************************************************************************
4  * Copyright (C) 2005 VLC authors and VideoLAN
5  *
6  * $Id: f3a769a9ba21ee635c172afbeb8fe51074975c4a $
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/vlc.h>
31 #include "libvlc_internal.h"
32 #include <vlc_common.h>
33 #include <vlc_interface.h>
34 
35 /*** Logging core dispatcher ***/
36 
libvlc_log_get_context(const libvlc_log_t * ctx,const char ** restrict module,const char ** restrict file,unsigned * restrict line)37 void libvlc_log_get_context(const libvlc_log_t *ctx,
38                             const char **restrict module,
39                             const char **restrict file,
40                             unsigned *restrict line)
41 {
42     if (module != NULL)
43         *module = ctx->psz_module;
44     if (file != NULL)
45         *file = ctx->file;
46     if (line != NULL)
47         *line = ctx->line;
48 }
49 
libvlc_log_get_object(const libvlc_log_t * ctx,const char ** restrict name,const char ** restrict header,uintptr_t * restrict id)50 void libvlc_log_get_object(const libvlc_log_t *ctx,
51                            const char **restrict name,
52                            const char **restrict header,
53                            uintptr_t *restrict id)
54 {
55     if (name != NULL)
56         *name = (ctx->psz_object_type != NULL)
57                 ? ctx->psz_object_type : "generic";
58     if (header != NULL)
59         *header = ctx->psz_header;
60     if (id != NULL)
61         *id = ctx->i_object_id;
62 }
63 
libvlc_logf(void * data,int level,const vlc_log_t * item,const char * fmt,va_list ap)64 static void libvlc_logf (void *data, int level, const vlc_log_t *item,
65                          const char *fmt, va_list ap)
66 {
67     libvlc_instance_t *inst = data;
68 
69     switch (level)
70     {
71         case VLC_MSG_INFO: level = LIBVLC_NOTICE;  break;
72         case VLC_MSG_ERR:  level = LIBVLC_ERROR;   break;
73         case VLC_MSG_WARN: level = LIBVLC_WARNING; break;
74         case VLC_MSG_DBG:  level = LIBVLC_DEBUG;   break;
75     }
76 
77     inst->log.cb (inst->log.data, level, item, fmt, ap);
78 }
79 
libvlc_log_unset(libvlc_instance_t * inst)80 void libvlc_log_unset (libvlc_instance_t *inst)
81 {
82     vlc_LogSet (inst->p_libvlc_int, NULL, NULL);
83 }
84 
libvlc_log_set(libvlc_instance_t * inst,libvlc_log_cb cb,void * data)85 void libvlc_log_set (libvlc_instance_t *inst, libvlc_log_cb cb, void *data)
86 {
87     libvlc_log_unset (inst); /* <- Barrier before modifying the callback */
88     inst->log.cb = cb;
89     inst->log.data = data;
90     vlc_LogSet (inst->p_libvlc_int, libvlc_logf, inst);
91 }
92 
93 /*** Helpers for logging to files ***/
libvlc_log_file(void * data,int level,const libvlc_log_t * log,const char * fmt,va_list ap)94 static void libvlc_log_file (void *data, int level, const libvlc_log_t *log,
95                              const char *fmt, va_list ap)
96 {
97     FILE *stream = data;
98 
99     flockfile (stream);
100     vfprintf (stream, fmt, ap);
101     fputc ('\n', stream);
102     funlockfile (stream);
103     (void) level; (void) log;
104 }
105 
libvlc_log_set_file(libvlc_instance_t * inst,FILE * stream)106 void libvlc_log_set_file (libvlc_instance_t *inst, FILE *stream)
107 {
108     libvlc_log_set (inst, libvlc_log_file, stream);
109 }
110 
111 /*** Stubs for the old interface ***/
libvlc_get_log_verbosity(const libvlc_instance_t * p_instance)112 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
113 {
114     (void) p_instance;
115     return -1;
116 }
117 
libvlc_set_log_verbosity(libvlc_instance_t * p_instance,unsigned level)118 void libvlc_set_log_verbosity( libvlc_instance_t *p_instance, unsigned level )
119 {
120     (void) p_instance;
121     (void) level;
122 }
123 
libvlc_log_open(libvlc_instance_t * p_instance)124 libvlc_log_t *libvlc_log_open( libvlc_instance_t *p_instance )
125 {
126     (void) p_instance;
127     return malloc(sizeof(libvlc_log_t));
128 }
129 
libvlc_log_close(libvlc_log_t * p_log)130 void libvlc_log_close( libvlc_log_t *p_log )
131 {
132     free(p_log);
133 }
134 
libvlc_log_count(const libvlc_log_t * p_log)135 unsigned libvlc_log_count( const libvlc_log_t *p_log )
136 {
137     (void) p_log;
138     return 0;
139 }
140 
libvlc_log_clear(libvlc_log_t * p_log)141 void libvlc_log_clear( libvlc_log_t *p_log )
142 {
143     (void) p_log;
144 }
145 
libvlc_log_get_iterator(const libvlc_log_t * p_log)146 libvlc_log_iterator_t *libvlc_log_get_iterator( const libvlc_log_t *p_log )
147 {
148     return (p_log != NULL) ? malloc(1) : NULL;
149 }
150 
libvlc_log_iterator_free(libvlc_log_iterator_t * p_iter)151 void libvlc_log_iterator_free( libvlc_log_iterator_t *p_iter )
152 {
153     free( p_iter );
154 }
155 
libvlc_log_iterator_has_next(const libvlc_log_iterator_t * p_iter)156 int libvlc_log_iterator_has_next( const libvlc_log_iterator_t *p_iter )
157 {
158     (void) p_iter;
159     return 0;
160 }
161 
libvlc_log_iterator_next(libvlc_log_iterator_t * p_iter,libvlc_log_message_t * buffer)162 libvlc_log_message_t *libvlc_log_iterator_next( libvlc_log_iterator_t *p_iter,
163                                                 libvlc_log_message_t *buffer )
164 {
165     (void) p_iter; (void) buffer;
166     return NULL;
167 }
168