Skip to content
This repository was archived by the owner on Oct 18, 2024. It is now read-only.

Commit 14d9f79

Browse files
committed
fix: use custom logging configurators for configuring logback
1 parent c0c1f0d commit 14d9f79

File tree

8 files changed

+176
-39
lines changed

8 files changed

+176
-39
lines changed

app/src/main/java/com/itsaky/androidide/app/IDEApplication.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import androidx.work.NetworkType
3030
import androidx.work.Operation
3131
import androidx.work.PeriodicWorkRequestBuilder
3232
import androidx.work.WorkManager
33+
import ch.qos.logback.classic.Logger
3334
import com.blankj.utilcode.util.ThrowableUtils.getFullStackTrace
3435
import com.google.android.material.color.DynamicColors
3536
import com.itsaky.androidide.BuildConfig
@@ -43,14 +44,15 @@ import com.itsaky.androidide.events.EditorEventsIndex
4344
import com.itsaky.androidide.events.LspApiEventsIndex
4445
import com.itsaky.androidide.events.LspJavaEventsIndex
4546
import com.itsaky.androidide.events.ProjectsApiEventsIndex
47+
import com.itsaky.androidide.logging.LogcatAppender
48+
import com.itsaky.androidide.logging.encoder.IDELogFormatEncoder
4649
import com.itsaky.androidide.preferences.internal.DevOpsPreferences
4750
import com.itsaky.androidide.preferences.internal.GeneralPreferences
4851
import com.itsaky.androidide.preferences.internal.StatPreferences
4952
import com.itsaky.androidide.resources.localization.LocaleProvider
5053
import com.itsaky.androidide.stats.AndroidIDEStats
5154
import com.itsaky.androidide.stats.StatUploadWorker
5255
import com.itsaky.androidide.syntax.colorschemes.SchemeAndroidIDE
53-
import com.itsaky.androidide.tasks.executeAsync
5456
import com.itsaky.androidide.treesitter.TreeSitter
5557
import com.itsaky.androidide.ui.themes.IDETheme
5658
import com.itsaky.androidide.ui.themes.IThemeManager
@@ -59,6 +61,9 @@ import com.itsaky.androidide.utils.VMUtils
5961
import com.itsaky.androidide.utils.flashError
6062
import com.termux.app.TermuxApplication
6163
import io.github.rosemoe.sora.widget.schemes.EditorColorScheme
64+
import kotlinx.coroutines.DelicateCoroutinesApi
65+
import kotlinx.coroutines.GlobalScope
66+
import kotlinx.coroutines.launch
6267
import org.greenrobot.eventbus.EventBus
6368
import org.greenrobot.eventbus.Subscribe
6469
import org.greenrobot.eventbus.ThreadMode
@@ -80,17 +85,17 @@ class IDEApplication : TermuxApplication() {
8085
RecyclableObjectPool.DEBUG = BuildConfig.DEBUG
8186
}
8287

