Skip to content

Commit

Permalink
fix: crash on android
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriankhl committed Sep 25, 2022
1 parent cbf5274 commit 3c0811e
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import org.apache.logging.log4j.Level
import org.apache.logging.log4j.core.config.Configurator
import relativitization.client.UniverseClient
import relativitization.game.GdxSettings
import relativitization.game.RelativitizationGame
import relativitization.server.UniverseServer
import relativitization.universe.UniverseClientSettings
import relativitization.universe.UniverseServerSettings
import relativitization.universe.utils.RelativitizationLogManager
import relativitization.utils.ServerPort
import kotlin.random.Random

@ExperimentalCoroutinesApi
class AndroidLauncher : AppCompatActivity(), AndroidFragmentApplication.Callbacks {
override fun onCreate(savedInstanceState: Bundle?) {
// This doesn't work
//val isLoggerRelease: Boolean =
// (applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0

// Reduce logging for release build
val isLoggerRelease: Boolean =
(applicationInfo.flags and ApplicationInfo.FLAG_DEBUGGABLE) != 0
val isLoggerRelease: Boolean = BuildConfig.VERSION_NAME.matches(Regex(".*Debug"))

// Set log level
// Set log level for default logger - the one used in android app
if (isLoggerRelease) {
Configurator.setRootLevel(Level.OFF)
RelativitizationLogManager.setSimpleLoggerLevel(Level.OFF)
} else {
Configurator.setRootLevel(Level.DEBUG)
//Configurator.setLevel("UniverseServerInternal", Level.ERROR)
//Configurator.setLevel("UniverseClient", Level.ERROR)
//Configurator.setLevel("ActorFunction", Level.ERROR)
RelativitizationLogManager.setSimpleLoggerLevel(Level.DEBUG)
}

super.onCreate(savedInstanceState)
Expand Down
2 changes: 1 addition & 1 deletion universe-server/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ kotlin {
implementation("io.ktor:ktor-server-status-pages:${Versions.ktorVersion}")
implementation("org.apache.logging.log4j:log4j-api:${Versions.log4jVersion}")
implementation("org.apache.logging.log4j:log4j-core:${Versions.log4jVersion}")
implementation("org.apache.logging.log4j:log4j-slf4j2-impl:${Versions.log4jVersion}")
//implementation("org.apache.logging.log4j:log4j-slf4j2-impl:${Versions.log4jVersion}")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,53 +1,114 @@
package relativitization.universe.utils

import org.apache.logging.log4j.Level
import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.apache.logging.log4j.util.StackLocatorUtil

object RelativitizationLogManager {
// Android does not support StackLocatorUtil.getCallerClass, use default logger instead
private val useDefaultLoggerName: Boolean =
private val useSimpleLogger: Boolean =
System.getProperty("java.specification.vendor") == "The Android Project"

private val commonLogger = RelativitizationLogger("CommonLogger")
private val simpleLogger = RelativitizationSimpleLogger(Level.OFF)

fun getLogger(): RelativitizationLogger = if (useDefaultLoggerName) {
RelativitizationLogger("DefaultLogger")
private val commonLogger: RelativitizationLogger = if (useSimpleLogger) {
simpleLogger
} else {
RelativitizationLogger()
RelativitizationLog4j2Logger("CommonLogger")
}

fun getLogger(): RelativitizationLogger = if (useSimpleLogger) {
simpleLogger
} else {
RelativitizationLog4j2Logger()
}

fun getCommonLogger(): RelativitizationLogger = commonLogger

fun getLogger(name: String): RelativitizationLogger = RelativitizationLogger(name)
fun setSimpleLoggerLevel(level: Level) {
simpleLogger.level = level
}

fun getLogger(name: String): RelativitizationLogger = if (useSimpleLogger) {
simpleLogger
} else {
RelativitizationLog4j2Logger(name)
}
}

abstract class RelativitizationLogger {
abstract fun error(message: String)

abstract fun warn(message: String)

abstract fun info(message: String)

abstract fun debug(message: String)

abstract fun trace(message: String)
}

class RelativitizationLogger(
class RelativitizationLog4j2Logger(
name: String = ""
) {
) : RelativitizationLogger() {
private val logger: Logger = if (name.isBlank()) {
LogManager.getLogger(StackLocatorUtil.getCallerClass(4))
} else {
LogManager.getLogger(name)
}

fun error(message: String) {
override fun error(message: String) {
logger.error(message)
}

fun warn(message: String) {
override fun warn(message: String) {
logger.warn(message)
}

fun info(message: String) {
override fun info(message: String) {
logger.info(message)
}

fun debug(message: String) {
override fun debug(message: String) {
logger.debug(message)
}

fun trace(message: String) {
override fun trace(message: String) {
logger.trace(message)
}
}

class RelativitizationSimpleLogger(
var level: Level,
) : RelativitizationLogger() {
override fun error(message: String) {
if (level >= Level.ERROR) {
println(message)
}
}

override fun warn(message: String) {
if (level >= Level.WARN) {
println(message)
}
}

override fun info(message: String) {
if (level >= Level.INFO) {
println(message)
}
}

override fun debug(message: String) {
if (level >= Level.DEBUG) {
println(message)
}
}

override fun trace(message: String) {
if (level >= Level.TRACE) {
println(message)
}
}
}

0 comments on commit 3c0811e

Please sign in to comment.