[logback-dev] svn commit: r1383 - in logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse: model reciever views

noreply.seb at qos.ch noreply.seb at qos.ch
Fri Mar 2 17:59:12 CET 2007


Author: seb
Date: Fri Mar  2 17:59:12 2007
New Revision: 1383

Modified:
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/model/EventFilter.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java
   logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventLabelProvider.java

Log:
The plugin now saves some informations:
- Evaluators
- Server port
- PatternLayout's pattern

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/model/EventFilter.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/model/EventFilter.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/model/EventFilter.java	Fri Mar  2 17:59:12 2007
@@ -4,6 +4,8 @@
 import java.util.Iterator;
 import java.util.List;
 
+import org.eclipse.ui.IMemento;
+
 import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
 import ch.qos.logback.classic.spi.LoggingEvent;
 import ch.qos.logback.core.Context;
@@ -13,79 +15,90 @@
 
 public class EventFilter {
 
+  private static final String TAG_FILTER = "filter";
+  private static final String TAG_EXPRESSION = "expression";
+  private static final String TAG_ONMATCH = "onMatch";
+  private static final String TAG_ONMISMATCH = "onMismatch";
+
   private static ContextBase context = new ContextBase();
-  
+
   private static List<EvaluatorFilter> filterList = new ArrayList<EvaluatorFilter>();
-  
-  static {
-    {
-      EvaluatorFilter filter = new EvaluatorFilter();
-      filter.setContext(context);
-      JaninoEventEvaluator eval = new JaninoEventEvaluator();
-      eval.setExpression("level == DEBUG");
-      eval.setContext(context);
-      eval.setName("eval1");
-      filter.setEvaluator(eval);
-      filter.setName("filter1");
-      filter.setOnMatch("ACCEPT");
-      eval.start();
-      filter.start();
-      filterList.add(filter);
-    }
-    {
-      EvaluatorFilter filter = new EvaluatorFilter();
-      filter.setContext(context);
-      JaninoEventEvaluator eval = new JaninoEventEvaluator();
-      eval.setExpression("level == INFO");
-      eval.setContext(context);
-      eval.setName("eval1");
-      filter.setEvaluator(eval);
-      filter.setName("filter1");
-      filter.setOnMatch("ACCEPT");
-      eval.start();
-      filter.start();
-      filterList.add(filter);
-    }
-  }
-  
+
   public static boolean filter(LoggingEvent event) {
     if (filterList.isEmpty()) {
       return true;
     }
-    
+
     Iterator<EvaluatorFilter> it = filterList.iterator();
     EvaluatorFilter filter;
     while (it.hasNext()) {
       filter = it.next();
       FilterReply reply = filter.decide(event);
-      if(reply == FilterReply.DENY) {
+      if (reply == FilterReply.DENY) {
         return false;
       } else if (reply == FilterReply.ACCEPT) {
         return true;
       }
     }
-    
+
     return true;
   }
-  
+
   public static Context getContext() {
     return context;
   }
-  
+
   public static List<EvaluatorFilter> getAllFilters() {
     return filterList;
   }
-  
+
   public static void add(EvaluatorFilter filter) {
     if (!filterList.contains(filter)) {
       filterList.add(filter);
     }
   }
-  
+
   public static void remove(EvaluatorFilter filter) {
     if (filterList.contains(filter)) {
       filterList.remove(filter);
-    }    
+    }
+  }
+
+  public static void saveState(IMemento memento) {
+    Iterator<EvaluatorFilter> it = filterList.iterator();
+    EvaluatorFilter filter;
+    JaninoEventEvaluator eval;
+    while (it.hasNext()) {
+      filter = it.next();
+      eval = (JaninoEventEvaluator) filter.getEvaluator();
+      IMemento mem = memento.createChild(TAG_FILTER);
+      mem.putString(TAG_EXPRESSION, eval.getExpression());
+      mem.putString(TAG_ONMATCH, filter.getOnMatch().toString());
+      mem.putString(TAG_ONMISMATCH, filter.getOnMismatch().toString());
+    }
   }
   
+  public static void init(IMemento memento) {
+    filterList = new ArrayList<EvaluatorFilter>();
+    EvaluatorFilter filter;
+    JaninoEventEvaluator eval;
+    IMemento[] mems = memento.getChildren(TAG_FILTER);
+    for (int i = 0; i < mems.length; i++) {
+      IMemento mem = mems[i];
+      filter = new EvaluatorFilter();
+      filter.setContext(context);
+      filter.setName("filter " + i);
+      filter.setOnMatch(mem.getString(TAG_ONMATCH));
+      filter.setOnMismatch(mem.getString(TAG_ONMISMATCH));
+      eval = new JaninoEventEvaluator();
+      eval.setContext(context);
+      eval.setName("Eval " + i);
+      eval.setExpression(mem.getString(TAG_EXPRESSION));
+      eval.start();
+      filter.setEvaluator(eval);
+      filter.start();
+      filterList.add(filter);
+    }
+  }
+
 }

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/reciever/SimpleSocketServer.java	Fri Mar  2 17:59:12 2007
@@ -4,6 +4,8 @@
 import java.net.ServerSocket;
 import java.net.Socket;
 
+import org.eclipse.ui.IMemento;
+
 import ch.qos.logback.eclipse.PluginLogger;
 
 /**
@@ -15,6 +17,9 @@
  */
 public class SimpleSocketServer implements Runnable {
 
+  private static final String TAG_SERVER = "lbserver";
+  private static final String TAG_PORT = "port";
+  
   private int port = 4321;
   private ServerSocket serverSocket;
   
@@ -50,4 +55,24 @@
       PluginLogger.logError(e);
     }
   }