88+
@OptIn(DelicateCoroutinesApi::class)
8389
override fun onCreate() {
8490
instance = this
8591
uncaughtExceptionHandler = Thread.getDefaultUncaughtExceptionHandler()
86-
8792
Thread.setDefaultUncaughtExceptionHandler { thread, th -> handleCrash(thread, th) }
93+
8894
super.onCreate()
8995

9096
if (BuildConfig.DEBUG) {
9197
StrictMode.setVmPolicy(
9298
StrictMode.VmPolicy.Builder(StrictMode.getVmPolicy()).penaltyLog().detectAll().build())
93-
9499
if (DevOpsPreferences.dumpLogs) {
95100
startLogcatReader()
96101
}
@@ -110,7 +115,9 @@ class IDEApplication : TermuxApplication() {
110115

111116
EditorColorScheme.setDefault(SchemeAndroidIDE.newInstance(null))
112117

113-
executeAsync { IDEColorSchemeProvider.init() }
118+
GlobalScope.launch {
119+
IDEColorSchemeProvider.init()
120+
}
114121
}
115122

116123
private fun handleCrash(thread: Thread, th: Throwable) {
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.logging
19+
20+
import ch.qos.logback.classic.Logger
21+
import ch.qos.logback.classic.LoggerContext
22+
import ch.qos.logback.classic.spi.Configurator
23+
import ch.qos.logback.classic.spi.ConfiguratorRank
24+
import ch.qos.logback.core.spi.ContextAwareBase
25+
import com.google.auto.service.AutoService
26+
import com.itsaky.androidide.logging.encoder.IDELogFormatEncoder
27+
28+
/**
29+
* Default IDE logging configurator.
30+
*
31+
* @author Akash Yadav
32+
*/
33+
@ConfiguratorRank(ConfiguratorRank.CUSTOM_TOP_PRIORITY)
34+
@AutoService(Configurator::class)
35+
@Suppress("UNUSED")
36+
class IDELoggingConfigurator : ContextAwareBase(), Configurator {
37+
38+
override fun configure(context: LoggerContext): Configurator.ExecutionStatus {
39+
addInfo("Setting up logging configuration")
40+
41+
val appender = LogcatAppender()
42+
appender.encoder = IDELogFormatEncoder()
43+
appender.start()
44+
45+
val rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME)
46+
rootLogger.addAppender(appender)
47+
48+
return Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY
49+
}
50+
}

logger/src/main/java/com/itsaky/androidide/logging/LogcatAppender.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
1919
import ch.qos.logback.classic.spi.ILoggingEvent;
2020
import ch.qos.logback.core.UnsynchronizedAppenderBase;
21+
import com.itsaky.androidide.logging.encoder.IDELogFormatEncoder;
2122
import com.itsaky.androidide.logging.utils.LogUtils;
2223

2324
/**
@@ -38,7 +39,7 @@
3839
*/
3940
public class LogcatAppender extends UnsynchronizedAppenderBase<ILoggingEvent> {
4041

41-
private PatternLayoutEncoder encoder = null;
42+
private IDELogFormatEncoder encoder = null;
4243
private boolean checkLoggable = false;
4344

4445
// AndroidIDE Changed: Appender is enabled only when running on Android.
@@ -132,7 +133,7 @@ public void append(ILoggingEvent event) {
132133
*
133134
* @return the pattern-layout encoder
134135
*/
135-
public PatternLayoutEncoder getEncoder() {
136+
public IDELogFormatEncoder getEncoder() {
136137
return this.encoder;
137138
}
138139

@@ -141,7 +142,7 @@ public PatternLayoutEncoder getEncoder() {
141142
*
142143
* @param encoder the pattern-layout encoder
143144
*/
144-
public void setEncoder(PatternLayoutEncoder encoder) {
145+
public void setEncoder(IDELogFormatEncoder encoder) {
145146
this.encoder = encoder;
146147
}
147148

logger/src/main/resources/logback.xml

Lines changed: 0 additions & 32 deletions
This file was deleted.

subprojects/tooling-api-impl/build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import com.itsaky.androidide.build.config.BuildConfig
2121
plugins {
2222
id("com.github.johnrengelman.shadow") version "8.1.1"
2323
id("java-library")
24+
id("kotlin-kapt")
2425
id("org.jetbrains.kotlin.jvm")
2526
}
2627

@@ -57,12 +58,15 @@ project.tasks.getByName("shadowJar") {
5758
}
5859

5960
dependencies {
61+
kapt(libs.google.auto.service)
62+
6063
api(projects.subprojects.toolingApi)
6164

6265
implementation(projects.buildInfo)
6366
implementation(projects.shared)
6467

6568
implementation(libs.common.jkotlin)
69+
implementation(libs.google.auto.service.annotations)
6670
implementation(libs.xml.xercesImpl)
6771
implementation(libs.xml.apis)
6872
implementation(libs.tooling.gradleApi)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.tooling.impl.logging
19+
20+
import ch.qos.logback.classic.Logger
21+
import ch.qos.logback.classic.LoggerContext
22+
import ch.qos.logback.classic.spi.Configurator
23+
import ch.qos.logback.classic.spi.ConfiguratorRank
24+
import ch.qos.logback.core.spi.ContextAwareBase
25+
import com.google.auto.service.AutoService
26+
import com.itsaky.androidide.logging.JvmStdErrAppender
27+
import com.itsaky.androidide.logging.encoder.IDELogFormatEncoder
28+
29+
/**
30+
* Default logging configurator for the Tooling API Runtime.
31+
*
32+
* @author Akash Yadav
33+
*/
34+
@ConfiguratorRank(ConfiguratorRank.CUSTOM_TOP_PRIORITY)
35+
@AutoService(Configurator::class)
36+
@Suppress("UNUSED")
37+
class ToolingLoggingConfigurator : ContextAwareBase(), Configurator {
38+
39+
override fun configure(context: LoggerContext): Configurator.ExecutionStatus {
40+
addInfo("Setting up logging configuration for tooling API")
41+
42+
val stdErrAppender = JvmStdErrAppender()
43+
stdErrAppender.encoder = IDELogFormatEncoder()
44+
stdErrAppender.start()
45+
46+
val toolingApiAppender = ToolingApiAppender()
47+
toolingApiAppender.start()
48+
49+
val rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME)
50+
rootLogger.addAppender(stdErrAppender)
51+
rootLogger.addAppender(toolingApiAppender)
52+
53+
return Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY
54+
}
55+
}

testing/common/build.gradle.kts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,15 @@
1818
@Suppress("JavaPluginLanguageLevel")
1919
plugins {
2020
id("java-library")
21+
id("kotlin-kapt")
2122
id("org.jetbrains.kotlin.jvm")
2223
}
2324

2425
dependencies {
26+
implementation(project(":subprojects:tooling-api-impl"))
27+
kapt(libs.google.auto.service)
28+
implementation(libs.google.auto.service.annotations)
29+
2530
api(libs.tests.junit)
31+
api(projects.logger)
2632
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of AndroidIDE.
3+
*
4+
* AndroidIDE is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* AndroidIDE is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with AndroidIDE. If not, see <https://www.gnu.org/licenses/>.
16+
*/
17+
18+
package com.itsaky.androidide.testing.common.logging
19+
20+
import ch.qos.logback.classic.Logger
21+
import ch.qos.logback.classic.LoggerContext
22+
import ch.qos.logback.classic.spi.Configurator
23+
import ch.qos.logback.classic.spi.ConfiguratorRank
24+
import ch.qos.logback.core.spi.ContextAwareBase
25+
import com.google.auto.service.AutoService
26+
import com.itsaky.androidide.logging.JvmStdErrAppender
27+
import com.itsaky.androidide.logging.encoder.IDELogFormatEncoder
28+
29+
@ConfiguratorRank(ConfiguratorRank.CUSTOM_HIGH_PRIORITY)
30+
@AutoService(Configurator::class)
31+
@Suppress("UNUSED")
32+
class ToolingLoggingConfigurator : ContextAwareBase(), Configurator {
33+
34+
override fun configure(context: LoggerContext): Configurator.ExecutionStatus {
35+
addInfo("Setting up logging configuration for test runtime")
36+
37+
val stdErrAppender = JvmStdErrAppender()
38+
stdErrAppender.encoder = IDELogFormatEncoder()
39+
stdErrAppender.start()
40+
41+
val rootLogger = context.getLogger(Logger.ROOT_LOGGER_NAME)
42+
rootLogger.addAppender(stdErrAppender)
43+
44+
return Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY
45+
}
46+
}

0 commit comments

Comments
 (0)