]> git.sesse.net Git - vlc/blobdiff - lib/log.c
libvlc: simplify and clean up external logging API (fixes #8129)
[vlc] / lib / log.c
index 451a6d014234d430d3401bf4eca40162d00810a8..ea883e8deed0152816c53e54d308e84a7c1cd8a8 100644 (file)
--- a/lib/log.c
+++ b/lib/log.c
 /*****************************************************************************
  * log.c: libvlc new API log functions
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005 VLC authors and VideoLAN
  *
  * $Id$
  *
  * Authors: Damien Fouilleul <damienf@videolan.org>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
  *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 #ifdef HAVE_CONFIG_H
 # include "config.h"
 #endif
 
-#include "libvlc_internal.h"
-#include <vlc/libvlc.h>
 #include <assert.h>
+#include <vlc/libvlc.h>
+#include "libvlc_internal.h"
+#include <vlc_common.h>
+#include <vlc_interface.h>
+
+/*** Logging core dispatcher ***/
+
+VLC_FORMAT(4,5)
+static void libvlc_log (libvlc_instance_t *inst, int level,
+                        const libvlc_log_t *ctx, const char *fmt, ...)
+{
+    va_list ap;
+
+    va_start (ap, fmt);
+    inst->log.cb (inst->log.data, level, ctx, fmt, ap);
+    va_end (ap);
+}
+
+static void libvlc_logf (void *data, int level, const vlc_log_t *item,
+                         const char *fmt, va_list ap)
+{
+    char *msg;
+
+    if (unlikely(vasprintf (&msg, fmt, ap) == -1))
+        return;
+
+    switch (level)
+    {
+        case VLC_MSG_INFO: level = LIBVLC_NOTICE;  break;
+        case VLC_MSG_ERR:  level = LIBVLC_ERROR;   break;
+        case VLC_MSG_WARN: level = LIBVLC_WARNING; break;
+        case VLC_MSG_DBG:  level = LIBVLC_DEBUG;   break;
+    }
 
-struct libvlc_log_t
+    if (item->psz_header != NULL)
+        libvlc_log (data, level, item, "[%p] [%s]: %s %s %s",
+                    (void *)item->i_object_id, item->psz_header,
+                    item->psz_module, item->psz_object_type, msg);
+    else
+        libvlc_log (data, level, item, "[%p]: %s %s %s",
+                    (void *)item->i_object_id, item->psz_module,
+                    item->psz_object_type, msg);
+    free (msg);
+}
+
+void libvlc_log_unset (libvlc_instance_t *inst)
 {
-    libvlc_instance_t  *p_instance;
-};
+    vlc_LogSet (inst->p_libvlc_int, NULL, NULL);
+}
+
+void libvlc_log_set (libvlc_instance_t *inst, libvlc_log_cb cb, void *data)
+{
+    libvlc_log_unset (inst); /* <- Barrier before modifying the callback */
+    inst->log.cb = cb;
+    inst->log.data = data;
+    vlc_LogSet (inst->p_libvlc_int, libvlc_logf, inst);
+}
+
+/*** Helpers for logging to files ***/
+static void libvlc_log_file (void *data, int level, const libvlc_log_t *log,
+                             const char *fmt, va_list ap)
+{
+    FILE *stream = data;
+
+    flockfile (stream);
+    vfprintf (stream, fmt, ap);
+    fputc ('\n', stream);
+    funlockfile (stream);
+    (void) level; (void) log;
+}
+
+void libvlc_log_set_file (libvlc_instance_t *inst, FILE *stream)
+{
+    libvlc_log_set (inst, libvlc_log_file, stream);
+}
 
+/*** Stubs for the old interface ***/
 unsigned libvlc_get_log_verbosity( const libvlc_instance_t *p_instance )
 {
     (void) p_instance;