[FFmpeg-devel] [PATCH v3 2/3] avutil/log: add av_log_set_opts function

Tobias Rapp t.rapp at noa-archive.com
Wed Mar 28 18:03:39 EEST 2018


Allows to set log level and flag values from string.

Signed-off-by: Tobias Rapp <t.rapp at noa-archive.com>
---
 doc/APIchanges      |  3 +++
 libavutil/log.c     | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/log.h     | 16 +++++++++++
 libavutil/version.h |  2 +-
 4 files changed, 96 insertions(+), 1 deletion(-)

diff --git a/doc/APIchanges b/doc/APIchanges
index 83c7a40..2d14452 100644
--- a/doc/APIchanges
+++ b/doc/APIchanges
@@ -15,6 +15,9 @@ libavutil:     2017-10-21
 
 API changes, most recent first:
 
+2018-03-xx - xxxxxxx - lavu 56.13.100 - log.h
+  Add av_log_set_opts().
+
 2018-03-xx - xxxxxxx - lavc 58.16.100 - avcodec.h
   Add FF_SUB_CHARENC_MODE_IGNORE.
 
diff --git a/libavutil/log.c b/libavutil/log.c
index 0a99d01..af32cd6 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -34,6 +34,7 @@
 #endif
 #include <stdarg.h>
 #include <stdlib.h>
+#include "avassert.h"
 #include "avutil.h"
 #include "bprint.h"
 #include "common.h"
@@ -402,6 +403,81 @@ void av_log_set_callback(void (*callback)(void*, int, const char*, va_list))
     av_log_callback = callback;
 }
 
+int av_log_set_opts(const char *arg)
+{
+    const struct { const char *name; int level; } log_levels[] = {
+        { "quiet"  , AV_LOG_QUIET   },
+        { "panic"  , AV_LOG_PANIC   },
+        { "fatal"  , AV_LOG_FATAL   },
+        { "error"  , AV_LOG_ERROR   },
+        { "warning", AV_LOG_WARNING },
+        { "info"   , AV_LOG_INFO    },
+        { "verbose", AV_LOG_VERBOSE },
+        { "debug"  , AV_LOG_DEBUG   },
+        { "trace"  , AV_LOG_TRACE   },
+    };
+    const char *token;
+    char *tail;
+    int flags = av_log_get_flags();
+    int level = av_log_get_level();
+    int cmd, i = 0;
+
+    av_assert0(arg);
+    while (*arg) {
+        token = arg;
+        if (*token == '+' || *token == '-') {
+            cmd = *token++;
+        } else {
+            cmd = 0;
+        }
+        if (!i && !cmd) {
+            flags = 0;  /* missing relative prefix, build absolute value */
+        }
+        if (!strncmp(token, "repeat", 6)) {
+            if (cmd == '-') {
+                flags |= AV_LOG_SKIP_REPEATED;
+            } else {
+                flags &= ~AV_LOG_SKIP_REPEATED;
+            }
+            arg = token + 6;
+        } else if (!strncmp(token, "level", 5)) {
+            if (cmd == '-') {
+                flags &= ~AV_LOG_PRINT_LEVEL;
+            } else {
+                flags |= AV_LOG_PRINT_LEVEL;
+            }
+            arg = token + 5;
+        } else {
+            break;
+        }
+        i++;
+    }
+    if (!*arg) {
+        goto end;
+    } else if (*arg == '+') {
+        arg++;
+    } else if (!i) {
+        flags = av_log_get_flags();  /* level value without prefix, reset flags */
+    }
+
+    for (i = 0; i < FF_ARRAY_ELEMS(log_levels); i++) {
+        if (!strcmp(arg, log_levels[i].name)) {
+            level = log_levels[i].level;
+            goto end;
+        }
+    }
+
+    level = strtol(arg, &tail, 10);
+    if (*tail) {
+        return -1;
+    }
+
+end:
+    av_log_set_flags(flags);
+    av_log_set_level(level);
+    return 0;
+}
+
 static void missing_feature_sample(int sample, void *avc, const char *msg,
                                    va_list argument_list)
 {
diff --git a/libavutil/log.h b/libavutil/log.h
index d9554e6..97010f7 100644
--- a/libavutil/log.h
+++ b/libavutil/log.h
@@ -356,6 +356,22 @@ void av_log_set_flags(int arg);
 int av_log_get_flags(void);
 
 /**
+ * Set log flags and level as an option string. Accepts "repeat" and "level"
+ * flags mapped to AV_LOG_SKIP_REPEATED (inverted) and AV_LOG_PRINT_LEVEL,
+ * followed by the log level specified either by name ("warning", "info",
+ * "verbose", etc.) or by number.
+ *
+ * When flags are prefixed with "+" or "-" the change is relative to the
+ * current flags value. When both flags and level are present a "+" separator
+ * is expected between last flag and before level.
+ *
+ * @param  arg  log option string
+ * @return Returns a negative value if parsing the option string failed,
+ *         otherwise returns 0.
+ */
+int av_log_set_opts(const char *arg);
+
+/**
  * @}
  */
 
diff --git a/libavutil/version.h b/libavutil/version.h
index d3dd2df..296c24b 100644
--- a/libavutil/version.h
+++ b/libavutil/version.h
@@ -79,7 +79,7 @@
  */
 
 #define LIBAVUTIL_VERSION_MAJOR  56
-#define LIBAVUTIL_VERSION_MINOR  12
+#define LIBAVUTIL_VERSION_MINOR  13
 #define LIBAVUTIL_VERSION_MICRO 100
 
 #define LIBAVUTIL_VERSION_INT   AV_VERSION_INT(LIBAVUTIL_VERSION_MAJOR, \
-- 
2.7.4




More information about the ffmpeg-devel mailing list