[FFmpeg-devel] [PATCH v3 1/2] avutil/log: added av_log_format_line2 which returns buffer length

Andreas Weis der_ghulbus at ghulbus-inc.de
Wed Apr 27 08:15:17 CEST 2016


From: Andreas Weis <github at ghulbus-inc.de>

The new function behaves the same as av_log_format_line, but also forwards
the return value from the underlying snprintf call. This will allow
callers to accurately determine the size requirements for the line buffer.

Signed-off-by: Andreas Weis <github at ghulbus-inc.de>
---
 doc/APIchanges      |  4 ++++
 libavutil/log.c     | 11 ++++++++++-
 libavutil/log.h     | 17 +++++++++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 32 insertions(+), 2 deletions(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index de72807..032eb1c 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,10 @@ libavutil:     2015-08-28
 
 API changes, most recent first:
 
+2016-04-26 - xxxxxxx - lavu 55.23.101 - log.h
+  Add a new function av_log_format_line2() which returns number of bytes
+  written to the target buffer.
+
 2016-xx-xx - xxxxxxx - lavc 57.37.100 - avcodec.h
   Add a new audio/video encoding and decoding API with decoupled input
   and output -- avcodec_send_packet(), avcodec_receive_frame(),
diff --git a/libavutil/log.c b/libavutil/log.c
index 4583519..0efba7a 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -284,10 +284,19 @@ static void format_line(void *avcl, int level, const char *fmt, va_list vl,
 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
                         char *line, int line_size, int *print_prefix)
 {
+    av_log_format_line2(ptr, level, fmt, vl, line, line_size, print_prefix);
+}
+
+int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
+                        char *line, int line_size, int *print_prefix)
+{
     AVBPrint part[4];
+    int ret;
+
     format_line(ptr, level, fmt, vl, part, print_prefix, NULL);
-    snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
+    ret = snprintf(line, line_size, "%s%s%s%s", part[0].str, part[1].str, part[2].str, part[3].str);
     av_bprint_finalize(part+3, NULL);
+    return ret;
 }
 
 void av_log_default_callback(void* ptr, int level, const char* fmt, va_list vl)
diff --git a/libavutil/log.h b/libavutil/log.h
index 321748c..9b1d66f 100644
--- a/libavutil/log.h
+++ b/libavutil/log.h
@@ -317,6 +317,23 @@ AVClassCategory av_default_get_category(void *ptr);
 void av_log_format_line(void *ptr, int level, const char *fmt, va_list vl,
                         char *line, int line_size, int *print_prefix);
 
+/**
+ * Format a line of log the same way as the default callback.
+ * @param line          buffer to receive the formatted line;
+ *                      may be NULL if line_size is 0
+ * @param line_size     size of the buffer; at most line_size-1 characters will
+ *                      be written to the buffer, plus one null terminator
+ * @param print_prefix  used to store whether the prefix must be printed;
+ *                      must point to a persistent integer initially set to 1
+ * @return Returns a negative value if an error occured, otherwise returns
+ *         the number of characters that would have been written for a
+ *         sufficiently large buffer, not including the terminating null
+ *         character. If the return value is not less than line_size, it means
+ *         that the log message was truncated to fit the buffer.
+ */
+int av_log_format_line2(void *ptr, int level, const char *fmt, va_list vl,
+                        char *line, int line_size, int *print_prefix);
+
 #if FF_API_DLOG
 /**
  * av_dlog macros
diff --git a/libavutil/version.h b/libavutil/version.h
index 24bd0d0..d862fb8 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -64,7 +64,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  55
-#define LIBAVUTIL_VERSION_MINOR  22
+#define LIBAVUTIL_VERSION_MINOR  23
 #define LIBAVUTIL_VERSION_MICRO 101
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
2.5.0.windows.1



More information about the ffmpeg-devel mailing list