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 com.sun.star.lang.NullPointerException; 24 import com.sun.star.uno.XComponentContext; 25 26 public class ResourceBasedEventLogger extends EventLogger { 27 private String resourceBundleBaseName; 28 private OfficeResourceBundle resourceBundle; 29 ResourceBasedEventLogger(XComponentContext context, String resourceBundleBaseName, String loggerName)30 public ResourceBasedEventLogger(XComponentContext context, String resourceBundleBaseName, String loggerName) { 31 super(context, loggerName); 32 this.resourceBundleBaseName = resourceBundleBaseName; 33 try { 34 resourceBundle = new OfficeResourceBundle(context, resourceBundleBaseName); 35 } catch (NullPointerException nullPointerException) { 36 throw new RuntimeException(nullPointerException); 37 } 38 } 39 ResourceBasedEventLogger(ResourceBasedEventLogger logger)40 public ResourceBasedEventLogger(ResourceBasedEventLogger logger) { 41 super(logger.context, logger.getName()); 42 this.resourceBundleBaseName = logger.resourceBundleBaseName; 43 try { 44 resourceBundle = new OfficeResourceBundle(logger.context, logger.resourceBundleBaseName); 45 } catch (NullPointerException nullPointerException) { 46 throw new RuntimeException(nullPointerException); 47 } 48 } 49 50 /** 51 * Logs a given message with its arguments, without the caller's class and method. 52 * @param logLevel the log level 53 * @param messageResID the resource ID of the message to log 54 * @param arguments the arguments to log, which are converted to strings and replace $1$, $2$, up to $n$ in the message 55 * @return whether logging succeeded 56 */ log(int logLevel, int messageResID, Object... arguments)57 public boolean log(int logLevel, int messageResID, Object... arguments) { 58 if (isLoggable(logLevel)) 59 return impl_log(logLevel, null, null, loadStringMessage(messageResID), arguments); 60 return false; 61 } 62 63 /** 64 * Logs a given message with its arguments, with the caller's class and method 65 * taken from a (relatively costly!) stack trace. 66 * @param logLevel the log level 67 * @param messageResID the resource ID of the message to log 68 * @param arguments the arguments to log, which are converted to strings and replace $1$, $2$, up to $n$ in the message 69 * @return whether logging succeeded 70 */ logp(int logLevel, int messageResID, Object... arguments)71 public boolean logp(int logLevel, int messageResID, Object... arguments) { 72 if (isLoggable(logLevel)) { 73 StackTraceElement caller = Thread.currentThread().getStackTrace()[2]; 74 return impl_log(logLevel, caller.getClassName(), caller.getMethodName(), loadStringMessage(messageResID), arguments); 75 } 76 return false; 77 } 78 loadStringMessage(int messageResID)79 private String loadStringMessage(int messageResID) { 80 String message = resourceBundle.loadString(messageResID); 81 if (message.isEmpty()) { 82 message = String.format("<invalid event resource: '%s:%d'>", resourceBundleBaseName, messageResID); 83 } 84 return message; 85 } 86 } 87