[vlc-commits] log: define constant operations structure

Rémi Denis-Courmont git at videolan.org
Sun Nov 18 16:57:31 CET 2018


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Sun Nov 18 15:48:49 2018 +0200| [49b6bc33512fee8f1e113ccaa72aaf7f9fd50963] | committer: Rémi Denis-Courmont

log: define constant operations structure

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=49b6bc33512fee8f1e113ccaa72aaf7f9fd50963
---

 include/vlc_interface.h                     |  3 +-
 include/vlc_messages.h                      |  6 ++++
 lib/log.c                                   |  6 +++-
 modules/gui/macosx/VLCLogWindowController.m |  6 ++--
 modules/gui/ncurses.c                       |  4 ++-
 modules/gui/qt/dialogs/messages.cpp         |  8 ++++-
 modules/logger/android.c                    |  6 ++--
 modules/logger/console.c                    | 18 ++++++++--
 modules/logger/file.c                       | 21 ++++++++---
 modules/logger/journal.c                    |  4 ++-
 modules/logger/syslog.c                     |  7 ++--
 src/misc/messages.c                         | 54 ++++++++++++++++++-----------
 12 files changed, 105 insertions(+), 38 deletions(-)

diff --git a/include/vlc_interface.h b/include/vlc_interface.h
index c18d32b2e7..632759060b 100644
--- a/include/vlc_interface.h
+++ b/include/vlc_interface.h
@@ -111,7 +111,8 @@ vlc_intf_GetMainPlaylist(intf_thread_t *intf);
  * @{
  */
 
-VLC_API void vlc_LogSet(libvlc_int_t *, vlc_log_cb cb, void *data);
+VLC_API void vlc_LogSet(libvlc_int_t *, const struct vlc_logger_operations *,
+                        void *data);
 
 /*@}*/
 
diff --git a/include/vlc_messages.h b/include/vlc_messages.h
index d14bbb8c3f..e69a80c013 100644
--- a/include/vlc_messages.h
+++ b/include/vlc_messages.h
@@ -103,6 +103,12 @@ VLC_API const char *vlc_strerror_c(int);
 typedef void (*vlc_log_cb) (void *data, int type, const vlc_log_t *item,
                             const char *fmt, va_list args);
 
+struct vlc_logger_operations
+{
+    vlc_log_cb log;
+    void (*destroy)(void *data);
+};
+
 /**
  * @}
  */
diff --git a/lib/log.c b/lib/log.c
index f54328840b..1265abe240 100644
--- a/lib/log.c
+++ b/lib/log.c
@@ -77,6 +77,10 @@ static void libvlc_logf (void *data, int level, const vlc_log_t *item,
     inst->log.cb (inst->log.data, level, item, fmt, ap);
 }
 
+static const struct vlc_logger_operations libvlc_log_ops = {
+    libvlc_logf, NULL
+};
+
 void libvlc_log_unset (libvlc_instance_t *inst)
 {
     vlc_LogSet (inst->p_libvlc_int, NULL, NULL);
@@ -87,7 +91,7 @@ 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);
+    vlc_LogSet(inst->p_libvlc_int, &libvlc_log_ops, inst);
 }
 
 /*** Helpers for logging to files ***/
diff --git a/modules/gui/macosx/VLCLogWindowController.m b/modules/gui/macosx/VLCLogWindowController.m
index a52336d93e..82e9f8fdb3 100644
--- a/modules/gui/macosx/VLCLogWindowController.m
+++ b/modules/gui/macosx/VLCLogWindowController.m
@@ -69,6 +69,8 @@ static void MsgCallback(void *data, int type, const vlc_log_t *item, const char
     }
 }
 
+static const struct vlc_logger_operations log_ops = { MsgCallback, NULL };
+
 @implementation VLCLogWindowController
 
 - (id)init
@@ -122,7 +124,7 @@ static void MsgCallback(void *data, int type, const vlc_log_t *item, const char
     }
 
     // Subscribe to LibVLCCore's messages
