Fix trace logging on Android
authorMarek Habersack <grendel@twistedcode.net>
Tue, 27 Sep 2016 18:31:19 +0000 (20:31 +0200)
committerMarek Habersack <grendel@twistedcode.net>
Tue, 27 Sep 2016 18:31:19 +0000 (20:31 +0200)
Introduction of the structured trace logger accidentally broke Android
logcat logging. The reason for this is that the default logging handler
defined in eglib (in goutput.c) for Android was never called, instead it
was superseeded by the log file logger defined in mono-logger.c

The net effect was that Mono tried to write the messages to some file on
Android leaving logcat devoid of any trace of life :)

This commit fixes the issue by introducing a set of structured logging
functions for Android.

As a side note - file logging on Android is not that useful. Writing to
a file on external storage medium requires app permissions and writing
to a location on internal storage medium, while allowed, might be
subject to space limitations and should not be used for possibly
voluminous data. It's therefore not very practical on Android.

mono/utils/Makefile.am
mono/utils/mono-log-android.c [new file with mode: 0644]
mono/utils/mono-logger-internals.h
mono/utils/mono-logger.c

index d67b9ff1fed2bb89a6a5697356203fd73b123b95..e82d22940e89e57e56365ce33e54d6f158815d2a 100644 (file)
@@ -30,6 +30,7 @@ monoutils_sources = \
        mono-log-windows.c      \
        mono-log-common.c       \
        mono-log-posix.c        \
+       mono-log-android.c \
        mono-internal-hash.c    \
        mono-internal-hash.h    \
        mono-io-portability.c   \
diff --git a/mono/utils/mono-log-android.c b/mono/utils/mono-log-android.c
new file mode 100644 (file)
index 0000000..fde2fd0
--- /dev/null
@@ -0,0 +1,92 @@
+/*
+ * mono-log-android.c: Android-specific interface to the logger
+ *
+ * This module contains the Android logcat logger interface
+ *
+ * Author:
+ *    Marek Habersack <grendel@twistedcode.net>
+ *
+ */
+#include <config.h>
+
+#if defined (PLATFORM_ANDROID)
+
+#include <android/log.h>
+#include "mono-logger-internals.h"
+
+/**
+ * mono_log_open_logcat
+ *
+ *     Open access to Android logcat (no-op)
+ *
+ *     @path - Not used
+ *     @userData - Not used
+ */   
+void
+mono_log_open_logcat (const char *path, void *userData)
+{
+       /* No-op on Android */
+}
+
+/**
+ * mono_log_write_logcat
+ *
+ *     Write data to Android logcat.
+ *
+ *     @domain - Identifier string
+ *     @level - Logging level flags
+ *     @format - Printf format string
+ *     @vargs - Variable argument list
+ */
+void
+mono_log_write_logcat (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message)
+{
+       android_LogPriority apriority;
+
+       switch (level & G_LOG_LEVEL_MASK)
+       {
+               case G_LOG_LEVEL_ERROR:
+                       apriority = ANDROID_LOG_FATAL;
+                       break;
+
+               case G_LOG_LEVEL_CRITICAL:
+                       apriority = ANDROID_LOG_ERROR;
+                       break;
+
+               case G_LOG_LEVEL_WARNING:
+                       apriority = ANDROID_LOG_WARN;
+                       break;
+
+               case G_LOG_LEVEL_MESSAGE:
+                       apriority = ANDROID_LOG_INFO;
+                       break;
+
+               case G_LOG_LEVEL_INFO:
+                       apriority = ANDROID_LOG_DEBUG;
+                       break;
+
+               case G_LOG_LEVEL_DEBUG:
+                       apriority = ANDROID_LOG_VERBOSE;
+                       break;
+
+               default:
+                       apriority = ANDROID_LOG_UNKNOWN;
+                       break;
+       }
+
+       __android_log_write (apriority, log_domain, message);
+       if (apriority == ANDROID_LOG_FATAL)
+               abort ();
+}
+
+/**
+ * mono_log_close_logcat
+ *
+ *     Close access to Android logcat (no-op)
+ */
+void
+mono_log_close_logcat ()
+{
+       /* No-op on Android */
+}
+#endif
index 57ef259677fd4b3ec81df0cadc1f70e6da914eb0..25392fcfc86e0d906bd8f1350250da0329917c05 100644 (file)
@@ -175,6 +175,12 @@ void mono_log_open_logfile (const char *, void *);
 void mono_log_write_logfile (const char *, GLogLevelFlags, mono_bool, const char *);
 void mono_log_close_logfile (void);
 
+#if PLATFORM_ANDROID
+void mono_log_open_logcat (const char *path, void *userData);
+void mono_log_write_logcat (const char *log_domain, GLogLevelFlags level, mono_bool hdr, const char *message);
+void mono_log_close_logcat (void);
+#endif
+
 G_END_DECLS
 
 #endif /* __MONO_LOGGER_INTERNAL_H__ */
index 20457c95b677c91eedb261eace27b409229db62d..8b03a1011c56031556150424d5947fbcfa1fc703 100644 (file)
@@ -148,19 +148,25 @@ mono_trace_set_logdest_string (const char *dest)
        if(level_stack == NULL)
                mono_trace_init();
 
+#if PLATFORM_ANDROID
+       logger.opener = mono_log_open_logcat;
+       logger.writer = mono_log_write_logcat;
+       logger.closer = mono_log_close_logcat;
+       logger.dest   = (char*) dest;
+#else
        if ((dest == NULL) || (strcmp("syslog", dest) != 0)) {
                logger.opener = mono_log_open_logfile;
                logger.writer = mono_log_write_logfile;
                logger.closer = mono_log_close_logfile;
                logger.dest   = (char *) dest;
-               mono_trace_set_log_handler_internal(&logger, NULL);
        } else {
                logger.opener = mono_log_open_syslog;
                logger.writer = mono_log_write_syslog;
                logger.closer = mono_log_close_syslog;
                logger.dest   = (char *) dest;
-               mono_trace_set_log_handler_internal(&logger, NULL);
        }
+#endif
+       mono_trace_set_log_handler_internal(&logger, NULL);
 }
 
 /**