]> git.sesse.net Git - vlc/commitdiff
file: convert file logger to a logger module
authorRémi Denis-Courmont <remi@remlab.net>
Sun, 8 Feb 2015 15:35:31 +0000 (17:35 +0200)
committerRémi Denis-Courmont <remi@remlab.net>
Sun, 8 Feb 2015 15:35:55 +0000 (17:35 +0200)
modules/logger/Makefile.am
modules/logger/file.c [new file with mode: 0644]
modules/misc/logger.c
src/libvlc-module.c
src/libvlc.c

index 54c924fa45a656162f4676961b57ab2826123f88..229e3dc08048b679fe5bbc40e42fe65540058a34 100644 (file)
@@ -1,7 +1,8 @@
 loggerdir = $(pluginsdir)/logger
 
 libconsole_logger_plugin_la_SOURCES = logger/console.c
-logger_LTLIBRARIES = libconsole_logger_plugin.la
+libfile_logger_plugin_la_SOURCES = logger/file.c
+logger_LTLIBRARIES = libconsole_logger_plugin.la libfile_logger_plugin.la
 
 libsyslog_plugin_la_SOURCES = logger/syslog.c
 if HAVE_SYSLOG
diff --git a/modules/logger/file.c b/modules/logger/file.c
new file mode 100644 (file)
index 0000000..df6d202
--- /dev/null
@@ -0,0 +1,227 @@
+/*****************************************************************************
+ * file.c: file logger plugin
+ *****************************************************************************
+ * Copyright (C) 2002-2008 the VideoLAN team
+ * Copyright © 2007-2015 Rémi Denis-Courmont
+ *
+ * Authors: Samuel Hocevar <sam@zoy.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
+ * (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.
+ *
+ * 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.
+ *****************************************************************************/
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
+#include <vlc_fs.h>
+//#include <vlc_charset.h>
+
+#include <stdarg.h>
+#include <assert.h>
+#include <errno.h>
+
+static const char msg_type[4][9] = { "", " error", " warning", " debug" };
+
+typedef struct
+{
+    FILE *stream;
+    const char *footer;
+    int verbosity;
+} vlc_logger_sys_t;
+
+#define TEXT_FILENAME "vlc-log.txt"
+#define TEXT_HEADER "\xEF\xBB\xBF" /* UTF-8 BOM */ \
+                    "-- logger module started --\n"
+#define TEXT_FOOTER "-- logger module stopped --\n"
+
+static void LogText(void *opaque, int type, const vlc_log_t *meta,
+                    const char *format, va_list ap)
+{
+    vlc_logger_sys_t *sys = opaque;
+    FILE *stream = sys->stream;
+
+    if (sys->verbosity < type)
+        return;
+
+    int canc = vlc_savecancel();
+
+    flockfile(stream);
+    fprintf(stream, "%s%s: ", meta->psz_module, msg_type[type]);
+    vfprintf(stream, format, ap);
+    putc_unlocked('\n', stream);
+    funlockfile(stream);
+
+    vlc_restorecancel(canc);
+}
+
+#define HTML_FILENAME "vlc-log.html"
+#define HTML_HEADER \
+    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
+    "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
+    "<html>\n" \
+    "  <head>\n" \
+    "    <title>vlc log</title>\n" \
+    "    <meta http-equiv=\"Content-Type\"" \
+             " content=\"text/html; charset=UTF-8\">\n" \
+    "  </head>\n" \
+    "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
+    "    <pre>\n" \
+    "      <strong>-- logger module started --</strong>\n"
+#define HTML_FOOTER \
+    "      <strong>-- logger module stopped --</strong>\n" \
+    "    </pre>\n" \
+    "  </body>\n" \
+    "</html>\n"
+
+static void LogHtml(void *opaque, int type, const vlc_log_t *meta,
+                    const char *format, va_list ap)
+{
+    static const unsigned color[4] = {
+        0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
+    };
+    vlc_logger_sys_t *sys = opaque;
+    FILE *stream = sys->stream;
+
+    if (sys->verbosity < type)
+        return;
+
+    int canc = vlc_savecancel();
+
+    flockfile(stream);
+    fprintf(stream, "%s%s: <span style=\"color: #%06x\">",
+            meta->psz_module, msg_type[type], color[type]);
+    /* FIXME: encode special ASCII characters */
+    vfprintf(stream, format, ap);
+    fputs("</span>\n", stream);
+    funlockfile(stream);
+
+    vlc_restorecancel(canc);
+}
+
+static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
+{
+    if (!var_InheritBool(obj, "file-logging"))
+        return NULL;
+
+    int verbosity = var_InheritInteger(obj, "log-verbose");
+    if (verbosity == -1)
+        verbosity = var_InheritInteger(obj, "verbose");
+    if (verbosity < 0)
+        return NULL; /* nothing to log */
+
+    verbosity += VLC_MSG_ERR;
+
+    vlc_logger_sys_t *sys = malloc(sizeof (*sys));
+    if (unlikely(sys == NULL))
+        return NULL;
+
+    const char *filename = TEXT_FILENAME;
+    const char *header = TEXT_HEADER;
+
+    vlc_log_cb cb = LogText;
+    sys->footer = TEXT_FOOTER;
+    sys->verbosity = verbosity;
+
+    char *mode = var_InheritString(obj, "logmode");
+    if (mode != NULL)
+    {
+        if (!strcmp(mode, "html"))
+        {
+            filename = HTML_FILENAME;
+            header = HTML_HEADER;
+            cb = LogHtml;
+            sys->footer = HTML_FOOTER;
+        }
+        else if (strcmp(mode, "text"))
+            msg_Warn(obj, "invalid log mode \"%s\"", mode);
+        free(mode);
+    }
+
+    char *path = var_InheritString(obj, "logfile");
+#ifdef __APPLE__
+    if (path == NULL)
+    {
+        char *home = config_GetUserDir(VLC_HOME_DIR);
+        if (home != NULL)
+        {
+            if (asprintf(&path, "%s/Library/Logs/%s", home, path) == -1)
+                path = NULL;
+            free(home);
+        }
+    }
+#endif
+    if (path != NULL)
+        filename = path;
+
+    /* Open the log file and remove any buffering for the stream */
+    msg_Dbg(obj, "opening logfile `%s'", filename);
+    sys->stream = vlc_fopen(filename, "at");
+    if (sys->stream == NULL)
+    {
+        msg_Err(obj, "error opening log file `%s': %s", filename,
+                vlc_strerror_c(errno) );
+        free(path);
+        free(sys);
+        return NULL;
+    }
+    free(path);
+
+    setvbuf(sys->stream, NULL, _IONBF, 0);
+    fputs(header, sys->stream);
+
+    *sysp = sys;
+    return cb;
+}
+
+static void Close(void *opaque)
+{
+    vlc_logger_sys_t *sys = opaque;
+
+    fputs(sys->footer, sys->stream);
+    fclose(sys->stream);
+    free(sys);
+}
+
+static const char *const mode_list[] = { "text", "html" };
+static const char *const mode_list_text[] = { N_("Text"), N_("HTML") };
+
+#define FILE_LOG_TEXT N_("Log to file")
+#define FILE_LOG_LONGTEXT N_("Log all VLC messages to a text file.")
+
+#define LOGMODE_TEXT N_("Log format")
+#define LOGMODE_LONGTEXT N_("Specify the logging format.")
+
+#define LOGVERBOSE_TEXT N_("Verbosity")
+#define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
+"use the same verbosity given by --verbose.")
+
+vlc_module_begin()
+    set_shortname(N_("Logger"))
+    set_description(N_("File logger"))
+    set_category(CAT_ADVANCED)
+    set_subcategory(SUBCAT_ADVANCED_MISC)
+    set_capability("logger", 15)
+    set_callbacks(Open, Close)
+
+    add_bool("file-logging", false, FILE_LOG_TEXT, FILE_LOG_LONGTEXT, false)
+    add_savefile("logfile", NULL,
+                 N_("Log filename"), N_("Specify the log filename."), false)
+    add_string("logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT, false)
+        change_string_list(mode_list, mode_list_text)
+    add_integer("log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
+                false)
+vlc_module_end ()
index 3b01f0ec4710ead968bda0c2354a25c07f5a7422..bc171287a254be8c04c98f20f60b20237682f595 100644 (file)
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_interface.h>
-#include <vlc_fs.h>
-#include <vlc_charset.h>
 
 #include <stdarg.h>
-#include <assert.h>
-#include <errno.h>
 
 #ifdef __ANDROID__
 # include <android/log.h>
 #endif
 
-#define LOG_FILE_TEXT "vlc-log.txt"
-#define LOG_FILE_HTML "vlc-log.html"
-
-#define TEXT_HEADER "\xEF\xBB\xBF-- logger module started --\n"
-#define TEXT_FOOTER "-- logger module stopped --\n"
-
-#define HTML_HEADER \
-    "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
-    "  \"http://www.w3.org/TR/html4/strict.dtd\">\n" \
-    "<html>\n" \
-    "  <head>\n" \
-    "    <title>vlc log</title>\n" \
-    "    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" \
-    "  </head>\n" \
-    "  <body style=\"background-color: #000000; color: #aaaaaa;\">\n" \
-    "    <pre>\n" \
-    "      <strong>-- logger module started --</strong>\n"
-#define HTML_FOOTER \
-    "      <strong>-- logger module stopped --</strong>\n" \
-    "    </pre>\n" \
-    "  </body>\n" \
-    "</html>\n"
-
-/*****************************************************************************
- * intf_sys_t: description and status of log interface
- *****************************************************************************/
-struct intf_sys_t
-{
-    FILE *p_file;
-    const char *footer;
-};
-
 /*****************************************************************************
  * Local prototypes
  *****************************************************************************/
 static int  Open    ( vlc_object_t * );
 static void Close   ( vlc_object_t * );
 
-static void TextPrint(void *, int, const vlc_log_t *, const char *, va_list);
-static void HtmlPrint(void *, int, const vlc_log_t *, const char *, va_list);
 #ifdef __ANDROID__
 static void AndroidPrint(void *, int, const vlc_log_t *, const char *, va_list);
 #endif
@@ -90,24 +52,6 @@ static void AndroidPrint(void *, int, const vlc_log_t *, const char *, va_list);
 /*****************************************************************************
  * Module descriptor
  *****************************************************************************/
-static const char *const mode_list[] = { "text", "html"
-#ifdef __ANDROID__
-,"android"
-#endif
-};
-static const char *const mode_list_text[] = { N_("Text"), "HTML"
-#ifdef __ANDROID__
-,"android"
-#endif
-};
-
-#define LOGMODE_TEXT N_("Log format")
-#define LOGMODE_LONGTEXT N_("Specify the logging format.")
-
-#define LOGVERBOSE_TEXT N_("Verbosity")
-#define LOGVERBOSE_LONGTEXT N_("Select the verbosity to use for log or -1 to " \
-"use the same verbosity given by --verbose.")
-
 vlc_module_begin ()
     set_shortname( N_( "Logging" ) )
     set_description( N_("File logging") )