-    vlc_LogSet(getIntf()->obj.libvlc, MsgCallback, (__bridge void*)self);
+    vlc_LogSet(getIntf()->obj.libvlc, &log_ops, (__bridge void*)self);
     _refreshTimer = [NSTimer scheduledTimerWithTimeInterval:0.3
                                                      target:self
                                                    selector:@selector(appendMessageBuffer)
@@ -221,7 +223,7 @@ static void MsgCallback(void *data, int type, const vlc_log_t *item, const char
     [self clearMessageTable];
 
     // Reregister handler, to write new header to log
-    vlc_LogSet(getIntf()->obj.libvlc, MsgCallback, (__bridge void*)self);
+    vlc_LogSet(getIntf()->obj.libvlc, &log_ops, (__bridge void*)self);
 }
 
 /* Refresh log action
diff --git a/modules/gui/ncurses.c b/modules/gui/ncurses.c
index 794479fb65..56022266b6 100644
--- a/modules/gui/ncurses.c
+++ b/modules/gui/ncurses.c
@@ -1764,6 +1764,8 @@ static void MsgCallback(void *data, int type, const vlc_log_t *msg,
     vlc_mutex_unlock(&sys->msg_lock);
 }
 
+static const struct vlc_logger_operations log_ops = { MsgCallback, NULL };
+
 /*****************************************************************************
  * Run: ncurses thread
  *****************************************************************************/
@@ -1802,7 +1804,7 @@ static int Open(vlc_object_t *p_this)
     vlc_mutex_init(&sys->msg_lock);
 
     sys->verbosity = var_InheritInteger(intf, "verbose");
-    vlc_LogSet(intf->obj.libvlc, MsgCallback, sys);
+    vlc_LogSet(intf->obj.libvlc, &log_ops, sys);
 
     sys->box_type = BOX_PLAYLIST;
     sys->plidx_follow = true;
diff --git a/modules/gui/qt/dialogs/messages.cpp b/modules/gui/qt/dialogs/messages.cpp
index 7f0bf9eb38..45b96dc8b8 100644
--- a/modules/gui/qt/dialogs/messages.cpp
+++ b/modules/gui/qt/dialogs/messages.cpp
@@ -133,7 +133,13 @@ MessagesDialog::MessagesDialog( intf_thread_t *_p_intf)
     restoreWidgetPosition( "Messages", QSize( 600, 450 ) );
 
     /* Hook up to LibVLC messaging */
-    vlc_LogSet( p_intf->obj.libvlc, MsgCallback, this );
+    static const struct vlc_logger_operations log_ops =
+    {
+        MessagesDialog::MsgCallback,
+        NULL
+    };
+
+    vlc_LogSet( p_intf->obj.libvlc, &log_ops, this );
 
     buildTree( NULL, VLC_OBJECT( p_intf->obj.libvlc ) );
 }
diff --git a/modules/logger/android.c b/modules/logger/android.c
index a2207d4ef3..99070f2c47 100644
--- a/modules/logger/android.c
+++ b/modules/logger/android.c
@@ -68,7 +68,9 @@ static void AndroidPrintMsg(void *opaque, int type, const vlc_log_t *p_item,
     free(format2);
 }
 
-static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
+static const struct vlc_logger_operations ops = { AndroidPrintMsg, NULL };
+
+static const struct vlc_logger_operations *Open(vlc_object_t *obj, void **sysp)
 {
     int verbosity = var_InheritInteger(obj, "verbose");
 
@@ -78,7 +80,7 @@ static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
     verbosity += VLC_MSG_ERR;
     *sysp = (void *)(uintptr_t)verbosity;
 
-    return AndroidPrintMsg;
+    return &ops;
 }
 
 vlc_module_begin()
diff --git a/modules/logger/console.c b/modules/logger/console.c
index 39d71489ad..37dc862bb0 100644
--- a/modules/logger/console.c
+++ b/modules/logger/console.c
@@ -93,6 +93,12 @@ static void LogConsoleColor(void *opaque, int type, const vlc_log_t *meta,
     fputs(GRAY"\n", stream);
     funlockfile(stream);
 }
