[vlc-commits] messages: create one logger object per module

Rémi Denis-Courmont git at videolan.org
Wed Feb 20 16:36:09 CET 2019


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Feb 19 19:57:39 2019 +0200| [d7536c769e592d9605d993097a2586ad647660db] | committer: Rémi Denis-Courmont

messages: create one logger object per module

This creates a logger object only if/when a logger module is in use.
This avoids needing a logger object when there are no logger modules,
notably during early initialization. This also avoids having a logger
object carrying the name of a former logger module if logging is
changed during the lifetime of the VLC instance.

This requires wrapping, since the logger private data pointer is now
used to store the pointer to the VLC object. Note though that there
only ever is one or zero logger module in any given instance.

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

 src/misc/messages.c | 95 ++++++++++++++++++++++++++++++++++++-----------------
 1 file changed, 64 insertions(+), 31 deletions(-)

diff --git a/src/misc/messages.c b/src/misc/messages.c
index 8fbcfcf377..c53e898fa9 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -304,19 +304,6 @@ static const struct vlc_logger_operations discard_ops =
     NULL,
 };
 
-static int vlc_logger_load(void *func, va_list ap)
-{
-    const struct vlc_logger_operations *(*activate)(vlc_object_t *,
-                                                    void **) = func;
-    vlc_logger_t *logger = va_arg(ap, vlc_logger_t *);
-    const struct vlc_logger_operations **ops = va_arg(ap,
-                                        const struct vlc_logger_operations **);
-    void **sys = va_arg(ap, void **);
-
-    *ops = activate(VLC_OBJECT(logger), sys);
-    return (*ops != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
-}
-
 static void vlc_LogSwitch(libvlc_int_t *vlc,
                           const struct vlc_logger_operations *ops,
                           void *opaque)
@@ -342,6 +329,70 @@ static void vlc_LogSwitch(libvlc_int_t *vlc,
 }
 
 /**
+ * Module-based message log.
+ */
+struct vlc_logger_module {
+    struct vlc_common_members obj;
+    const struct vlc_logger_operations *ops;
+    void *sys;
+};
+
+static int vlc_logger_load(void *func, va_list ap)
+{
+    const struct vlc_logger_operations *(*activate)(vlc_object_t *,
+                                                    void **) = func;
+    struct vlc_logger_module *module = va_arg(ap, struct vlc_logger_module *);
+
+    module->ops = activate(VLC_OBJECT(module), &module->sys);
+    return (module->ops != NULL) ? VLC_SUCCESS : VLC_EGENERIC;
+}
+
+static void vlc_vaLogModule(void *d, int type, const vlc_log_t *item,
+                            const char *format, va_list ap)
+{
+    struct vlc_logger_module *module = d;
+
+    module->ops->log(module->sys, type, item, format, ap);
+}
+
+static void vlc_LogModuleClose(void *d)
+{
+    struct vlc_logger_module *module = d;
+
+    if (module->ops->destroy != NULL)
+        module->ops->destroy(module->sys);
+
+    vlc_object_release(VLC_OBJECT(module));
+}
+
+static const struct vlc_logger_operations module_ops = {
+    vlc_vaLogModule,
+    vlc_LogModuleClose,
+};
+
+/**
+ * Initializes the messages logging subsystem and drain the early messages to
+ * the configured log.
+ */
+void vlc_LogInit(libvlc_int_t *vlc)
+{
+    struct vlc_logger_module *module;
+    const struct vlc_logger_operations *ops = &discard_ops;
+
+    module = vlc_custom_create(vlc, sizeof (*module), "logger");
+    if (likely(module != NULL)) {
+        /* TODO: module configuration item */
+        if (vlc_module_load(VLC_OBJECT(module), "logger", NULL, false,
+                            vlc_logger_load, module) != NULL)
+            ops = &module_ops;
+        else
+            vlc_object_release(VLC_OBJECT(module));
+    }
+
+    vlc_LogSwitch(vlc, ops, module);
+}
+
+/**
  * Performs preinitialization of the messages logging subsystem.
  *
  * Early log messages will be stored in memory until the subsystem is fully
@@ -369,24 +420,6 @@ int vlc_LogPreinit(libvlc_int_t *vlc)
 }
 
 /**
- * Initializes the messages logging subsystem and drain the early messages to
- * the configured log.
- */
-void vlc_LogInit(libvlc_int_t *vlc)
-{
-    vlc_logger_t *logger = libvlc_priv(vlc)->logger;
-    const struct vlc_logger_operations *ops;
-    void *opaque;
-
-    /* TODO: module configuration item */
-    if (vlc_module_load(logger, "logger", NULL, false,
-                        vlc_logger_load, logger, &ops, &opaque) == NULL)
-        ops = NULL;
-
-    vlc_LogSwitch(vlc, ops, opaque);
-}
-
-/**
  * Sets the message logging callback.
  * \param ops message callback, or NULL to clear
  * \param data data pointer for the message callback



More information about the vlc-commits mailing list