@@ -115,14 +59,6 @@ vlc_module_begin ()
     set_category( CAT_ADVANCED )
     set_subcategory( SUBCAT_ADVANCED_MISC )
 
-    add_savefile( "logfile", NULL,
-             N_("Log filename"), N_("Specify the log filename."), false )
-    add_string( "logmode", "text", LOGMODE_TEXT, LOGMODE_LONGTEXT,
-                false )
-        change_string_list( mode_list, mode_list_text )
-    add_integer( "log-verbose", -1, LOGVERBOSE_TEXT, LOGVERBOSE_LONGTEXT,
-           false )
-    
     add_obsolete_string( "rrd-file" )
 
     set_capability( "interface", 0 )
@@ -135,85 +71,21 @@ vlc_module_end ()
 static int Open( vlc_object_t *p_this )
 {
     intf_thread_t *p_intf = (intf_thread_t *)p_this;
-    intf_sys_t *p_sys;
-
-    CONSOLE_INTRO_MSG;
-    msg_Info( p_intf, "using logger." );
-
-    /* Allocate instance and initialize some members */
-    p_sys = p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
-    if( p_sys == NULL )
-        return VLC_ENOMEM;
-
-    p_sys->p_file = NULL;
-    vlc_log_cb cb = TextPrint;
-    const char *filename = LOG_FILE_TEXT, *header = TEXT_HEADER;
-    p_sys->footer = TEXT_FOOTER;
-
-    char *mode = var_InheritString( p_intf, "logmode" );
-    if( mode != NULL )
-    {
-        if( !strcmp( mode, "html" ) )
-        {
-            p_sys->footer = HTML_FOOTER;
-            filename = LOG_FILE_HTML;
-            header = HTML_HEADER;
-            cb = HtmlPrint;
-        }
-#ifdef __ANDROID__
-        else if( !strcmp( mode, "android" ) )
-            cb = AndroidPrint;
-#endif
-        else if( strcmp( mode, "text" ) )
-            msg_Warn( p_intf, "invalid log mode `%s', using `text'", mode );
-        free( mode );
-    }
 
 #ifdef __ANDROID__
-    if( cb == AndroidPrint )
-    {
-        /* nothing to do */
-    }
-    else
-#endif
-    {
-        char *psz_file = var_InheritString( p_intf, "logfile" );
-        if( !psz_file )
-        {
-#ifdef __APPLE__
-# define LOG_DIR "Library/Logs"
-            char *home = config_GetUserDir(VLC_HOME_DIR);
-            if( home == NULL
-             || asprintf( &psz_file, "%s/"LOG_DIR"/%s", home,
-                          filename ) == -1 )
-                psz_file = NULL;
-            free(home);
-            filename = psz_file;
-#endif
-            msg_Warn( p_intf, "no log filename provided, using `%s'",
-                      filename );
-        }
-        else
-            filename = psz_file;
+    msg_Info( p_this, "using logger." );
 
-        /* Open the log file and remove any buffering for the stream */
-        msg_Dbg( p_intf, "opening logfile `%s'", filename );
-        p_sys->p_file = vlc_fopen( filename, "at" );
-        if( p_sys->p_file == NULL )
-        {
-            msg_Err( p_intf, "error opening logfile `%s': %s", filename,
-                     vlc_strerror_c(errno) );
-            free( psz_file );
-            free( p_sys );
-            return VLC_EGENERIC;
-        }
-        free( psz_file );
-        setvbuf( p_sys->p_file, NULL, _IONBF, 0 );
-        fputs( header, p_sys->p_file );
-    }
-
-    vlc_LogSet( p_intf->p_libvlc, cb, p_intf );
+    vlc_LogSet( p_intf->p_libvlc, AndroidPrint, p_intf );
     return VLC_SUCCESS;
+#else
+    msg_Err( p_intf, "The logger interface no longer exists." );
+    msg_Info( p_intf, "As of VLC version 0.9.0, use --file-logging to write "
+              "logs to a file." );
+# ifndef _WIN32
+    msg_Info( p_intf, "Use --syslog to send logs to the system logger." );
+# endif
+    return VLC_EGENERIC;
+#endif
 }
 
 /*****************************************************************************
@@ -221,23 +93,11 @@ static int Open( vlc_object_t *p_this )
  *****************************************************************************/
 static void Close( vlc_object_t *p_this )
 {
-    intf_thread_t *p_intf = (intf_thread_t *)p_this;
-    intf_sys_t *p_sys = p_intf->p_sys;
-
     /* Flush the queue and unsubscribe from the message queue */
-    vlc_LogSet( p_intf->p_libvlc, NULL, NULL );
-
-    /* Close the log file */
-    if( p_sys->p_file )
-    {
-        fputs( p_sys->footer, p_sys->p_file );
-        fclose( p_sys->p_file );
-    }
-
-    /* Destroy structure */
-    free( p_sys );
+    vlc_LogSet( p_this->p_libvlc, NULL, NULL );
 }
 
