From 7a71c97491cf537790eeeec530ff024f0b0ea477 Mon Sep 17 00:00:00 2001 From: Vadim Lopatin Date: Wed, 20 Apr 2016 10:48:57 +0300 Subject: [PATCH] Android support for dlangui.core.logger - #119 --- .../android/android_native_app_glue_impl.d | 10 -- 3rdparty/android/log.d | 15 +++ examples/android/jni/main.d | 11 +- src/dlangui/core/logger.d | 112 ++++++++++++++---- 4 files changed, 113 insertions(+), 35 deletions(-) diff --git a/3rdparty/android/android_native_app_glue_impl.d b/3rdparty/android/android_native_app_glue_impl.d index 01d7169b..7b001099 100644 --- a/3rdparty/android/android_native_app_glue_impl.d +++ b/3rdparty/android/android_native_app_glue_impl.d @@ -35,16 +35,6 @@ import core.sys.posix.unistd; import android.android_native_app_glue; import android.log; -void LOGI(S...)(string fmt, S args) { - __android_log_print(android_LogPriority.ANDROID_LOG_INFO, "threaded_app", fmt.ptr, args); -} -void LOGE(S...)(string fmt, S args) { - __android_log_print(android_LogPriority.ANDROID_LOG_ERROR, "threaded_app", fmt.ptr, args); -} -void LOGV(S...)(string fmt, S args) { - debug __android_log_print(android_LogPriority.ANDROID_LOG_VERBOSE, "threaded_app", fmt.ptr, args); -} - static void free_saved_state(android_app* android_app) { pthread_mutex_lock(&android_app.mutex); if (android_app.savedState != null) { diff --git a/3rdparty/android/log.d b/3rdparty/android/log.d index aa92dadb..26dc5e92 100644 --- a/3rdparty/android/log.d +++ b/3rdparty/android/log.d @@ -27,3 +27,18 @@ int __android_log_write(int prio, const(char)* tag, const(char)* text); int __android_log_print(int prio, const(char)* tag, const(char)* fmt, ...); int __android_log_vprint(int prio, const(char)* tag, const(char)* fmt, va_list ap); void __android_log_assert(const(char)* cond, const(char)* tag, const(char)* fmt, ...); + +__gshared const(char) * ANDROID_LOG_TAG = "dlangui_app"; + +void LOGI(S...)(const(char) * fmt, S args) { + __android_log_print(android_LogPriority.ANDROID_LOG_INFO, ANDROID_LOG_TAG, fmt, args); +} +void LOGE(S...)(const(char) * fmt, S args) { + __android_log_print(android_LogPriority.ANDROID_LOG_ERROR, ANDROID_LOG_TAG, fmt, args); +} +void LOGW(S...)(const(char) * fmt, S args) { + __android_log_print(android_LogPriority.ANDROID_LOG_WARN, ANDROID_LOG_TAG, fmt, args); +} +void LOGV(S...)(const(char) * fmt, S args) { + debug __android_log_print(android_LogPriority.ANDROID_LOG_VERBOSE, ANDROID_LOG_TAG, fmt, args); +} diff --git a/examples/android/jni/main.d b/examples/android/jni/main.d index 37a72fb2..e27fb7f8 100644 --- a/examples/android/jni/main.d +++ b/examples/android/jni/main.d @@ -17,6 +17,7 @@ import core.stdc.stdlib : malloc; import core.stdc.string : memset; +import dlangui.core.logger; import EGL.eglplatform : EGLint; import EGL.egl, GLES.gl; @@ -25,8 +26,8 @@ import android.input, android.looper : ALooper_pollAll; import android.native_window : ANativeWindow_setBuffersGeometry; import android.sensor, android.log, android.android_native_app_glue; -int LOGI(const(char)* fmt, float x, float y, float z) { return __android_log_print(android_LogPriority.ANDROID_LOG_INFO, "native-activity", fmt, x, y, z); } -int LOGW(const(char)* warning) { return __android_log_print(android_LogPriority.ANDROID_LOG_WARN, "native-activity", warning); } +//int LOGI(const(char)* fmt, float x, float y, float z) { return __android_log_print(android_LogPriority.ANDROID_LOG_INFO, "native-activity", fmt, x, y, z); } +//int LOGW(const(char)* warning) { return __android_log_print(android_LogPriority.ANDROID_LOG_WARN, "native-activity", warning); } /** * Our saved state data. @@ -231,6 +232,12 @@ void main(){} * event loop for receiving input events and doing other things. */ extern (C) void android_main(android_app* state) { + LOGI("Inside android_main"); + Log.setLogTag("myApp"); + Log.setLogLevel(LogLevel.Trace); + Log.i("Testing logger - Log.i"); + Log.fi("Testing logger - Log.fi %d %s", 12345, "asdfgh"); + engine engine; // Make sure glue isn't stripped. diff --git a/src/dlangui/core/logger.d b/src/dlangui/core/logger.d index f377ff03..a28decce 100644 --- a/src/dlangui/core/logger.d +++ b/src/dlangui/core/logger.d @@ -27,6 +27,13 @@ Log.fd("mouse clicked at %d,%d", x, y); Log.e("exception while reading file", e); ---- + +For Android, set log tag instead of setXXXLogger: + +---- +Log.setLogTag("myApp"); +---- + Copyright: Vadim Lopatin, 2014 License: Boost License 1.0 Authors: Vadim Lopatin, coolreader.org@gmail.com @@ -37,6 +44,10 @@ import std.stdio; import std.datetime : SysTime, Clock; import core.sync.mutex; +version (Android) { + import android.log; +} + /// Log levels enum LogLevel : int { /// Fatal error, cannot resume @@ -141,108 +152,163 @@ class Log { default: return "?"; } } + version (Android) { + static android_LogPriority toAndroidLogPriority(LogLevel level) { + switch (level) with (LogLevel) { + /// Fatal error, cannot resume + case Fatal: + return android_LogPriority.ANDROID_LOG_FATAL; + /// Error + case Error: + return android_LogPriority.ANDROID_LOG_ERROR; + /// Warning + case Warn: + return android_LogPriority.ANDROID_LOG_WARN; + /// Informational message + case Info: + return android_LogPriority.ANDROID_LOG_INFO; + /// Debug message + case Debug: + return android_LogPriority.ANDROID_LOG_DEBUG; + /// Tracing message + case Trace: + default: + return android_LogPriority.ANDROID_LOG_VERBOSE; + } + } + } /// Log message with arbitrary log level static public void log(S...)(LogLevel level, S args) { - if (logLevel >= level && logFile !is null && logFile.isOpen) { - SysTime ts = Clock.currTime(); - logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); - logFile.writeln(args); - logFile.flush(); + if (logLevel >= level) { + version (Android) { + import std.format; + import std.string : toStringz; + import std.format; + import std.conv : to; + char[] msg; + foreach(arg; args) { + msg ~= to!string(arg); + } + msg ~= cast(char)0; + __android_log_write(toAndroidLogPriority(level), ANDROID_LOG_TAG, msg.ptr); + } else { + if (logFile !is null && logFile.isOpen) { + SysTime ts = Clock.currTime(); + logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); + logFile.writeln(args); + logFile.flush(); + } + } } } /// Log message with arbitrary log level with format string - static public void logf(S...)(LogLevel level, S args) { - if (logLevel >= level && logFile !is null && logFile.isOpen) { - SysTime ts = Clock.currTime(); - logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); - logFile.writefln(args); - logFile.flush(); + static public void logf(S...)(LogLevel level, string fmt, S args) { + if (logLevel >= level) { + version (Android) { + import std.string : toStringz; + import std.format; + string msg = fmt.format(args); + __android_log_write(toAndroidLogPriority(level), ANDROID_LOG_TAG, msg.toStringz); + } else { + if (logFile !is null && logFile.isOpen) { + SysTime ts = Clock.currTime(); + logFile.writef("%04d-%02d-%02d %02d:%02d:%02d.%03d %s ", ts.year, ts.month, ts.day, ts.hour, ts.minute, ts.second, ts.fracSecs.split!("msecs").msecs, logLevelName(level)); + logFile.writefln(fmt, args); + logFile.flush(); + } + } } } /// Log verbose / trace message static public void v(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Trace && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Trace) log(LogLevel.Trace, args); } } /// Log verbose / trace message with format string static public void fv(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Trace && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Trace) logf(LogLevel.Trace, args); } } /// Log debug message static public void d(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Debug && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Debug) log(LogLevel.Debug, args); } } /// Log debug message with format string static public void fd(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Debug && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Debug) logf(LogLevel.Debug, args); } } /// Log info message static public void i(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Info && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Info) log(LogLevel.Info, args); } } /// Log info message static public void fi(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Info && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Info) logf(LogLevel.Info, args); } } /// Log warn message static public void w(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Warn && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Warn) log(LogLevel.Warn, args); } } /// Log warn message static public void fw(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Warn && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Warn) logf(LogLevel.Warn, args); } } /// Log error message static public void e(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Error && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Error) log(LogLevel.Error, args); } } /// Log error message static public void fe(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Error && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Error) logf(LogLevel.Error, args); } } /// Log fatal error message static public void f(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Fatal && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Fatal) log(LogLevel.Fatal, args); } } /// Log fatal error message static public void ff(S...)(S args) { synchronized(mutex) { - if (logLevel >= LogLevel.Fatal && logFile !is null && logFile.isOpen) + if (logLevel >= LogLevel.Fatal) logf(LogLevel.Fatal, args); } } + + version (Android) { + static public void setLogTag(const char * tag) { + ANDROID_LOG_TAG = tag; + } + } } debug {