+  
+  public void saveState(IMemento memento) {
+    IMemento mem = memento.createChild(TAG_SERVER);
+    mem.putInteger(TAG_PORT, port);
+  }
+  
+  public void init(IMemento memento) {
+    IMemento mem = memento.getChild(TAG_SERVER);
+    if (mem == null)  {
+      return;
+    }
+    
+    int newPort = mem.getInteger(TAG_PORT);
+    
+    if (newPort != 0) {
+      this.port = newPort;
+    }
+    
+  }
+  
 }

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LogbackView.java	Fri Mar  2 17:59:12 2007
@@ -12,7 +12,10 @@
 import org.eclipse.swt.widgets.Composite;
 import org.eclipse.swt.widgets.Display;
 import org.eclipse.ui.IActionBars;
+import org.eclipse.ui.IMemento;
 import org.eclipse.ui.ISharedImages;
+import org.eclipse.ui.IViewSite;
+import org.eclipse.ui.PartInitException;
 import org.eclipse.ui.PlatformUI;
 import org.eclipse.ui.console.MessageConsole;
 import org.eclipse.ui.console.TextConsoleViewer;
@@ -21,6 +24,7 @@
 import ch.qos.logback.eclipse.PluginLogger;
 import ch.qos.logback.eclipse.actions.LogbackFilterAction;
 import ch.qos.logback.eclipse.actions.LogbackPreferencesAction;
+import ch.qos.logback.eclipse.model.EventFilter;
 import ch.qos.logback.eclipse.model.LoggingEventManager;
 import ch.qos.logback.eclipse.reciever.SimpleSocketServer;
 
@@ -49,8 +53,10 @@
   private LogbackPreferencesAction preferencesAction;
   private LogbackFilterAction filterAction;
 
+  private IMemento memento;
+  
   public LogbackView() {
-    launchSocketServer(null);
+    //launchSocketServer(null);
   }
 
   public void createPartControl(Composite parent) {
@@ -73,9 +79,16 @@
 //      new JavaLinkDetector()}, SWT.NONE);
 
     provider = new LoggingEventContentProvider(myConsole, viewer);
+    
     labelProvider = new LoggingEventLabelProvider();
-    provider.setLabelProvider(labelProvider);
+    if (memento != null) {
+      labelProvider.init(memento);
+      EventFilter.init(memento);
+    }
     
+    provider.setLabelProvider(labelProvider);
+   
+   
     LoggingEventManager.getManager().addLoggingEventManagerListener(provider);
 
     makeActions();
@@ -180,4 +193,26 @@
     PluginLogger.logInfo("Now listening on port: " + serverPort);
   }
 
+  private void initServer() {
+    server = new SimpleSocketServer();
+    if (memento != null) {
+      server.init(memento);
+    }
+    serverThread = new Thread(server);
+    serverThread.start();
+  }
+  
+  public void saveState(IMemento memento) {
+    super.saveState(memento);
+    EventFilter.saveState(memento);
+    labelProvider.saveState(memento);
+    server.saveState(memento);
+  }
+  
+  public void init(IViewSite site, IMemento memento) throws PartInitException {
+    super.init(site, memento);
+    this.memento = memento;
+    initServer();
+  }
+  
 }
\ No newline at end of file

Modified: logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventLabelProvider.java
==============================================================================
--- logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventLabelProvider.java	(original)
+++ logbackPDE/trunk/plugins/ConsolePlugin/src/main/java/ch/qos/logback/eclipse/views/LoggingEventLabelProvider.java	Fri Mar  2 17:59:12 2007
@@ -2,6 +2,7 @@
 
 import org.eclipse.jface.viewers.LabelProvider;
 import org.eclipse.swt.graphics.Image;
+import org.eclipse.ui.IMemento;
 
 import ch.qos.logback.classic.LoggerContext;
 import ch.qos.logback.classic.PatternLayout;
@@ -15,6 +16,9 @@
  */
 class LoggingEventLabelProvider extends LabelProvider {
 
+  private static final String TAG_LABEL = "labelProvider";
+  private static final String TAG_PATTERN = "pattern";
+  
   private static String DEFAULT_PATTERN = "%relative %level [%thread] %logger{25} %message";
   private LoggerContext context;
   private PatternLayout patternLayout;
@@ -22,10 +26,6 @@
   public LoggingEventLabelProvider() {
     context = new LoggerContext();
     context.setName("Logback Plugin Context");
-    patternLayout = new PatternLayout();
-    patternLayout.setContext(context);
-    patternLayout.setPattern(DEFAULT_PATTERN);
-    patternLayout.start();
   }
 
   public String getText(Object element) {
@@ -55,4 +55,35 @@
     patternLayout.setPattern(pattern);
     patternLayout.start();
   }
+  
+  public void saveState(IMemento memento) {
+    IMemento mem = memento.createChild(TAG_LABEL);
+    mem.putString(TAG_PATTERN, patternLayout.getPattern());
+  }
+  
+  public void init(IMemento memento) {
+    IMemento mem = memento.getChild(TAG_LABEL);
+    if (mem == null) {
+      createDefaultPatternLayout();
+      return;
+    }
+    
+    String pattern = mem.getString(TAG_PATTERN);
+    if (pattern == null || pattern.length() == 0) {
+      createDefaultPatternLayout();
+      return;
+    }
+    
+    patternLayout = new PatternLayout();
+    patternLayout.setContext(context);
+    patternLayout.setPattern(pattern);
+    patternLayout.start();
+  }
+  
+  private void createDefaultPatternLayout() {
+    patternLayout = new PatternLayout();
+    patternLayout.setContext(context);
+    patternLayout.setPattern(DEFAULT_PATTERN);
+    patternLayout.start();
+  }
 }
\ No newline at end of file



More information about the logback-dev mailing list