Skip to content

Commit

Permalink
Merge pull request #2075 from joshmoore/plugin-logger-impl
Browse files Browse the repository at this point in the history
Separate PluginLoggerImpl from LoggerImpl
  • Loading branch information
joshmoore committed Feb 10, 2014
2 parents d1e5251 + 9680f71 commit 24d1283
Show file tree
Hide file tree
Showing 3 changed files with 200 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

//Application-internal dependencies
import org.openmicroscopy.shoola.env.Container;
import org.openmicroscopy.shoola.env.Environment;
import org.openmicroscopy.shoola.env.LookupNames;
import org.openmicroscopy.shoola.env.config.Registry;

Expand Down Expand Up @@ -104,7 +103,11 @@ public static Logger makeNew(Container c)
Integer v = (Integer) reg.lookup(LookupNames.PLUGIN);
int value = -1;
if (v != null) value = v.intValue();
return new LoggerImpl(relPathName, logFile.getAbsolutePath(), value);
if (value < 0) {
return new LoggerImpl(relPathName, logFile.getAbsolutePath());
} else {
return new PluginLoggerImpl(value);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,6 @@ class LoggerImpl

/** The absolute pathname of the log file.*/
private String absFile;

/** Value identifying the plugin or <code>-1</code>.*/
private int runAsPlugin;

/**
* Returns the <i>Log4j</i> logger for the specified object.
Expand All @@ -82,55 +79,36 @@ private org.slf4j.Logger getAdaptee(Object target)
return org.slf4j.LoggerFactory.getLogger(org.slf4j.Logger.ROOT_LOGGER_NAME);
}

/**
* Handles the error if run as a plug-in.
*
* @param logMsg The message to handle.
*/
private void handlePlugin(String logMsg)
{
if (runAsPlugin == LookupNames.IMAGE_J && IJ.debugMode) {
IJ.log(logMsg);
}
}

/**
* Initializes slf4j.
*
* @param configFile The pathname of a configuration file.
* @param absFile The absolute pathname of the log file.
* @param runAsPlugin Value identifying the plugin or <code>-1</code>.
*/
LoggerImpl(String configFile, String absFile, int runAsPlugin)
LoggerImpl(String configFile, String absFile)
{
this.runAsPlugin = runAsPlugin;
if (runAsPlugin < 0) {
LoggerContext context = (LoggerContext)
org.slf4j.LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
context.putProperty(LOG_FILE_NAME, absFile);
configurator.doConfigure(configFile);
} catch (JoranException je) {
// StatusPrinter will handle this
}
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
LoggerContext context = (LoggerContext)
org.slf4j.LoggerFactory.getILoggerFactory();
try {
JoranConfigurator configurator = new JoranConfigurator();
configurator.setContext(context);
context.reset();
context.putProperty(LOG_FILE_NAME, absFile);
configurator.doConfigure(configFile);
} catch (JoranException je) {
// StatusPrinter will handle this
}
StatusPrinter.printInCaseOfErrorsOrWarnings(context);
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#debug(Object, String)
*/
public void debug(Object c, String logMsg)
{
if (runAsPlugin < 0) {
getAdaptee(c).debug(logMsg);
} else {
handlePlugin(logMsg);
}
getAdaptee(c).debug(logMsg);
}

/**
Expand All @@ -139,11 +117,7 @@ public void debug(Object c, String logMsg)
*/
public void debug(Object c, LogMessage msg)
{
if (runAsPlugin < 0) {
getAdaptee(c).debug(msg == null ? null : msg.toString());
} else {
handlePlugin(msg == null ? null : msg.toString());
}
getAdaptee(c).debug(msg == null ? null : msg.toString());
}

/**
Expand All @@ -152,11 +126,7 @@ public void debug(Object c, LogMessage msg)
*/
public void error(Object c, String logMsg)
{
if (runAsPlugin < 0) {
getAdaptee(c).error(logMsg);
} else {
handlePlugin(logMsg);
}
getAdaptee(c).error(logMsg);
}

/**
Expand All @@ -165,11 +135,7 @@ public void error(Object c, String logMsg)
*/
public void error(Object c, LogMessage msg)
{
if (runAsPlugin < 0) {
getAdaptee(c).error(msg == null ? null : msg.toString());
} else {
handlePlugin(msg == null ? null : msg.toString());
}
getAdaptee(c).error(msg == null ? null : msg.toString());
}

/**
Expand All @@ -178,11 +144,7 @@ public void error(Object c, LogMessage msg)
*/
public void fatal(Object c, String logMsg)
{
if (runAsPlugin < 0) {
getAdaptee(c).error(logMsg);
} else {
handlePlugin(logMsg);
}
getAdaptee(c).error(logMsg);
}

/**
Expand All @@ -191,11 +153,7 @@ public void fatal(Object c, String logMsg)
*/
public void fatal(Object c, LogMessage msg)
{
if (runAsPlugin < 0) {
getAdaptee(c).error(msg == null ? null : msg.toString());
} else {
handlePlugin(msg == null ? null : msg.toString());
}
getAdaptee(c).error(msg == null ? null : msg.toString());
}

/**
Expand All @@ -204,11 +162,7 @@ public void fatal(Object c, LogMessage msg)
*/
public void info(Object c, String logMsg)
{
if (runAsPlugin < 0) {
getAdaptee(c).info(logMsg);
} else {
handlePlugin(logMsg);
}
getAdaptee(c).info(logMsg);
}

/**
Expand All @@ -217,11 +171,7 @@ public void info(Object c, String logMsg)
*/
public void info(Object c, LogMessage msg)
{
if (runAsPlugin < 0) {
getAdaptee(c).info(msg == null ? null : msg.toString());
} else {
handlePlugin(msg == null ? null : msg.toString());
}
getAdaptee(c).info(msg == null ? null : msg.toString());
}

/**
Expand All @@ -230,11 +180,7 @@ public void info(Object c, LogMessage msg)
*/
public void warn(Object c, String logMsg)
{
if (runAsPlugin < 0) {
getAdaptee(c).warn(logMsg);
} else {
handlePlugin(logMsg);
}
getAdaptee(c).warn(logMsg);
}

/**
Expand All @@ -243,11 +189,7 @@ public void warn(Object c, String logMsg)
*/
public void warn(Object c, LogMessage msg)
{
if (runAsPlugin < 0) {
getAdaptee(c).warn(msg == null ? null : msg.toString());
} else {
handlePlugin(msg == null ? null : msg.toString());
}
getAdaptee(c).warn(msg == null ? null : msg.toString());
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
/*
* org.openmicroscopy.shoola.env.log.PluginLoggerImpl
*
*------------------------------------------------------------------------------
* Copyright (C) 2014 University of Dundee. All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
*------------------------------------------------------------------------------
*/

package org.openmicroscopy.shoola.env.log;


//Java imports

//Third-party libraries
import ij.IJ;

//Application-internal dependencies
import org.openmicroscopy.shoola.env.LookupNames;


/**
* Provides the log service for cases where execution
* is taking place as a plugin.
*
* This is just a simple adapter that forwards calls to <i>slf4j</i>.
* Thread-safety is already enforced by <i>slf4j</i>, so we don't deal with it.
*
* @since 5.0.0
*/

class PluginLoggerImpl
implements Logger
{

/** Value identifying the plugin or <code>-1</code>.*/
private int runAsPlugin;

/**
* Handles the error if run as a plug-in.
*
* @param logMsg The message to handle.
*/
private void handlePlugin(String logMsg)
{
if (runAsPlugin == LookupNames.IMAGE_J && IJ.debugMode) {
IJ.log(logMsg);
}
}

/**
* Initializes slf4j.
*
* @param configFile The pathname of a configuration file.
* @param absFile The absolute pathname of the log file.
* @param runAsPlugin Value identifying the plugin or <code>-1</code>.
*/
PluginLoggerImpl(int runAsPlugin)
{
this.runAsPlugin = runAsPlugin;
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#debug(Object, String)
*/
public void debug(Object c, String logMsg)
{
handlePlugin(logMsg);
}

/**
* Implemented as specified by {@link Logger}
* @see Logger#debug(Object, LogMessage)
*/
public void debug(Object c, LogMessage msg)
{
handlePlugin(msg == null ? null : msg.toString());
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#error(Object, String)
*/
public void error(Object c, String logMsg)
{
handlePlugin(logMsg);
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#error(Object, LogMessage)
*/
public void error(Object c, LogMessage msg)
{
handlePlugin(msg == null ? null : msg.toString());
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#fatal(Object, String)
*/
public void fatal(Object c, String logMsg)
{
handlePlugin(logMsg);
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#fatal(Object, LogMessage)
*/
public void fatal(Object c, LogMessage msg)
{
handlePlugin(msg == null ? null : msg.toString());
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#info(Object, String)
*/
public void info(Object c, String logMsg)
{
handlePlugin(logMsg);
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#info(Object, LogMessage)
*/
public void info(Object c, LogMessage msg)
{
handlePlugin(msg == null ? null : msg.toString());
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#warn(Object, String)
*/
public void warn(Object c, String logMsg)
{
handlePlugin(logMsg);
}

/**
* Implemented as specified by {@link Logger}.
* @see Logger#warn(Object, LogMessage)
*/
public void warn(Object c, LogMessage msg)
{
handlePlugin(msg == null ? null : msg.toString());
}

@Override
public String getLogFile() {
return null;
}

}

0 comments on commit 24d1283

Please sign in to comment.