demux: mp4: fix potential NULL deref
[vlc.git] / lib / log.c
blob66e0d996d1b005fadfffac0f21ef31f581869527
1 /*****************************************************************************
2 * log.c: libvlc new API log functions
3 *****************************************************************************
4 * Copyright (C) 2005 VLC authors and VideoLAN
7 * Authors: Damien Fouilleul <damienf@videolan.org>
9 * This program is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU Lesser General Public License as published by
11 * the Free Software Foundation; either version 2.1 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public License
20 * along with this program; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #include <assert.h>
29 #include <vlc/vlc.h>
30 #include "libvlc_internal.h"
31 #include <vlc_common.h>
32 #include <vlc_interface.h>
34 /*** Logging core dispatcher ***/
36 void libvlc_log_get_context(const libvlc_log_t *ctx,
37 const char **restrict module,
38 const char **restrict file,
39 unsigned *restrict line)
41 if (module != NULL)
42 *module = ctx->psz_module;
43 if (file != NULL)
44 *file = ctx->file;
45 if (line != NULL)
46 *line = ctx->line;
49 void libvlc_log_get_object(const libvlc_log_t *ctx,
50 const char **restrict name,
51 const char **restrict header,
52 uintptr_t *restrict id)
54 if (name != NULL)
55 *name = (ctx->psz_object_type != NULL)
56 ? ctx->psz_object_type : "generic";
57 if (header != NULL)
58 *header = ctx->psz_header;
59 if (id != NULL)
60 *id = ctx->i_object_id;
63 static void libvlc_logf (void *data, int level, const vlc_log_t *item,
64 const char *fmt, va_list ap)
66 libvlc_instance_t *inst = data;
68 switch (level)
70 case VLC_MSG_INFO: level = LIBVLC_NOTICE; break;
71 case VLC_MSG_ERR: level = LIBVLC_ERROR; break;
72 case VLC_MSG_WARN: level = LIBVLC_WARNING; break;
73 case VLC_MSG_DBG: level = LIBVLC_DEBUG; break;
76 inst->log.cb (inst->log.data, level, item, fmt, ap);
79 static const struct vlc_logger_operations libvlc_log_ops = {
80 libvlc_logf, NULL
83 void libvlc_log_unset (libvlc_instance_t *inst)
85 vlc_LogSet (inst->p_libvlc_int, NULL, NULL);
88 void libvlc_log_set (libvlc_instance_t *inst, libvlc_log_cb cb, void *data)
90 libvlc_log_unset (inst); /* <- Barrier before modifying the callback */
91 inst->log.cb = cb;
92 inst->log.data = data;
93 vlc_LogSet(inst->p_libvlc_int, &libvlc_log_ops, inst);
96 /*** Helpers for logging to files ***/
97 static void libvlc_log_file (void *data, int level, const libvlc_log_t *log,
98 const char *fmt, va_list ap)
100 FILE *stream = data;
102 flockfile (stream);
103 vfprintf (stream, fmt, ap);
104 fputc ('\n', stream);
105 funlockfile (stream);
106 (void) level; (void) log;
109 void libvlc_log_set_file (libvlc_instance_t *inst, FILE *stream)
111 libvlc_log_set (inst, libvlc_log_file, stream);