Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   * 
   *      http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.log4j.chainsaw;
 
 import java.util.Date;
 import java.util.List;
Represents a list of EventDetails objects that are sorted on logging time. Methods are provided to filter the events that are visible.

Author(s):
Oliver Burn
 
     extends AbstractTableModel
 {

    
used to log messages
 
     private static final Logger LOG = Logger.getLogger(MyTableModel.class);

    
use the compare logging events
 
     private static final Comparator MY_COMP = new Comparator()
     {
        
 
         public int compare(Object aObj1Object aObj2) {
             if ((aObj1 == null) && (aObj2 == null)) {
                 return 0; // treat as equal
             } else if (aObj1 == null) {
                 return -1; // null less than everything
             } else if (aObj2 == null) {
                 return 1; // think about it. :->
             }
 
             // will assume only have LoggingEvent
             final EventDetails le1 = (EventDetailsaObj1;
             final EventDetails le2 = (EventDetailsaObj2;
 
             if (le1.getTimeStamp() < le2.getTimeStamp()) {
                 return 1;
             }
             // assume not two events are logged at exactly the same time
             return -1;
         }
         };

    
Helper that actually processes incoming events.

Author(s):
Oliver Burn
 
     private class Processor
         implements Runnable
     {
        
loops getting the events
 
         public void run() {
             while (true) {
                 try {
                     Thread.sleep(1000);
                 } catch (InterruptedException e) {
                     // ignore
                 }
 
                 synchronized () {
                     if () {
                         continue;
                     }
 
                     boolean toHead = true// were events added to head
                     boolean needUpdate = false;
                     final Iterator it = .iterator();
                     while (it.hasNext()) {
                         final EventDetails event = (EventDetailsit.next();
                         .add(event);
                         toHead = toHead && (event == .first());
                         needUpdate = needUpdate || matchFilter(event);
                     }
                     .clear();
                    if (needUpdate) {
                        updateFilteredEvents(toHead);
                    }
                }
            }
        }
    }


    
names of the columns in the table
    private static final String[] COL_NAMES = {
        "Time""Priority""Trace""Category""NDC""Message"};

    
definition of an empty list
    private static final EventDetails[] EMPTY_LIST =  new EventDetails[] {};

    
used to format dates
    private static final DateFormat DATE_FORMATTER =
        DateFormat.getDateTimeInstance(..);

    
the lock to control access
    private final Object mLock = new Object();
    
set of all logged events - not filtered
    private final SortedSet mAllEvents = new TreeSet();
    
events that are visible after filtering
    private EventDetails[] mFilteredEvents = ;
    
list of events that are buffered for processing
    private final List mPendingEvents = new ArrayList();
    
indicates whether event collection is paused to the UI
    private boolean mPaused = false;

    
filter for the thread
    private String mThreadFilter = "";
    
filter for the message
    private String mMessageFilter = "";
    
filter for the NDC
    private String mNDCFilter = "";
    
filter for the category
    private String mCategoryFilter = "";
    
filter for the priority
    private Priority mPriorityFilter = .;


    
Creates a new MyTableModel instance.
    MyTableModel() {
        final Thread t = new Thread(new Processor());
        t.setDaemon(true);
        t.start();
    }
    ////////////////////////////////////////////////////////////////////////////
    // Table Methods
    ////////////////////////////////////////////////////////////////////////////

    
    public int getRowCount() {
        synchronized () {
            return .;
        }
    }

    
    public int getColumnCount() {
        // does not need to be synchronized
        return .;
    }

    
    public String getColumnName(int aCol) {
        // does not need to be synchronized
        return [aCol];
    }

    
    public Class getColumnClass(int aCol) {
        // does not need to be synchronized
        return (aCol == 2) ? Boolean.class : Object.class;
    }

    
    public Object getValueAt(int aRowint aCol) {
        synchronized () {
            final EventDetails event = [aRow];
            if (aCol == 0) {
                return .format(new Date(event.getTimeStamp()));
            } else if (aCol == 1) {
                return event.getPriority();
            } else if (aCol == 2) {
                return (event.getThrowableStrRep() == null)
                    ? . : .;
            } else if (aCol == 3) {
                return event.getCategoryName();
            } else if (aCol == 4) {
                return event.getNDC();
            }
            return event.getMessage();
        }
    }
    ////////////////////////////////////////////////////////////////////////////
    // Public Methods
    ////////////////////////////////////////////////////////////////////////////

    
Sets the priority to filter events on. Only events of equal or higher property are now displayed.

Parameters:
aPriority the priority to filter on
    public void setPriorityFilter(Priority aPriority) {
        synchronized () {
             = aPriority;
            updateFilteredEvents(false);
        }
    }

    
Set the filter for the thread field.

Parameters:
aStr the string to match
    public void setThreadFilter(String aStr) {
        synchronized () {
             = aStr.trim();
            updateFilteredEvents(false);
        }
    }

    
Set the filter for the message field.

Parameters:
aStr the string to match
    public void setMessageFilter(String aStr) {
        synchronized () {
             = aStr.trim();
            updateFilteredEvents(false);
        }
    }

    
Set the filter for the NDC field.

Parameters:
aStr the string to match
    public void setNDCFilter(String aStr) {
        synchronized () {
             = aStr.trim();
            updateFilteredEvents(false);
        }
    }

    
Set the filter for the category field.

Parameters:
aStr the string to match
    public void setCategoryFilter(String aStr) {
        synchronized () {
             = aStr.trim();
            updateFilteredEvents(false);
        }
    }

    
Add an event to the list.

Parameters:
aEvent a EventDetails value
    public void addEvent(EventDetails aEvent) {
        synchronized () {
            .add(aEvent);
        }
    }

    
Clear the list of all events.
    public void clear() {
        synchronized () {
            .clear();
             = new EventDetails[0];
            .clear();
            fireTableDataChanged();
        }
    }

    
Toggle whether collecting events
    public void toggle() {
        synchronized () {
             = !;
        }
    }

    

Returns:
whether currently paused collecting events
    public boolean isPaused() {
        synchronized () {
            return ;
        }
    }

    
Get the throwable information at a specified row in the filtered events.

Parameters:
aRow the row index of the event
Returns:
the throwable information
    public EventDetails getEventDetails(int aRow) {
        synchronized () {
            return [aRow];
        }
    }
    ////////////////////////////////////////////////////////////////////////////
    // Private methods
    ////////////////////////////////////////////////////////////////////////////

    
Update the filtered events data structure.

Parameters:
aInsertedToFront indicates whether events were added to front of the events. If true, then the current first event must still exist in the list after the filter is applied.
    private void updateFilteredEvents(boolean aInsertedToFront) {
        final long start = System.currentTimeMillis();
        final List filtered = new ArrayList();
        final int size = .size();
        final Iterator it = .iterator();
        while (it.hasNext()) {
            final EventDetails event = (EventDetailsit.next();
            if (matchFilter(event)) {
                filtered.add(event);
            }
        }
        final EventDetails lastFirst = (. == 0)
            ? null
            : [0];
         = (EventDetails[]) filtered.toArray();
        if (aInsertedToFront && (lastFirst != null)) {
            final int index = filtered.indexOf(lastFirst);
            if (index < 1) {
                .warn("In strange state");
                fireTableDataChanged();
            } else {
                fireTableRowsInserted(0, index - 1);
            }
        } else {
            fireTableDataChanged();
        }
        final long end = System.currentTimeMillis();
        .debug("Total time [ms]: " + (end - start)
                  + " in update, size: " + size);
    }

    
Returns whether an event matches the filters.

Parameters:
aEvent the event to check for a match
Returns:
whether the event matches
    private boolean matchFilter(EventDetails aEvent) {
        if (aEvent.getPriority().isGreaterOrEqual() &&
            (aEvent.getThreadName().indexOf() >= 0) &&
            (aEvent.getCategoryName().indexOf() >= 0) &&
            ((.length() == 0) ||
             ((aEvent.getNDC() != null) &&
              (aEvent.getNDC().indexOf() >= 0))))
        {
            final String rm = aEvent.getMessage();
            if (rm == null) {
                // only match if we have not filtering in place
                return (.length() == 0);
            } else {
                return (rm.indexOf() >= 0);
            }
        }
        return false// by default not match
    }
New to GrepCode? Check out our FAQ X