append to file

This commit is contained in:
Fredrik Osterlind 2011-12-06 10:54:51 +01:00
parent e291eb43f4
commit 329a75525e

View file

@ -48,9 +48,11 @@ import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent; import java.awt.event.MouseEvent;
import java.io.File; import java.io.File;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Date;
import java.util.List; import java.util.List;
import java.util.regex.PatternSyntaxException; import java.util.regex.PatternSyntaxException;
@ -146,6 +148,8 @@ public class LogListener extends VisPlugin {
private boolean hideDebug = false; private boolean hideDebug = false;
private JCheckBoxMenuItem hideDebugCheckbox; private JCheckBoxMenuItem hideDebugCheckbox;
private JCheckBoxMenuItem appendCheckBox;
private static final int UPDATE_INTERVAL = 250; private static final int UPDATE_INTERVAL = 250;
private UpdateAggregator<LogData> logUpdateAggregator = new UpdateAggregator<LogData>(UPDATE_INTERVAL) { private UpdateAggregator<LogData> logUpdateAggregator = new UpdateAggregator<LogData>(UPDATE_INTERVAL) {
private Runnable scroll = new Runnable() { private Runnable scroll = new Runnable() {
@ -348,6 +352,8 @@ public class LogListener extends VisPlugin {
popupMenu.add(new JMenuItem(clearAction)); popupMenu.add(new JMenuItem(clearAction));
popupMenu.addSeparator(); popupMenu.addSeparator();
popupMenu.add(new JMenuItem(saveAction)); popupMenu.add(new JMenuItem(saveAction));
appendCheckBox = new JCheckBoxMenuItem(appendAction);
popupMenu.add(appendCheckBox);
popupMenu.addSeparator(); popupMenu.addSeparator();
JMenu focusMenu = new JMenu("Show in"); JMenu focusMenu = new JMenu("Show in");
focusMenu.add(new JMenuItem(showInAllAction)); focusMenu.add(new JMenuItem(showInAllAction));
@ -436,7 +442,16 @@ public class LogListener extends VisPlugin {
hasHours = true; hasHours = true;
repaintTimeColumn(); repaintTimeColumn();
} }
logUpdateAggregator.add(new LogData(ev)); LogData data = new LogData(ev);
logUpdateAggregator.add(data);
if (appendToFile) {
appendToFile(appendStreamFile,
data.getTime() + "\t" +
data.getID() + "\t" +
data.ev.getMessage() + "\n"
);
}
} }
public void removedLogOutput(LogOutputEvent ev) { public void removedLogOutput(LogOutputEvent ev) {
} }
@ -498,6 +513,7 @@ public class LogListener extends VisPlugin {
public void closePlugin() { public void closePlugin() {
/* Stop observing motes */ /* Stop observing motes */
appendToFile(null, null);
logUpdateAggregator.stop(); logUpdateAggregator.stop();
simulation.getEventCentral().removeLogOutputListener(logOutputListener); simulation.getEventCentral().removeLogOutputListener(logOutputListener);
} }
@ -526,6 +542,11 @@ public class LogListener extends VisPlugin {
element = new Element("inversefilter"); element = new Element("inversefilter");
config.add(element); config.add(element);
} }
if (appendToFile) {
element = new Element("append");
element.setText(simulation.getGUI().createPortablePath(appendStreamFile).getPath());
config.add(element);
}
return config; return config;
} }
@ -551,6 +572,16 @@ public class LogListener extends VisPlugin {
} else if ("formatted_time".equals(name)) { } else if ("formatted_time".equals(name)) {
formatTimeString = true; formatTimeString = true;
repaintTimeColumn(); repaintTimeColumn();
} else if ("append".equals(name)) {
appendToFile = true;
appendStreamFile = simulation.getGUI().restorePortablePath(new File(element.getText()));
appendCheckBox.setSelected(true);
if (!appendStreamFile.exists()) {
try {
appendStreamFile.createNewFile();
} catch (IOException e) {
}
}
} }
} }
@ -699,6 +730,86 @@ public class LogListener extends VisPlugin {
} }
}; };
private boolean appendToFile = false;
private File appendStreamFile = null;
private boolean appendToFileWroteHeader = false;
private PrintWriter appendStream = null;
public boolean appendToFile(File file, String text) {
/* Close stream */
if (file == null) {
if (appendStream != null) {
appendStream.close();
appendStream = null;
}
return false;
}
/* Open stream */
if (appendStream == null || file != appendStreamFile) {
try {
if (appendStream != null) {
appendStream.close();
appendStream = null;
}
appendStream = new PrintWriter(new FileWriter(file,true));
appendStreamFile = file;
appendToFileWroteHeader = false;
} catch (Exception ex) {
logger.fatal("Append file failed: " + ex.getMessage(), ex);
return false;
}
}
/* Append to file */
if (!appendToFileWroteHeader) {
appendStream.println("-- Log Listener [" + simulation.getTitle() + "]: Started at " + (new Date()).toString());
appendToFileWroteHeader = true;
}
appendStream.print(text);
appendStream.flush();
return true;
}
private Action appendAction = new AbstractAction("Append to file") {
private static final long serialVersionUID = -3041714249257346688L;
public void actionPerformed(ActionEvent e) {
JCheckBoxMenuItem cb = (JCheckBoxMenuItem) e.getSource();
appendToFile = cb.isSelected();
if (!appendToFile) {
appendToFile(null, null);
appendStreamFile = null;
return;
}
JFileChooser fc = new JFileChooser();
File suggest = new File(GUI.getExternalToolsSetting("LOG_LISTENER_APPENDFILE", "loglistener_append.txt"));
fc.setSelectedFile(suggest);
int returnVal = fc.showSaveDialog(GUI.getTopParentContainer());
if (returnVal != JFileChooser.APPROVE_OPTION) {
appendToFile = false;
cb.setSelected(appendToFile);
return;
}
File saveFile = fc.getSelectedFile();
GUI.setExternalToolsSetting("LOG_LISTENER_APPENDFILE", saveFile.getPath());
if (saveFile.exists() && !saveFile.canWrite()) {
logger.fatal("No write access to file: " + saveFile);
appendToFile = false;
cb.setSelected(appendToFile);
return;
}
appendToFile = true;
appendStreamFile = saveFile;
if (!appendStreamFile.exists()) {
try {
appendStreamFile.createNewFile();
} catch (IOException ex) {
}
}
}
};
private Action timeLineAction = new AbstractAction("Timeline") { private Action timeLineAction = new AbstractAction("Timeline") {
private static final long serialVersionUID = -6358463434933029699L; private static final long serialVersionUID = -6358463434933029699L;
public void actionPerformed(ActionEvent e) { public void actionPerformed(ActionEvent e) {