OSDN Git Service

Add callbacks for error and info messages.
authorEmmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Wed, 19 Oct 2016 12:37:36 +0000 (13:37 +0100)
committerSean V Kelley <seanvk@posteo.de>
Thu, 27 Oct 2016 18:41:25 +0000 (11:41 -0700)
This lets any application using libva choose the best way to report
info and error messages to the user, for example graphical application
can open a popup on errors and write info messages in the toolbar.

Signed-off-by: Emmanuel Gil Peyrot <linkmauve@linkmauve.fr>
Reviewed-by: Sean V Kelley <seanvk@posteo.de>
va/sysdeps.h
va/va.c
va/va.h

index 4de764d..164a274 100644 (file)
 
 /* Android logging utilities */
 # include <utils/Log.h>
-
-# ifdef ANDROID_ALOG
-#  define va_log_error(buffer)  do { ALOGE("%s", buffer); } while (0)
-#  define va_log_info(buffer)   do { ALOGI("%s", buffer); } while (0)
-# elif ANDROID_LOG
-#  define va_log_error(buffer)  do { LOGE("%s", buffer); } while (0)
-#  define va_log_info(buffer)   do { LOGI("%s", buffer); } while (0)
-# endif
-#endif
-
-#ifndef va_log_error
-#define va_log_error(buffer) do {                       \
-        fprintf(stderr, "libva error: %s", buffer);     \
-    } while (0)
-#endif
-
-#ifndef va_log_info
-#define va_log_info(buffer) do {                        \
-        fprintf(stderr, "libva info: %s", buffer);      \
-    } while (0)
 #endif
 
 #if defined __GNUC__ && defined HAVE_GNUC_VISIBILITY_ATTRIBUTE
diff --git a/va/va.c b/va/va.c
index b524fc7..5cf7220 100644 (file)
--- a/va/va.c
+++ b/va/va.c
@@ -106,12 +106,62 @@ int vaDisplayIsValid(VADisplay dpy)
     return pDisplayContext && (pDisplayContext->vadpy_magic == VA_DISPLAY_MAGIC) && pDisplayContext->vaIsValid(pDisplayContext);
 }
 
+static void default_log_error(const char *buffer)
+{
+# ifdef ANDROID_ALOG
+    ALOGE("%s", buffer);
+# elif ANDROID_LOG
+    LOGE("%s", buffer);
+# else
+    fprintf(stderr, "libva error: %s", buffer);
+# endif
+}
+
+static void default_log_info(const char *buffer)
+{
+# ifdef ANDROID_ALOG
+    ALOGI("%s", buffer);
+# elif ANDROID_LOG
+    LOGI("%s", buffer);
+# else
+    fprintf(stderr, "libva info: %s", buffer);
+# endif
+}
+
+static vaMessageCallback va_log_error = default_log_error;
+static vaMessageCallback va_log_info = default_log_info;
+
+/**
+ * Set the callback for error messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetErrorCallback(vaMessageCallback callback)
+{
+    vaMessageCallback old_callback = va_log_error;
+    va_log_error = callback;
+    return old_callback;
+}
+
+/**
+ * Set the callback for info messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetInfoCallback(vaMessageCallback callback)
+{
+    vaMessageCallback old_callback = va_log_info;
+    va_log_info = callback;
+    return old_callback;
+}
+
 void va_errorMessage(const char *msg, ...)
 {
     char buf[512], *dynbuf;
     va_list args;
     int n, len;
 
+    if (va_log_error == NULL)
+        return;
+
     va_start(args, msg);
     len = vsnprintf(buf, sizeof(buf), msg, args);
     va_end(args);
@@ -137,6 +187,9 @@ void va_infoMessage(const char *msg, ...)
     va_list args;
     int n, len;
 
+    if (va_log_info == NULL)
+        return;
+
     va_start(args, msg);
     len = vsnprintf(buf, sizeof(buf), msg, args);
     va_end(args);
diff --git a/va/va.h b/va/va.h
index 665aafb..88628a8 100644 (file)
--- a/va/va.h
+++ b/va/va.h
@@ -230,6 +230,21 @@ typedef struct _VARectangle
     unsigned short height;
 } VARectangle;
 
+/** Type of a message callback, used for both error and info log. */
+typedef void (*vaMessageCallback)(const char *message);
+
+/**
+ * Set the callback for error messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetErrorCallback(vaMessageCallback);
+
+/**
+ * Set the callback for info messages, or NULL for no logging.
+ * Returns the previous one, or NULL if it was disabled.
+ */
+vaMessageCallback vaSetInfoCallback(vaMessageCallback);
+
 /**
  * Initialization:
  * A display must be obtained by calling vaGetDisplay() before calling