[vlc-commits] messages: add vlc_LogHeaderCreate()

Rémi Denis-Courmont git at videolan.org
Wed Feb 20 20:08:55 CET 2019


vlc | branch: master | Rémi Denis-Courmont <remi at remlab.net> | Tue Feb 19 22:42:29 2019 +0200| [396394473c70fd0f32ebbb238cec94184c3affae] | committer: Rémi Denis-Courmont

messages: add vlc_LogHeaderCreate()

This substitutes vlc_common_members.header.

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

 src/libvlc.h        | 11 +++++++++++
 src/misc/messages.c | 42 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 53 insertions(+)

diff --git a/src/libvlc.h b/src/libvlc.h
index 2f676e6188..173f3f71a2 100644
--- a/src/libvlc.h
+++ b/src/libvlc.h
@@ -81,6 +81,17 @@ int vlc_LogPreinit(libvlc_int_t *) VLC_USED;
 void vlc_LogInit(libvlc_int_t *);
 
 /**
+ * Creates a prefixed message log.
+ *
+ * This creates a message log that prefixes all its messages and forwards them
+ * in another log.
+ * \param parent message log to inject into
+ * \param str nul-terminated prefix (a.k.a. "header")
+ * \return a new message log on success or @c NULL on error
+ */
+vlc_logger_t *vlc_LogHeaderCreate(vlc_logger_t *parent, const char *str);
+
+/**
  * Destroys a message log.
  */
 void vlc_LogDestroy(vlc_logger_t *);
diff --git a/src/misc/messages.c b/src/misc/messages.c
index c64be5a04e..fbdbac4a5f 100644
--- a/src/misc/messages.c
+++ b/src/misc/messages.c
@@ -466,6 +466,48 @@ int vlc_LogPreinit(libvlc_int_t *vlc)
 }
 
 /**
+ * Message log with "header".
+ */
+struct vlc_logger_header {
+    struct vlc_logger logger;
+    struct vlc_logger *parent;
+    char header[];
+};
+
+static void vlc_vaLogHeader(void *d, int type, const vlc_log_t *item,
+                            const char *format, va_list ap)
+{
+    struct vlc_logger_header *header = d;
+    struct vlc_logger *logger = header->parent;
+    vlc_log_t hitem = *item;
+
+    hitem.psz_header = header->header;
+    logger->ops->log(logger->sys, type, &hitem, format, ap);
+}
+
+static const struct vlc_logger_operations header_ops = {
+    vlc_vaLogHeader,
+    NULL,
+};
+
+struct vlc_logger *vlc_LogHeaderCreate(struct vlc_logger *parent,
+                                       const char *str)
+{
+    size_t len = strlen(str) + 1;
+    struct vlc_logger_header *header = malloc(sizeof (*header) + len);
+    if (unlikely(header == NULL))
+        return NULL;
+
+    static_assert (offsetof (struct vlc_logger_header, logger) == 0,
+                   "Bad free");
+    header->logger.ops = &header_ops;
+    header->logger.sys = header;
+    header->parent = parent;
+    memcpy(header->header, str, len);
+    return &header->logger;
+}
+
+/**
  * 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