Author: cziegeler Date: Wed Nov 15 05:43:31 2017 New Revision: 1815290 URL: http://svn.apache.org/viewvc?rev=1815290&view=rev Log: Update logging, make log service optional
Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java (with props) felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java (with props) felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java (with props) felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java (with props) felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java (with props) Removed: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/AbstractLogger.java felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/ConsoleLogger.java felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceLogger.java Modified: felix/trunk/osgi-r7/configurator/pom.xml felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Activator.java felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/SystemLogger.java Modified: felix/trunk/osgi-r7/configurator/pom.xml URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/pom.xml?rev=1815290&r1=1815289&r2=1815290&view=diff ============================================================================== --- felix/trunk/osgi-r7/configurator/pom.xml (original) +++ felix/trunk/osgi-r7/configurator/pom.xml Wed Nov 15 05:43:31 2017 @@ -60,11 +60,13 @@ ${project.artifactId} </Bundle-SymbolicName> <Import-Package> - !org.osgi.service.coordinator, + org.osgi.service.coordinator;resolution:=optional, + org.osgi.service.log;resolution:=optional, * </Import-Package> <DynamicImport-Package> - org.osgi.service.coordinator + org.osgi.service.coordinator;version="[1.0,2)", + org.osgi.service.log;version="[1.3,2)" </DynamicImport-Package> <Provide-Capability> osgi.extender;osgi.extender="osgi.configurator";version:Version="1.0" Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Activator.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Activator.java?rev=1815290&r1=1815289&r2=1815290&view=diff ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Activator.java (original) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/Activator.java Wed Nov 15 05:43:31 2017 @@ -16,7 +16,6 @@ */ package org.apache.felix.configurator.impl; -import org.apache.felix.configurator.impl.logger.LogServiceLogger; import org.apache.felix.configurator.impl.logger.SystemLogger; import org.osgi.framework.BundleActivator; import org.osgi.framework.BundleContext; @@ -27,15 +26,12 @@ import org.osgi.framework.BundleContext; */ public class Activator implements BundleActivator { - private volatile LogServiceLogger logger; - private volatile ServicesListener listener; @Override public final void start(final BundleContext context) throws Exception { - this.logger = new LogServiceLogger(context); - SystemLogger.setLogService(this.logger); + SystemLogger.init(context); listener = new ServicesListener(context); } @@ -47,6 +43,6 @@ public class Activator implements Bundle listener.deactivate(); listener = null; } - this.logger.close(); + SystemLogger.destroy(); } } Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java?rev=1815290&view=auto ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java (added) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java Wed Nov 15 05:43:31 2017 @@ -0,0 +1,24 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.configurator.impl.logger; + +interface InternalLogger { + + void log(int level, String message, Throwable exception); +} Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/InternalLogger.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java?rev=1815290&view=auto ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java (added) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java Wed Nov 15 05:43:31 2017 @@ -0,0 +1,111 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.configurator.impl.logger; + +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceReference; +import org.osgi.util.tracker.ServiceTracker; +import org.osgi.util.tracker.ServiceTrackerCustomizer; + +/** + * This class adds support for using a LogService + */ +public class LogServiceEnabledLogger { + // name of the LogService class (this is a string to not create a reference to the class) + private static final String LOGSERVICE_CLASS = "org.osgi.service.log.LogService"; + + // the log service to log messages to + protected final ServiceTracker<Object, Object> logServiceTracker; + + private volatile InternalLogger currentLogger; + + protected volatile int trackingCount = -2; + + public LogServiceEnabledLogger(final BundleContext bundleContext) { + // Start a tracker for the log service + // we only track a single log service which in reality should be enough + logServiceTracker = new ServiceTracker<>( bundleContext, LOGSERVICE_CLASS, new ServiceTrackerCustomizer<Object, Object>() { + private volatile boolean hasService = false; + + @Override + public Object addingService(final ServiceReference<Object> reference) { + if ( !hasService ) { + final Object logService = bundleContext.getService(reference); + if ( logService != null ) { + hasService = true; + final LogServiceSupport lsl = new LogServiceSupport(logService); + return lsl; + } + } + return null; + } + + @Override + public void modifiedService(final ServiceReference<Object> reference, final Object service) { + // nothing to do + } + + @Override + public void removedService(final ServiceReference<Object> reference, final Object service) { + hasService = false; + bundleContext.ungetService(reference); + } + } ); + logServiceTracker.open(); + } + + /** + * Close the logger + */ + public void close() { + // stop the tracker + logServiceTracker.close(); + } + + /** + * Method to actually emit the log message. If the LogService is available, + * the message will be logged through the LogService. Otherwise the message + * is logged to stdout (or stderr in case of LOG_ERROR level messages), + * + * @param level The log level of the messages. This corresponds to the log + * levels defined by the OSGi LogService. + * @param message The message to print + * @param ex The <code>Throwable</code> causing the message to be logged. + */ + public void log(final int level, final String message, final Throwable ex) { + getLogger().log(level, message, ex); + } + + private InternalLogger getLogger() { + if ( this.trackingCount < this.logServiceTracker.getTrackingCount() ) { + final Object logServiceSupport = this.logServiceTracker.getService(); + if ( logServiceSupport == null ) { + this.currentLogger = this.getDefaultLogger(); + } else { + this.currentLogger = ((LogServiceSupport)logServiceSupport).getLogger(); + } + this.trackingCount = this.logServiceTracker.getTrackingCount(); + } + return currentLogger; + } + + private InternalLogger getDefaultLogger() { + return new StdOutLogger(); + } +} \ No newline at end of file Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceEnabledLogger.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java?rev=1815290&view=auto ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java (added) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java Wed Nov 15 05:43:31 2017 @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.configurator.impl.logger; + +import org.osgi.service.log.LogService; + +/** + * This is a logger based on the LogService. + */ +class LogServiceSupport { + + private final LogService logService; + + public LogServiceSupport(final Object logService) { + this.logService = (LogService) logService; + } + + InternalLogger getLogger() { + return new R6LogServiceLogger(this.logService); + } +} \ No newline at end of file Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/LogServiceSupport.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java?rev=1815290&view=auto ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java (added) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java Wed Nov 15 05:43:31 2017 @@ -0,0 +1,37 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.configurator.impl.logger; + +import org.osgi.service.log.LogService; + +/** + * This is a logger based on the R6 LogService. + */ +class R6LogServiceLogger implements InternalLogger { + private final LogService logService; + + public R6LogServiceLogger(final LogService logService) { + this.logService = logService; + } + + @Override + public void log(final int level, final String message, final Throwable ex) { + this.logService.log(level, message, ex); + } +} \ No newline at end of file Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/R6LogServiceLogger.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Added: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java?rev=1815290&view=auto ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java (added) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java Wed Nov 15 05:43:31 2017 @@ -0,0 +1,69 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.felix.configurator.impl.logger; + +import java.io.PrintStream; + +import org.osgi.service.log.LogService; + +/** + * This logger logs to std out / err + */ +class StdOutLogger implements InternalLogger { + + @Override + public void log(final int level, final String message, final Throwable ex) { + // output depending on level + final PrintStream out = ( level == LogService.LOG_ERROR )? System.err: System.out; + + // level as a string + final StringBuilder buf = new StringBuilder(); + switch (level) { + case ( LogService.LOG_DEBUG ): + buf.append( "[DEBUG] " ); + break; + case ( LogService.LOG_INFO ): + buf.append( "[INFO] " ); + break; + case ( LogService.LOG_WARNING ): + buf.append( "[WARN] " ); + break; + case ( LogService.LOG_ERROR ): + buf.append( "[ERROR] " ); + break; + default: + buf.append( "[UNK] " ); + break; + } + + buf.append(message); + + final String msg = buf.toString(); + + if ( ex == null ) { + out.println(msg); + } else { + // keep the message and the stacktrace together + synchronized ( out ) { + out.println( msg ); + ex.printStackTrace( out ); + } + } + } +} Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/StdOutLogger.java ------------------------------------------------------------------------------ svn:keywords = author date id revision rev url Modified: felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/SystemLogger.java URL: http://svn.apache.org/viewvc/felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/SystemLogger.java?rev=1815290&r1=1815289&r2=1815290&view=diff ============================================================================== --- felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/SystemLogger.java (original) +++ felix/trunk/osgi-r7/configurator/src/main/java/org/apache/felix/configurator/impl/logger/SystemLogger.java Wed Nov 15 05:43:31 2017 @@ -18,41 +18,56 @@ */ package org.apache.felix.configurator.impl.logger; +import org.osgi.framework.BundleContext; import org.osgi.service.log.LogService; public final class SystemLogger { - private static volatile LogService LOGGER = new ConsoleLogger(); + private static volatile LogServiceEnabledLogger LOGGER; - public static void setLogService(final LogService service) { - LOGGER = service == null ? new ConsoleLogger() : service; + public static void init(final BundleContext bundleContext) { + LOGGER = new LogServiceEnabledLogger(bundleContext); + } + + public static void destroy() { + if ( LOGGER != null ) { + LOGGER.close(); + LOGGER = null; + } + } + + private static void log(final int level, final String message, final Throwable cause) { + final LogServiceEnabledLogger l = LOGGER; + if ( l != null ) { + l.log(level, message, cause); + } } public static void debug(final String message) { - LOGGER.log(LogService.LOG_DEBUG, message); + log(LogService.LOG_DEBUG, message, null); } public static void debug(final String message, final Throwable cause) { - LOGGER.log(LogService.LOG_DEBUG, message, cause); + log(LogService.LOG_DEBUG, message, cause); } public static void info(final String message) { - LOGGER.log(LogService.LOG_INFO, message); + log(LogService.LOG_INFO, message, null); } public static void warning(final String message) { - LOGGER.log(LogService.LOG_WARNING, message); + log(LogService.LOG_WARNING, message, null); } public static void warning(final String message, final Throwable cause) { - LOGGER.log(LogService.LOG_WARNING, message, cause); + log(LogService.LOG_WARNING, message, cause); } public static void error(final String message) { - LOGGER.log(LogService.LOG_ERROR, message); + log(LogService.LOG_ERROR, message, null); } public static void error(final String message, final Throwable cause) { - LOGGER.log(LogService.LOG_ERROR, message, cause); + log(LogService.LOG_ERROR, message, cause); } }