diff --git a/build.gradle.kts b/build.gradle.kts index f4822bd..b3cd2aa 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -25,5 +25,5 @@ extra.apply { set("kotlinxAtomicfuVersion", "0.23.2") set("androidxAppcompatVersion", "1.6.1") set("mavenGroup", "com.github.SpryRocks.ionic-plugin-core") - set("mavenVersion", "0.1.14-alpha.4") + set("mavenVersion", "0.1.15-alpha.3") } diff --git a/core/src/commonMain/kotlin/com/ionic/plugin/core/Plugin.kt b/core/src/commonMain/kotlin/com/ionic/plugin/core/Plugin.kt index 894bf12..941ff35 100644 --- a/core/src/commonMain/kotlin/com/ionic/plugin/core/Plugin.kt +++ b/core/src/commonMain/kotlin/com/ionic/plugin/core/Plugin.kt @@ -16,8 +16,7 @@ abstract class Plugin, TMappers : Map protected constructor() : CoroutineScope, WithLogger, - IEventSender -{ + IEventSender { private val _actionsLockObject = SynchronizedObject() protected abstract val delegate: TDelegate @@ -48,11 +47,19 @@ protected constructor() : this._wrapperDelegate = wrapperDelegate } - fun call(action: TActionKey, call: CallContext): Boolean { + fun call(action: TActionKey, call: CallContext) = wrapActionSafely(call) { print("plugin action: $action") + val baseAction = createAction(action, call) + setCurrentActionAndRunSafely(baseAction, call) + } + + fun callAction(baseAction: BaseAction, call: CallContext) = wrapActionSafely(call) { + setCurrentActionAndRunSafely(baseAction, call) + } + + private fun wrapActionSafely(call: CallContext, block: () -> Unit): Boolean { try { - val baseAction = createAction(action, call) - setCurrentActionAndRunSafely(baseAction, call) + block() } catch (error: Throwable) { mappers.reportError(error, call, true) } @@ -73,12 +80,18 @@ protected constructor() : message: String, params: Array ) { - sendEvent(LogEvent(action, tag, level, message, params)) + val event = LogEvent(action, tag, level, message, params) + if (!testLog(event)) return + sendEvent(event) } override fun sendEvent(event: EventBase) { this@Plugin.sendEvent(event) } + + override fun setLogLevels(logLevels: Array?) { + this@Plugin.setLogLevels(logLevels) + } } @Throws(PluginException::class) @@ -100,4 +113,15 @@ protected constructor() : event.initialize(callback, delegate) wrapperDelegate.sendEvent(event.name, event.getData()) } + + private var logLevels: Array? = null + + fun setLogLevels(logLevels: Array?) { + this.logLevels = logLevels + } + + private fun testLog(data: LogEvent): Boolean { + val logLevels = this.logLevels ?: return true + return logLevels.contains(data.level) + } } diff --git a/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/PluginCallbackInternal.kt b/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/PluginCallbackInternal.kt index 5e0ec2c..2f59b0c 100644 --- a/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/PluginCallbackInternal.kt +++ b/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/PluginCallbackInternal.kt @@ -2,6 +2,7 @@ package com.ionic.plugin.core.actions import com.ionic.plugin.core.events.IEventSender import com.ionic.plugin.core.logger.ILoggerRaw +import com.ionic.plugin.core.logger.LogLevel interface PluginCallbackInternal< TDelegate : Delegate, @@ -11,4 +12,5 @@ interface PluginCallbackInternal< ILoggerRaw, IEventSender { fun finishActionSafely(action: TAction) + fun setLogLevels(logLevels: Array?) } diff --git a/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/SetLogLevelsAction.kt b/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/SetLogLevelsAction.kt new file mode 100644 index 0000000..eb714c0 --- /dev/null +++ b/core/src/commonMain/kotlin/com/ionic/plugin/core/actions/SetLogLevelsAction.kt @@ -0,0 +1,7 @@ +package com.ionic.plugin.core.actions + +open class SetLogLevelsAction, TMappers : Mappers>: BaseAction() { + override fun onExecute() { + this.callback.setLogLevels(null) + } +} diff --git a/core/src/commonMain/kotlin/com/ionic/plugin/core/logger/LogEvent.kt b/core/src/commonMain/kotlin/com/ionic/plugin/core/logger/LogEvent.kt index 5b2ebd2..9ac0c89 100644 --- a/core/src/commonMain/kotlin/com/ionic/plugin/core/logger/LogEvent.kt +++ b/core/src/commonMain/kotlin/com/ionic/plugin/core/logger/LogEvent.kt @@ -8,7 +8,7 @@ import com.spryrocks.kson.mutableJsonObject class LogEvent, TMappers : Mappers>( private val action: String?, private val tag: String?, - private val level: LogLevel, + val level: LogLevel, private val message: String, private val params: Array, ) : EventBase() {