danielp | 54e997e | 2013-02-21 01:54:23 +0000 | [diff] [blame^] | 1 | /** |
| 2 | * |
| 3 | */ |
| 4 | package org.frc971; |
| 5 | |
| 6 | import java.io.FileOutputStream; |
| 7 | import java.io.FileNotFoundException; |
| 8 | import java.io.PrintWriter; |
| 9 | |
| 10 | import java.util.logging.Handler; |
| 11 | import java.util.logging.LogRecord; |
| 12 | |
| 13 | /** |
| 14 | * @author daniel |
| 15 | * logs data to custom files, using specific formatting. |
| 16 | */ |
| 17 | public class LogHandler extends Handler { |
| 18 | |
| 19 | private FileOutputStream ofstream; |
| 20 | PrintWriter writer; |
| 21 | |
| 22 | public LogHandler (String filename) throws FileNotFoundException { |
| 23 | super(); |
| 24 | |
| 25 | if (filename == null || filename == "") { |
| 26 | filename = "logfile.log"; |
| 27 | } |
| 28 | |
| 29 | //check if file exists, and if not, create it |
| 30 | ofstream = new FileOutputStream(filename); |
| 31 | writer = new PrintWriter(ofstream); |
| 32 | setFormatter(new TimeFormatter()); |
| 33 | } |
| 34 | |
| 35 | /*Required methods*/ |
| 36 | |
| 37 | public void publish(LogRecord message) { |
| 38 | //record a message |
| 39 | if (!isLoggable(message)) { |
| 40 | //ensure that this message should be logged by this handler |
| 41 | return; |
| 42 | } |
| 43 | writer.print(getFormatter().format(message)); //Formatter adds trailing \n |
| 44 | } |
| 45 | public void flush() { |
| 46 | writer.flush(); |
| 47 | } |
| 48 | public void close() throws SecurityException { |
| 49 | writer.close(); |
| 50 | } |
| 51 | } |