diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 6a1b0ea..650935b 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -28,6 +28,7 @@ import java.util.logging.*; import org.ice4j.*; import org.ice4j.ice.harvest.*; import org.ice4j.stack.*; +import org.ice4j.util.Logger; // Disambiguation. /** * An Agent could be described as the main class (i.e. the chef @@ -89,11 +90,13 @@ public class Agent = new PropertyChangeListener[0]; /** - * The Logger used by the Agent class and its instances - * for logging output. + * The {@link Logger} used by the {@link Agent} class for logging output. + * Note that this shouldn't be used directly by instances of {@link Agent}, + * because it doesn't take into account the per-instance log level. + * Instances should use {@link #logger} instead. */ - private static final Logger logger - = Logger.getLogger(Agent.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger(Agent.class.getName()); /** * The name of the {@link PropertyChangeEvent} that we use to deliver @@ -287,11 +290,27 @@ public class Agent */ private Boolean useHostHarvester = null; + /** + * The {@link Logger} used by {@link Agent} instances. + */ + private final Logger logger; + /** * Creates an empty Agent with no streams, and no address. */ public Agent() { + this(Level.INFO); + } + + /** + * Creates an empty Agent with no streams, and no address. + * @param loggingLevel the logging level to be used by the agent and all of + * its components. + */ + public Agent(Level loggingLevel) + { + logger = new Logger(classLogger, loggingLevel); SecureRandom random = new SecureRandom(); connCheckServer = new ConnectivityCheckServer(this); @@ -338,7 +357,8 @@ public class Agent */ public IceMediaStream createMediaStream(String mediaStreamName) { - logger.fine("Create media stream for " + mediaStreamName); + logger.debug("Create media stream for " + mediaStreamName); + IceMediaStream mediaStream = new IceMediaStream(Agent.this, mediaStreamName); @@ -401,12 +421,6 @@ public class Agent gatherCandidates(component, preferredPort, minPort, maxPort); - for(Candidate candidate : component.getLocalCandidates()) - { - logger.info("\t" + candidate.getTransportAddress() + " (" + - candidate.getType() + ")"); - } - /* * Lyubomir: After we've gathered the LocalCandidate for a Component and * before we've made them available to the caller, we have to make sure @@ -474,8 +488,8 @@ public class Agent throws IllegalArgumentException, IOException { - logger.info("Gather candidates for component " + - component.toShortString()); + logger.info("Gathering candidates for component " + + component.toShortString() +". Local ufrag " + getLocalUfrag()); if (useHostHarvester()) { @@ -565,7 +579,8 @@ public class Agent { synchronized(startLock) { - logger.info("Start ICE connectivity establishment"); + logger.info("Start ICE connectivity establishment. Local ufrag " + + getLocalUfrag()); shutdown = false; pruneNonMatchedStreams(); @@ -776,7 +791,7 @@ public class Agent if (!oldState.equals(newState)) { logger.info("ICE state changed from " + oldState + " to " - + newState); + + newState + ". Local ufrag " + getLocalUfrag()); fireStateChange(oldState, newState); return true; @@ -1485,8 +1500,9 @@ public class Agent //so it's now safe to go and see whether this is a new PR cand. if(triggeredPair.getParentComponent().getSelectedPair() == null) { - logger.info("Received check from " + - triggeredPair.toShortString() + " triggered a check"); + logger.info("Received check from " + + triggeredPair.toShortString() + " triggered a check. " + + "Local ufrag " + getLocalUfrag()); } triggerCheck(triggeredPair); } @@ -1806,7 +1822,8 @@ public class Agent StringBuffer buf = new StringBuffer( "Harvester used for selected pair for "); buf.append(component.toShortString()); - buf.append(": "); + buf.append(" (local ufrag ").append(getLocalUfrag()); + buf.append("): "); if(selectedPair == null) { @@ -2015,64 +2032,6 @@ public class Agent } } - /** - * RFC 5245 says: Once ICE processing has reached the Completed state for - * all peers for media streams using those candidates, the agent SHOULD - * wait an additional three seconds, and then it MAY cease responding to - * checks or generating triggered checks on that candidate. It MAY free - * the candidate at that time. - *

