diff --git a/pom.xml b/pom.xml index d013bfe..84dc5dd 100644 --- a/pom.xml +++ b/pom.xml @@ -35,6 +35,12 @@ weupnp 0.1.4 + + ${project.groupId} + jitsi-utils + 1.0-12-gcdf6537 + + junit @@ -249,4 +255,19 @@ + + + + jitsi-maven-repository-releases + default + Jitsi Maven Repository (Releases) + + true + + + false + + https://github.com/jitsi/jitsi-maven-repository/raw/master/releases/ + + diff --git a/src/main/java/org/ice4j/ice/Agent.java b/src/main/java/org/ice4j/ice/Agent.java index 02f83ea..29236a3 100644 --- a/src/main/java/org/ice4j/ice/Agent.java +++ b/src/main/java/org/ice4j/ice/Agent.java @@ -31,7 +31,9 @@ import org.ice4j.*; import org.ice4j.ice.harvest.*; import org.ice4j.stack.*; import org.ice4j.util.*; -import org.ice4j.util.Logger; // Disambiguation. +import org.jitsi.utils.collections.*; +import org.jitsi.utils.logging2.*; +import org.jitsi.utils.logging2.Logger; /** * An Agent could be described as the main class (i.e. the chef @@ -343,7 +345,12 @@ public class Agent */ public Agent() { - this(Level.INFO, null); + this(null, null); + } + + public Agent(Logger parentLogger) + { + this(null, parentLogger); } /** @@ -351,33 +358,24 @@ public class Agent * @param ufragPrefix an optional prefix to the generated local ICE username * fragment. */ - public Agent(String ufragPrefix) + public Agent(String ufragPrefix, Logger parentLogger) { - this(Level.INFO, ufragPrefix); - } - - /** - * 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) - { - this(loggingLevel, null); - } - - /** - * 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. - * @param ufragPrefix an optional prefix to the generated local ICE username - * fragment. - */ - public Agent(Level loggingLevel, String ufragPrefix) - { - logger = new Logger(classLogger, loggingLevel); SecureRandom random = new SecureRandom(); + String ufrag = ufragPrefix == null ? "" : ufragPrefix; + ufrag += new BigInteger(24, random).toString(32); + ufrag += BigInteger.valueOf(System.currentTimeMillis()).toString(32); + ufrag = ensureIceAttributeLength(ufrag, /* min */ 4, /* max */ 256); + this.ufrag = ufrag; + if (parentLogger != null) + { + logger = parentLogger.createChildLogger(this.getClass().getName(), JMap.of("ufrag", this.ufrag)); + } + else + { + logger = new LoggerImpl(Agent.class.getName(), new LogContext(JMap.of("ufrag", this.ufrag))); + } + connCheckServer = new ConnectivityCheckServer(this); connCheckClient = new ConnectivityCheckClient( this, agentTasksScheduler, agentTasksExecutor); @@ -389,11 +387,6 @@ public class Agent if (StackProperties.getString(StackProperties.SOFTWARE) == null) System.setProperty(StackProperties.SOFTWARE, "ice4j.org"); - String ufrag = ufragPrefix == null ? "" : ufragPrefix; - ufrag += new BigInteger(24, random).toString(32); - ufrag += BigInteger.valueOf(System.currentTimeMillis()).toString(32); - ufrag = ensureIceAttributeLength(ufrag, /* min */ 4, /* max */ 256); - this.ufrag = ufrag; password = ensureIceAttributeLength( @@ -409,10 +402,7 @@ public class Agent addCandidateHarvester(harvester); } - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Created a new Agent, ufrag=" + ufrag); - } + logger.debug(() -> "Created a new Agent"); } /** @@ -434,7 +424,7 @@ public class Agent */ public IceMediaStream createMediaStream(String mediaStreamName) { - logger.debug("Create media stream for " + mediaStreamName); + logger.debug(() -> "Create media stream for " + mediaStreamName); IceMediaStream mediaStream = new IceMediaStream(Agent.this, mediaStreamName); @@ -679,7 +669,7 @@ public class Agent else { if (hostHarvesters.isEmpty()) - logger.warning("No host harvesters available!"); + logger.warn("No host harvesters available!"); } for (CandidateHarvester harvester : hostHarvesters) @@ -688,7 +678,7 @@ public class Agent } if (component.getLocalCandidateCount() == 0) - logger.warning("Failed to gather any host candidates!"); + logger.warn("Failed to gather any host candidates!"); //in case we are not trickling, apply other harvesters here if (!isTrickling()) @@ -697,7 +687,7 @@ public class Agent harvesters.harvest(component); } - logger.fine("Candidate count in first harvest: " + + logger.debug(() -> "Candidate count in first harvest: " + component.getLocalCandidateCount()); // Emil: Because of trickle, we now assign foundations, compute @@ -733,7 +723,7 @@ public class Agent if (harvestingStarted) { - logger.warning( + logger.warn( "Hmmm ... why are you harvesting twice? You shouldn't be!"); } @@ -1102,7 +1092,7 @@ public class Agent if (stream == null) { ret = null; - logger.warning( + logger.warn( "Agent contains no IceMediaStream with name " + media + "!"); } @@ -1113,7 +1103,7 @@ public class Agent if (remoteUfrag == null) { ret = null; - logger.warning( + logger.warn( "Remote ufrag of IceMediaStream with name " + media + " is null!"); } @@ -1660,7 +1650,7 @@ public class Agent CandidatePair triggeredPair = createCandidatePair(localCandidate, remoteCandidate); - logger.fine("set use-candidate " + useCandidate + " for pair " + + logger.debug(() -> "set use-candidate " + useCandidate + " for pair " + triggeredPair.toShortString()); if (useCandidate) { @@ -1671,7 +1661,7 @@ public class Agent { if (state == IceProcessingState.WAITING) { - logger.fine("Receive STUN checks before our ICE has started"); + logger.debug(() -> "Receive STUN checks before our ICE has started"); //we are not started yet so we'd better wait until we get the //remote candidates in case we are holding to a new PR one. this.preDiscoveredPairsQueue.add(triggeredPair); @@ -1682,12 +1672,8 @@ public class Agent } else //Running, Connected or Terminated. { - if (logger.isLoggable(Level.FINE)) - { - logger.debug("Received check from " - + triggeredPair.toShortString() + " triggered a check. " - + "Local ufrag " + getLocalUfrag()); - } + logger.debug(() -> "Received check from " + + triggeredPair.toShortString() + " triggered a check."); // We have been started, and have not failed (yet). If this is // a new pair, handle it (even if we have already completed). @@ -1735,7 +1721,7 @@ public class Agent //7.2.1.5. Updating the Nominated Flag if (!isControlling() && useCand) { - logger.fine("update nominated flag"); + logger.debug(() -> "update nominated flag"); // If the Binding request received by the agent had the // USE-CANDIDATE attribute set, and the agent is in the // controlled role, the agent looks at the state of the @@ -1943,7 +1929,7 @@ public class Agent if (!atLeastOneListSucceeded) { //all lists ended but none succeeded. No love today ;( - if (logger.isLoggable(Level.INFO)) + if (logger.isInfoEnabled()) { if (connCheckClient.isAlive() || connCheckServer.isAlive()) @@ -2344,7 +2330,7 @@ public class Agent */ public void free() { - logger.fine("Free ICE agent"); + logger.debug(() -> "Free ICE agent"); shutdown = true; @@ -2380,17 +2366,17 @@ public class Agent // Free its IceMediaStreams, Components and Candidates. boolean interrupted = false; - logger.fine("remove streams"); + logger.debug(() -> "remove streams"); for (IceMediaStream stream : getStreams()) { try { removeStream(stream); - logger.fine("remove stream " + stream.getName()); + logger.debug("remove stream " + stream.getName()); } catch (Throwable t) { - logger.fine( + logger.debug(() -> "remove stream " + stream.getName() + " failed: " + t); if (t instanceof InterruptedException) interrupted = true; @@ -2403,7 +2389,7 @@ public class Agent getStunStack().shutDown(); - logger.fine("ICE agent freed"); + logger.debug(() -> "ICE agent freed"); } /** @@ -2738,7 +2724,7 @@ public class Agent } catch (Exception e) { - logger.log(Level.WARNING, "Error while sending keep alive", e); + logger.warn("Error while sending keep alive", e); } } diff --git a/src/main/java/org/ice4j/ice/CheckList.java b/src/main/java/org/ice4j/ice/CheckList.java index a4fc48f..cdfb4e0 100644 --- a/src/main/java/org/ice4j/ice/CheckList.java +++ b/src/main/java/org/ice4j/ice/CheckList.java @@ -17,11 +17,11 @@ */ package org.ice4j.ice; +import org.jitsi.utils.logging2.*; + import java.beans.*; import java.util.*; -import org.ice4j.util.Logger; - /** * A check list is a list of CandidatePairs with a state (i.e. a * CheckListState). The pairs in a check list are those that an ICE @@ -35,15 +35,6 @@ import org.ice4j.util.Logger; public class CheckList extends Vector { - /** - * 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 java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger(CheckList.class.getName()); - /** * A dummy serialization id. */ @@ -107,8 +98,7 @@ public class CheckList protected CheckList(IceMediaStream parentStream) { this.parentStream = parentStream; - logger - = new Logger(classLogger, parentStream.getParentAgent().getLogger()); + logger = parentStream.getLogger().createChildLogger(this.getClass().getName()); } /** diff --git a/src/main/java/org/ice4j/ice/Component.java b/src/main/java/org/ice4j/ice/Component.java index 62976a3..858ab40 100644 --- a/src/main/java/org/ice4j/ice/Component.java +++ b/src/main/java/org/ice4j/ice/Component.java @@ -22,11 +22,11 @@ import java.io.*; import java.net.*; import java.util.*; import java.util.concurrent.*; -import java.util.logging.*; import org.ice4j.*; import org.ice4j.socket.*; -import org.ice4j.util.Logger; //Disambiguation +import org.jitsi.utils.collections.*; +import org.jitsi.utils.logging2.*; /** * A component is a piece of a media stream requiring a single transport @@ -43,15 +43,6 @@ import org.ice4j.util.Logger; //Disambiguation public class Component implements PropertyChangeListener { - /** - * 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 java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger(Component.class.getName()); - /** * The component ID to use with RTP streams. */ @@ -175,9 +166,10 @@ public class Component */ protected Component(int componentID, IceMediaStream mediaStream, - KeepAliveStrategy keepAliveStrategy) + KeepAliveStrategy keepAliveStrategy, + Logger parentLogger) { - this(componentID, mediaStream, keepAliveStrategy, true); + this(componentID, mediaStream, keepAliveStrategy, true, parentLogger); } /** @@ -192,21 +184,24 @@ public class Component protected Component(int componentID, IceMediaStream mediaStream, KeepAliveStrategy keepAliveStrategy, - boolean useComponentSocket) + boolean useComponentSocket, + Logger parentLogger) { // the max value for componentID is 256 this.componentID = componentID; this.parentStream = mediaStream; this.keepAliveStrategy = Objects.requireNonNull(keepAliveStrategy, "keepAliveStrategy"); - - Logger agentLogger = mediaStream.getParentAgent().getLogger(); + this.logger = parentLogger.createChildLogger( + this.getClass().getName(), + JMap.of("componentId", Integer.toString(componentID)) + ); if (useComponentSocket) { try { - componentSocket = new ComponentSocket(this, agentLogger); + componentSocket = new ComponentSocket(this, logger); socket = new MultiplexingDatagramSocket(componentSocket); socketWrapper = new IceUdpSocketWrapper(socket); } @@ -223,7 +218,6 @@ public class Component } mediaStream.addPairChangeListener(this); - logger = new Logger(classLogger, agentLogger); } /** @@ -694,11 +688,7 @@ public class Component && (cand.getPriority() >= cand2.getPriority())) { localCandidates.remove(j); - if (logger.isLoggable(Level.FINEST)) - { - logger.finest( - "eliminating redundant cand: "+ cand2); - } + logger.trace(() -> "eliminating redundant cand: "+ cand2); } else j++; @@ -899,12 +889,7 @@ public class Component */ if (t instanceof ThreadDeath) throw (ThreadDeath) t; - if (logger.isLoggable(Level.INFO)) - { - logger.log( - Level.INFO, - "Failed to free LocalCandidate: " + localCandidate); - } + logger.info(() -> "Failed to free LocalCandidate: " + localCandidate); } } @@ -1017,7 +1002,7 @@ public class Component * parent of this component. * @return Component */ - public static Component build(int componentID, IceMediaStream mediaStream) + public static Component build(int componentID, IceMediaStream mediaStream, Logger parentLogger) { return new Component( @@ -1025,7 +1010,9 @@ public class Component mediaStream, KeepAliveStrategy.SELECTED_ONLY, StackProperties.getBoolean( - StackProperties.USE_COMPONENT_SOCKET, true)); + StackProperties.USE_COMPONENT_SOCKET, true), + parentLogger + ); } /** @@ -1136,4 +1123,9 @@ public class Component keepAlivePairs.add(pair); } } + + public Logger getLogger() + { + return logger; + } } diff --git a/src/main/java/org/ice4j/ice/ComponentSocket.java b/src/main/java/org/ice4j/ice/ComponentSocket.java index 92eb949..c2368ed 100644 --- a/src/main/java/org/ice4j/ice/ComponentSocket.java +++ b/src/main/java/org/ice4j/ice/ComponentSocket.java @@ -33,14 +33,6 @@ public class ComponentSocket extends MergingDatagramSocket implements PropertyChangeListener { - /** - * The {@link Logger} used by the {@link MergingDatagramSocket} class and - * its instances for logging output. - */ - private static final java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger - (MergingDatagramSocket.class.getName()); - /** * Controls access to {@link #authorizedAddresses}. */ @@ -68,22 +60,16 @@ public class ComponentSocket */ private final Object initializedActiveSyncRoot = new Object(); - /** - * The {@link Logger} used by {@link MergingDatagramSocket} instances. - */ - private final Logger logger; - /** * Initializes a new {@link MergingDatagramSocket} instance. * @throws SocketException */ - ComponentSocket(Component component, Logger levelDelegate) + ComponentSocket(Component component, org.jitsi.utils.logging2.Logger parentLogger) throws SocketException { - super(levelDelegate); + super(parentLogger); this.component = component; - this.logger = new Logger(classLogger, levelDelegate); component.getParentStream().addPairChangeListener(this); } diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java index a5d60da..702f47c 100644 --- a/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java +++ b/src/main/java/org/ice4j/ice/ConnectivityCheckClient.java @@ -21,7 +21,6 @@ import java.net.*; import java.time.*; import java.util.*; import java.util.concurrent.*; -import java.util.logging.*; import org.ice4j.*; import org.ice4j.attribute.*; @@ -29,7 +28,8 @@ import org.ice4j.message.*; import org.ice4j.socket.*; import org.ice4j.stack.*; import org.ice4j.util.*; -import org.ice4j.util.Logger; //Disambiguation +import org.jitsi.utils.logging2.*; +import org.jitsi.utils.logging2.Logger; /** * The class that will be generating our outgoing connectivity checks and that @@ -41,17 +41,6 @@ import org.ice4j.util.Logger; //Disambiguation class ConnectivityCheckClient implements ResponseCollector { - /** - * The Logger used by the ConnectivityCheckClient - * 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 java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger( - ConnectivityCheckClient.class.getName()); - /** * The agent that created us. */ @@ -114,7 +103,7 @@ class ConnectivityCheckClient this.parentAgent = parentAgent; this.scheduledExecutorService = scheduledExecutorService; this.executorService = executorService; - logger = new Logger(classLogger, parentAgent.getLogger()); + logger = parentAgent.getLogger().createChildLogger(this.getClass().getName()); stunStack = this.parentAgent.getStunStack(); } @@ -195,11 +184,8 @@ class ConnectivityCheckClient indication, candidatePair.getRemoteCandidate().getTransportAddress(), localCandidate.getBase().getTransportAddress()); - if (logger.isLoggable(Level.FINEST)) - { - logger.finest( - "sending binding indication to pair " + candidatePair); - } + logger.trace(() -> + "sending binding indication to pair " + candidatePair); } catch (Exception ex) { @@ -207,11 +193,9 @@ class ConnectivityCheckClient if (stunSocket != null) { - logger.log( - Level.INFO, - "Failed to send " + indication + " through " - + stunSocket.getLocalSocketAddress(), - ex); + logger.info("Failed to send " + indication + " through " + + stunSocket.getLocalSocketAddress() + "\n" + + ex.toString()); } } } @@ -282,7 +266,7 @@ class ConnectivityCheckClient //nominated pairs. if (candidatePair.isNominated()) { - logger.fine( + logger.debug(() -> "Add USE-CANDIDATE in check for: " + candidatePair.toShortString()); request.putAttribute( @@ -326,9 +310,12 @@ class ConnectivityCheckClient tran.setApplicationData(candidatePair); - logger.fine( - "start check for " + candidatePair.toShortString() + " tid " - + tran); + if (logger.isDebugEnabled()) + { + logger.debug( + "start check for " + candidatePair.toShortString() + " tid " + + tran); + } try { tran @@ -342,9 +329,9 @@ class ConnectivityCheckClient originalWaitInterval, maxWaitInterval, maxRetransmissions); - if (logger.isLoggable(Level.FINEST)) + if (logger.isTraceEnabled()) { - logger.finest( + logger.trace( "checking pair " + candidatePair + " tid " + tran); } } @@ -367,7 +354,7 @@ class ConnectivityCheckClient msg += " No route to host."; ex = null; } - logger.log(Level.INFO, msg, ex); + logger.info(msg + (ex == null ? "" : "\n" + ex.toString())); } } @@ -407,7 +394,7 @@ class ConnectivityCheckClient { if (!response.containsAttribute(Attribute.ERROR_CODE)) { - logger.fine("Received a malformed error response."); + logger.debug(() -> "Received a malformed error response."); return; //malformed error response } @@ -528,7 +515,7 @@ class ConnectivityCheckClient if (!response.containsAttribute(Attribute.XOR_MAPPED_ADDRESS)) { - logger.fine("Received a success response with no " + logger.debug(() -> "Received a success response with no " + "XOR_MAPPED_ADDRESS attribute."); logger.info("Pair failed (no XOR-MAPPED-ADDRESS): " + checkedPair.toShortString() + ". Local ufrag" @@ -740,7 +727,7 @@ class ConnectivityCheckClient } else { - logger.fine( + logger.debug(() -> "Keep alive for pair: " + validPair.toShortString()); } } @@ -762,7 +749,7 @@ class ConnectivityCheckClient } else { - logger.fine( + logger.debug(() -> "Keep alive for pair: " + validPair.toShortString()); } } @@ -832,7 +819,7 @@ class ConnectivityCheckClient CandidatePair pair = (CandidatePair) ev.getTransactionID().getApplicationData(); - logger.finer("Received error code " + ((int) errorCode)); + logger.trace(() -> "Received error code " + ((int) errorCode)); //RESOLVE ROLE_CONFLICTS if (errorCode == ErrorCodeAttribute.ROLE_CONFLICT) @@ -840,7 +827,7 @@ class ConnectivityCheckClient boolean wasControlling = originalRequest.containsAttribute(Attribute.ICE_CONTROLLING); - logger.finer("Switching to isControlling=" + !wasControlling); + logger.trace(() -> "Switching to isControlling=" + !wasControlling); parentAgent.setControlling(!wasControlling); pair.getParentComponent().getParentStream().getCheckList() @@ -950,7 +937,7 @@ class ConnectivityCheckClient * its final state in either the processResponse(), * processTimeout() or processFailure() method. */ - logger.finest("will skip a check beat."); + logger.trace(() -> "will skip a check beat."); checkList.fireEndOfOrdinaryChecks(); } } diff --git a/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java b/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java index 6b8f930..a094a85 100644 --- a/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java +++ b/src/main/java/org/ice4j/ice/ConnectivityCheckServer.java @@ -17,14 +17,12 @@ */ package org.ice4j.ice; -import java.util.logging.*; - import org.ice4j.*; import org.ice4j.attribute.*; import org.ice4j.message.*; import org.ice4j.security.*; import org.ice4j.stack.*; -import org.ice4j.util.Logger; // Disambiguation. +import org.jitsi.utils.logging2.*; /** * The class that would be handling and responding to incoming connectivity @@ -37,17 +35,6 @@ class ConnectivityCheckServer implements RequestListener, CredentialsAuthority { - /** - * 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 java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger( - ConnectivityCheckServer.class.getName()); - /** * Compares a and b as unsigned long values. Serves the * same purpose as the Long.compareUnsigned method available in @@ -101,7 +88,7 @@ class ConnectivityCheckServer public ConnectivityCheckServer(Agent parentAgent) { this.parentAgent = parentAgent; - logger = new Logger(classLogger, parentAgent.getLogger()); + logger = parentAgent.getLogger().createChildLogger(this.getClass().getName()); stunStack = this.parentAgent.getStunStack(); stunStack.getCredentialsManager().registerAuthority(this); @@ -141,8 +128,7 @@ class ConnectivityCheckServer public void processRequest(StunMessageEvent evt) throws IllegalArgumentException { - if(logger.isLoggable(Level.FINER)) - logger.finer("Received request " + evt); + logger.trace(() -> "Received request " + evt); alive = true; @@ -212,11 +198,9 @@ class ConnectivityCheckServer } catch (Exception e) { - logger.log( - Level.INFO, - "Failed to send " + response - + " through " + evt.getLocalAddress(), - e); + logger.info("Failed to send " + response + + " through " + evt.getLocalAddress() + "\n" + + e.toString()); //try to trigger a 500 response although if this one failed, //then chances are the 500 will fail too. throw new RuntimeException("Failed to send a response", e); @@ -249,11 +233,8 @@ class ConnectivityCheckServer //extract priority if(priorityAttr == null) { - if(logger.isLoggable(Level.FINE)) - { - logger.log(Level.FINE, "Received a connectivity check with" - + "no PRIORITY attribute. Discarding."); - } + logger.debug(() -> "Received a connectivity check with" + + "no PRIORITY attribute. Discarding."); throw new IllegalArgumentException("Missing PRIORITY attribute!"); } @@ -322,7 +303,7 @@ class ConnectivityCheckServer //role. else { - logger.finer( + logger.trace(() -> "Switching to controlled because theirTieBreaker=" + theirTieBreaker + " and ourTieBreaker=" + ourTieBreaker); @@ -345,7 +326,7 @@ class ConnectivityCheckServer //the controlling role. if(compareUnsignedLong(ourTieBreaker, theirTieBreaker) >= 0) { - logger.finer( + logger.trace(() -> "Switching to controlling because theirTieBreaker=" + theirTieBreaker + " and ourTieBreaker=" + ourTieBreaker); diff --git a/src/main/java/org/ice4j/ice/DefaultNominator.java b/src/main/java/org/ice4j/ice/DefaultNominator.java index 077cd88..77b9236 100644 --- a/src/main/java/org/ice4j/ice/DefaultNominator.java +++ b/src/main/java/org/ice4j/ice/DefaultNominator.java @@ -17,11 +17,11 @@ */ package org.ice4j.ice; +import org.jitsi.utils.logging2.*; + import java.beans.*; import java.util.*; -import org.ice4j.util.*; - /** * Implements ice4j internal nomination strategies. * @@ -30,15 +30,6 @@ import org.ice4j.util.*; public class DefaultNominator implements PropertyChangeListener { - /** - * 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 java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger(DefaultNominator.class.getName()); - /** * The Agent that created us. */ @@ -71,7 +62,7 @@ public class DefaultNominator public DefaultNominator(Agent parentAgent) { this.parentAgent = parentAgent; - logger = new Logger(classLogger, parentAgent.getLogger()); + logger = parentAgent.getLogger().createChildLogger(this.getClass().getName()); parentAgent.addStateChangeListener(this); } @@ -117,7 +108,7 @@ public class DefaultNominator // the component if (validPair.getParentComponent().getSelectedPair() != null) { - logger.fine( + logger.debug(() -> "Keep-alive for pair: " + validPair.toShortString()); return; } diff --git a/src/main/java/org/ice4j/ice/IceMediaStream.java b/src/main/java/org/ice4j/ice/IceMediaStream.java index 8056e3a..a223c5a 100644 --- a/src/main/java/org/ice4j/ice/IceMediaStream.java +++ b/src/main/java/org/ice4j/ice/IceMediaStream.java @@ -19,9 +19,10 @@ package org.ice4j.ice; import java.beans.*; import java.util.*; -import java.util.logging.Logger; import org.ice4j.*; +import org.jitsi.utils.collections.*; +import org.jitsi.utils.logging2.*; /** * The class represents a media stream from the ICE perspective, i.e. a @@ -32,15 +33,6 @@ import org.ice4j.*; */ public class IceMediaStream { - /** - * 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 classLogger = - Logger.getLogger(IceMediaStream.class.getName()); - /** * The property name that we use when delivering events notifying listeners * that the consent freshness of a pair has changed. @@ -150,7 +142,7 @@ public class IceMediaStream /** * The {@link Logger} used by {@link IceMediaStream} instances. */ - private org.ice4j.util.Logger logger; + private Logger logger; /** * Initializes a new IceMediaStream object. @@ -161,7 +153,7 @@ public class IceMediaStream */ protected IceMediaStream(Agent parentAgent, String name) { - logger = new org.ice4j.util.Logger(classLogger, parentAgent.getLogger()); + logger = parentAgent.getLogger().createChildLogger(IceMediaStream.class.getName(), JMap.of("name", name)); this.name = name; this.parentAgent = parentAgent; checkList = new CheckList(this); @@ -182,8 +174,8 @@ public class IceMediaStream * the stream first. */ protected Component createComponent( - KeepAliveStrategy keepAliveStrategy, - boolean useComponentSocket) + KeepAliveStrategy keepAliveStrategy, + boolean useComponentSocket) { Component component; @@ -194,7 +186,8 @@ public class IceMediaStream ++lastComponentID, this, keepAliveStrategy, - useComponentSocket); + useComponentSocket, + logger); components.put(component.getComponentID(), component); } @@ -367,7 +360,7 @@ public class IceMediaStream orderCheckList(); pruneCheckList(checkList); - logger.finest("Checklist initialized."); + logger.trace(() -> "Checklist initialized."); } } @@ -921,4 +914,9 @@ public class IceMediaStream { return remotePassword; } + + public Logger getLogger() + { + return logger; + } } diff --git a/src/main/java/org/ice4j/ice/LocalCandidate.java b/src/main/java/org/ice4j/ice/LocalCandidate.java index b362b0d..076d46e 100644 --- a/src/main/java/org/ice4j/ice/LocalCandidate.java +++ b/src/main/java/org/ice4j/ice/LocalCandidate.java @@ -19,12 +19,11 @@ package org.ice4j.ice; import java.io.*; import java.net.*; -import java.util.logging.*; import org.ice4j.*; import org.ice4j.socket.*; import org.ice4j.stack.*; -import org.ice4j.util.Logger; // Disambiguation. +import org.jitsi.utils.logging2.*; /** * LocalCandidates are obtained by an agent for every stream component @@ -66,7 +65,7 @@ public abstract class LocalCandidate /** * The {@link Logger} used by {@link LocalCandidate} instances. */ - private Logger logger; + private final Logger logger; /** * Creates a LocalCandidate instance for the specified transport @@ -91,12 +90,10 @@ public abstract class LocalCandidate CandidateType type, CandidateExtendedType extendedType, LocalCandidate relatedCandidate) + { super(transportAddress, parentComponent, type, relatedCandidate); - logger - = new Logger(classLogger, - parentComponent. - getParentStream().getParentAgent().getLogger()); + logger = parentComponent.getLogger().createChildLogger(this.getClass().getName()); this.extendedType = extendedType; } @@ -205,8 +202,7 @@ public abstract class LocalCandidate } catch (SocketException sex) //don't u just luv da name? ;) { - logger.log(Level.SEVERE, - "Failed to acquire Socket" + logger.error("Failed to acquire Socket" + " specific to STUN communication.", sex); exception = sex; @@ -262,8 +258,7 @@ public abstract class LocalCandidate } catch (SocketException sex) //don't u just luv da name? ;) { - logger.log(Level.SEVERE, - "Failed to acquire DatagramSocket" + logger.error("Failed to acquire DatagramSocket" + " specific to STUN communication.", sex); exception = sex; diff --git a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java index 147d32e..ed38629 100644 --- a/src/main/java/org/ice4j/socket/MergingDatagramSocket.java +++ b/src/main/java/org/ice4j/socket/MergingDatagramSocket.java @@ -18,13 +18,12 @@ package org.ice4j.socket; import org.ice4j.*; +import org.jitsi.utils.logging2.*; import java.io.*; import java.net.*; import java.util.*; import java.util.concurrent.*; -import java.util.logging.*; -import org.ice4j.util.Logger; //Disambiguation /** * A {@link DatagramSocket} implementation which merges a set of sockets. @@ -44,14 +43,6 @@ import org.ice4j.util.Logger; //Disambiguation public class MergingDatagramSocket extends DatagramSocket { - /** - * The {@link Logger} used by the {@link MergingDatagramSocket} class and - * its instances for logging output. - */ - private static final java.util.logging.Logger classLogger - = java.util.logging.Logger.getLogger( - MergingDatagramSocket.class.getName()); - /** * Used to control access to {@link #socketContainers}. */ @@ -97,7 +88,7 @@ public class MergingDatagramSocket /** * The {@link Logger} used by {@link MergingDatagramSocket} instances. */ - private final Logger logger; + protected final Logger logger; /** * Initializes a new {@link MergingDatagramSocket} instance. @@ -111,14 +102,14 @@ public class MergingDatagramSocket /** * Initializes a new {@link MergingDatagramSocket} instance. - * @param levelDelegate the {@link Logger} instance which dictates the - * logging level for the new {@link MergingDatagramSocket} instance. + * @param parentLogger the {@link Logger} parent logger from which we'll + * derive this class' logger instance * @throws SocketException */ - public MergingDatagramSocket(Logger levelDelegate) + public MergingDatagramSocket(Logger parentLogger) throws SocketException { - logger = new Logger(classLogger, levelDelegate); + logger = parentLogger.createChildLogger(this.getClass().getName()); } /** @@ -220,11 +211,8 @@ public class MergingDatagramSocket public void add(DelegatingSocket socket) { Objects.requireNonNull(socket, "socket"); - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Adding a DelegatingSocket instance: " - + socket.getLocalAddress()); - } + logger.debug(() -> "Adding a DelegatingSocket instance: " + + socket.getLocalAddress()); doAdd(socket); } @@ -251,11 +239,8 @@ public class MergingDatagramSocket public void add(DatagramSocket socket) { Objects.requireNonNull(socket, "socket"); - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Adding a DatagramSocket instance: " - + socket.getLocalAddress()); - } + logger.debug(() -> "Adding a DatagramSocket instance: " + + socket.getLocalAddress()); doAdd(socket); } @@ -280,7 +265,7 @@ public class MergingDatagramSocket { if (indexOf(socketContainers, socket) != -1) { - logger.warning("Socket already added."); + logger.warn("Socket already added."); return; } @@ -361,7 +346,7 @@ public class MergingDatagramSocket if (socketContainer == active) { // TODO: proper selection of a new active socket - logger.warning( + logger.warn( "Removing the active socket. Won't be able to send " + "until a new one is elected."); active = null; @@ -369,13 +354,13 @@ public class MergingDatagramSocket } else { - logger.severe("Cannot find socket to remove."); + logger.error("Cannot find socket to remove."); } } - if (logger.isLoggable(Level.FINE)) + if (logger.isDebugEnabled()) { - logger.fine("Removed: " + socketContainer); + logger.debug("Removed: " + socketContainer); } if (socketContainer != null) { @@ -595,10 +580,10 @@ public class MergingDatagramSocket socket = socketWrapper.getUDPSocket(); } - if (logger.isLoggable(Level.FINE)) + if (logger.isDebugEnabled()) { - logger.fine("Initializing the active container, socket=" + socket - + "; remote address=" + remoteAddress); + logger.debug("Initializing the active container, socket=" + socket + + "; remote address=" + remoteAddress); } synchronized (socketContainersSyncRoot) @@ -611,7 +596,7 @@ public class MergingDatagramSocket // is not necessarily incorrect. // Still, go on and replace the active socket with whatever // ICE selected. - logger.warning("Active socket already initialized."); + logger.warn("Active socket already initialized."); } SocketContainer newActive = null; @@ -627,7 +612,7 @@ public class MergingDatagramSocket if (newActive == null) { - logger.severe("No SocketContainer found!"); + logger.error("No SocketContainer found!"); return; } @@ -742,12 +727,9 @@ public class MergingDatagramSocket + getLocalSocketAddress() + " -> " + getRemoteSocketAddress()); - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Starting the thread for socket " - + getLocalSocketAddress() + " -> " - + getRemoteSocketAddress()); - } + logger.debug(() -> "Starting the thread for socket " + + getLocalSocketAddress() + " -> " + + getRemoteSocketAddress()); thread.start(); } @@ -810,10 +792,7 @@ public class MergingDatagramSocket // container anymore. close(true); - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Finished: " + toString()); - } + logger.debug(() -> "Finished: " + toString()); } /** @@ -874,11 +853,7 @@ public class MergingDatagramSocket synchronized (socketContainersSyncRoot) { MergingDatagramSocket.this.active = this; - if (logger.isLoggable(Level.FINE)) - { - logger.fine("Switching to new active socket: " - + this); - } + logger.debug(() -> "Switching to new active socket: " + this); } } } diff --git a/src/main/java/org/ice4j/util/Logger.java b/src/main/java/org/ice4j/util/Logger.java deleted file mode 100644 index 206909b..0000000 --- a/src/main/java/org/ice4j/util/Logger.java +++ /dev/null @@ -1,234 +0,0 @@ -/* - * 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); - } -}