Pierre De Rop | 96c881f | 2013-07-06 08:35:29 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | * or more contributor license agreements. See the NOTICE file |
| 4 | * distributed with this work for additional information |
| 5 | * regarding copyright ownership. The ASF licenses this file |
| 6 | * to you under the Apache License, Version 2.0 (the |
| 7 | * "License"); you may not use this file except in compliance |
| 8 | * with the License. You may obtain a copy of the License at |
| 9 | * |
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | * |
| 12 | * Unless required by applicable law or agreed to in writing, |
| 13 | * software distributed under the License is distributed on an |
| 14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | * KIND, either express or implied. See the License for the |
| 16 | * specific language governing permissions and limitations |
| 17 | * under the License. |
| 18 | */ |
| 19 | package org.apache.felix.scrplugin.bnd; |
| 20 | |
| 21 | import java.io.File; |
| 22 | import java.io.FileWriter; |
| 23 | import java.io.IOException; |
| 24 | import java.io.PrintWriter; |
| 25 | import java.io.StringWriter; |
| 26 | import java.text.SimpleDateFormat; |
| 27 | import java.util.Date; |
| 28 | |
| 29 | import org.apache.felix.scrplugin.Log; |
| 30 | |
| 31 | import aQute.service.reporter.Reporter; |
| 32 | |
| 33 | /** |
| 34 | * Scrplugin Log implementation, which redirects log to both bnd "Reporter" and |
| 35 | * to /tmp/scrplugin/<BundleSymbolicName>.log |
| 36 | */ |
| 37 | public class BndLog implements Log { |
| 38 | /** |
| 39 | * The BndLib logging reported. |
| 40 | */ |
| 41 | private final Reporter reporter; |
| 42 | |
| 43 | /** |
| 44 | * Enabled log level, which can be configured in bnd scrplugin declaration. |
| 45 | */ |
| 46 | private Level logEnabled = Level.Warn; |
| 47 | |
| 48 | /** |
| 49 | * Writer to log file, in tmp dir/scrplugin-BSN.log |
| 50 | */ |
| 51 | private final PrintWriter logWriter; |
| 52 | |
| 53 | /** |
| 54 | * DateFormat used when logging. |
| 55 | */ |
| 56 | private final static SimpleDateFormat dateFormat = new SimpleDateFormat( |
| 57 | "E yyyy.MM.dd hh:mm:ss.S"); |
| 58 | |
| 59 | /** |
| 60 | * Log Levels. |
| 61 | */ |
| 62 | enum Level { |
| 63 | Error, Warn, Info, Debug |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Creates a new bnd Log implementaiton |
| 68 | * |
| 69 | * @param reporter |
| 70 | * the bnd logger |
| 71 | * @param logLevel |
| 72 | * @param bsn |
| 73 | */ |
| 74 | BndLog(Reporter reporter, String bsn) { |
| 75 | this.reporter = reporter; |
| 76 | File logFilePath = new File(System.getProperty("java.io.tmpdir") |
| 77 | + File.separator + "scrplugin" + File.separator + bsn + ".log"); |
| 78 | new File(logFilePath.getParent()).mkdirs(); |
| 79 | |
| 80 | PrintWriter writer = null; |
| 81 | try { |
| 82 | writer = new PrintWriter(new FileWriter(logFilePath, false)); |
| 83 | } catch (IOException e) { |
| 84 | reporter.exception(e, "Could not create scrplugin log file: %s", |
| 85 | logFilePath); |
| 86 | writer = null; |
| 87 | } |
| 88 | this.logWriter = writer; |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Close log file. |
| 93 | */ |
| 94 | public void close() { |
| 95 | if (logWriter != null) { |
| 96 | logWriter.close(); |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Sets the enabled log level. |
| 102 | * |
| 103 | * @param level |
| 104 | * the enabled level ("Error", "Warn", "Info", or "Debug") |
| 105 | */ |
| 106 | public void setLevel(String level) { |
| 107 | try { |
| 108 | level = Character.toUpperCase(level.charAt(0)) |
| 109 | + level.substring(1).toLowerCase(); |
| 110 | this.logEnabled = Level.valueOf(level); |
| 111 | } catch (IllegalArgumentException e) { |
| 112 | this.logEnabled = Level.Warn; |
| 113 | warn("Bnd scrplugin logger initialized with invalid log level: " |
| 114 | + level); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | // Reporter |
| 119 | |
| 120 | public boolean isDebugEnabled() { |
| 121 | return logEnabled.ordinal() >= Level.Debug.ordinal(); |
| 122 | } |
| 123 | |
| 124 | public boolean isInfoEnabled() { |
| 125 | return logEnabled.ordinal() >= Level.Info.ordinal(); |
| 126 | } |
| 127 | |
| 128 | public boolean isWarnEnabled() { |
| 129 | return logEnabled.ordinal() >= Level.Warn.ordinal(); |
| 130 | } |
| 131 | |
| 132 | public boolean isErrorEnabled() { |
| 133 | return logEnabled.ordinal() >= Level.Error.ordinal(); |
| 134 | } |
| 135 | |
| 136 | public void debug(String content) { |
| 137 | if (isDebugEnabled()) { |
| 138 | reporter.trace("%s", content); |
| 139 | logDebug(content, null); |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | public void debug(String content, Throwable error) { |
| 144 | if (isDebugEnabled()) { |
| 145 | reporter.trace("%s:%s", content, toString(error)); |
| 146 | logDebug(content, error); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | public void debug(Throwable error) { |
| 151 | if (isDebugEnabled()) { |
| 152 | reporter.trace("%s", toString(error)); |
| 153 | logDebug("exception", error); |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | public void info(String content) { |
| 158 | if (isInfoEnabled()) { |
| 159 | reporter.trace("%s", content); |
| 160 | logInfo(content, null); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | public void info(String content, Throwable error) { |
| 165 | if (isInfoEnabled()) { |
| 166 | reporter.trace("%s:%s", content, toString(error)); |
| 167 | logInfo(content, error); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | public void info(Throwable error) { |
| 172 | if (isInfoEnabled()) { |
| 173 | reporter.trace("%s", toString(error)); |
| 174 | logInfo("exception:", error); |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | public void warn(String content) { |
| 179 | if (isWarnEnabled()) { |
| 180 | reporter.warning("%s", content); |
| 181 | logWarn(content, null); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | public void warn(String content, Throwable error) { |
| 186 | if (isWarnEnabled()) { |
| 187 | reporter.warning("%s:%s", content, toString(error)); |
| 188 | logWarn(content, error); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | public void warn(Throwable error) { |
| 193 | if (isWarnEnabled()) { |
| 194 | reporter.warning("%s", toString(error)); |
| 195 | logWarn("exception:", error); |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | public void warn(String content, String location, int lineNumber) { |
| 200 | warn(String.format("%s [%s,%d]", content, location, lineNumber)); |
| 201 | } |
| 202 | |
| 203 | public void warn(String content, String location, int lineNumber, |
| 204 | int columNumber) { |
| 205 | warn(String.format("%s [%s,%d:%d]", content, location, lineNumber, |
| 206 | columNumber)); |
| 207 | } |
| 208 | |
| 209 | public void error(String content) { |
| 210 | reporter.error("%s", content); |
| 211 | logErr(content, null); |
| 212 | } |
| 213 | |
| 214 | public void error(String content, Throwable error) { |
| 215 | reporter.error("%s:%s", content, toString(error)); |
| 216 | logErr(content, error); |
| 217 | } |
| 218 | |
| 219 | public void error(Throwable error) { |
| 220 | reporter.error("%s", toString(error)); |
| 221 | logErr("exception:", error); |
| 222 | } |
| 223 | |
| 224 | public void error(String content, String location, int lineNumber) { |
| 225 | error(String.format("%s [%s,%d]", content, location, lineNumber)); |
| 226 | } |
| 227 | |
| 228 | public void error(String content, String location, int lineNumber, |
| 229 | int columnNumber) { |
| 230 | error(String.format("%s [%s,%d:%d]", content, location, lineNumber, |
| 231 | columnNumber)); |
| 232 | } |
| 233 | |
| 234 | private void logErr(String msg, Throwable t) { |
| 235 | log(Level.Error, msg, t); |
| 236 | } |
| 237 | |
| 238 | private void logWarn(String msg, Throwable t) { |
| 239 | log(Level.Warn, msg, t); |
| 240 | } |
| 241 | |
| 242 | private void logInfo(String msg, Throwable t) { |
| 243 | log(Level.Info, msg, t); |
| 244 | } |
| 245 | |
| 246 | private void logDebug(String msg, Throwable t) { |
| 247 | log(Level.Debug, msg, t); |
| 248 | } |
| 249 | |
| 250 | private void log(Level level, String msg, Throwable t) { |
| 251 | if (logWriter != null) { |
| 252 | StringBuilder sb = new StringBuilder(); |
| 253 | sb.append(dateFormat.format(new Date())); |
| 254 | sb.append(" - "); |
| 255 | sb.append(level); |
| 256 | sb.append(": "); |
| 257 | sb.append(msg); |
| 258 | if (t != null) { |
| 259 | sb.append(" - ").append(toString(t)); |
| 260 | } |
| 261 | logWriter.println(sb.toString()); |
| 262 | } |
| 263 | } |
| 264 | |
| 265 | private static String toString(Throwable e) { |
| 266 | StringWriter buffer = new StringWriter(); |
| 267 | PrintWriter pw = new PrintWriter(buffer); |
| 268 | e.printStackTrace(pw); |
| 269 | return (buffer.toString()); |
| 270 | } |
| 271 | } |