[FFmpeg-devel] [PATCH v3 1/4] avutil/log: Add callback for context prefix formatting

softworkz ffmpegagent at gmail.com
Thu Mar 6 22:59:08 EET 2025


From: softworkz <softworkz at hotmail.com>

also adds a log flag AV_LOG_PRINT_MEMADDRESSES, which is meant to
control prefix formatting. The actual formatting has to be performed
by the consuming application which needs to provide a formatting
callback via av_log_set_formatprefix_callback.

Signed-off-by: softworkz <softworkz at hotmail.com>
---
 doc/APIchanges      |  4 ++++
 libavutil/log.c     | 24 ++++++++++++++++++------
 libavutil/log.h     | 29 +++++++++++++++++++++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 52 insertions(+), 7 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 5a64836e25..92e4a2e055 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -1,5 +1,9 @@
 The last version increases of all libraries were on 2024-03-07
 
+2025-03-xx - xxxxxxxxxx - lavu 59.59.100 - log.h
+  Add flag AV_LOG_PRINT_MEMADDRESSES, av_log_set_formatprefix_callback,
+  av_log_formatprefix_default_callback
+
 API changes, most recent first:
 
 2025-03-01 - xxxxxxxxxx - lavu 59.58.100 - pixfmt.h
diff --git a/libavutil/log.c b/libavutil/log.c
index c5ee876a88..6697bdba70 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -315,6 +315,14 @@ static void format_date_now(AVBPrint* bp_time, int include_date)
     }
 }
 
+void av_log_formatprefix_default_callback(AVBPrint* buffer, AVClass** avcl, int log_flags)
+{
+    av_bprintf(buffer+0, "[%s @ %p] ", item_name(avcl, *avcl), avcl);
+}
+
+static void (*av_log_formatprefix_callback)(AVBPrint* part, AVClass** avcl, int log_flags) =
+    av_log_formatprefix_default_callback;
+
 static void format_line(void *avcl, int level, const char *fmt, va_list vl,
                         AVBPrint part[5], int *print_prefix, int type[2])
 {
@@ -327,17 +335,16 @@ static void format_line(void *avcl, int level, const char *fmt, va_list vl,
 
     if(type) type[0] = type[1] = AV_CLASS_CATEGORY_NA + 16;
     if (*print_prefix && avc) {
+
         if (avc->parent_log_context_offset) {
-            AVClass** parent = *(AVClass ***) (((uint8_t *) avcl) +
-                                   avc->parent_log_context_offset);
+            AVClass** parent = *(AVClass ***) ((uint8_t *)avcl + avc->parent_log_context_offset);
             if (parent && *parent) {
-                av_bprintf(part+0, "[%s @ %p] ",
-                           item_name(parent, *parent), parent);
+                av_log_formatprefix_callback(part, parent, flags);
                 if(type) type[0] = get_category(parent);
             }
         }
-        av_bprintf(part+1, "[%s @ %p] ",
-                   item_name(avcl, avc), avcl);
+        av_log_formatprefix_callback(part, avcl, flags);
+
         if(type) type[1] = get_category(avcl);
     }
 
@@ -485,6 +492,11 @@ int av_log_get_flags(void)
     return flags;
 }
 
+void av_log_set_formatprefix_callback(void (*callback)(AVBPrint* buffer, AVClass** avcl, int log_flags))
+{
+    av_log_formatprefix_callback = callback;
+}
+
 void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
 {
     av_log_callback = callback;
diff --git a/libavutil/log.h b/libavutil/log.h
index dd094307ce..a69888c1ad 100644
--- a/libavutil/log.h
+++ b/libavutil/log.h
@@ -24,6 +24,7 @@
 #include <stdarg.h>
 #include "attributes.h"
 #include "version.h"
+#include "bprint.h"
 
 typedef enum {
     AV_CLASS_CATEGORY_NA = 0,
@@ -323,6 +324,29 @@ int av_log_get_level(void);
  */
 void av_log_set_level(int level);
 
+/**
+ * Set the prefix formatting callback
+ *
+ * @note The callback must be thread safe, even if the application does not use
+ *       threads itself as some codecs are multithreaded.
+ *
+ * @see av_log_formatprefix_default_callback
+ *
+ * @param callback A formatting function with a compatible signature.
+ */
+void av_log_set_formatprefix_callback(void (*callback)(AVBPrint* buffer, AVClass** avcl, int log_flags));
+
+/**
+ * Default prefix formatting callback
+ *
+ * It prints the message to stderr, optionally colorizing it.
+ *
+ * @param buffer A pointer to the print buffer.
+ * @param avcl The AVClass reference for which to format the prefix.
+ * @param log_flags The enabled logging flags
+ */
+void av_log_formatprefix_default_callback(AVBPrint* buffer, AVClass** avcl, int log_flags);
+
 /**
  * Set the logging callback
  *
@@ -416,6 +440,11 @@ int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
  */
 #define AV_LOG_PRINT_DATETIME 8
 
+/**
+ * Print memory addresses instead of logical ids in the AVClass prefix.
+ */
+#define AV_LOG_PRINT_MEMADDRESSES 16
+
 void av_log_set_flags(int arg);
 int av_log_get_flags(void);
 
diff --git a/libavutil/version.h b/libavutil/version.h
index 4b584fd569..b6467e2a6d 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  59
-#define LIBAVUTIL_VERSION_MINOR  58
+#define LIBAVUTIL_VERSION_MINOR  59
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
ffmpeg-codebot



More information about the ffmpeg-devel mailing list