868N/A/*
868N/A * CDDL HEADER START
868N/A *
868N/A * The contents of this file are subject to the terms of the
868N/A * Common Development and Distribution License, Version 1.0 only
868N/A * (the "License"). You may not use this file except in compliance
868N/A * with the License.
868N/A *
6983N/A * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
6983N/A * or http://forgerock.org/license/CDDLv1.0.html.
868N/A * See the License for the specific language governing permissions
868N/A * and limitations under the License.
868N/A *
868N/A * When distributing Covered Code, include this CDDL HEADER in each
6983N/A * file and include the License file at legal-notices/CDDLv1_0.txt.
6983N/A * If applicable, add the following below this CDDL HEADER, with the
6983N/A * fields enclosed by brackets "[]" replaced with your own identifying
6983N/A * information:
868N/A * Portions Copyright [yyyy] [name of copyright owner]
868N/A *
868N/A * CDDL HEADER END
868N/A *
868N/A *
4564N/A * Copyright 2007-2009 Sun Microsystems, Inc.
6207N/A * Portions Copyright 2013 ForgeRock AS
868N/A */
868N/Apackage org.opends.server.loggers.debug;
868N/A
6209N/Aimport static org.opends.messages.ConfigMessages.*;
6209N/A
6209N/Aimport java.util.Map;
1280N/Aimport java.util.concurrent.ConcurrentHashMap;
1280N/A
6207N/Aimport org.opends.server.admin.ClassPropertyDefinition;
6207N/Aimport org.opends.server.admin.std.meta.DebugLogPublisherCfgDefn;
6207N/Aimport org.opends.server.admin.std.server.DebugLogPublisherCfg;
6207N/Aimport org.opends.server.api.DebugLogPublisher;
6209N/Aimport org.opends.server.loggers.AbstractLogger;
6207N/Aimport org.opends.server.loggers.LogLevel;
6207N/Aimport org.opends.server.types.DebugLogLevel;
868N/A
868N/A/**
868N/A * A logger for debug and trace logging. DebugLogger provides a debugging
868N/A * management access point. It is used to configure the Tracers, as well as
868N/A * to register a per-class tracer.
868N/A *
868N/A * Various stub debug methods are provided to log different types of debug
868N/A * messages. However, these methods do not contain any actual implementation.
868N/A * Tracer aspects are later weaved to catch alls to these stub methods and
868N/A * do the work of logging the message.
868N/A *
868N/A * DebugLogger is self-initializing.
868N/A */
6209N/Apublic class DebugLogger extends AbstractLogger
6209N/A <DebugLogPublisher<DebugLogPublisherCfg>, DebugLogPublisherCfg>
868N/A{
6209N/A /** The default level to log constructor executions. */
1280N/A static final LogLevel DEFAULT_CONSTRUCTOR_LEVEL =
1280N/A DebugLogLevel.VERBOSE;
6209N/A /** The default level to log method entry and exit pointcuts. */
1280N/A static final LogLevel DEFAULT_ENTRY_EXIT_LEVEL =
1280N/A DebugLogLevel.VERBOSE;
6209N/A /** The default level to log method entry and exit pointcuts. */
1280N/A static final LogLevel DEFAULT_THROWN_LEVEL =
1280N/A DebugLogLevel.ERROR;
1280N/A
6209N/A /** The set of all DebugTracer instances. */
6209N/A private static Map<String, DebugTracer> classTracers =
1400N/A new ConcurrentHashMap<String, DebugTracer>();
1280N/A
6209N/A /**
6209N/A * Trace methods will use this static boolean to determine if debug is enabled
6209N/A * so to not incur the cost of calling debugPublishers.isEmpty().
6209N/A */
1280N/A static boolean enabled = false;
1280N/A
6209N/A private static final LoggerStorage
6209N/A <DebugLogPublisher<DebugLogPublisherCfg>, DebugLogPublisherCfg>
6209N/A loggerStorage = new LoggerStorage
6209N/A <DebugLogPublisher<DebugLogPublisherCfg>, DebugLogPublisherCfg>();
6209N/A
6209N/A /** The singleton instance of this class for configuration purposes. */
1280N/A static final DebugLogger instance = new DebugLogger();
1280N/A
1280N/A /**
6209N/A * The constructor for this class.
6209N/A */
6209N/A public DebugLogger()
6209N/A {
6209N/A super((Class) DebugLogPublisher.class,
6209N/A ERR_CONFIG_LOGGER_INVALID_DEBUG_LOGGER_CLASS);
6209N/A }
6209N/A
6209N/A /** {@inheritDoc} */
6209N/A @Override
6209N/A protected ClassPropertyDefinition getJavaClassPropertyDefinition()
6209N/A {
6209N/A return DebugLogPublisherCfgDefn.getInstance()
6209N/A .getJavaClassPropertyDefinition();
6209N/A }
6209N/A
6209N/A /** {@inheritDoc} */
6209N/A @Override
6209N/A protected LoggerStorage<DebugLogPublisher<DebugLogPublisherCfg>,
6209N/A DebugLogPublisherCfg> getStorage()
6209N/A {
6209N/A return loggerStorage;
6209N/A }
6209N/A
6209N/A /**
1280N/A * Add an debug log publisher to the debug logger.
1280N/A *
6209N/A * @param publisher The debug log publisher to add.
1280N/A */
1514N/A public synchronized static void addDebugLogPublisher(
1514N/A DebugLogPublisher publisher)
1280N/A {
6209N/A loggerStorage.addLogPublisher(publisher);
1294N/A
1400N/A updateTracerSettings();
1294N/A
1400N/A enabled = true;
1280N/A }
1280N/A
1280N/A /**
1280N/A * Remove an debug log publisher from the debug logger.
1280N/A *
1514N/A * @param publisher The debug log publisher to remove.
1280N/A * @return The publisher that was removed or null if it was not found.
1280N/A */
1514N/A public synchronized static boolean removeDebugLogPublisher(
1514N/A DebugLogPublisher publisher)
1280N/A {
6209N/A boolean removed = loggerStorage.removeLogPublisher(publisher);
1280N/A
1400N/A updateTracerSettings();
1400N/A
6209N/A enabled = !loggerStorage.getLogPublishers().isEmpty();
1280N/A
1280N/A return removed;
1280N/A }
1280N/A
1280N/A /**
1280N/A * Removes all existing debug log publishers from the logger.
1280N/A */
1280N/A public synchronized static void removeAllDebugLogPublishers()
1280N/A {
6209N/A loggerStorage.removeAllLogPublishers();
1280N/A
1400N/A updateTracerSettings();
1400N/A
1280N/A enabled = false;
1280N/A }
1280N/A
1280N/A /**
1400N/A * Update all debug tracers with the settings in the registered
1400N/A * publishers.
1280N/A */
1400N/A static void updateTracerSettings()
1280N/A {
6209N/A DebugLogPublisher<DebugLogPublisherCfg>[] publishers =
6209N/A loggerStorage.getLogPublishers().toArray(new DebugLogPublisher[0]);
1400N/A
1400N/A for(DebugTracer tracer : classTracers.values())
1280N/A {
1400N/A tracer.updateSettings(publishers);
1280N/A }
1280N/A }
1280N/A
1280N/A /**
1280N/A * Indicates if debug logging is enabled.
868N/A *
1280N/A * @return True if debug logging is enabled. False otherwise.
868N/A */
868N/A public static boolean debugEnabled()
868N/A {
4564N/A return enabled;
868N/A }
868N/A
868N/A /**
1280N/A * Retrieve the singleton instance of this class.
1280N/A *
1280N/A * @return The singleton instance of this logger.
868N/A */
1280N/A public static DebugLogger getInstance()
868N/A {
1280N/A return instance;
868N/A }
868N/A
868N/A /**
1400N/A * Creates a new Debug Tracer for the caller class and registers it
1400N/A * with the Debug Logger.
868N/A *
1400N/A * @return The tracer created for the caller class.
868N/A */
1400N/A public static DebugTracer getTracer()
1400N/A {
1400N/A DebugTracer tracer =
6209N/A new DebugTracer(loggerStorage.getLogPublishers().toArray(
6209N/A new DebugLogPublisher[0]));
1400N/A classTracers.put(tracer.getTracedClassName(), tracer);
868N/A
1400N/A return tracer;
1400N/A }
868N/A
868N/A /**
1400N/A * Returns the registered Debug Tracer for a traced class.
868N/A *
1400N/A * @param className The name of the class tracer to retrieve.
1400N/A * @return The tracer for the provided class or null if there are
1400N/A * no tracers registered.
868N/A */
1400N/A public static DebugTracer getTracer(String className)
1400N/A {
1400N/A return classTracers.get(className);
1400N/A }
868N/A
1280N/A /**
1280N/A * Classes and methods annotated with @NoDebugTracing will not be weaved with
1280N/A * debug logging statements by AspectJ.
1280N/A */
1280N/A public @interface NoDebugTracing {}
1280N/A
1280N/A /**
1280N/A * Methods annotated with @NoEntryDebugTracing will not be weaved with
1280N/A * entry debug logging statements by AspectJ.
1280N/A */
1280N/A public @interface NoEntryDebugTracing {}
1280N/A
1280N/A /**
1280N/A * Methods annotated with @NoExitDebugTracing will not be weaved with
1280N/A * exit debug logging statements by AspectJ.
1280N/A */
1280N/A public @interface NoExitDebugTracing {}
1280N/A
1280N/A /**
1280N/A * Methods annotated with @TraceThrown will be weaved by AspectJ with
1280N/A * debug logging statements when an exception is thrown from the method.
1280N/A */
1280N/A public @interface TraceThrown {}
868N/A
868N/A}