|
| 1 | +/* |
| 2 | + * #%L |
| 3 | + * SciJava Common shared library for SciJava software. |
| 4 | + * %% |
| 5 | + * Copyright (C) 2009 - 2013 Board of Regents of the University of |
| 6 | + * Wisconsin-Madison, Broad Institute of MIT and Harvard, and Max Planck |
| 7 | + * Institute of Molecular Cell Biology and Genetics. |
| 8 | + * %% |
| 9 | + * Redistribution and use in source and binary forms, with or without |
| 10 | + * modification, are permitted provided that the following conditions are met: |
| 11 | + * |
| 12 | + * 1. Redistributions of source code must retain the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer. |
| 14 | + * 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | + * this list of conditions and the following disclaimer in the documentation |
| 16 | + * and/or other materials provided with the distribution. |
| 17 | + * |
| 18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 19 | + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE |
| 22 | + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 23 | + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 24 | + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 25 | + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 26 | + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 27 | + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 28 | + * POSSIBILITY OF SUCH DAMAGE. |
| 29 | + * |
| 30 | + * The views and conclusions contained in the software and documentation are |
| 31 | + * those of the authors and should not be interpreted as representing official |
| 32 | + * policies, either expressed or implied, of any organization. |
| 33 | + * #L% |
| 34 | + */ |
| 35 | + |
| 36 | +package org.scijava.log; |
| 37 | + |
| 38 | +import org.scijava.service.AbstractService; |
| 39 | + |
| 40 | +/** |
| 41 | + * Base implementation of an abstract {@link LogService}. |
| 42 | + * |
| 43 | + * @author Johannes Schindelin |
| 44 | + */ |
| 45 | +public abstract class AbstractLogService extends AbstractService implements LogService { |
| 46 | + private int currentLevel = WARN; |
| 47 | + |
| 48 | + // -- abstract methods -- |
| 49 | + |
| 50 | + /** |
| 51 | + * Displays a message. |
| 52 | + * |
| 53 | + * @param msg the message to display. |
| 54 | + */ |
| 55 | + protected abstract void log(final String msg); |
| 56 | + |
| 57 | + /** |
| 58 | + * Displays an exception. |
| 59 | + * |
| 60 | + * @param t the exception to display. |
| 61 | + */ |
| 62 | + protected abstract void log(final Throwable t); |
| 63 | + |
| 64 | + // -- constructor -- |
| 65 | + |
| 66 | + public AbstractLogService() { |
| 67 | + // check SciJava log level system property for initial logging level |
| 68 | + final String logProp = System.getProperty(LOG_LEVEL_PROPERTY); |
| 69 | + if (logProp != null) { |
| 70 | + // check whether it's a string label (e.g., "debug") |
| 71 | + final String log = logProp.trim().toLowerCase(); |
| 72 | + if (log.startsWith("n")) setLevel(NONE); |
| 73 | + else if (log.startsWith("e")) setLevel(ERROR); |
| 74 | + else if (log.startsWith("w")) setLevel(WARN); |
| 75 | + else if (log.startsWith("i")) setLevel(INFO); |
| 76 | + else if (log.startsWith("d")) setLevel(DEBUG); |
| 77 | + else if (log.startsWith("t")) setLevel(TRACE); |
| 78 | + else { |
| 79 | + // check whether it's a numerical value (e.g., 5) |
| 80 | + try { |
| 81 | + setLevel(Integer.parseInt(log)); |
| 82 | + } |
| 83 | + catch (final NumberFormatException exc) { |
| 84 | + // nope! |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + if (getLevel() == 0) { |
| 90 | + // use the default, which is INFO unless the DEBUG env. variable is set |
| 91 | + setLevel(System.getenv("DEBUG") == null ? INFO : DEBUG); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + // -- helper methods -- |
| 96 | + |
| 97 | + protected void log(final int level, final Object msg, final Throwable t) { |
| 98 | + if (level > currentLevel) return; |
| 99 | + |
| 100 | + if (msg != null || t == null) { |
| 101 | + log(level, msg); |
| 102 | + } |
| 103 | + if (t != null) log(t); |
| 104 | + } |
| 105 | + |
| 106 | + protected void log(final int level, final Object msg) { |
| 107 | + final String prefix = getPrefix(level); |
| 108 | + log((prefix == null ? "" : prefix + " ") + msg); |
| 109 | + } |
| 110 | + |
| 111 | + protected String getPrefix(int level) { |
| 112 | + switch (level) { |
| 113 | + case ERROR: |
| 114 | + return "[ERROR]"; |
| 115 | + case WARN: |
| 116 | + return "[WARNING]"; |
| 117 | + case INFO: |
| 118 | + return "[INFO]"; |
| 119 | + case DEBUG: |
| 120 | + return "[DEBUG]"; |
| 121 | + case TRACE: |
| 122 | + return "[TRACE]"; |
| 123 | + default: |
| 124 | + return null; |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + // -- LogService methods -- |
| 129 | + |
| 130 | + @Override |
| 131 | + public void debug(Object msg) { |
| 132 | + log(DEBUG, msg, null); |
| 133 | + } |
| 134 | + |
| 135 | + @Override |
| 136 | + public void debug(Throwable t) { |
| 137 | + log(DEBUG, null, t); |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void debug(Object msg, Throwable t) { |
| 142 | + log(DEBUG, msg, t); |
| 143 | + } |
| 144 | + |
| 145 | + @Override |
| 146 | + public void error(Object msg) { |
| 147 | + log(ERROR, msg, null); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void error(Throwable t) { |
| 152 | + log(ERROR, null, t); |
| 153 | + } |
| 154 | + |
| 155 | + @Override |
| 156 | + public void error(Object msg, Throwable t) { |
| 157 | + log(ERROR, msg, t); |
| 158 | + } |
| 159 | + |
| 160 | + @Override |
| 161 | + public void info(Object msg) { |
| 162 | + log(INFO, msg, null); |
| 163 | + } |
| 164 | + |
| 165 | + @Override |
| 166 | + public void info(Throwable t) { |
| 167 | + log(INFO, null, t); |
| 168 | + } |
| 169 | + |
| 170 | + @Override |
| 171 | + public void info(Object msg, Throwable t) { |
| 172 | + log(INFO, msg, t); |
| 173 | + } |
| 174 | + |
| 175 | + @Override |
| 176 | + public void trace(Object msg) { |
| 177 | + log(TRACE, msg, null); |
| 178 | + } |
| 179 | + |
| 180 | + @Override |
| 181 | + public void trace(Throwable t) { |
| 182 | + log(TRACE, null, t); |
| 183 | + } |
| 184 | + |
| 185 | + @Override |
| 186 | + public void trace(Object msg, Throwable t) { |
| 187 | + log(TRACE, msg, t); |
| 188 | + } |
| 189 | + |
| 190 | + @Override |
| 191 | + public void warn(Object msg) { |
| 192 | + log(WARN, msg, null); |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + public void warn(Throwable t) { |
| 197 | + log(WARN, null, t); |
| 198 | + } |
| 199 | + |
| 200 | + @Override |
| 201 | + public void warn(Object msg, Throwable t) { |
| 202 | + log(WARN, msg, t); |
| 203 | + } |
| 204 | + |
| 205 | + @Override |
| 206 | + public boolean isDebug() { |
| 207 | + return currentLevel >= DEBUG; |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public boolean isError() { |
| 212 | + return currentLevel >= ERROR; |
| 213 | + } |
| 214 | + |
| 215 | + @Override |
| 216 | + public boolean isInfo() { |
| 217 | + return currentLevel >= INFO; |
| 218 | + } |
| 219 | + |
| 220 | + @Override |
| 221 | + public boolean isTrace() { |
| 222 | + return currentLevel >= TRACE; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public boolean isWarn() { |
| 227 | + return currentLevel >= WARN; |
| 228 | + } |
| 229 | + |
| 230 | + @Override |
| 231 | + public int getLevel() { |
| 232 | + return currentLevel; |
| 233 | + } |
| 234 | + |
| 235 | + @Override |
| 236 | + public void setLevel(int level) { |
| 237 | + currentLevel = level; |
| 238 | + } |
| 239 | +} |
0 commit comments