+#ifdef __ANDROID__
 static bool IgnoreMessage( intf_thread_t *p_intf, int type )
 {
     /* TODO: cache value... */
@@ -259,7 +119,6 @@ static const char ppsz_type[4][9] = {
     " debug",
 };
 
-#ifdef __ANDROID__
 static const android_LogPriority prioritytype[4] = {
     ANDROID_LOG_INFO,
     ANDROID_LOG_ERROR,
@@ -281,45 +140,3 @@ static void AndroidPrint( void *opaque, int type, const vlc_log_t *item,
     vlc_restorecancel( canc );
 }
 #endif
-
-static void TextPrint( void *opaque, int type, const vlc_log_t *item,
-                       const char *fmt, va_list ap )
-{
-    intf_thread_t *p_intf = opaque;
-    FILE *stream = p_intf->p_sys->p_file;
-
-    if( IgnoreMessage( p_intf, type ) )
-        return;
-
-    int canc = vlc_savecancel();
-    flockfile( stream );
-    fprintf( stream, "%s%s: ", item->psz_module, ppsz_type[type] );
-    vfprintf( stream, fmt, ap );
-    putc_unlocked( '\n', stream );
-    funlockfile( stream );
-    vlc_restorecancel( canc );
-}
-
-static void HtmlPrint( void *opaque, int type, const vlc_log_t *item,
-                       const char *fmt, va_list ap )
-{
-    static const unsigned color[4] = {
-        0xffffff, 0xff6666, 0xffff66, 0xaaaaaa,
-    };
-
-    intf_thread_t *p_intf = opaque;
-    FILE *stream = p_intf->p_sys->p_file;
-
-    if( IgnoreMessage( p_intf, type ) )
-        return;
-
-    int canc = vlc_savecancel();
-    flockfile( stream );
-    fprintf( stream, "%s%s: <span style=\"color: #%06x\">",
-             item->psz_module, ppsz_type[type], color[type] );
-    /* FIXME: encode special ASCII characters */
-    vfprintf( stream, fmt, ap );
-    fputs( "</span>\n", stream );
-    funlockfile( stream );
-    vlc_restorecancel( canc );
-}
index 6176f4ff93adb3d35c6f2c599512ae888d82d9cd..76b1914f0210e3d7ee38de8f1a5e9cb4e7944db6 100644 (file)
@@ -1036,10 +1036,6 @@ static const char *const ppsz_prefres[] = {
 #define PIDFILE_LONGTEXT N_( \
        "Writes process id into specified file.")
 
-#define FILE_LOG_TEXT N_( "Log to file" )
-#define FILE_LOG_LONGTEXT N_( \
-    "Log all VLC messages to a text file." )
-
 #define ONEINSTANCE_TEXT N_("Allow only one running instance")
 #if defined( _WIN32 ) || defined( __OS2__ )
 #define ONEINSTANCE_LONGTEXT N_( \
@@ -2034,9 +2030,6 @@ vlc_module_begin ()
                                        false )
 #endif
 
-    add_bool( "file-logging", false, FILE_LOG_TEXT, FILE_LOG_LONGTEXT,
-              true )
-
 #if defined (_WIN32) || defined (__APPLE__)
     add_obsolete_string( "language" ) /* since 2.1.0 */
 #endif
index 6aa0edb81927cc2905eeee7816e8f0f31e731a9c..cff3784c1660d59886f8ce6e58967df8811a4fbc 100644 (file)
@@ -455,9 +455,6 @@ dbus_out:
     free( psz_modules );
     free( psz_control );
 
-    if( var_InheritBool( p_libvlc, "file-logging" ) )
-        libvlc_InternalAddIntf( p_libvlc, "logger,none" );
-
     if( var_InheritBool( p_libvlc, "network-synchronisation") )
         libvlc_InternalAddIntf( p_libvlc, "netsync,none" );