+
+static const struct vlc_logger_operations color_ops =
+{
+    LogConsoleColor,
+    NULL
+};
 #endif /* !_WIN32 */
 
 static void LogConsoleGray(void *opaque, int type, const vlc_log_t *meta,
@@ -118,7 +124,13 @@ static void LogConsoleGray(void *opaque, int type, const vlc_log_t *meta,
     funlockfile(stream);
 }
 
-static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
+static const struct vlc_logger_operations gray_ops =
+{
+    LogConsoleGray,
+    NULL
+};
+
+static const struct vlc_logger_operations *Open(vlc_object_t *obj, void **sysp)
 {
     int verbosity = -1;
 
@@ -139,9 +151,9 @@ static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
 
 #if defined (HAVE_ISATTY) && !defined (_WIN32)
     if (isatty(STDERR_FILENO) && var_InheritBool(obj, "color"))
-        return LogConsoleColor;
+        return &color_ops;
 #endif
-    return LogConsoleGray;
+    return &gray_ops;
 }
 
 #define QUIET_TEXT N_("Be quiet")
diff --git a/modules/logger/file.c b/modules/logger/file.c
index b52caac34e..6c6972d661 100644
--- a/modules/logger/file.c
+++ b/modules/logger/file.c
@@ -65,6 +65,12 @@ static void LogText(void *opaque, int type, const vlc_log_t *meta,
     funlockfile(stream);
 }
 
+static const struct vlc_logger_operations text_ops =
+{
+    LogText,
+    NULL
+};
+
 #define HTML_FILENAME "vlc-log.html"
 #define HTML_HEADER \
     "<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\"\n" \
@@ -105,7 +111,14 @@ static void LogHtml(void *opaque, int type, const vlc_log_t *meta,
     funlockfile(stream);
 }
 
-static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
+static const struct vlc_logger_operations html_ops =
+{
+    LogHtml,
+    NULL
+};
+
+static const struct vlc_logger_operations *Open(vlc_object_t *obj,
+                                                void **restrict sysp)
 {
     if (!var_InheritBool(obj, "file-logging"))
         return NULL;
@@ -125,7 +138,7 @@ static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
     const char *filename = TEXT_FILENAME;
     const char *header = TEXT_HEADER;
 
-    vlc_log_cb cb = LogText;
+    const struct vlc_logger_operations *ops = &text_ops;
     sys->footer = TEXT_FOOTER;
     sys->verbosity = verbosity;
 
@@ -136,7 +149,7 @@ static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
         {
             filename = HTML_FILENAME;
             header = HTML_HEADER;
-            cb = LogHtml;
+            ops = &html_ops;
             sys->footer = HTML_FOOTER;
         }
         else if (strcmp(mode, "text"))
@@ -177,7 +190,7 @@ static vlc_log_cb Open(vlc_object_t *obj, void **restrict sysp)
     fputs(header, sys->stream);
 
     *sysp = sys;
-    return cb;
+    return ops;
 }
 
 static void Close(void *opaque)
diff --git a/modules/logger/journal.c b/modules/logger/journal.c
index bafb7b7d6f..83111be107 100644
--- a/modules/logger/journal.c
+++ b/modules/logger/journal.c
@@ -64,13 +64,15 @@ static void Log(void *opaque, int type, const vlc_log_t *meta,
     (void) opaque;
 }
 
+static const struct vlc_logger_operations ops = { Log, NULL };
+
 static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
 {
     if (!var_InheritBool(obj, "syslog"))
         return NULL;
 
     (void) sysp;
-    return Log;
+    return &ops;
 }
 
 vlc_module_begin()
diff --git a/modules/logger/syslog.c b/modules/logger/syslog.c
index fcb408b5c1..0a71983c38 100644
--- a/modules/logger/syslog.c
+++ b/modules/logger/syslog.c
@@ -101,7 +101,10 @@ static int var_InheritFacility(vlc_object_t *obj, const char *varname)
 
 static const char default_ident[] = PACKAGE;
 
-static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
+static const struct vlc_logger_operations ops = { Log, NULL };
+
+static const struct vlc_logger_operations *Open(vlc_object_t *obj,
+                                                void **restrict sysp)
 {
     if (!var_InheritBool(obj, "syslog"))
         return NULL;
@@ -123,7 +126,7 @@ static vlc_log_cb Open(vlc_object_t *obj, void **sysp)
 
     setlogmask(mask);
 
-    return Log;
+    return &ops;
 }
 
 static void Close(void *opaque)
diff --git a/src/misc/messages.c b/src/misc/messages.c
index 8dda0641f5..581008816f 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -47,7 +47,7 @@ struct vlc_logger_t
 {
     struct vlc_common_members obj;
     vlc_rwlock_t lock;
-    vlc_log_cb log;
+    const struct vlc_logger_operations *ops;
     void *sys;
     module_t *module;
 };
@@ -62,7 +62,7 @@ static void vlc_vaLogCallback(libvlc_int_t *vlc, int type,
     assert(logger != NULL);
     canc = vlc_savecancel();
     vlc_rwlock_rdlock(&logger->lock);
-    logger->log(logger->sys, type, item, format, ap);
+    logger->ops->log(logger->sys, type, item, format, ap);
     vlc_rwlock_unlock(&logger->lock);
     vlc_restorecancel(canc);
 }
@@ -251,6 +251,11 @@ static void vlc_vaLogEarly(void *d, int type, const vlc_log_t *item,
     vlc_mutex_unlock(&sys->lock);
 }
 
+static const struct vlc_logger_operations early_ops = {
+    vlc_vaLogEarly,
+    NULL,
+};
+
 static int vlc_LogEarlyOpen(vlc_logger_t *logger)
 {
     vlc_logger_early_t *sys = malloc(sizeof (*sys));
@@ -262,7 +267,7 @@ static int vlc_LogEarlyOpen(vlc_logger_t *logger)
     sys->head = NULL;
     sys->tailp = &sys->head;
 
-    logger->log = vlc_vaLogEarly;
+    logger->ops = &early_ops;
     logger->sys = sys;
     return 0;
 }
@@ -292,15 +297,23 @@ static void vlc_vaLogDiscard(void *d, int type, const vlc_log_t *item,
     (void) d; (void) type; (void) item; (void) format; (void) ap;
 }
 
+static const struct vlc_logger_operations discard_ops =
+{
+    vlc_vaLogDiscard,
+    NULL,
+};
+
 static int vlc_logger_load(void *func, va_list ap)
 {
-    vlc_log_cb (*activate)(vlc_object_t *, void **) = func;
+    const struct vlc_logger_operations *(*activate)(vlc_object_t *,
+                                                    void **) = func;
     vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
-    vlc_log_cb *cb = va_arg(ap, vlc_log_cb *);
+    const struct vlc_logger_operations **ops = va_arg(ap,
+                                        const struct vlc_logger_operations **);
     void **sys = va_arg(ap, void **);
 
-    *cb = activate(VLC_OBJECT(logger), sys);
-    return (*cb != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
+    *ops = activate(VLC_OBJECT(logger), sys);
+    return (*ops != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
 }
 
 static void vlc_logger_unload(void *func, va_list ap)
@@ -333,7 +346,7 @@ int vlc_LogPreinit(libvlc_int_t *vlc)
 
     if (vlc_LogEarlyOpen(logger))
     {
-        logger->log = vlc_vaLogDiscard;
+        logger->ops = &discard_ops;
         return -1;
     }
 
@@ -357,20 +370,20 @@ int vlc_LogInit(libvlc_int_t *vlc)
     if (unlikely(logger == NULL))
         return -1;
 
-    vlc_log_cb cb;
+    const struct vlc_logger_operations *ops;
     void *sys, *early_sys = NULL;
 
     /* TODO: module configuration item */
     module_t *module = vlc_module_load(logger, "logger", NULL, false,
-                                       vlc_logger_load, logger, &cb, &sys);
+                                       vlc_logger_load, logger, &ops, &sys);
     if (module == NULL)
-        cb = vlc_vaLogDiscard;
+        ops = &discard_ops;
 
     vlc_rwlock_wrlock(&logger->lock);
-    if (logger->log == vlc_vaLogEarly)
+    if (logger->ops == &early_ops)
         early_sys = logger->sys;
 
-    logger->log = cb;
+    logger->ops = ops;
     logger->sys = sys;
     assert(logger->module == NULL); /* Only one call to vlc_LogInit()! */
     logger->module = module;
@@ -384,10 +397,11 @@ int vlc_LogInit(libvlc_int_t *vlc)
 
 /**
  * Sets the message logging callback.
- * \param cb message callback, or NULL to clear
+ * \param ops message callback, or NULL to clear
  * \param data data pointer for the message callback
  */
-void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
+void vlc_LogSet(libvlc_int_t *vlc, const struct vlc_logger_operations *ops,
+                void *opaque)
 {
     vlc_logger_t *logger = libvlc_priv(vlc)->logger;
 
@@ -397,14 +411,14 @@ void vlc_LogSet(libvlc_int_t *vlc, vlc_log_cb cb, void *opaque)
     module_t *module;
     void *sys;
 
-    if (cb == NULL)
-        cb = vlc_vaLogDiscard;
+    if (ops == NULL)
+        ops = &discard_ops;
 
     vlc_rwlock_wrlock(&logger->lock);
     sys = logger->sys;
     module = logger->module;
 
-    logger->log = cb;
+    logger->ops = ops;
     logger->sys = opaque;
     logger->module = NULL;
     vlc_rwlock_unlock(&logger->lock);
@@ -430,9 +444,9 @@ void vlc_LogDeinit(libvlc_int_t *vlc)
         vlc_module_unload(vlc, logger->module, vlc_logger_unload, logger->sys);
     else
     /* Flush early log messages (corner case: no call to vlc_LogInit()) */
-    if (logger->log == vlc_vaLogEarly)
+    if (logger->ops == &early_ops)
     {
-        logger->log = vlc_vaLogDiscard;
+        logger->ops = &discard_ops;
         vlc_LogEarlyClose(logger, logger->sys);
     }
 



More information about the vlc-commits mailing list