This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/main by this push:
     new 8868461cc2db CAMEL-26911: camel-jbang - camel log prints without 
colors when the console strips them (#29826)
8868461cc2db is described below

commit 8868461cc2dbb0eebfdafb90b4cc4bc25379ad7a
Author: Claus Ibsen <[email protected]>
AuthorDate: Fri Nov 13 22:03:04 2026 +0100

    CAMEL-26911: camel-jbang - camel log prints without colors when the console 
strips them (#29826)
    
    ## Summary
    
    An item of 
[CAMEL-26911](https://issues.apache.org/jira/browse/CAMEL-26911): **`camel log` 
was many times slower with colors (the default) when its output is a file or a 
pipe**, and gave the same output as without colors.
    
    With `--logging-color` (on by default), `camel log` prints each line, and 
the prefix of its integration, through jline's `AnsiConsole.out()`. When the 
output is not a terminal, that console takes the colors out of each piece of 
text it is given (`AnsiMode.Strip`), which is slow, and leaves the same text as 
`--logging-color=false`.
    
    100,000 lines of a log, `camel log <name> --follow=false --tail=-1 > file`, 
with the unpacked launcher:
    
    | | `--logging-color=true` (default) | `--logging-color=false` |
    |---|---|---|
    | before | 19.8s | 0.6s |
    | after | 1.1s | 0.6s |
    
    The output with colors is byte-identical before and after (no ANSI codes in 
the file, as before).
    
    ### Fix
    
    - `CamelCommand.colorsStripped()`: the command prints to the console and 
the console strips the colors (the output is not a terminal).
    - `camel log` then prints the lines as with `--logging-color=false`: the 
ANSI codes of the log are taken out of each line once, without the console in 
between.
    - On a terminal the colors are printed as before (checked through a 
pseudo-terminal: every line has its ANSI codes), and so is the output for a CLI 
that runs the command in its own JVM (the shell, the TUI).
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 .../camel/dsl/jbang/core/commands/CamelCommand.java  | 11 +++++++++++
 .../jbang/core/commands/action/CamelLogAction.java   |  5 +++++
 .../core/commands/action/CamelLogActionTest.java     | 20 ++++++++++++++++++++
 3 files changed, 36 insertions(+)

diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
index 174f542d7945..2deb0cd83c4e 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/CamelCommand.java
@@ -40,6 +40,7 @@ import 
org.apache.camel.dsl.jbang.core.common.TerminalWidthHelper;
 import org.apache.camel.dsl.jbang.run.common.Printer;
 import org.apache.camel.dsl.jbang.run.common.RuntimeUtil;
 import org.jline.jansi.AnsiConsole;
+import org.jline.jansi.AnsiMode;
 
 public abstract class CamelCommand implements Command<CommandInvocation>, 
CommandTree.ParsedCommandAware {
 
@@ -326,6 +327,16 @@ public abstract class CamelCommand implements 
Command<CommandInvocation>, Comman
         }
     }
 
+    /**
+     * Whether the console strips the ANSI colors of {@link 
#printColored(Object)}: the command prints to the console,
+     * and the output is not a terminal (a file or a pipe). A command that 
prints much text in colors, such as camel
+     * log, prints it without colors then, as letting the console strip the 
colors of each piece of text is slow
+     * (CAMEL-26911).
+     */
+    protected boolean colorsStripped() {
+        return printsToConsole() && AnsiConsole.out().getMode() == 
AnsiMode.Strip;
+    }
+
     private boolean printsToConsole() {
         return printer() instanceof Printer.SystemOutPrinter && 
!EnvironmentHelper.isEmbedded();
     }
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
index d1ece3fd2ac4..3759f152e1b1 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/main/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogAction.java
@@ -153,6 +153,11 @@ public class CamelLogAction extends ActionBaseCommand {
 
     @Override
     public Integer doCall() throws Exception {
+        if (loggingColor && colorsStripped()) {
+            // the console would take the colors out of each line, which is 
many times slower than to print the lines
+            // without colors, and gives the same output
+            loggingColor = false;
+        }
         // a previous run has ended, so there is nothing to follow
         follow = !previous && isFollowing(follow, 
durationOption.getDuration());
         if (follow && EnvironmentHelper.isEmbedded()) {
diff --git 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogActionTest.java
 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogActionTest.java
index 475d56e50d2b..00dfea4ffabd 100644
--- 
a/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogActionTest.java
+++ 
b/dsl/camel-jbang/camel-jbang-core/src/test/java/org/apache/camel/dsl/jbang/core/commands/action/CamelLogActionTest.java
@@ -216,6 +216,26 @@ class CamelLogActionTest extends ActionCommandTestSupport {
         assertThat(lines().get(1)).startsWith("orders").endsWith(": Failed");
     }
 
+    @Test
+    void coloredLinesArePrintedWithoutColorsWhenTheConsoleStripsThem() throws 
Exception {
+        writeStatusFile(TEST_PID, "orders");
+        writeLog(TEST_PID + ".log", line(-300, "\u001b[32mStarting\u001b[m"), 
line(-1, "Failed"));
+        // the output of the console is not a terminal (a file or a pipe), 
which takes the colors out of each line
+        CamelLogAction command = new CamelLogAction(new 
CamelJBangMain().withPrinter(printer)) {
+            @Override
+            protected boolean colorsStripped() {
+                return true;
+            }
+        };
+        CommandLineTestHelper.parse(command, "orders", "--follow=false", 
"--prefix=true");
+
+        assertThat(callWithSingleProcess(command)).isZero();
+
+        // printed as without --logging-color: the same text, without the 
console in between
+        assertThat(lines()).hasSize(2).noneMatch(l -> l.contains("\u001b"));
+        assertThat(lines().get(0)).startsWith("orders").endsWith(": Starting");
+    }
+
     @Test
     void previousPrintsTheLogOfTheRunThatEnded() throws Exception {
         keepPreviousRun("orders", line(-300, "Starting"), line(-1, "Failed to 
start route route1"));

Reply via email to