Module: Mesa
Branch: main
Commit: bbd19527c1f746f2d9babca03385ac4a8b545f59
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=bbd19527c1f746f2d9babca03385ac4a8b545f59

Author: Chia-I Wu <[email protected]>
Date:   Fri Feb 17 15:22:17 2023 -0800

util/log: improve logger_android

Avoid __android_log_vprint which can truncate messages.  Also add
MESA_LOG=wait to lower the chance of logger_android dropping messages.

Reviewed-by: Emma Anholt <[email protected]>
Reviewed-by: Jesse Natalie <[email protected]>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/21454>

---

 src/android_stub/log_stub.cpp |  5 +++++
 src/util/log.c                | 24 +++++++++++++++++++++++-
 2 files changed, 28 insertions(+), 1 deletion(-)

diff --git a/src/android_stub/log_stub.cpp b/src/android_stub/log_stub.cpp
index 29ba87b3748..45689f3b10e 100644
--- a/src/android_stub/log_stub.cpp
+++ b/src/android_stub/log_stub.cpp
@@ -2,6 +2,11 @@
 
 extern "C" {
 
+int __android_log_write(int prio, const char* tag, const char* text)
+{
+   return 0;
+}
+
 int __android_log_print(int prio, const char* tag, const char* fmt, ...)
 {
    return 0;
diff --git a/src/util/log.c b/src/util/log.c
index 5a5527defac..fa1c58cc970 100644
--- a/src/util/log.c
+++ b/src/util/log.c
@@ -40,6 +40,8 @@ enum mesa_log_control {
    MESA_LOG_CONTROL_FILE = 1 << 1,
    MESA_LOG_CONTROL_ANDROID = 1 << 2,
    MESA_LOG_CONTROL_LOGGER_MASK = 0xff,
+
+   MESA_LOG_CONTROL_WAIT = 1 << 8,
 };
 
 static const struct debug_control mesa_log_control_options[] = {
@@ -47,6 +49,8 @@ static const struct debug_control mesa_log_control_options[] 
= {
    { "null", MESA_LOG_CONTROL_NULL },
    { "file", MESA_LOG_CONTROL_FILE },
    { "android", MESA_LOG_CONTROL_ANDROID },
+   /* flags */
+   { "wait", MESA_LOG_CONTROL_WAIT },
    { NULL, 0 },
 };
 
@@ -203,7 +207,25 @@ logger_android(enum mesa_log_level level,
                const char *format,
                va_list va)
 {
-   __android_log_vprint(level_to_android(level), tag, format, va);
+   /* Android can truncate/drop messages
+    *
+    *  - the internal buffer for vsnprintf has a fixed size (usually 1024)
+    *  - the socket to logd is non-blocking
+    *
+    * and provides no way to detect.  Try our best.
+    */
+   char local_msg[1024];
+   char *msg = logger_vasnprintf(local_msg, sizeof(local_msg), 0, level, tag,
+         format, va);
+
+   __android_log_write(level_to_android(level), tag, msg);
+
+   if (msg != local_msg)
+      free(msg);
+
+   /* increase the chance of logd doing its part */
+   if (mesa_log_control & MESA_LOG_CONTROL_WAIT)
+      thrd_yield();
 }
 
 #endif /* DETECT_OS_ANDROID */

Reply via email to