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

Commit

Permalink
fix(tooling-api): logs are not properly appended in tooling API tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itsaky committed Apr 30, 2024
1 parent 0abaeca commit fc96f28
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@

package com.itsaky.androidide.logging.encoder;

import ch.qos.logback.classic.spi.ILoggingEvent;
import ch.qos.logback.core.encoder.LayoutWrappingEncoder;

/**
* Encoder to format the log events in AndroidIDE.
*
* @author Akash Yadav
*/
public class IDELogFormatEncoder extends LayoutWrappingEncoder<IDELogFormatLayout> {
public class IDELogFormatEncoder extends LayoutWrappingEncoder<ILoggingEvent> {

public IDELogFormatEncoder() {
super();
setLayout(new IDELogFormatLayout());
}
}
12 changes: 8 additions & 4 deletions logger/src/main/resources/logback.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,21 @@
-->

<configuration>
<appender name="LOGCAT" class="com.itsaky.androidide.logging.LogcatAppender">
<!-- From Logback docs: -->
<!-- Recommendation: place status listeners towards the the top of the configuration file -->
<statuslistener class="com.itsaky.androidide.tooling.impl.util.LogbackStatusListener" />

<appender class="com.itsaky.androidide.logging.LogcatAppender" name="LOGCAT">
<checkLoggable>false</checkLoggable>
<encoder class="com.itsaky.androidide.logging.encoder.IDELogFormatEncoder" />
</appender>
<appender name="JVM_STDERR" class="com.itsaky.androidide.logging.JvmStdErrAppender">
<appender class="com.itsaky.androidide.logging.JvmStdErrAppender" name="JVM_STDERR">
<encoder class="com.itsaky.androidide.logging.encoder.IDELogFormatEncoder" />
</appender>

<appender class="com.itsaky.androidide.tooling.impl.logging.ToolingApiAppender" name="TOOLING" />
<root level="DEBUG">
<appender-ref ref="LOGCAT" />
<appender-ref ref="JVM_STDERR" />
<appender-ref ref="TOOLING" />
</root>

</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import com.itsaky.androidide.tooling.api.IToolingApiClient;
import com.itsaky.androidide.tooling.api.util.ToolingApiLauncher;
import com.itsaky.androidide.tooling.impl.internal.ProjectImpl;
import com.itsaky.androidide.tooling.impl.logging.ToolingApiAppender;
import com.itsaky.androidide.tooling.impl.progress.ForwardingProgressListener;
import com.itsaky.androidide.tooling.impl.util.LogbackStatusListener;
import java.io.ByteArrayInputStream;
Expand All @@ -44,14 +43,10 @@ public class Main {
public static IToolingApiClient client;
public static Future<Void> future;

private static ToolingApiAppender toolingApiLogAppender;

public static void main(String[] args) {

// disable the JVM std.err appender
System.setProperty(JvmStdErrAppender.PROP_JVM_STDERR_APPENDER_ENABLED, "false");
System.setProperty(CoreConstants.STATUS_LISTENER_CLASS_KEY,
LogbackStatusListener.class.getName());

LOG.debug("Starting Tooling API server...");
final var project = new ProjectImpl();
Expand All @@ -62,9 +57,6 @@ public static void main(String[] args) {
Main.client = (IToolingApiClient) launcher.getRemoteProxy();
server.connect(client);

Main.toolingApiLogAppender = new ToolingApiAppender(client);
Main.toolingApiLogAppender.attachToRoot();

LOG.debug("Server started. Will run until shutdown message is received...");
LOG.debug("Running on Java version: {}", System.getProperty("java.version", "<unknown>"));

Expand All @@ -89,14 +81,11 @@ public static void main(String[] args) {
server.shutdown().get();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
LOG.error("An error occurred while shutting down tooling API server", e);
} finally {
Main.future = null;
Main.client = null;

Main.toolingApiLogAppender.detachFromRoot();
Main.toolingApiLogAppender = null;

LOG.info("Tooling API server shutdown complete");
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,24 @@

package com.itsaky.androidide.tooling.impl.logging

import ch.qos.logback.classic.Logger
import ch.qos.logback.classic.LoggerContext
import ch.qos.logback.classic.spi.ILoggingEvent
import ch.qos.logback.core.AppenderBase
import com.itsaky.androidide.tooling.api.IToolingApiClient
import com.itsaky.androidide.tooling.api.messages.LogMessageParams
import org.slf4j.LoggerFactory
import com.itsaky.androidide.tooling.impl.Main

/**
* [AppenderBase] implementation which forwards all logs to the tooling API client.
*
* @author Akash Yadav
*/
class ToolingApiAppender(
private val client: IToolingApiClient
) : AppenderBase<ILoggingEvent>() {

fun attachToRoot() {
val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger
setContext(LoggerFactory.getILoggerFactory() as LoggerContext)
start()

rootLogger.addAppender(this)
}

fun detachFromRoot() {
stop()

val rootLogger = LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME) as Logger
rootLogger.detachAppender(this)
}
class ToolingApiAppender : AppenderBase<ILoggingEvent>() {

override fun append(eventObject: ILoggingEvent?) {
if (eventObject == null || !isStarted) {
return
}

client.logMessage(
Main.client?.logMessage(
LogMessageParams(
eventObject.level.levelStr[0],
eventObject.loggerName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import org.junit.runners.model.Statement
/**
* A [TestRule] which only runs the tests in a CI environment.
*/
class CIOnlyTestRule : TestRule {
class CIOnlyTestRule(
var isEnabled: Boolean = true
) : TestRule {

override fun apply(base: Statement, description: Description?): Statement {
if (!isEnabled) {
return base
}

return object : Statement() {
override fun evaluate() {
if (isCi()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package com.itsaky.androidide.testing.tooling

import com.google.common.truth.Truth.assertThat
import ch.qos.logback.core.CoreConstants
import com.itsaky.androidide.testing.tooling.models.ToolingApiTestLauncherParams
import com.itsaky.androidide.testing.tooling.models.ToolingApiTestScope
import com.itsaky.androidide.tooling.api.IProject
Expand All @@ -33,6 +33,7 @@ import com.itsaky.androidide.tooling.api.messages.toLogLine
import com.itsaky.androidide.tooling.api.util.ToolingApiLauncher
import com.itsaky.androidide.tooling.api.util.ToolingProps
import com.itsaky.androidide.tooling.events.ProgressEvent
import com.itsaky.androidide.tooling.impl.util.LogbackStatusListener
import com.itsaky.androidide.utils.FileProvider
import com.itsaky.androidide.utils.ILogger
import org.slf4j.Logger
Expand Down Expand Up @@ -182,6 +183,9 @@ object ToolingApiTestLauncher {
cmd.add("-D$key=$value")
}

cmd.add(
"-D${CoreConstants.STATUS_LISTENER_CLASS_KEY}=${LogbackStatusListener::class.java.name}")

Collections.addAll(cmd, "-jar", jar)

println(
Expand Down

0 comments on commit fc96f28

Please sign in to comment.