A log backed by a file which can be deleted.
| 38 | * A log backed by a file which can be deleted. |
| 39 | */ |
| 40 | public class LogFile implements LogStream { |
| 41 | |
| 42 | |
| 43 | private final PrintStream mStream; |
| 44 | |
| 45 | private final File mFile; |
| 46 | |
| 47 | /** |
| 48 | * @param file which is used to back the stream. |
| 49 | */ |
| 50 | public LogFile(final File file) { |
| 51 | if (file == null) { |
| 52 | throw new NullPointerException(); |
| 53 | } |
| 54 | mFile = file; |
| 55 | FileOutputStream fileOutputStream; |
| 56 | try { |
| 57 | fileOutputStream = new FileOutputStream(file, true); |
| 58 | } catch (final FileNotFoundException e) { |
| 59 | fileOutputStream = null; |
| 60 | } |
| 61 | if (fileOutputStream == null) { |
| 62 | mStream = null; |
| 63 | } else { |
| 64 | mStream = new PrintStream(fileOutputStream); |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | @Override |
| 69 | public void close() { |
| 70 | if (mStream != null) { |
| 71 | mStream.close(); |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | @Override |
| 76 | public File file() { |
| 77 | return mFile; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public void removeLog() { |
| 82 | close(); |
| 83 | if (mStream != null) { |
| 84 | if (mFile.exists() && !mFile.delete()) { |
| 85 | throw new RuntimeException("Unable to delete log file: " + mFile.getPath()); |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | @Override |
| 91 | public PrintStream stream() { |
| 92 | return mStream; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public String toString() { |
| 97 | return "LogFile " + mFile; |
nothing calls this directly
no outgoing calls
no test coverage detected