Skip to content

Commit

Permalink
Add log level testing
Browse files Browse the repository at this point in the history
  • Loading branch information
maksim.zhemerenko committed Feb 23, 2024
1 parent 2a01b2a commit c059b78
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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.1")
}
36 changes: 30 additions & 6 deletions core/src/commonMain/kotlin/com/ionic/plugin/core/Plugin.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ abstract class Plugin<TActionKey, TDelegate : Delegate<TMappers>, TMappers : Map
protected constructor() :
CoroutineScope,
WithLogger,
IEventSender<TDelegate, TMappers>
{
IEventSender<TDelegate, TMappers> {
private val _actionsLockObject = SynchronizedObject()

protected abstract val delegate: TDelegate
Expand Down Expand Up @@ -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<TDelegate, TMappers>, 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)
}
Expand All @@ -73,12 +80,18 @@ protected constructor() :
message: String,
params: Array<out LogParam>
) {
sendEvent(LogEvent<TDelegate, TMappers>(action, tag, level, message, params))
val event = LogEvent<TDelegate, TMappers>(action, tag, level, message, params)
if (!testLog(event))
sendEvent(event)
}

override fun sendEvent(event: EventBase<TDelegate, TMappers>) {
this@Plugin.sendEvent(event)
}

override fun setLogLevels(logLevels: Array<LogLevel>?) {
this@Plugin.setLogLevels(logLevels)
}
}

@Throws(PluginException::class)
Expand All @@ -100,4 +113,15 @@ protected constructor() :
event.initialize(callback, delegate)
wrapperDelegate.sendEvent(event.name, event.getData())
}

private var logLevels: Array<LogLevel>? = null

fun setLogLevels(logLevels: Array<LogLevel>?) {
this.logLevels = logLevels
}

private fun testLog(data: LogEvent<TDelegate, TMappers>): Boolean {
val logLevels = this.logLevels ?: return true
return logLevels.contains(data.level)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<TMappers>,
Expand All @@ -11,4 +12,5 @@ interface PluginCallbackInternal<
ILoggerRaw,
IEventSender<TDelegate, TMappers> {
fun finishActionSafely(action: TAction)
fun setLogLevels(logLevels: Array<LogLevel>?)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.ionic.plugin.core.actions

class SetLogLevelsAction<TDelegate : Delegate<TMappers>, TMappers : Mappers>: BaseAction<TDelegate, TMappers>() {
override fun onExecute() {
this.callback.setLogLevels(null)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import com.spryrocks.kson.mutableJsonObject
class LogEvent<TDelegate : Delegate<TMappers>, 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<out LogParam>,
) : EventBase<TDelegate, TMappers>() {
Expand Down

0 comments on commit c059b78

Please sign in to comment.