Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</parent>

<artifactId>scijava-common</artifactId>
<version>2.41.1-SNAPSHOT</version>
<version>3.0.0-SNAPSHOT</version>

<name>SciJava Common</name>
<description>SciJava Common is a shared library for SciJava software. It provides a plugin framework, with an extensible mechanism for service discovery, backed by its own annotation processor, so that plugins can be loaded dynamically. It is used by both ImageJ and SCIFIO.</description>
Expand Down
168 changes: 84 additions & 84 deletions src/main/java/org/scijava/log/AbstractLogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Expand All @@ -31,40 +31,27 @@

package org.scijava.log;

import java.io.PrintStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.scijava.service.AbstractService;

/**
* Base implementation of an abstract {@link LogService}.
*
* Base class for {@link LogService} implementationst.
*
* @author Johannes Schindelin
*/
public abstract class AbstractLogService extends AbstractService implements LogService {
public abstract class AbstractLogService extends AbstractService implements
LogService
{

private int currentLevel = System.getenv("DEBUG") == null ? INFO : DEBUG;

private Map<String, Integer> classAndPackageLevels =
private final Map<String, Integer> classAndPackageLevels =
new HashMap<String, Integer>();

// -- abstract methods --

/**
* Displays a message.
*
* @param msg the message to display.
*/
protected abstract void log(final String msg);

/**
* Displays an exception.
*
* @param t the exception to display.
*/
protected abstract void log(final Throwable t);

// -- constructor --

public AbstractLogService() {
Expand All @@ -87,120 +74,116 @@ public AbstractLogService() {
if (!(propKey instanceof String)) continue;
final String propName = (String) propKey;
if (!propName.startsWith(logLevelPrefix)) continue;
final String classOrPackageName = propName.substring(logLevelPrefix.length());
final String classOrPackageName =
propName.substring(logLevelPrefix.length());
setLevel(classOrPackageName, level(props.getProperty(propName)));
}

}

// -- helper methods --

protected void log(final int level, final Object msg, final Throwable t) {
if (level > getLevel()) return;

if (msg != null || t == null) {
log(level, msg);
}
if (t != null) log(t);
}
// -- Internal methods --

protected void log(final int level, final Object msg) {
final String prefix = getPrefix(level);
log((prefix == null ? "" : prefix + " ") + msg);
}
/**
* Displays a message and/or exception at the given logging level.
*
* @param level The logging level of the information.
* @param msg The message to display.
* @param t The exception to display.
*/
protected abstract void log(final int level, final Object msg,
final Throwable t);

protected String getPrefix(int level) {
switch (level) {
case ERROR:
return "[ERROR]";
case WARN:
return "[WARNING]";
case INFO:
return "[INFO]";
case DEBUG:
return "[DEBUG]";
case TRACE:
return "[TRACE]";
default:
return null;
}
/**
* Displays a message and/or exception at the given logging level, using the
* specific output stream.
*
* @param stream The output stream to which the information should be sent.
* @param level The logging level of the information.
* @param msg The message to display.
* @param t The exception to display.
*/
protected void log(final PrintStream stream, final int level,
final Object msg, final Throwable t)
{
if (msg != null || t == null) stream.println(getPrefix(level) + msg);
if (t != null) t.printStackTrace(stream);
}

// -- LogService methods --

@Override
public void debug(Object msg) {
log(DEBUG, msg, null);
public void debug(final Object msg) {
if (isDebug()) log(DEBUG, msg, null);
}

@Override
public void debug(Throwable t) {
log(DEBUG, null, t);
public void debug(final Throwable t) {
if (isDebug()) log(DEBUG, null, t);
}

@Override
public void debug(Object msg, Throwable t) {
log(DEBUG, msg, t);
public void debug(final Object msg, final Throwable t) {
if (isDebug()) log(DEBUG, msg, t);
}

@Override
public void error(Object msg) {
log(ERROR, msg, null);
public void error(final Object msg) {
if (isError()) log(ERROR, msg, null);
}

@Override
public void error(Throwable t) {
log(ERROR, null, t);
public void error(final Throwable t) {
if (isError()) log(ERROR, null, t);
}

@Override
public void error(Object msg, Throwable t) {
log(ERROR, msg, t);
public void error(final Object msg, final Throwable t) {
if (isError()) log(ERROR, msg, t);
}

@Override
public void info(Object msg) {
log(INFO, msg, null);
public void info(final Object msg) {
if (isInfo()) log(INFO, msg, null);
}

@Override
public void info(Throwable t) {
log(INFO, null, t);
public void info(final Throwable t) {
if (isInfo()) log(INFO, null, t);
}

@Override
public void info(Object msg, Throwable t) {
log(INFO, msg, t);
public void info(final Object msg, final Throwable t) {
if (isInfo()) log(INFO, msg, t);
}

@Override
public void trace(Object msg) {
log(TRACE, msg, null);
public void trace(final Object msg) {
if (isTrace()) log(TRACE, msg, null);
}

@Override
public void trace(Throwable t) {
log(TRACE, null, t);
public void trace(final Throwable t) {
if (isTrace()) log(TRACE, null, t);
}

@Override
public void trace(Object msg, Throwable t) {
log(TRACE, msg, t);
public void trace(final Object msg, final Throwable t) {
if (isTrace()) log(TRACE, msg, t);
}

@Override
public void warn(Object msg) {
log(WARN, msg, null);
public void warn(final Object msg) {
if (isWarn()) log(WARN, msg, null);
}

@Override
public void warn(Throwable t) {
log(WARN, null, t);
public void warn(final Throwable t) {
if (isWarn()) log(WARN, null, t);
}

@Override
public void warn(Object msg, Throwable t) {
log(WARN, msg, t);
public void warn(final Object msg, final Throwable t) {
if (isWarn()) log(WARN, msg, t);
}

@Override
Expand Down Expand Up @@ -248,13 +231,30 @@ public void setLevel(final int level) {
currentLevel = level;
}

//@Override
@Override
public void setLevel(final String classOrPackageName, final int level) {
classAndPackageLevels.put(classOrPackageName, level);
}

// -- Helper methods --

private String getPrefix(final int level) {
switch (level) {
case ERROR:
return "[ERROR] ";
case WARN:
return "[WARNING] ";
case INFO:
return "[INFO] ";
case DEBUG:
return "[DEBUG] ";
case TRACE:
return "[TRACE] ";
default:
return "";
}
}

/** Extracts the log level value from a string. */
private int level(final String logProp) {
if (logProp == null) return -1;
Expand Down Expand Up @@ -289,7 +289,7 @@ private String callingClass() {
}

private String parentPackage(final String classOrPackageName) {
int dot = classOrPackageName.lastIndexOf(".");
final int dot = classOrPackageName.lastIndexOf(".");
if (dot < 0) return null;
return classOrPackageName.substring(0, dot);
}
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/org/scijava/log/DefaultLogService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* #%L
* SciJava Common shared library for SciJava software.
* %%
* Copyright (C) 2009 - 2015 Board of Regents of the University of
* Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck
* Institute of Molecular Cell Biology and Genetics.
* %%
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
* #L%
*/

package org.scijava.log;

import org.scijava.Priority;
import org.scijava.plugin.Plugin;
import org.scijava.service.Service;

/**
* Default implementation of {@link LogService}.
* <p>
* It uses the standard output and error streams, logging critical messages (
* {@link LogService#WARN} and {@link LogService#ERROR} levels) to
* {@code stderr}, and non-critical messages ({@link LogService#INFO},
* {@link LogService#DEBUG} and {@link LogService#TRACE} levels) to
* {@code stdout}.
* </p>
*
* @author Curtis Rueden
*/
@Plugin(type = Service.class, priority = Priority.LOW_PRIORITY)
public class DefaultLogService extends AbstractLogService {

@Override
protected void log(final int level, final Object msg, final Throwable t) {
log(level > WARN ? System.out : System.err, level, msg, t);
}

}
2 changes: 2 additions & 0 deletions src/main/java/org/scijava/log/LogService.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,4 +100,6 @@ public interface LogService extends SciJavaService {

void setLevel(int level);

void setLevel(String classOrPackageName, int level);

}
4 changes: 2 additions & 2 deletions src/main/java/org/scijava/script/AbstractScriptEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@
import javax.script.ScriptEngineFactory;
import javax.script.ScriptException;

import org.scijava.log.DefaultLogService;
import org.scijava.log.LogService;
import org.scijava.log.StderrLogService;

/**
* This class implements dummy versions for ScriptEngine's methods that are not
Expand Down Expand Up @@ -70,7 +70,7 @@ public abstract class AbstractScriptEngine implements ScriptEngine {

public synchronized LogService log() {
if (log == null) {
log = new StderrLogService();
log = new DefaultLogService();
}
return log;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/scijava/script/History.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@

import org.scijava.prefs.PrefService;
import org.scijava.util.LastRecentlyUsed;
import org.scijava.util.Prefs;

/**
* Container for a script language's interpreter history.
Expand Down
Loading