- * This TerminationThread is scheduling such a termination and - * garbage collection in three seconds. - */ - private class TerminationThread - extends Thread - { - - /** - * Creates a new termination timer. - */ - private TerminationThread() - { - super("TerminationThread"); - } - - /** - * Waits for a period of three seconds (or whatever termination - * interval the user has specified) and then moves this Agent - * into the terminated state and frees all non-nominated candidates. - */ - @Override - public synchronized void run() - { - long terminationDelay - = Integer.getInteger( - StackProperties.TERMINATION_DELAY, - DEFAULT_TERMINATION_DELAY); - - if (terminationDelay >= 0) - { - try - { - wait(terminationDelay); - } - catch (InterruptedException ie) - { - logger.log( - Level.FINEST, "Interrupted while waiting. Will " - +"speed up termination", - ie); - } - } - - terminate(IceProcessingState.TERMINATED); - - synchronized (terminationThreadSyncRoot) - { - terminationThread = null; - } - } - } /** * Terminates this Agent by stopping the handling of connectivity @@ -2549,4 +2508,87 @@ public class Agent { this.useHostHarvester = useHostHarvester; } + + /** + * Sets the logging level for this {@link Agent} and its components. + * @param level the level to set. + */ + public void setLoggingLevel(Level level) + { + logger.setLevel(level); + } + + /** + * Gets the logging level for this {@link Agent} and its components. + */ + public Level getLoggingLevel() + { + return logger.getLevel(); + } + + /** + * @return this {@link Agent}'s {@link Logger}. + */ + protected Logger getLogger() + { + return logger; + } + + /** + * RFC 5245 says: Once ICE processing has reached the Completed state for + * all peers for media streams using those candidates, the agent SHOULD + * wait an additional three seconds, and then it MAY cease responding to + * checks or generating triggered checks on that candidate. It MAY free + * the candidate at that time. + *

+ * This TerminationThread is scheduling such a termination and + * garbage collection in three seconds. + */ + private class TerminationThread + extends Thread + { + + /** + * Creates a new termination timer. + */ + private TerminationThread() + { + super("TerminationThread"); + } + + /** + * Waits for a period of three seconds (or whatever termination + * interval the user has specified) and then moves this Agent + * into the terminated state and frees all non-nominated candidates. + */ + @Override + public synchronized void run() + { + long terminationDelay + = Integer.getInteger( + StackProperties.TERMINATION_DELAY, + DEFAULT_TERMINATION_DELAY); + + if (terminationDelay >= 0) + { + try + { + wait(terminationDelay); + } + catch (InterruptedException ie) + { + logger.log(Level.FINEST, "Interrupted while waiting. Will " + + "speed up termination", + ie); + } + } + + terminate(IceProcessingState.TERMINATED); + + synchronized (terminationThreadSyncRoot) + { + terminationThread = null; + } + } + } } diff --git a/src/main/java/org/ice4j/ice/CheckList.java b/src/main/java/org/ice4j/ice/CheckList.java index 8e23779..a4fc48f 100644 --- a/src/main/java/org/ice4j/ice/CheckList.java +++ b/src/main/java/org/ice4j/ice/CheckList.java @@ -19,7 +19,8 @@ package org.ice4j.ice; import java.beans.*; import java.util.*; -import java.util.logging.*; + +import org.ice4j.util.Logger; /** * A check list is a list of CandidatePairs with a state (i.e. a @@ -35,16 +36,31 @@ public class CheckList extends Vector { /** - * The logger. + * The class logger. + * Note that this shouldn't be used directly by instances of + * {@link CheckList}, because it doesn't take into account the per-instance + * log level. Instances should use {@link #logger} instead. */ - private static final Logger logger = - Logger.getLogger(CheckList.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger(CheckList.class.getName()); /** * A dummy serialization id. */ private static final long serialVersionUID = 1L; + /** + * The name of the {@link PropertyChangeEvent} that we use to deliver + * changes on the state of this check list. + */ + public static final String PROPERTY_CHECK_LIST_STATE = "CheckListState"; + + /** + * The name of the {@link PropertyChangeEvent} that we use to deliver + * changes on the end of checks of this check list. + */ + public static final String PROPERTY_CHECK_LIST_CHECKS = "CheckListChecks"; + /** * The state of this check list. */ @@ -78,16 +94,9 @@ public class CheckList = new LinkedList<>(); /** - * The name of the {@link PropertyChangeEvent} that we use to deliver - * changes on the state of this check list. + * The {@link Logger} used by {@link CheckList} instances. */ - public static final String PROPERTY_CHECK_LIST_STATE = "CheckListState"; - - /** - * The name of the {@link PropertyChangeEvent} that we use to deliver - * changes on the end of checks of this check list. - */ - public static final String PROPERTY_CHECK_LIST_CHECKS = "CheckListChecks"; + private Logger logger; /** * Creates a check list with the specified name. @@ -98,6 +107,8 @@ public class CheckList protected CheckList(IceMediaStream parentStream) { this.parentStream = parentStream; + logger + = new Logger(classLogger, parentStream.getParentAgent().getLogger()); } /** diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 64708a3..cac8432 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -21,6 +21,7 @@ import java.util.*; import java.util.logging.*; import org.ice4j.*; +import org.ice4j.util.Logger; //Disambiguation /** * A component is a piece of a media stream requiring a single transport @@ -38,9 +39,12 @@ public class Component { /** * Our class logger. + * Note that this shouldn't be used directly by instances of + * {@link IceMediaStream}, because it doesn't take into account the + * per-instance log level.updateRemoteCandidates Instances should use {@link #logger} instead. */ - private static final Logger logger - = Logger.getLogger(Component.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger(Component.class.getName()); /** * The component ID to use with RTP streams. @@ -112,6 +116,11 @@ public class Component */ private Candidate defaultRemoteCandidate = null; + /** + * The {@link Logger} used by {@link Component} instances. + */ + private Logger logger; + /** * Creates a new Component with the specified componentID * as a child of the specified IceMediaStream. @@ -126,6 +135,9 @@ public class Component // the max value for componentID is 256 this.componentID = componentID; this.parentStream = mediaStream; + + logger + = new Logger(classLogger, mediaStream.getParentAgent().getLogger()); } /** @@ -345,7 +357,9 @@ public class Component = getParentStream() .getParentAgent() .createCandidatePair(localCnd, remoteCnd); - logger.info("new Pair added: " + pair.toShortString()); + logger.info("new Pair added: " + pair.toShortString() + + ". Local ufrag " + + parentStream.getParentAgent().getLocalUfrag()); checkList.add(pair); } } diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java index 5cefdbc..196e643 100644 --- a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java @@ -26,6 +26,7 @@ import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.socket.*; import org.ice4j.stack.*; +import org.ice4j.util.Logger; //Disambiguation /** * The class that will be generating our outgoing connectivity checks and that @@ -39,10 +40,14 @@ class ConnectivityCheckClient { /** * The Logger used by the ConnectivityCheckClient - * class and its instances for logging output. + * class for logging output. + * Note that this shouldn't be used directly by instances of + * {@link ConnectivityCheckClient}, because it doesn't take into account + * the per-instance log level. Instances should use {@link #logger} instead. */ - private static final Logger logger - = Logger.getLogger(ConnectivityCheckClient.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger( + ConnectivityCheckClient.class.getName()); /** * The agent that created us. @@ -70,6 +75,11 @@ class ConnectivityCheckClient */ private boolean alive = false; + /** + * The {@link Logger} used by {@link ConnectivityCheckClient} instances. + */ + private Logger logger; + /** * Creates a new ConnectivityCheckClient setting * parentAgent as the agent that will be used for retrieving @@ -80,6 +90,7 @@ class ConnectivityCheckClient public ConnectivityCheckClient(Agent parentAgent) { this.parentAgent = parentAgent; + logger = new Logger(classLogger, parentAgent.getLogger()); stunStack = this.parentAgent.getStunStack(); } @@ -115,7 +126,8 @@ class ConnectivityCheckClient if (streamsWithPendingConnectivityEstablishment.size() > 0) { - logger.info("Start connectivity checks!"); + logger.info("Start connectivity checks. Local ufrag " + + parentAgent.getLocalUfrag()); startChecks( streamsWithPendingConnectivityEstablishment .get(0).getCheckList()); @@ -490,7 +502,8 @@ class ConnectivityCheckClient logger.fine("Received a success response with no " + "XOR_MAPPED_ADDRESS attribute."); logger.info("Pair failed (no XOR-MAPPED-ADDRESS): " - + checkedPair.toShortString()); + + checkedPair.toShortString() + ". Local ufrag" + + parentAgent.getLocalUfrag()); checkedPair.setStateFailed(); return; //malformed error response } @@ -558,9 +571,9 @@ class ConnectivityCheckClient if (checkedPair.getParentComponent().getSelectedPair() == null) { - logger.info( - "Receive a peer-reflexive candidate: " - + peerReflexiveCandidate.getTransportAddress()); + logger.info("Receive a peer-reflexive candidate: " + + peerReflexiveCandidate.getTransportAddress() + + ". Local ufrag " + parentAgent.getLocalUfrag()); } } @@ -596,14 +609,16 @@ class ConnectivityCheckClient //Succeeded. Note that, the pair which *generated* the check may be //different than the valid pair constructed above if (checkedPair.getParentComponent().getSelectedPair() == null) - logger.info("Pair succeeded: " + checkedPair.toShortString()); + logger.info("Pair succeeded: " + checkedPair.toShortString() + + ". Local ufrag " + parentAgent.getLocalUfrag()); checkedPair.setStateSucceeded(); } if (!validPair.isValid()) { if (validPair.getParentComponent().getSelectedPair() == null) - logger.info("Pair validated: " + validPair.toShortString()); + logger.info("Pair validated: " + validPair.toShortString() + + ". Local ufrag " + parentAgent.getLocalUfrag()); parentAgent.validatePair(validPair); } @@ -674,7 +689,8 @@ class ConnectivityCheckClient logger.info("IsControlling: " + parentAgent.isControlling() + " USE-CANDIDATE:" + (request.containsAttribute(Attribute.USE_CANDIDATE) || - checkedPair.useCandidateSent())); + checkedPair.useCandidateSent()) + + ". Local ufrag " + parentAgent.getLocalUfrag()); } //If the agent was a controlling agent, and it had included a USE- @@ -685,9 +701,9 @@ class ConnectivityCheckClient { if(validPair.getParentComponent().getSelectedPair() == null) { - logger.info( - "Nomination confirmed for pair: " - + validPair.toShortString()); + logger.info("Nomination confirmed for pair: " + + validPair.toShortString() + + ". Loal ufrag " + parentAgent.getLocalUfrag()); parentAgent.nominationConfirmed( validPair ); } else diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java b/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java index fdcaa59..6b8f930 100644 --- a/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java @@ -24,6 +24,7 @@ import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.security.*; import org.ice4j.stack.*; +import org.ice4j.util.Logger; // Disambiguation. /** * The class that would be handling and responding to incoming connectivity @@ -39,9 +40,13 @@ class ConnectivityCheckServer /** * The Logger used by the ConnectivityCheckServer * class and its instances for logging output. + * Note that this shouldn't be used directly by instances of + * {@link ConnectivityCheckServer}, because it doesn't take into account + * the per-instance log level. Instances should use {@link #logger} instead. */ - private static final Logger logger - = Logger.getLogger(ConnectivityCheckServer.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger( + ConnectivityCheckServer.class.getName()); /** * Compares a and b as unsigned long values. Serves the @@ -81,6 +86,11 @@ class ConnectivityCheckServer */ private boolean alive = false; + /** + * The {@link Logger} used by {@link ConnectivityCheckServer} instances. + */ + private Logger logger; + /** * Creates a new ConnectivityCheckServer setting * parentAgent as the agent that will be used for retrieving @@ -91,6 +101,7 @@ class ConnectivityCheckServer public ConnectivityCheckServer(Agent parentAgent) { this.parentAgent = parentAgent; + logger = new Logger(classLogger, parentAgent.getLogger()); stunStack = this.parentAgent.getStunStack(); stunStack.getCredentialsManager().registerAuthority(this); diff --git a/src/main/java/org/ice4j/ice/DefaultNominator.java b/src/main/java/org/ice4j/ice/DefaultNominator.java index dea47f7..0229d21 100644 --- a/src/main/java/org/ice4j/ice/DefaultNominator.java +++ b/src/main/java/org/ice4j/ice/DefaultNominator.java @@ -19,7 +19,8 @@ package org.ice4j.ice; import java.beans.*; import java.util.*; -import java.util.logging.*; + +import org.ice4j.util.*; /** * Implements ice4j internal nomination strategies. @@ -30,10 +31,13 @@ public class DefaultNominator implements PropertyChangeListener { /** - * The logger. + * The class logger. + * Note that this shouldn't be used directly by instances of + * {@link DefaultNominator}, because it doesn't take into account the + * per-instance log level. Instances should use {@link #logger} instead. */ - private static final Logger logger - = Logger.getLogger(DefaultNominator.class.getName()); + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger(DefaultNominator.class.getName()); /** * The Agent that created us. @@ -52,6 +56,11 @@ public class DefaultNominator */ private final Map validatedCandidates = new HashMap<>(); + /** + * The {@link Logger} used by {@link DefaultNominator} instances. + */ + private Logger logger; + /** * Creates a new instance of this nominator using parentAgent as * a reference to the Agent instance that we should use to @@ -62,6 +71,7 @@ public class DefaultNominator public DefaultNominator(Agent parentAgent) { this.parentAgent = parentAgent; + logger = new Logger(classLogger, parentAgent.getLogger()); parentAgent.addStateChangeListener(this); } @@ -135,7 +145,8 @@ public class DefaultNominator { CandidatePair validPair = (CandidatePair)evt.getSource(); - logger.info("Nominate (first valid): " + validPair.toShortString()); + logger.info("Nominate (first valid): " + validPair.toShortString() + + ". Local ufrag " + parentAgent.getLocalUfrag()); parentAgent.nominate(validPair); } } diff --git a/src/main/java/org/ice4j/ice/IceMediaStream.java b/src/main/java/org/ice4j/ice/IceMediaStream.java index 2bf3b80..ea11e68 100644 --- a/src/main/java/org/ice4j/ice/IceMediaStream.java +++ b/src/main/java/org/ice4j/ice/IceMediaStream.java @@ -20,8 +20,10 @@ package org.ice4j.ice; import java.beans.*; import java.util.*; import java.util.logging.*; +import java.util.logging.Logger; import org.ice4j.*; +import org.ice4j.util.*; /** * The class represents a media stream from the ICE perspective, i.e. a @@ -33,9 +35,12 @@ import org.ice4j.*; public class IceMediaStream { /** - * Our class logger. + * Our class logger + * Note that this shouldn't be used directly by instances of + * {@link IceMediaStream}, because it doesn't take into account the + * per-instance log level. Instances should use {@link #logger} instead. */ - private static final Logger logger = + private static final Logger classLogger = Logger.getLogger(IceMediaStream.class.getName()); /** @@ -131,6 +136,11 @@ public class IceMediaStream */ private String remotePassword = null; + /** + * The {@link Logger} used by {@link IceMediaStream} instances. + */ + private org.ice4j.util.Logger logger; + /** * Initializes a new IceMediaStream object. * @@ -140,6 +150,7 @@ public class IceMediaStream */ protected IceMediaStream(Agent parentAgent, String name) { + logger = new org.ice4j.util.Logger(classLogger, parentAgent.getLogger()); this.name = name; this.parentAgent = parentAgent; checkList = new CheckList(this); diff --git a/src/main/java/org/ice4j/ice/LocalCandidate.java b/src/main/java/org/ice4j/ice/LocalCandidate.java index 73a459f..8e05090 100644 --- a/src/main/java/org/ice4j/ice/LocalCandidate.java +++ b/src/main/java/org/ice4j/ice/LocalCandidate.java @@ -24,6 +24,7 @@ import java.util.logging.*; import org.ice4j.*; import org.ice4j.socket.*; import org.ice4j.stack.*; +import org.ice4j.util.Logger; // Disambiguation. /** * LocalCandidates are obtained by an agent for every stream component @@ -35,6 +36,16 @@ import org.ice4j.stack.*; public abstract class LocalCandidate extends Candidate { + /** + * The Logger used by the LocalCandidate class for logging + * output. + * Note that this shouldn't be used directly by instances of + * {@link DefaultNominator}, because it doesn't take into account the + * per-instance log level. Instances should use {@link #logger} instead. + */ + private static final java.util.logging.Logger classLogger + = java.util.logging.Logger.getLogger(HostCandidate.class.getName()); + /** * The type of method used to discover this candidate ("host", "upnp", "stun * peer reflexive", "stun server reflexive", "turn relayed", "google turn @@ -53,11 +64,9 @@ public abstract class LocalCandidate private boolean isSSL = false; /** - * The Logger used by the LocalCandidate class and its - * instances for logging output. + * The {@link Logger} used by {@link LocalCandidate} instances. */ - private static final Logger logger - = Logger.getLogger(HostCandidate.class.getName()); + private Logger logger; /** * Creates a LocalCandidate instance for the specified transport @@ -84,6 +93,10 @@ public abstract class LocalCandidate LocalCandidate relatedCandidate) { super(transportAddress, parentComponent, type, relatedCandidate); + logger + = new Logger(classLogger, + parentComponent. + getParentStream().getParentAgent().getLogger()); this.extendedType = extendedType; } diff --git a/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSetElement.java b/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSetElement.java index 7ed27ed..e825696 100644 --- a/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSetElement.java +++ b/src/main/java/org/ice4j/ice/harvest/CandidateHarvesterSetElement.java @@ -143,7 +143,7 @@ class CandidateHarvesterSetElement */ public void setEnabled(boolean enabled) { - logger.info((enabled ? "Enabling: " : "Disabling: ") + harvester); + logger.fine((enabled ? "Enabling: " : "Disabling: ") + harvester); this.enabled = enabled; } diff --git a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java index 07b58b8..04d5965 100644 --- a/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java +++ b/src/main/java/org/ice4j/ice/harvest/SinglePortUdpHarvester.java @@ -30,6 +30,7 @@ import java.net.*; import java.util.*; import java.util.concurrent.*; import java.util.logging.*; +import java.util.logging.Logger; // Disambiguation /** * A harvester implementation which binds to a single DatagramSocket diff --git a/src/main/java/org/ice4j/util/Logger.java b/src/main/java/org/ice4j/util/Logger.java new file mode 100644 index 0000000..a67cc12 --- /dev/null +++ b/src/main/java/org/ice4j/util/Logger.java @@ -0,0 +1,234 @@ +/* + * ice4j, the OpenSource Java Solution for NAT and Firewall Traversal. + * + * Copyright @ 2015 Atlassian Pty Ltd + * + * Licensed 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.ice4j.util; + +import java.util.logging.*; + +/** + * Implements a logger, which delegates to a {@link java.util.logging.Logger} + * instance, but maintains its own logging level. + * + * @author Boris Grozev + */ +public class Logger +{ + /** + * The level of this logger. + */ + private Level level; + + /** + * The underlying {@link java.util.logging.Logger} instance to which logging + * is delegated. + */ + private java.util.logging.Logger delegate; + + /** + * The {@link Logger} instance (if any) which provides the log level of this + * instance. + */ + private Logger levelDelegate; + + /** + * Initializes a new {@link Logger} instance with a given delegate and a + * given log level. + * @param delegate the delegate. + * @param level the log level. + */ + public Logger(java.util.logging.Logger delegate, Level level) + { + this.delegate = delegate; + this.level = level; + } + + /** + * Initializes a new {@link Logger} instance with a given delegate and which + * uses the log level from another {@link Logger} instance. + * @param delegate the delegate. + * @param levelDelegate the {@link Logger} instance whose log level will be + * used. + */ + public Logger(java.util.logging.Logger delegate, Logger levelDelegate) + { + this.delegate = delegate; + this.levelDelegate = levelDelegate; + } + + /** + * Sets the logging {@link Level} for this {@link Logger}. + * @param level the level to set. + */ + public void setLevel(Level level) + { + this.level = level; + } + + /** + * @return the logging {@link Level} configured for this {@link Logger}. + */ + public Level getLevel() + { + return levelDelegate != null ? levelDelegate.getLevel() : level; + } + + /** + * Checks whether messages with a particular level should be logged + * according to the log level configured for this {@link Logger.} + * @param level the log level. + */ + public boolean isLoggable(Level level) + { + Level currentLevel + = levelDelegate != null ? levelDelegate.getLevel() : this.level; + int levelValue = currentLevel.intValue(); + + return level.intValue() >= levelValue + && levelValue != Level.OFF.intValue(); + } + + /** + * Logs a message at a given level, if that level is loggable according to + * the log level configured by this instance. + * @param level the level at which to log the message. + * @param msg the message to log. + */ + public void log(Level level, String msg) + { + if (isLoggable(level)) + { + delegate.log(level, msg); + } + } + + /** + * Logs a message at a given level, if that level is loggable according to + * the log level configured by this instance. + * @param level the level at which to log the message. + * @param msg the message to log. + * @param thrown a {@link Throwable} associated with log message. + */ + public void log(Level level, String msg, Throwable thrown) + { + if (isLoggable(level)) + { + delegate.log(level, msg, thrown); + } + } + + /** + * Logs a message with level {@link Level.SEVERE}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void severe(String msg) + { + log(Level.SEVERE, msg); + } + + /** + * Logs a message with level {@link Level.WARNING}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void warning(String msg) + { + log(Level.WARNING, msg); + } + + /** + * Logs a message with level {@link Level.INFO}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void info(String msg) + { + log(Level.INFO, msg); + } + + /** + * Logs a message with level {@link Level.CONFIG}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void config(String msg) + { + log(Level.CONFIG, msg); + } + + /** + * Logs a message with level {@link Level.FINE}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void fine(String msg) + { + log(Level.FINE, msg); + } + + /** + * Logs a message with level {@link Level.FINER}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void finer(String msg) + { + log(Level.FINER, msg); + } + + /** + * Logs a message with level {@link Level.FINEST}, if that level is + * loggable according to the log level configured for this {@link Logger}. + * @param msg the message to log. + */ + public void finest(String msg) + { + log(Level.FINEST, msg); + } + + /** + * An alias for {@link #fine(String)}. + */ + public void debug(String msg) + { + fine(msg); + } + + /** + * An alias for {@link #warning(String)}. + */ + public void warn(String msg) + { + warning(msg); + } + + /** + * An alias for {@link #severe(String)}. + */ + public void error(String msg) + { + severe(msg); + } + + /** + * An alias for {@link #finest(String)}. + */ + public void trace(String msg) + { + finest(msg); + } +} diff --git a/src/main/java/org/ice4j/util/PacketQueue.java b/src/main/java/org/ice4j/util/PacketQueue.java index ce38d37..2e5c56b 100644 --- a/src/main/java/org/ice4j/util/PacketQueue.java +++ b/src/main/java/org/ice4j/util/PacketQueue.java @@ -17,7 +17,7 @@ package org.ice4j.util; import java.util.*; import java.util.concurrent.*; -import java.util.logging.*; +import java.util.logging.Logger; // Disambiguation. /** * An abstract queue of packets. This is meant to eventually be able to be used @@ -193,7 +193,7 @@ public abstract class PacketQueue handler = null; } - logger.info("Initialized a PacketQueue instance with ID " + id); + logger.fine("Initialized a PacketQueue instance with ID " + id); } /** diff --git a/src/main/java/org/ice4j/util/QueueStatistics.java b/src/main/java/org/ice4j/util/QueueStatistics.java index a6502d5..d110541 100644 --- a/src/main/java/org/ice4j/util/QueueStatistics.java +++ b/src/main/java/org/ice4j/util/QueueStatistics.java @@ -16,6 +16,7 @@ package org.ice4j.util; import java.util.logging.*; +import java.util.logging.Logger; // Disambiguation. public class QueueStatistics {