You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by sd...@apache.org on 2003/05/01 06:58:48 UTC

cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw FileSaveAction.java

sdeboy      2003/04/30 21:58:48

  Added:       src/java/org/apache/log4j/chainsaw FileSaveAction.java
  Log:
  Added save capability - saves all entries for current tab (even those currently filtered).  ThrowableInformation and LoggingEvent constructors were modified to accept a string[] because reconstructing a loggingevent with an exception by generating a new throwable added the xmldecoder's stack to the throwable's trace..
  
  Revision  Changes    Path
  1.1                  jakarta-log4j-sandbox/src/java/org/apache/log4j/chainsaw/FileSaveAction.java
  
  Index: FileSaveAction.java
  ===================================================================
  /*
   * @author Paul Smith <ps...@apache.org>
   *
  */
  package org.apache.log4j.chainsaw;
  
  import java.awt.event.ActionEvent;
  import java.awt.event.InputEvent;
  import java.awt.event.KeyEvent;
  import java.io.BufferedWriter;
  import java.io.File;
  import java.io.FileWriter;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.Iterator;
  import java.util.Vector;
  
  import javax.swing.AbstractAction;
  import javax.swing.Action;
  import javax.swing.JFileChooser;
  import javax.swing.KeyStroke;
  
  import org.apache.log4j.spi.LoggingEvent;
  import org.apache.log4j.xml.XMLLayout;
  
  /**
   * Allows the user to specify a particular file to which the current tab's
   * displayed events will be saved.
   * 
   * @author Scott Deboy <sd...@apache.org>
   *
   */
  class FileSaveAction extends AbstractAction {
  	private LogUI parent;
  
  	/**
  	 * This action must have a reference to a LogUI
  	 * in order to retrieve events to save
  	 * 
  	 */
  	public FileSaveAction(LogUI parent) {
  		super("Save");
  	    putValue(Action.ACCELERATOR_KEY, KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.ALT_MASK));
  		putValue(Action.MNEMONIC_KEY, new Integer(KeyEvent.VK_S));
  		putValue(
  			Action.SHORT_DESCRIPTION,
  			"Saves displayed events for the current tab");
  		//    TODO be nice if this also had an Icon
  		this.parent = parent;
  	}
  
  	/* 
  	 * When the user chooses the Save action,
  	 * a File chooser is presented to allow them to 
  	 * find an XML file to save events to.
  	 * 
  	 * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
  	 */
  	public void actionPerformed(ActionEvent e) {
  
  		JFileChooser chooser = new JFileChooser();
  
  		chooser.setAcceptAllFileFilterUsed(true);
  
  		chooser.showOpenDialog(parent);
  
  		File selectedFile = chooser.getSelectedFile();
  		XMLLayout layout = new XMLLayout();
  		LoggingEvent event = null;
  		PrintWriter out = null;
  		if (selectedFile != null) {
  			try {
  				Vector v = parent.getAllEvents(parent.getActiveTabName());
  				if ((v!=null && v.size()==0) || (v==null)) {
  					//no events to save
  					return;
  				}
  
  				Iterator iter = v.iterator();
  
  				out =
  					new PrintWriter(
  						new BufferedWriter(new FileWriter(selectedFile)));
  
  				while (iter.hasNext()) {
  					event = (LoggingEvent) iter.next();
  					layout.setLocationInfo(
  						event.getThrowableInformation() != null);
  					out.write(layout.format(event));
  				}
  			} catch (IOException ioe) {
  				ioe.printStackTrace();
  			} finally {
  				if (out != null) {
  					out.flush();
  					out.close();
  				}
  			}
  		}
  	}
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org