1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 package org.apache.openoffice.comp.sdbc.dbtools.comphelper; 22 23 import java.io.PrintWriter; 24 import java.io.StringWriter; 25 26 import com.sun.star.logging.LogLevel; 27 import com.sun.star.logging.XLogHandler; 28 import com.sun.star.logging.XLogger; 29 import com.sun.star.logging.XLoggerPool; 30 import com.sun.star.uno.DeploymentException; 31 import com.sun.star.uno.UnoRuntime; 32 import com.sun.star.uno.XComponentContext; 33 34 public class EventLogger { 35 protected XComponentContext context; 36 private String loggerName; 37 private XLogger logger; 38 getLoggerPool(XComponentContext context)39 public static XLoggerPool getLoggerPool(XComponentContext context) { 40 Object loggerPoolObject = context.getValueByName("/singletons/com.sun.star.logging.LoggerPool"); 41 XLoggerPool loggerPool = UnoRuntime.queryInterface(XLoggerPool.class, loggerPoolObject); 42 if (loggerPool == null) { 43 throw new DeploymentException( 44 "component context fails to supply singleton com.sun.star.logging.LoggerPool of type com.sun.star.logging.XLoggerPool", 45 context); 46 } 47 return loggerPool; 48 } 49 EventLogger(XComponentContext context)50 public EventLogger(XComponentContext context) { 51 this(context, ""); 52 } 53 54 /** 55 * Creates an <code>EventLogger</code> instance working with a css.logging.XLogger 56 * instance given by name. 57 * 58 * @param context 59 * the component context to create services. 60 * @param loggerName 61 * the name of the logger to create. If empty, the office-wide default logger will be used. 62 */ EventLogger(XComponentContext context, String loggerName)63 public EventLogger(XComponentContext context, String loggerName) { 64 this.context = context; 65 this.loggerName = loggerName; 66 67 try { 68 XLoggerPool loggerPool = getLoggerPool(context); 69 if (!loggerName.isEmpty()) { 70 logger = loggerPool.getNamedLogger(loggerName); 71 } else { 72 logger = loggerPool.getDefaultLogger(); 73 } 74 } catch (com.sun.star.uno.RuntimeException exception) { 75 } 76 } 77 78 /** 79 * Returns the name of the logger 80 */ getName()81 public String getName() { 82 return loggerName; 83 } 84 85 /// Returns the current log level threshold of the logger. getLogLevel()86 public int getLogLevel() { 87 try { 88 if (logger != null) { 89 return logger.getLevel(); 90 } 91 } catch (com.sun.star.uno.RuntimeException exception) { 92 } 93 return LogLevel.OFF; 94 } 95 96 /// Sets a new log level threshold of the logger. setLogLevel(int logLevel)97 void setLogLevel(int logLevel) { 98 try { 99 if (logger != null) { 100 logger.setLevel(logLevel); 101 } 102 } catch (com.sun.star.uno.RuntimeException exception) { 103 } 104 } 105 106 /// Determines whether an event with the given level would be logged. isLoggable(int logLevel)107 public boolean isLoggable(int logLevel) { 108 if (logger == null) { 109 return false; 110 } 111 112 try { 113 return logger.isLoggable(logLevel); 114 } catch (com.sun.star.uno.RuntimeException exception) { 115 } 116 117 return false; 118 } 119 120 /** 121 * Adds the given log handler to the logger's set of handlers. 122 * 123 * Note that normally, you would not use this method: The logger implementations 124 * initialize themselves from the configuration, where usually, a default log handler 125 * is specified. In this case, the logger will create and use this handler. 126 * 127 * @return 128 * true if and only if the addition was successful (as far as this can be detected 129 * from outside the <code>XLogger</code>'s implementation. 130 */ addLogHandler(XLogHandler logHandler)131 public boolean addLogHandler(XLogHandler logHandler) { 132 try { 133 if (logger != null) { 134 logger.addLogHandler(logHandler); 135 return true; 136 } 137 } catch (com.sun.star.uno.RuntimeException exception) { 138 } 139 return false; 140 } 141 142 /** removes the given log handler from the logger's set of handlers. 143 * 144 * @return 145 * true if and only if the addition was successful (as far as this can be detected 146 * from outside the <code>XLogger</code>'s implementation. 147 */ removeLogHandler(XLogHandler logHandler)148 public boolean removeLogHandler(XLogHandler logHandler) { 149 try { 150 if (logger != null) { 151 logger.removeLogHandler(logHandler); 152 return true; 153 } 154 } catch (com.sun.star.uno.RuntimeException exception) { 155 } 156 return false; 157 } 158 159 /** 160 * Logs a given message with its arguments, without the caller's class and method. 161 * @param logLevel the log level 162 * @param message the message to log 163 * @param arguments the arguments to log, which are converted to strings and replace $1$, $2$, up to $n$ in the message 164 * @return whether logging succeeded 165 */ log(int logLevel, String message, Object... arguments)166 public boolean log(int logLevel, String message, Object... arguments) { 167 if (isLoggable(logLevel)) 168 return impl_log(logLevel, null, null, message, arguments); 169 return false; 170 } 171 172 /** 173 * Logs the given exception. 174 * @param logLevel the log level 175 * @param exception the exception 176 * @return whether logging succeeded 177 */ log(int logLevel, Throwable exception)178 public boolean log(int logLevel, Throwable exception) { 179 return log(logLevel, "", exception); 180 } 181 182 /** 183 * Logs the given message and exception. 184 * @param logLevel the log level 185 * @param message the message 186 * @param exception the exception 187 * @return whether logging succeeded 188 */ log(int logLevel, String message, Throwable exception)189 public boolean log(int logLevel, String message, Throwable exception) { 190 if (isLoggable(logLevel)) { 191 StringWriter stringWriter = new StringWriter(); 192 PrintWriter printerWriter = new PrintWriter(stringWriter); 193 exception.printStackTrace(printerWriter); 194 message += "\n" + stringWriter.getBuffer().toString(); 195 return impl_log(logLevel, null, null, message); 196 } 197 return true; 198 } 199 200 /** 201 * Logs a given message with its arguments, with the caller's class and method 202 * taken from a (relatively costly!) stack trace. 203 * @param logLevel the log level 204 * @param message the message to log 205 * @param arguments the arguments to log, which are converted to strings and replace $1$, $2$, up to $n$ in the message 206 * @return whether logging succeeded 207 */ logp(int logLevel, String message, Object...arguments)208 public boolean logp(int logLevel, String message, Object...arguments) { 209 if (isLoggable(logLevel)) { 210 StackTraceElement caller = Thread.currentThread().getStackTrace()[2]; 211 return impl_log(logLevel, caller.getClassName(), caller.getMethodName(), message, arguments); 212 } 213 return false; 214 } 215 impl_log(int logLevel, String sourceClass, String sourceMethod, String message, Object... arguments)216 protected boolean impl_log(int logLevel, String sourceClass, String sourceMethod, String message, Object... arguments) { 217 if (logger == null) { 218 return false; 219 } 220 try { 221 for (int i = 0; i < arguments.length; i++) { 222 String placeholder = "$" + (i+1) + "$"; 223 int position = message.indexOf(placeholder); 224 if (position >= 0) { 225 message = message.substring(0, position) + arguments[i].toString() + 226 message.substring(position + placeholder.length()); 227 } 228 } 229 230 if (sourceClass != null && sourceMethod != null) { 231 logger.logp(logLevel, sourceClass, sourceMethod, message); 232 } else { 233 logger.log(logLevel, message); 234 } 235 } catch (com.sun.star.uno.RuntimeException exception) { 236 } 237 return true; 238 } 239 } 240