karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf git commit: [KARAF-5267] Redesigned log:tail to not start threads
Date Fri, 21 Jul 2017 10:02:38 GMT
Repository: karaf
Updated Branches:
  refs/heads/KARAF-3429 [created] f1ce8fa40


[KARAF-5267] Redesigned log:tail to not start threads


Project: http://git-wip-us.apache.org/repos/asf/karaf/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf/commit/f1ce8fa4
Tree: http://git-wip-us.apache.org/repos/asf/karaf/tree/f1ce8fa4
Diff: http://git-wip-us.apache.org/repos/asf/karaf/diff/f1ce8fa4

Branch: refs/heads/KARAF-3429
Commit: f1ce8fa4087d834e67527a4ea05ff68a2fdfc7da
Parents: f35f5b5
Author: Christian Schneider <chris@die-schneider.net>
Authored: Fri Jul 21 12:02:16 2017 +0200
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Fri Jul 21 12:02:16 2017 +0200

----------------------------------------------------------------------
 .../apache/karaf/log/command/DisplayLog.java    |  14 ++-
 .../org/apache/karaf/log/command/LogTail.java   | 117 ++++++-------------
 2 files changed, 42 insertions(+), 89 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf/blob/f1ce8fa4/log/src/main/java/org/apache/karaf/log/command/DisplayLog.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/command/DisplayLog.java b/log/src/main/java/org/apache/karaf/log/command/DisplayLog.java
index 0d73c1d..82aec16 100644
--- a/log/src/main/java/org/apache/karaf/log/command/DisplayLog.java
+++ b/log/src/main/java/org/apache/karaf/log/command/DisplayLog.java
@@ -66,18 +66,20 @@ public class DisplayLog implements Action {
 
     @Override
     public Object execute() throws Exception {
-
-        int minLevel = getMinLevel(level);
-
-
         final PrintStream out = System.out;
+        int minLevel = getMinLevel(level);
+        display(out, minLevel);
+        out.println();
+        return null;
+    }
 
+    protected void display(final PrintStream out, int minLevel) {
+        
         Iterable<PaxLoggingEvent> le = logService.getEvents(entries == 0 ? Integer.MAX_VALUE
: entries);
         for (PaxLoggingEvent event : le) {
             printEvent(out, event, minLevel);
         }
-        out.println();
-        return null;
+        out.flush();
     }
 
     protected static int getMinLevel(String levelSt) {

http://git-wip-us.apache.org/repos/asf/karaf/blob/f1ce8fa4/log/src/main/java/org/apache/karaf/log/command/LogTail.java
----------------------------------------------------------------------
diff --git a/log/src/main/java/org/apache/karaf/log/command/LogTail.java b/log/src/main/java/org/apache/karaf/log/command/LogTail.java
index dc48e00..8cd70f8 100644
--- a/log/src/main/java/org/apache/karaf/log/command/LogTail.java
+++ b/log/src/main/java/org/apache/karaf/log/command/LogTail.java
@@ -16,12 +16,7 @@
  */
 package org.apache.karaf.log.command;
 
-import java.io.IOException;
 import java.io.PrintStream;
-import java.util.concurrent.BlockingQueue;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.LinkedBlockingQueue;
 
 import org.apache.karaf.log.core.LogService;
 import org.apache.karaf.shell.api.action.Command;
@@ -29,7 +24,9 @@ import org.apache.karaf.shell.api.action.lifecycle.Reference;
 import org.apache.karaf.shell.api.action.lifecycle.Service;
 import org.apache.karaf.shell.api.console.Session;
 import org.ops4j.pax.logging.spi.PaxAppender;
-import org.ops4j.pax.logging.spi.PaxLoggingEvent;
+import org.osgi.framework.BundleContext;
+import org.osgi.framework.ServiceReference;
+import org.osgi.util.tracker.ServiceTracker;
 
 @Command(scope = "log", name = "tail", description = "Continuously display log entries. Use
ctrl-c to quit this command")
 @Service
@@ -39,92 +36,46 @@ public class LogTail extends DisplayLog {
     Session session;
 
     @Reference
-    LogService logService;
-
-    private ExecutorService executorService = Executors.newFixedThreadPool(2);
+    BundleContext context;
 
     @Override
     public Object execute() throws Exception {
-        PrintEventThread printThread = new PrintEventThread();
-        ReadKeyBoardThread readKeyboardThread = new ReadKeyBoardThread(Thread.currentThread());
-        executorService.execute(printThread);
-        executorService.execute(readKeyboardThread);
-        while (!Thread.currentThread().isInterrupted()) {
-            try {
-                Thread.sleep(200);
-            } catch (java.lang.InterruptedException e) {
-                break;
-            }
-        }
-        printThread.abort();
-        readKeyboardThread.abort();
-        executorService.shutdownNow();  
-        return null;      
-    }
-   
-    class ReadKeyBoardThread implements Runnable {
-        private Thread sessionThread;
-        boolean readKeyboard = true;
-        public ReadKeyBoardThread(Thread thread) {
-            this.sessionThread = thread;
-        }
-
-        public void abort() {
-            readKeyboard = false;            
-        }
-
-        public void run() {
-            while (readKeyboard) {
-                try {
-                    int c = session.getKeyboard().read();
-                    if (c < 0) {
-                        sessionThread.interrupt();
-                        break;
-                    }
-                } catch (IOException e) {
-                    break;
-                }
-                
+        int minLevel = getMinLevel(level);
+        PrintStream out = session.getConsole();
+        display(out, minLevel);
+        PaxAppender appender = event -> printEvent(out, event, minLevel);
+        ServiceTracker<LogService, LogService> tracker = new ServiceTracker<LogService,
LogService>(context, LogService.class, null) {
+            
+            @Override
+            public LogService addingService(ServiceReference<LogService> reference)
{
+                LogService service = super.addingService(reference);
+                service.addAppender(appender);
+                return service;
             }
-        }
-    } 
-    
-    class PrintEventThread implements Runnable {
-
-        PrintStream out = System.out;
-        boolean doDisplay = true;
 
-        public void run() {
-            int minLevel = getMinLevel(level);
-            Iterable<PaxLoggingEvent> le = logService.getEvents(entries == 0 ? Integer.MAX_VALUE
: entries);
-            for (PaxLoggingEvent event : le) {
-                printEvent(out, event, minLevel);
-            }
-            out.flush();
-            // Tail
-            final BlockingQueue<PaxLoggingEvent> queue = new LinkedBlockingQueue<>();
-            PaxAppender appender = queue::add;
-            try {
-                logService.addAppender(appender);
-                while (doDisplay) {
-                    printEvent(out, queue.take(), minLevel);
-                    if (queue.isEmpty()) {
-                        out.flush();
-                    }
+            @Override
+            public void removedService(ServiceReference<LogService> reference, LogService
service) {
+                service.removeAppender(appender);
+                synchronized (LogTail.this) {
+                    LogTail.this.notifyAll();
                 }
-            } catch (InterruptedException e) {
-                // Ignore
-            } finally {
-                logService.removeAppender(appender);
+            };
+        };
+        tracker.open();
+        
+        try {
+            synchronized (this) {
+                wait();
             }
-            out.println();
-            
-        }
-
-        public void abort() {
-            doDisplay = false;
+            out.println("Stopping tail as log.core bundle was stopped.");
+        } catch (InterruptedException e) {
+            // Ignore as it will happen if the user breaks the tail using Ctrl-C
+        } finally {
+            tracker.close();
         }
+        out.println();
 
+        return null;
     }
 
 }


Mime
View raw message