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 86da1cbb7ff CAMEL-18538: camel-jbang - Log command
86da1cbb7ff is described below

commit 86da1cbb7ff4a9853354de1a1a2d0fc8543acf2e
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri Jan 6 09:30:24 2023 +0100

    CAMEL-18538: camel-jbang - Log command
---
 .../jbang/core/commands/action/CamelLogAction.java | 63 ++++++++++++++++++----
 1 file changed, 54 insertions(+), 9 deletions(-)

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 2e398d68c92..6862633f47e 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
@@ -53,7 +53,8 @@ public class CamelLogAction extends ActionBaseCommand {
     @CommandLine.Option(names = { "--logging-color" }, defaultValue = "true", 
description = "Use colored logging")
     boolean loggingColor = true;
 
-    @CommandLine.Option(names = { "--follow" }, defaultValue = "true", 
description = "Keep following and outputting new log lines (use ctrl + c to 
exit).")
+    @CommandLine.Option(names = { "--follow" }, defaultValue = "true",
+                        description = "Keep following and outputting new log 
lines (use ctrl + c to exit).")
     boolean follow = true;
 
     @CommandLine.Option(names = { "--tail" },
@@ -68,6 +69,10 @@ public class CamelLogAction extends ActionBaseCommand {
                         description = "Find and highlight matching text 
(ignore case).", arity = "0..*")
     String[] find;
 
+    @CommandLine.Option(names = { "--grep" },
+                        description = "Filter logs to only output lines 
matching text (ignore case).", arity = "0..*")
+    String[] grep;
+
     String findAnsi;
 
     private int nameMaxWidth;
@@ -119,6 +124,14 @@ public class CamelLogAction extends ActionBaseCommand {
                     find[i] = f;
                 }
             }
+            if (grep != null) {
+                findAnsi = 
Ansi.ansi().fg(Ansi.Color.BLACK).bg(Ansi.Color.YELLOW).a("$0").reset().toString();
+                for (int i = 0; i < grep.length; i++) {
+                    String f = grep[i];
+                    f = Pattern.quote(f);
+                    grep[i] = f;
+                }
+            }
             Date limit = null;
             if (since != null) {
                 long millis;
@@ -164,12 +177,18 @@ public class CamelLogAction extends ActionBaseCommand {
                 try {
                     String line = row.reader.readLine();
                     if (line != null) {
-                        lines++;
-                        // switch fifo to be unlimited as we use it for new 
log lines
-                        if (row.fifo == null || row.fifo instanceof 
ArrayBlockingQueue) {
-                            row.fifo = new ArrayDeque<>();
+                        boolean valid = true;
+                        if (grep != null) {
+                            valid = isValidGrep(line);
+                        }
+                        if (valid) {
+                            lines++;
+                            // switch fifo to be unlimited as we use it for 
new log lines
+                            if (row.fifo == null || row.fifo instanceof 
ArrayBlockingQueue) {
+                                row.fifo = new ArrayDeque<>();
+                            }
+                            row.fifo.offer(line);
                         }
-                        row.fifo.offer(line);
                     }
                 } catch (IOException e) {
                     // ignore
@@ -236,11 +255,18 @@ public class CamelLogAction extends ActionBaseCommand {
                 System.out.print(": ");
             }
         }
-        if (find != null) {
+        if (find != null || grep != null) {
             String before = StringHelper.before(line, "---");
             String after = StringHelper.after(line, "---");
-            for (String f : find) {
-                after = after.replaceAll("(?i)" + f, findAnsi);
+            if (find != null) {
+                for (String f : find) {
+                    after = after.replaceAll("(?i)" + f, findAnsi);
+                }
+            }
+            if (grep != null) {
+                for (String g : grep) {
+                    after = after.replaceAll("(?i)" + g, findAnsi);
+                }
             }
             line = before + "---" + after;
         }
@@ -268,6 +294,9 @@ public class CamelLogAction extends ActionBaseCommand {
                     line = row.reader.readLine();
                     if (line != null) {
                         boolean valid = isValidSince(limit, line);
+                        if (valid && grep != null) {
+                            valid = isValidGrep(line);
+                        }
                         if (valid) {
                             while (!row.fifo.offer(line)) {
                                 row.fifo.poll();
@@ -297,6 +326,22 @@ public class CamelLogAction extends ActionBaseCommand {
         return false;
     }
 
+    private boolean isValidGrep(String line) {
+        if (grep == null) {
+            return true;
+        }
+        // the log can be in color or not so we need to unescape always
+        line = unescapeAnsi(line);
+        String after = StringHelper.after(line, "---");
+        for (String g : grep) {
+            boolean m = Pattern.compile("(?i)" + g).matcher(after).find();
+            if (m) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private String unescapeAnsi(String line) {
         // unescape ANSI colors
         StringBuilder sb = new StringBuilder();

Reply via email to