1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-09 00:59:39 +02:00

Rework Smack debugger.

Also fixes SMACK-728.
This commit is contained in:
Florian Schmaus 2017-07-28 11:59:11 +02:00
parent 104146c5ed
commit b8ee8d808f
24 changed files with 328 additions and 390 deletions

View file

@ -17,9 +17,6 @@
package org.jivesoftware.smackx.debugger.slf4j;
import java.io.Reader;
import java.io.Writer;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
@ -27,9 +24,15 @@ import org.jivesoftware.smack.debugger.SmackDebuggerFactory;
/**
* Implementation of SmackDebuggerFactory which always creates instance of SLF4JSmackDebugger.
*/
public class SLF4JDebuggerFactory implements SmackDebuggerFactory {
public final class SLF4JDebuggerFactory implements SmackDebuggerFactory {
public static final SLF4JDebuggerFactory INSTANCE = new SLF4JDebuggerFactory();
private SLF4JDebuggerFactory() {
}
@Override
public SmackDebugger create(XMPPConnection connection, Writer writer, Reader reader) throws IllegalArgumentException {
return new SLF4JSmackDebugger(connection, writer, reader);
public SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException {
return new SLF4JSmackDebugger(connection);
}
}

View file

@ -1,41 +0,0 @@
/**
*
* Copyright 2014 Vyacheslav Blinov
*
* 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.jivesoftware.smackx.debugger.slf4j;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.packet.Stanza;
import org.slf4j.Logger;
class SLF4JLoggingPacketListener implements StanzaListener {
private final Logger logger;
private final String prefix;
public SLF4JLoggingPacketListener(Logger logger, String prefix) {
this.logger = Validate.notNull(logger);
this.prefix = Validate.notNull(prefix);
}
@Override
public void processStanza(Stanza packet) {
if (SLF4JSmackDebugger.printInterpreted.get() && logger.isDebugEnabled()) {
logger.debug("{}: PKT [{}] '{}'", prefix, packet.getClass().getName(), packet.toXML());
}
}
}

View file

@ -22,9 +22,9 @@ import java.io.Writer;
import java.util.concurrent.atomic.AtomicBoolean;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.StanzaListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.debugger.SmackDebugger;
import org.jivesoftware.smack.packet.TopLevelStreamElement;
import org.jivesoftware.smack.util.ObservableReader;
import org.jivesoftware.smack.util.ObservableWriter;
@ -38,7 +38,8 @@ import org.slf4j.LoggerFactory;
* Use in conjunction with your SLF4J bindings of choice.
* See SLF4J manual for more details about bindings usage.
*/
public class SLF4JSmackDebugger implements SmackDebugger {
public class SLF4JSmackDebugger extends SmackDebugger {
public static final String LOGGER_NAME = "SMACK";
private static final Logger logger = LoggerFactory.getLogger(LOGGER_NAME);
public static final AtomicBoolean printInterpreted = new AtomicBoolean(true);
@ -46,10 +47,6 @@ public class SLF4JSmackDebugger implements SmackDebugger {
public static final String SENT_TAG = "SENT";
public static final String RECEIVED_TAG = "RECV";
private final XMPPConnection connection;
private final StanzaListener receivedListener = new SLF4JLoggingPacketListener(logger, RECEIVED_TAG);
private final StanzaListener sentListener = new SLF4JLoggingPacketListener(logger, SENT_TAG);
private final SLF4JRawXmlListener slf4JRawXmlListener = new SLF4JRawXmlListener(logger);
private ObservableWriter writer;
@ -59,17 +56,16 @@ public class SLF4JSmackDebugger implements SmackDebugger {
* Makes Smack use this Debugger.
*/
public static void enable() {
SmackConfiguration.setDebuggerFactory(new SLF4JDebuggerFactory());
SmackConfiguration.DEBUG = true;
SmackConfiguration.setDefaultSmackDebuggerFactory(SLF4JDebuggerFactory.INSTANCE);
}
/**
* Create new SLF4J Smack Debugger instance.
* @param connection Smack connection to debug
* @param writer connection data writer to observe
* @param reader connection data reader to observe
*/
public SLF4JSmackDebugger(XMPPConnection connection, Writer writer, Reader reader) {
this.connection = connection;
SLF4JSmackDebugger(XMPPConnection connection) {
super(connection);
this.writer = new ObservableWriter(writer);
this.writer.addWriterListener(slf4JRawXmlListener);
this.reader = new ObservableReader(Validate.notNull(reader));
@ -101,22 +97,17 @@ public class SLF4JSmackDebugger implements SmackDebugger {
}
@Override
public Reader getReader() {
return reader;
public void onIncomingStreamElement(TopLevelStreamElement streamElement) {
if (SLF4JSmackDebugger.printInterpreted.get() && logger.isDebugEnabled()) {
logger.debug("IN {}: {}", streamElement.getClass().getName(), streamElement.toXML());
}
}
@Override
public Writer getWriter() {
return writer;
public void onOutgoingStreamElement(TopLevelStreamElement streamElement) {
if (SLF4JSmackDebugger.printInterpreted.get() && logger.isDebugEnabled()) {
logger.debug("OUT {}: {}", streamElement.getClass().getName(), streamElement.toXML());
}
}
@Override
public StanzaListener getReaderListener() {
return receivedListener;
}
@Override
public StanzaListener getWriterListener() {
return sentListener;
}
}