1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

android: provide more predictable logging, add comptime termux support (#14984)

This commit is contained in:
Larpon 2022-07-07 17:28:29 +02:00 committed by GitHub
parent 71a85249ea
commit 9f3b6e3e3a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 49 additions and 44 deletions

30
thirdparty/android/android.h vendored Normal file
View File

@ -0,0 +1,30 @@
#if defined(__ANDROID__)
#include <android/log.h>
// 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

View File

@ -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 <android/log.h>
#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

View File

@ -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 {

View File

@ -0,0 +1,3 @@
module builtin
#include "@VEXEROOT/thirdparty/android/android.h"

View File

@ -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

View File

@ -355,9 +355,6 @@ pub fn user_os() string {
$if vinix {
return 'vinix'
}
if getenv('TERMUX_VERSION') != '' {
return 'termux'
}
return 'unknown'
}

View File

@ -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 ? {

View File

@ -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'

View File

@ -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