Clover coverage report - Java Language Levels Test Coverage (javalanglevels-20120305-r5436)
Coverage timestamp: Sun Mar 4 2012 22:02:46 CST
file stats: LOC: 149   Methods: 10
NCLOC: 62   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Log.java 25% 32.4% 50% 33.9%
coverage coverage
 1    /*BEGIN_COPYRIGHT_BLOCK
 2    *
 3    * Copyright (c) 2001-2010, JavaPLT group at Rice University (drjava@rice.edu)
 4    * All rights reserved.
 5    *
 6    * Redistribution and use in source and binary forms, with or without
 7    * modification, are permitted provided that the following conditions are met:
 8    * * Redistributions of source code must retain the above copyright
 9    * notice, this list of conditions and the following disclaimer.
 10    * * Redistributions in binary form must reproduce the above copyright
 11    * notice, this list of conditions and the following disclaimer in the
 12    * documentation and/or other materials provided with the distribution.
 13    * * Neither the names of DrJava, the JavaPLT group, Rice University, nor the
 14    * names of its contributors may be used to endorse or promote products
 15    * derived from this software without specific prior written permission.
 16    *
 17    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 18    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 19    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 20    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 21    * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 22    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 23    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 24    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 25    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 26    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 27    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 28    *
 29    * This software is Open Source Initiative approved Open Source Software.
 30    * Open Source Initative Approved is a trademark of the Open Source Initiative.
 31    *
 32    * This file is part of DrJava. Download the current version of this project
 33    * from http://www.drjava.org/ or http://sourceforge.net/projects/drjava/
 34    *
 35    * END_COPYRIGHT_BLOCK*/
 36   
 37    package edu.rice.cs.javalanglevels.util;
 38   
 39    import java.io.*;
 40   
 41    import java.util.Date;
 42    import java.text.SimpleDateFormat;
 43    import java.util.TimeZone;
 44   
 45    /** Logging class to record errors or unexpected behavior to a file. The file is created in the current directory,
 46    * and is only used if the log is enabled. All logs can be enabled at once with the ENABLE_ALL field.
 47    * @version $Id: Log.java 5389 2010-09-17 19:52:54Z rcartwright $
 48    */
 49    public class Log {
 50    public static final boolean ENABLE_ALL = false;
 51   
 52    /** Whether this particular log is enabled in development mode. */
 53    protected volatile boolean _isEnabled;
 54   
 55    /** The filename of this log. */
 56    protected volatile String _name;
 57   
 58    /** The file object for this log. */
 59    protected volatile File _file;
 60   
 61    /** PrintWriter to print messages to a file. */
 62    protected volatile PrintWriter _writer;
 63   
 64    public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("d MMM yyyy H:mm:ss z");
 65    static {
 66  29 DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT"));
 67  29 DATE_FORMAT.setLenient(false);
 68    }
 69   
 70    /** Creates a new Log with the given name. If enabled is true, a file is created in the current directory with the
 71    * given name.
 72    * @param name File name for the log
 73    * @param isEnabled Whether to actively use this log
 74    */
 75  79 public Log(String name, boolean isEnabled) { this(new File(name), isEnabled); }
 76   
 77  79 public Log(File f, boolean isEnabled) { _file = f;
 78  79 _name = f.getName();
 79  79 _isEnabled = isEnabled;
 80  79 _init();
 81    }
 82   
 83    /** Creates the log file, if enabled. */
 84  0 protected void _init() { if (_writer == null) { if (_isEnabled || ENABLE_ALL) { try {
 85  0 FileWriter w = new FileWriter(_file.getAbsolutePath(), true);
 86  0 _writer = new PrintWriter(w);
 87  0 log("Log '" + _name + "' opened: " + DATE_FORMAT.format(new Date()));
 88    }
 89    catch (IOException ioe) {
 90    // Utilities.show("Could not create log: " + ioe);
 91  0 throw new RuntimeException("Could not create log: " + ioe);
 92    }
 93    }
 94    }
 95    }
 96   
 97    /** Sets whether this log is enabled. Only has an effect if the code is in development mode.
 98    * @param isEnabled Whether to print messages to the log file
 99    */
 100  0 public void setEnabled(boolean isEnabled) { _isEnabled = isEnabled; }
 101   
 102    /** Returns whether this log is currently enabled. */
 103  1797 public boolean isEnabled() { return (_isEnabled || ENABLE_ALL); }
 104   
 105    /** Prints a message to the log, if enabled.
 106    * @param message Message to print.
 107    */
 108  1797 public synchronized void log(String message) {
 109  1797 if (isEnabled()) {
 110  0 if (_writer == null) _init();
 111  0 _writer.println(DATE_FORMAT.format(new Date()) + ": " + message);
 112  0 _writer.flush();
 113    }
 114    }
 115   
 116    /** Converts a stack trace (StackTraceElement[]) to string form */
 117  0 public static String traceToString(StackTraceElement[] trace) {
 118  0 final StringBuilder traceImage = new StringBuilder();
 119  0 for (StackTraceElement e: trace) traceImage.append("\n\tat " + e.toString());
 120  0 return traceImage.toString();
 121    }
 122   
 123    /** Prints a message and exception stack trace to the log, if enabled.
 124    * @param s Message to print
 125    * @param trace Stack track to log
 126    */
 127  0 public synchronized void log(String s, StackTraceElement[] trace) {
 128  0 if (isEnabled()) log(s + traceToString(trace));
 129    }
 130   
 131    /** Prints a message and exception stack trace to the log, if enabled.
 132    * @param s Message to print
 133    * @param t Throwable to log
 134    */
 135  0 public synchronized void log(String s, Throwable t) {
 136  0 if (isEnabled()) {
 137  0 StringWriter sw = new StringWriter();
 138  0 PrintWriter pw = new PrintWriter(sw);
 139  0 t.printStackTrace(pw);
 140  0 log(s + "\n" + sw.toString());
 141    }
 142    }
 143   
 144    /** Closes a log file. */
 145  0 public void close() {
 146  0 _writer.close();
 147  0 _writer = null;
 148    }
 149    }