Clover coverage report - PLT Utilities Test Coverage (plt-20120304-r5436)
Coverage timestamp: Sat Mar 3 2012 22:01:56 CST
file stats: LOC: 122   Methods: 13
NCLOC: 66   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
FileLogSink.java 20% 19% 30.8% 21.5%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK*
 2   
 3    PLT Utilities BSD License
 4   
 5    Copyright (c) 2007-2010 JavaPLT group at Rice University
 6    All rights reserved.
 7   
 8    Developed by: Java Programming Languages Team
 9    Rice University
 10    http://www.cs.rice.edu/~javaplt/
 11   
 12    Redistribution and use in source and binary forms, with or without modification, are permitted
 13    provided that the following conditions are met:
 14   
 15    - Redistributions of source code must retain the above copyright notice, this list of conditions
 16    and the following disclaimer.
 17    - Redistributions in binary form must reproduce the above copyright notice, this list of
 18    conditions and the following disclaimer in the documentation and/or other materials provided
 19    with the distribution.
 20    - Neither the name of the JavaPLT group, Rice University, nor the names of the library's
 21    contributors may be used to endorse or promote products derived from this software without
 22    specific prior written permission.
 23   
 24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
 25    IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 26    FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND
 27    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 28    DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 29    DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 30    IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 31    OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 32   
 33    *END_COPYRIGHT_BLOCK*/
 34   
 35    package edu.rice.cs.plt.debug;
 36   
 37    import java.io.*;
 38    import java.util.Date;
 39    import edu.rice.cs.plt.io.IOUtil;
 40    import edu.rice.cs.plt.lambda.LazyThunk;
 41    import edu.rice.cs.plt.lambda.Thunk;
 42    import edu.rice.cs.plt.lambda.WrappedException;
 43    import edu.rice.cs.plt.text.TextUtil;
 44   
 45    /**
 46    * A log sink that writes tagged, indented text to a file. The file is not opened until required for logging,
 47    * and is closed on program exit.
 48    */
 49    public class FileLogSink extends IndentedTextLogSink {
 50   
 51    private final Thunk<BufferedWriter> _writer;
 52    private volatile boolean _active;
 53   
 54  4 public FileLogSink(String filename) { this(new File(filename), null, true); }
 55   
 56  0 public FileLogSink(File f) { this(f, null, true); }
 57   
 58  0 public FileLogSink(File f, int idealLineWidth) { this(f, null, idealLineWidth, true); }
 59   
 60  0 public FileLogSink(File f, boolean closeOnExit) { this(f, null, closeOnExit); }
 61   
 62  0 public FileLogSink(File f, int idealLineWidth, boolean closeOnExit) {
 63  0 this(f, null, idealLineWidth, closeOnExit);
 64    }
 65   
 66  0 public FileLogSink(File f, String charset) { this(f, charset, true); }
 67   
 68  4 public FileLogSink(File f, String charset, boolean closeOnExit) {
 69  4 super();
 70  4 _writer = initWriter(f, charset);
 71  4 _active = false;
 72  4 if (closeOnExit) { IOUtil.closeOnExit(this); }
 73    }
 74   
 75  0 public FileLogSink(File f, String charset, int idealLineWidth) {
 76  0 this(f, charset, idealLineWidth, true);
 77    }
 78   
 79  0 public FileLogSink(File f, String charset, int idealLineWidth, boolean closeOnExit) {
 80  0 super(idealLineWidth);
 81  0 _writer = initWriter(f, charset);
 82  0 _active = false;
 83  0 if (closeOnExit) { IOUtil.closeOnExit(this); }
 84    }
 85   
 86    /** @param charset {@code null} if the default should be used */
 87  4 private Thunk<BufferedWriter> initWriter(final File f, final String charset) {
 88  4 return LazyThunk.make(new Thunk<BufferedWriter>() {
 89  0 public BufferedWriter value() {
 90  0 try {
 91  0 OutputStream out = new FileOutputStream(f, true);
 92  0 try {
 93  0 Writer w = (charset == null) ? new OutputStreamWriter(out) : new OutputStreamWriter(out, charset);
 94  0 BufferedWriter result = new BufferedWriter(w);
 95  0 IOUtil.closeOnExit(result);
 96  0 String stars = TextUtil.repeat('*', 40);
 97  0 result.write(stars);
 98  0 result.newLine();
 99  0 result.write("Opened log file " + formatTime(new Date()));
 100  0 result.newLine();
 101  0 result.write(stars);
 102  0 result.newLine();
 103  0 result.newLine();
 104  0 result.flush();
 105  0 _active = true;
 106  0 return result;
 107    }
 108  0 finally { if (!_active) { out.close(); } }
 109    }
 110  0 catch (IOException e) { throw new WrappedException(e); }
 111    }
 112    });
 113    }
 114   
 115  0 @Override protected BufferedWriter writer(Message m) { return _writer.value(); }
 116   
 117    /** Close the file stream. */
 118  4 public void close() throws IOException {
 119  0 if (_active) { _writer.value().close(); _active = false; }
 120    }
 121   
 122    }