diff --git a/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerFactory.java b/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerFactory.java index 9be14373d3d..269c5486f31 100644 --- a/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerFactory.java +++ b/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerFactory.java @@ -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; @@ -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); + } } /** diff --git a/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerImpl.java b/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerImpl.java index 018bdd99f48..0f7b523d5b6 100644 --- a/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerImpl.java +++ b/components/insight/SRC/org/openmicroscopy/shoola/env/log/LoggerImpl.java @@ -63,9 +63,6 @@ class LoggerImpl /** The absolute pathname of the log file.*/ private String absFile; - - /** Value identifying the plugin or -1.*/ - private int runAsPlugin; /** * Returns the Log4j logger for the specified object. @@ -82,18 +79,6 @@ 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. * @@ -101,36 +86,29 @@ private void handlePlugin(String logMsg) * @param absFile The absolute pathname of the log file. * @param runAsPlugin Value identifying the plugin or -1. */ - 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); } /** @@ -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()); } /** @@ -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); } /** @@ -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()); } /** @@ -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); } /** @@ -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()); } /** @@ -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); } /** @@ -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()); } /** @@ -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); } /** @@ -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()); } /** diff --git a/components/insight/SRC/org/openmicroscopy/shoola/env/log/PluginLoggerImpl.java b/components/insight/SRC/org/openmicroscopy/shoola/env/log/PluginLoggerImpl.java new file mode 100644 index 00000000000..b983a2c093b --- /dev/null +++ b/components/insight/SRC/org/openmicroscopy/shoola/env/log/PluginLoggerImpl.java @@ -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 slf4j. + * Thread-safety is already enforced by slf4j, so we don't deal with it. + * + * @since 5.0.0 + */ + +class PluginLoggerImpl + implements Logger +{ + + /** Value identifying the plugin or -1.*/ + 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 -1. + */ + 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; + } + +}