From 9f3b6e3e3ae71a3b353ac0cf8e9bb5db240ad5a0 Mon Sep 17 00:00:00 2001 From: Larpon Date: Thu, 7 Jul 2022 17:28:29 +0200 Subject: [PATCH] android: provide more predictable logging, add comptime termux support (#14984) --- thirdparty/android/android.h | 30 +++++++++++++++++++ thirdparty/sokol/sokol_v.pre.h | 21 ------------- vlib/builtin/builtin.c.v | 24 +++++++-------- .../builtin_android_outside_termux.c.v | 3 ++ vlib/builtin/cfns.c.v | 3 ++ vlib/os/os.v | 3 -- vlib/sokol/c/declaration.c.v | 1 - vlib/v/gen/c/comptime.v | 4 +-- vlib/v/pref/os.v | 4 +-- 9 files changed, 49 insertions(+), 44 deletions(-) create mode 100644 thirdparty/android/android.h delete mode 100644 thirdparty/sokol/sokol_v.pre.h create mode 100644 vlib/builtin/builtin_android_outside_termux.c.v diff --git a/thirdparty/android/android.h b/thirdparty/android/android.h new file mode 100644 index 000000000..72c7dec84 --- /dev/null +++ b/thirdparty/android/android.h @@ -0,0 +1,30 @@ +#if defined(__ANDROID__) + #include + + // Adapted from https://stackoverflow.com/a/196018/1904615 + #define V_ANDROID_LOG_STR_VALUE(arg) #arg + #define V_ANDROID_LOG_NAME(tag_name) V_ANDROID_LOG_STR_VALUE(tag_name) + + #ifndef V_ANDROID_LOG_TAG + #if defined(APPNAME) + #define V_ANDROID_LOG_TAG APPNAME + #else + #define V_ANDROID_LOG_TAG "V" + #endif + #endif + + #define V_ANDROID_LOG_TAG_NAME V_ANDROID_LOG_NAME(V_ANDROID_LOG_TAG) + + int android_print(FILE *stream, const char *format, ...) { + // int __android_log_vprint(int prio, const char *tag, const char *fmt, va_list ap) + int res; + va_list argptr; + va_start(argptr, format); + if (stream == stdout) { + res = __android_log_vprint(ANDROID_LOG_INFO, V_ANDROID_LOG_TAG_NAME, format, argptr); + } else { + res = __android_log_vprint(ANDROID_LOG_ERROR, V_ANDROID_LOG_TAG_NAME, format, argptr); + } + return res; + } +#endif diff --git a/thirdparty/sokol/sokol_v.pre.h b/thirdparty/sokol/sokol_v.pre.h deleted file mode 100644 index 703f6fb66..000000000 --- a/thirdparty/sokol/sokol_v.pre.h +++ /dev/null @@ -1,21 +0,0 @@ -#if defined(V_ANDROID_LOG_PRINT) - #if defined(__ANDROID__) - // Adapted from https://stackoverflow.com/a/196018/1904615 - #define V_ANDROID_LOG_STR_VALUE(arg) #arg - #define V_ANDROID_LOG_NAME(tag_name) V_ANDROID_LOG_STR_VALUE(tag_name) - - #ifndef V_ANDROID_LOG_TAG - #if defined(APPNAME) - #define V_ANDROID_LOG_TAG APPNAME - #else - #define V_ANDROID_LOG_TAG "V_ANDROID" - #endif - #endif - - #define V_ANDROID_LOG_TAG_NAME V_ANDROID_LOG_NAME(V_ANDROID_LOG_TAG) - - #include - #define printf(...) __android_log_print(ANDROID_LOG_INFO, V_ANDROID_LOG_TAG_NAME, __VA_ARGS__) - #define fprintf(a, ...) __android_log_print(ANDROID_LOG_ERROR, V_ANDROID_LOG_TAG_NAME, __VA_ARGS__) - #endif -#endif diff --git a/vlib/builtin/builtin.c.v b/vlib/builtin/builtin.c.v index 10f676446..c76914bf3 100644 --- a/vlib/builtin/builtin.c.v +++ b/vlib/builtin/builtin.c.v @@ -159,8 +159,8 @@ pub fn eprintln(s string) { C.fflush(C.stdout) C.fflush(C.stderr) // eprintln is used in panics, so it should not fail at all - $if android { - C.fprintf(C.stderr, c'%.*s\n', s.len, s.str) + $if android && !termux { + C.android_print(C.stderr, c'%.*s\n', s.len, s.str) } _writeln_to_fd(2, s) C.fflush(C.stderr) @@ -182,8 +182,8 @@ pub fn eprint(s string) { } $else { C.fflush(C.stdout) C.fflush(C.stderr) - $if android { - C.fprintf(C.stderr, c'%.*s', s.len, s.str) + $if android && !termux { + C.android_print(C.stderr, c'%.*s', s.len, s.str) } _write_buf_to_fd(2, s.str, s.len) C.fflush(C.stderr) @@ -211,11 +211,9 @@ pub fn flush_stderr() { // print prints a message to stdout. Unlike `println` stdout is not automatically flushed. [manualfree] pub fn print(s string) { - $if android { - C.fprintf(C.stdout, c'%.*s', s.len, s.str) // logcat - } - // no else if for android termux support - $if ios { + $if android && !termux { + C.android_print(C.stdout, c'%.*s\n', s.len, s.str) + } $else $if ios { // TODO: Implement a buffer as NSLog doesn't have a "print" C.WrappedNSLog(s.str) } $else $if freestanding { @@ -232,12 +230,10 @@ pub fn println(s string) { println('println(NIL)') return } - $if android { - C.fprintf(C.stdout, c'%.*s\n', s.len, s.str) // logcat + $if android && !termux { + C.android_print(C.stdout, c'%.*s\n', s.len, s.str) return - } - // no else if for android termux support - $if ios { + } $else $if ios { C.WrappedNSLog(s.str) return } $else $if freestanding { diff --git a/vlib/builtin/builtin_android_outside_termux.c.v b/vlib/builtin/builtin_android_outside_termux.c.v new file mode 100644 index 000000000..2ae846740 --- /dev/null +++ b/vlib/builtin/builtin_android_outside_termux.c.v @@ -0,0 +1,3 @@ +module builtin + +#include "@VEXEROOT/thirdparty/android/android.h" diff --git a/vlib/builtin/cfns.c.v b/vlib/builtin/cfns.c.v index fa3369a1a..50b37f08a 100644 --- a/vlib/builtin/cfns.c.v +++ b/vlib/builtin/cfns.c.v @@ -487,5 +487,8 @@ fn C.glTexImage2D() // used by ios for println fn C.WrappedNSLog(str &u8) +// used by Android for (e)println to output to the Android log system / logcat +pub fn C.android_print(voidptr, &char, ...voidptr) + // absolute value fn C.abs(number int) int diff --git a/vlib/os/os.v b/vlib/os/os.v index d2b935dac..83659f0f8 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -355,9 +355,6 @@ pub fn user_os() string { $if vinix { return 'vinix' } - if getenv('TERMUX_VERSION') != '' { - return 'termux' - } return 'unknown' } diff --git a/vlib/sokol/c/declaration.c.v b/vlib/sokol/c/declaration.c.v index b41829fb3..3f2f977ad 100644 --- a/vlib/sokol/c/declaration.c.v +++ b/vlib/sokol/c/declaration.c.v @@ -56,7 +56,6 @@ $if gcboehm ? { #define SOKOL_FREE GC_FREE } -#include "sokol_v.pre.h" // To allow for thirdparty initializing window / acceleration contexts // but still be able to use sokol.gfx e.g. SDL+sokol_gfx $if !no_sokol_app ? { diff --git a/vlib/v/gen/c/comptime.v b/vlib/v/gen/c/comptime.v index 3c449e9d9..2d3affdef 100644 --- a/vlib/v/gen/c/comptime.v +++ b/vlib/v/gen/c/comptime.v @@ -637,8 +637,8 @@ fn (mut g Gen) comptime_if_to_ifdef(name string, is_comptime_optional bool) ?str return '__ANDROID__' } 'termux' { - // Note: termux is running on Android natively - return '__ANDROID__' + // Note: termux is running on Android natively so __ANDROID__ will also be defined + return '__TERMUX__' } 'solaris' { return '__sun' diff --git a/vlib/v/pref/os.v b/vlib/v/pref/os.v index efc1934c2..81ff56418 100644 --- a/vlib/v/pref/os.v +++ b/vlib/v/pref/os.v @@ -3,8 +3,6 @@ // that can be found in the LICENSE file. module pref -import os - pub enum OS { _auto // Reserved so .macos cannot be misunderstood as auto ios @@ -95,7 +93,7 @@ pub fn (o OS) str() string { pub fn get_host_os() OS { $if linux { $if android { - if os.getenv('TERMUX_VERSION') != '' { + $if termux { return .termux } return .android -- 2.30.2