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 29, 2024
1 parent 2a01b2a commit 2ae685f
Show file tree
Hide file tree
Showing 7 changed files with 67 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.5")
}
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)) return
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 @@ -3,6 +3,7 @@ package com.ionic.plugin.core.actions
import com.ionic.plugin.core.PluginExceptionOld
import com.ionic.plugin.core.PluginExceptionBase
import com.ionic.plugin.core.PluginException
import com.ionic.plugin.core.logger.LogLevel
import com.ionic.plugin.core.logger.LogParam
import com.spryrocks.kson.*
import kotlin.js.JsExport
Expand All @@ -20,6 +21,14 @@ abstract class Mappers {

abstract val errorMapper: ErrorMapper
open val logMapper: LogMapper = LogMapper()

fun mapLogLevelsFromArg(levels: JsonArray): Array<LogLevel> {
val result = mutableListOf<LogLevel>()
for(i in 0 until levels.size) {
result.add(LogLevel.fromValue(levels.getString(i)))
}
return result.toTypedArray()
}
}

@JsExport
Expand Down
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,13 @@
package com.ionic.plugin.core.actions

import com.spryrocks.kson.IJsonObjectProperties

open class SetLogLevelsAction<TDelegate : Delegate<TMappers>, TMappers : Mappers>(args: IJsonObjectProperties): BaseAction<TDelegate, TMappers>() {
private val levels = args.getJsonArray("levels")

override fun onExecute() {
val levels = mappers.mapLogLevelsFromArg(levels)
this.callback.setLogLevels(levels)
success()
}
}
11 changes: 11 additions & 0 deletions core/src/commonMain/kotlin/com/ionic/plugin/core/logger/ILogger.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.ionic.plugin.core.logger

import com.ionic.plugin.core.PluginException

interface WithLogger {
fun logger(tag: String? = null): ILogger
}
Expand All @@ -12,6 +14,15 @@ enum class LogLevel(val value: String) {
Info("Info"),
Error("Error"),
Trace("Trace"),

;

companion object {
fun fromValue(value: String) =
LogLevel.entries
.find { it.value === value }
?: throw PluginException("Log level '$value' not exists")
}
}

interface ILogger {
Expand Down
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 2ae685f

Please sign in to comment.