diff mbox series

[FFmpeg-devel] avutil/log: Add av_log_once() for printing a message just once with a high log level

Message ID 20200202104956.11999-1-michael@niedermayer.cc
State Accepted
Headers show
Series [FFmpeg-devel] avutil/log: Add av_log_once() for printing a message just once with a high log level | expand

Checks

Context Check Description
andriy/ffmpeg-patchwork success Make fate finished

Commit Message

Michael Niedermayer Feb. 2, 2020, 10:49 a.m. UTC
Compared to ad-hoc if(printed) ... code this allows the user to disable
it by adjusting the log level

TODO: APIChanges & version bump

Signed-off-by: Michael Niedermayer <michael@niedermayer.cc>
---
 libavutil/log.c |  9 +++++++++
 libavutil/log.h | 21 +++++++++++++++++++++
 2 files changed, 30 insertions(+)

Comments

Michael Niedermayer Feb. 12, 2020, 9:23 a.m. UTC | #1
On Sun, Feb 02, 2020 at 11:49:56AM +0100, Michael Niedermayer wrote:
> Compared to ad-hoc if(printed) ... code this allows the user to disable
> it by adjusting the log level
> 

> TODO: APIChanges & version bump

will add and will apply


[...]
diff mbox series

Patch

diff --git a/libavutil/log.c b/libavutil/log.c
index 0a7b169bc0..57301d42d6 100644
--- a/libavutil/log.c
+++ b/libavutil/log.c
@@ -368,6 +368,15 @@  void av_log(void* avcl, int level, const char *fmt, ...)
     va_end(vl);
 }
 
+void av_log_once(void* avcl, int initial_level, int subsequent_level, int *state, const char *fmt, ...)
+{
+    va_list vl;
+    va_start(vl, fmt);
+    av_vlog(avcl, *state ? subsequent_level : initial_level, fmt, vl);
+    va_end(vl);
+    *state = 1;
+}
+
 void av_vlog(void* avcl, int level, const char *fmt, va_list vl)
 {
     AVClass* avc = avcl ? *(AVClass **) avcl : NULL;
diff --git a/libavutil/log.h b/libavutil/log.h
index d9554e609d..9c14188a9c 100644
--- a/libavutil/log.h
+++ b/libavutil/log.h
@@ -233,6 +233,27 @@  typedef struct AVClass {
  */
 void av_log(void *avcl, int level, const char *fmt, ...) av_printf_format(3, 4);
 
+/**
+ * Send the specified message to the log once with the initial_level and then with
+ * the subsequent_level. By default, all logging messages are sent to
+ * stderr. This behavior can be altered by setting a different logging callback
+ * function.
+ * @see av_log
+ *
+ * @param avcl A pointer to an arbitrary struct of which the first field is a
+ *        pointer to an AVClass struct or NULL if general log.
+ * @param initial_level importance level of the message expressed using a @ref
+ *        lavu_log_constants "Logging Constant" for the first occurance.
+ * @param subsequent_level importance level of the message expressed using a @ref
+ *        lavu_log_constants "Logging Constant" after the first occurance.
+ * @param fmt The format string (printf-compatible) that specifies how
+ *        subsequent arguments are converted to output.
+ * @param state a variable to keep trak of if a message has already been printed
+ *        this must be initialized to 0 before the first use. The same state
+ *        must not be accessed by 2 Threads simultaneously.
+ */
+void av_log_once(void* avcl, int initial_level, int subsequent_level, int *state, const char *fmt, ...) av_printf_format(5, 6);
+
 
 /**
  * Send the specified message to the log if the level is less than or equal