psmith      2003/09/14 16:47:27

  Modified:    src/java/org/apache/log4j/chainsaw LogUI.java
                        ChainsawAppenderHandler.java ChainsawStatusBar.java
  Log:
  For a bit of fun, I added a Data Rate concept to the
  object that receives and processes events inside Chainsaw,
  and displayed it in the Status bar.
  
  Revision  Changes    Path
  1.27      +9 -0      jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java
  
  Index: LogUI.java
  ===================================================================
  RCS file: /home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/LogUI.java,v
  retrieving revision 1.26
  retrieving revision 1.27
  diff -u -r1.26 -r1.27
  --- LogUI.java        11 Sep 2003 09:42:31 -0000      1.26
  +++ LogUI.java        14 Sep 2003 23:47:26 -0000      1.27
  @@ -553,6 +553,15 @@
         });
   
       pack();
  +    
  +    this.handler.addPropertyChangeListener("dataRate", new PropertyChangeListener(){
  +
  +             public void propertyChange(PropertyChangeEvent evt) {
  +                     double dataRate = ((Double)evt.getNewValue()).doubleValue();
  +                     LogLog.debug("dataRate=" + dataRate);
  +                     statusBar.setDataRate(dataRate);
  +                     
  +             }});
   
       getSettingsManager().addSettingsListener(this);
       getSettingsManager().addSettingsListener(getToolBarAndMenus());
  
  
  
  1.6       +121 -39   
jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java
  
  Index: ChainsawAppenderHandler.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawAppenderHandler.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- ChainsawAppenderHandler.java      4 Sep 2003 02:27:15 -0000       1.5
  +++ ChainsawAppenderHandler.java      14 Sep 2003 23:47:26 -0000      1.6
  @@ -57,6 +57,9 @@
   import org.apache.log4j.spi.LocationInfo;
   import org.apache.log4j.spi.LoggingEvent;
   
  +import java.beans.PropertyChangeListener;
  +import java.beans.PropertyChangeSupport;
  +
   import java.util.ArrayList;
   import java.util.Collection;
   import java.util.Date;
  @@ -85,6 +88,9 @@
     private final Object mutex = new Object();
     private int sleepInterval = 1000;
     private EventListenerList listenerList = new EventListenerList();
  +  private double dataRate = 0.0;
  +  private PropertyChangeSupport propertySupport =
  +    new PropertyChangeSupport(this);
   
     public ChainsawAppenderHandler(ChainsawAppender appender) {
       this.appender = appender;
  @@ -133,7 +139,7 @@
      * Converts a LoggingEvent into a Vector of element (columns really).
      * @param event
      * @return
  -   * 
  +   *
      * @deprecated
      */
     public static Vector convert(LoggingEvent event) {
  @@ -234,7 +240,7 @@
      * @param v
      * @return
      */
  -    private static String getTabIdentifier(LoggingEvent e) {
  +  private static String getTabIdentifier(LoggingEvent e) {
       StringBuffer ident = new StringBuffer();
       String machinename = e.getProperty(ChainsawConstants.LOG4J_MACHINE_KEY);
   
  @@ -243,7 +249,7 @@
       }
   
       String appname = e.getProperty(ChainsawConstants.LOG4J_APP_KEY);
  -    
  +
       if (appname != null) {
         ident.append("-");
         ident.append(appname);
  @@ -253,16 +259,19 @@
         /**
              * Maybe there's a Remote Host entry?
              */
  -      String remoteHost = e.getProperty(ChainsawConstants.LOG4J_REMOTEHOST_KEY);
  -      if(remoteHost!=null) {
  -            int colonIndex = remoteHost.indexOf(":");
  -    
  -            if (colonIndex == -1) {
  -              colonIndex = remoteHost.length();
  -            }
  -    
  -            remoteHost = remoteHost.substring(0, colonIndex);
  -      }    
  +      String remoteHost =
  +        e.getProperty(ChainsawConstants.LOG4J_REMOTEHOST_KEY);
  +
  +      if (remoteHost != null) {
  +        int colonIndex = remoteHost.indexOf(":");
  +
  +        if (colonIndex == -1) {
  +          colonIndex = remoteHost.length();
  +        }
  +
  +        remoteHost = remoteHost.substring(0, colonIndex);
  +      }
  +
         if (remoteHost != null) {
           ident.append(remoteHost);
         }
  @@ -276,6 +285,91 @@
     }
   
     /**
  +   * A little test bed
  +   * @param args
  +   */
  +  public static void main(String[] args) throws InterruptedException {
  +    ChainsawAppenderHandler handler = new ChainsawAppenderHandler();
  +    handler.addEventBatchListener(
  +      new EventBatchListener() {
  +        public String getInterestedIdentifier() {
  +          return null;
  +        }
  +
  +        public void receiveEventBatch(
  +          String identifier, List eventBatchEntrys) {
  +          LogLog.debug(
  +            "received batch for '" + identifier + "', list.size()="
  +            + eventBatchEntrys.size());
  +          LogLog.debug(eventBatchEntrys.toString());
  +        }
  +      });
  +    LogManager.getRootLogger().addAppender(handler);
  +
  +    SocketReceiver receiver = new SocketReceiver(4445);
  +    PluginRegistry.startPlugin(receiver);
  +
  +    Thread.sleep(60000);
  +  }
  +
  +  /**
  +   * Exposes the current Data rate calculated.  This is periodically updated
  +   * by an internal Thread as is the number of events that have
  +   * been processed, and dispatched to all listeners since the last sample period
  +   * divided by the number of seconds since the last sample period.
  +   *
  +   * This method fires a PropertyChange event so listeners can monitor the rate
  +   * @return double # of events processed per second
  +   */
  +  public double getDataRate() {
  +    return dataRate;
  +  }
  +
  +  /**
  +   * @param dataRate
  +   */
  +  private void setDataRate(double dataRate) {
  +    double oldValue = this.dataRate;
  +    this.dataRate = dataRate;
  +    propertySupport.firePropertyChange(
  +      "dataRate", new Double(oldValue), new Double(this.dataRate));
  +  }
  +
  +  /**
  +   * @param listener
  +   */
  +  public synchronized void addPropertyChangeListener(
  +    PropertyChangeListener listener) {
  +    propertySupport.addPropertyChangeListener(listener);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param listener
  +   */
  +  public synchronized void addPropertyChangeListener(
  +    String propertyName, PropertyChangeListener listener) {
  +    propertySupport.addPropertyChangeListener(propertyName, listener);
  +  }
  +
  +  /**
  +   * @param listener
  +   */
  +  public synchronized void removePropertyChangeListener(
  +    PropertyChangeListener listener) {
  +    propertySupport.removePropertyChangeListener(listener);
  +  }
  +
  +  /**
  +   * @param propertyName
  +   * @param listener
  +   */
  +  public synchronized void removePropertyChangeListener(
  +    String propertyName, PropertyChangeListener listener) {
  +    propertySupport.removePropertyChangeListener(propertyName, listener);
  +  }
  +
  +  /**
      * Queue of Events are placed in here, which are picked up by an
      * asychronous thread.  The WorkerThread looks for events once a second and
      * processes all events accumulated during that time..
  @@ -314,6 +408,8 @@
           List innerList = new ArrayList();
   
           while (isAlive()) {
  +          long timeStart = System.currentTimeMillis();
  +
             synchronized (mutex) {
               if (stopped) {
                 return;
  @@ -325,6 +421,8 @@
               }
             }
   
  +          int size = innerList.size();
  +
             if (innerList.size() > 0) {
               Iterator iter = innerList.iterator();
               Map identifiersEventsMap = new HashMap();
  @@ -348,10 +446,20 @@
               innerList.clear();
             }
   
  +
  +
             try {
               Thread.sleep(getQueueInterval());
             } catch (InterruptedException ie) {
             }
  +               if (size == 0) {
  +                     setDataRate(0.0);
  +               } else {
  +                     long timeEnd = System.currentTimeMillis();
  +                     long diffInSeconds = (timeEnd - timeStart)/1000;
  +                     double rate = (((double) size) / diffInSeconds);
  +                     setDataRate(rate);
  +               }
           }
         }
   
  @@ -389,30 +497,4 @@
         }
       }
     }
  -  
  -  /**
  -   * A little test bed 
  -   * @param args
  -   */
  -  public static void main(String[] args) throws InterruptedException {
  -     
  -      ChainsawAppenderHandler handler = new ChainsawAppenderHandler();
  -      handler.addEventBatchListener(new EventBatchListener() {
  -
  -             public String getInterestedIdentifier() {
  -                     return null;
  -             }
  -
  -             public void receiveEventBatch(String identifier, List 
eventBatchEntrys) {
  -            LogLog.debug("received batch for '" + identifier + "', list.size()=" + 
eventBatchEntrys.size());
  -            LogLog.debug(eventBatchEntrys.toString());
  -                     
  -             }});
  -      LogManager.getRootLogger().addAppender(handler);
  -      
  -      SocketReceiver receiver = new SocketReceiver(4445);
  -      PluginRegistry.startPlugin(receiver);
  -      
  -      Thread.sleep(60000);
  -}
   }
  
  
  
  1.2       +24 -9     
jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawStatusBar.java
  
  Index: ChainsawStatusBar.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-log4j/src/java/org/apache/log4j/chainsaw/ChainsawStatusBar.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- ChainsawStatusBar.java    25 Jun 2003 04:05:21 -0000      1.1
  +++ ChainsawStatusBar.java    14 Sep 2003 23:47:26 -0000      1.2
  @@ -56,6 +56,7 @@
   import java.awt.GridBagConstraints;
   import java.awt.GridBagLayout;
   import java.awt.Insets;
  +import java.text.NumberFormat;
   
   import javax.swing.BorderFactory;
   import javax.swing.Icon;
  @@ -78,7 +79,7 @@
     private final JLabel statusMsg = new JLabel(DEFAULT_MSG);
     private final JLabel pausedLabel = new JLabel("", JLabel.CENTER);
     private final JLabel lineSelectionLabel = new JLabel("", JLabel.CENTER);
  -  private final JLabel receivedEventLabel = new JLabel("", JLabel.CENTER);
  +  private final JLabel receivedEventLabel = new JLabel("0.0", JLabel.CENTER);
     private final JLabel receivedConnectionlabel = new JLabel("", JLabel.CENTER);
     private volatile long lastReceivedEvent = System.currentTimeMillis();
     private volatile long lastReceivedConnection = System.currentTimeMillis();
  @@ -86,8 +87,10 @@
     private final Thread connectionThread;
     private final Icon radioTowerIcon =
       new ImageIcon(ChainsawIcons.ANIM_RADIO_TOWER);
  -  private final Icon netConnectIcon = new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT);
  -  
  +  private final Icon netConnectIcon =
  +    new ImageIcon(ChainsawIcons.ANIM_NET_CONNECT);
  +    
  +    private final NumberFormat nf = NumberFormat.getNumberInstance();
   
     //  private final Border statusBarComponentBorder =
     //    BorderFactory.createEmptyBorder();
  @@ -99,6 +102,9 @@
     ChainsawStatusBar() {
       setLayout(new GridBagLayout());
   
  +     nf.setMaximumFractionDigits(1);
  +     nf.setMinimumFractionDigits(1);
  +     nf.setGroupingUsed(false);
       JPanel statusMsgPanel = new JPanel(new FlowLayout(FlowLayout.LEFT, 2, 2));
   
       statusMsgPanel.add(statusMsg);
  @@ -115,11 +121,13 @@
   
       receivedEventLabel.setBorder(statusBarComponentBorder);
       receivedEventLabel.setToolTipText(
  -      "Indicates whether Chainsaw is receiving events");
  +      "Indicates whether Chainsaw is receiving events, and how fast it is 
processing them");
       receivedEventLabel.setPreferredSize(
         new Dimension(
  -        radioTowerIcon.getIconWidth() + 4,
  +        radioTowerIcon.getIconWidth() + 
receivedEventLabel.getFontMetrics(receivedEventLabel.getFont()).stringWidth(
  +     "9999.9") + 10,
           (int) receivedEventLabel.getPreferredSize().getHeight()));
  +    receivedEventLabel.setMinimumSize(receivedEventLabel.getPreferredSize());
   
       receivedConnectionlabel.setBorder(statusBarComponentBorder);
       receivedConnectionlabel.setToolTipText(
  @@ -129,7 +137,6 @@
           netConnectIcon.getIconWidth() + 4,
           (int) receivedConnectionlabel.getPreferredSize().getHeight()));
   
  -
       lineSelectionLabel.setBorder(statusBarComponentBorder);
       lineSelectionLabel.setPreferredSize(
         new Dimension(
  @@ -204,7 +211,7 @@
             }
           });
       receiveThread.start();
  -    
  +
       connectionThread =
         new Thread(
           new Runnable() {
  @@ -248,6 +255,14 @@
       receiveThread.interrupt();
     }
   
  +  void setDataRate(final double dataRate) {
  +     SwingUtilities.invokeLater(new Runnable(){
  +
  +             public void run() {
  +             receivedEventLabel.setText(nf.format(dataRate) + "/s");
  +             }});
  +  }
  +
     /**
      * Indicates a new connection has been established between
      * Chainsaw and some remote host
  @@ -257,7 +272,7 @@
       lastReceivedConnection = System.currentTimeMillis();
       setMessage("Connection received from " + source);
       connectionThread.interrupt();
  -    
  +
       //    TODO and maybe play a sound?
     }
   
  @@ -309,7 +324,7 @@
       SwingUtilities.invokeLater(
         new Runnable() {
           public void run() {
  -          statusMsg.setText(" " +msg);
  +          statusMsg.setText(" " + msg);
           }
         });
     }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to