2013-02-07 15:19:47 +01:00
|
|
|
/**
|
|
|
|
*
|
2014-02-17 18:57:38 +01:00
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2013-02-07 15:19:47 +01:00
|
|
|
* 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.
|
|
|
|
*/
|
2005-06-07 22:43:02 +02:00
|
|
|
package org.jivesoftware.smack.debugger;
|
|
|
|
|
2015-03-17 21:19:06 +01:00
|
|
|
import java.io.PrintWriter;
|
|
|
|
import java.io.StringWriter;
|
2005-06-09 00:00:08 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Date;
|
2005-06-07 22:43:02 +02:00
|
|
|
|
2017-06-14 17:12:43 +02:00
|
|
|
import org.jivesoftware.smack.XMPPConnection;
|
|
|
|
|
2005-06-07 22:43:02 +02:00
|
|
|
/**
|
|
|
|
* Very simple debugger that prints to the console (stdout) the sent and received stanzas. Use
|
|
|
|
* this debugger with caution since printing to the console is an expensive operation that may
|
2017-12-25 12:51:41 +01:00
|
|
|
* even block the thread since only one thread may print at a time.
|
|
|
|
* <p>
|
2005-06-07 22:43:02 +02:00
|
|
|
* It is possible to not only print the raw sent and received stanzas but also the interpreted
|
|
|
|
* packets by Smack. By default interpreted packets won't be printed. To enable this feature
|
|
|
|
* just change the <tt>printInterpreted</tt> static variable to <tt>true</tt>.
|
2017-12-25 12:51:41 +01:00
|
|
|
* </p>
|
2005-06-07 22:43:02 +02:00
|
|
|
*
|
|
|
|
* @author Gaston Dombiak
|
|
|
|
*/
|
2014-11-21 18:21:54 +01:00
|
|
|
public class ConsoleDebugger extends AbstractDebugger {
|
2016-10-20 17:44:23 +02:00
|
|
|
private final SimpleDateFormat dateFormatter = new SimpleDateFormat("HH:mm:ss");
|
2005-06-07 22:43:02 +02:00
|
|
|
|
2017-07-28 11:59:11 +02:00
|
|
|
public ConsoleDebugger(XMPPConnection connection) {
|
|
|
|
super(connection);
|
2005-06-07 22:43:02 +02:00
|
|
|
}
|
|
|
|
|
2014-11-21 18:21:54 +01:00
|
|
|
@Override
|
|
|
|
protected void log(String logMessage) {
|
|
|
|
String formatedDate;
|
|
|
|
synchronized (dateFormatter) {
|
|
|
|
formatedDate = dateFormatter.format(new Date());
|
|
|
|
}
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:OFF
|
2014-11-21 18:21:54 +01:00
|
|
|
System.out.println(formatedDate + ' ' + logMessage);
|
2015-03-17 21:19:06 +01:00
|
|
|
// CHECKSTYLE:ON
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void log(String logMessage, Throwable throwable) {
|
|
|
|
StringWriter sw = new StringWriter();
|
|
|
|
PrintWriter pw = new PrintWriter(sw);
|
|
|
|
// CHECKSTYLE:OFF
|
|
|
|
throwable.printStackTrace(pw);
|
|
|
|
// CHECKSTYLE:ON
|
|
|
|
log(logMessage + sw);
|
2005-08-27 04:23:36 +02:00
|
|
|
}
|
|
|
|
|
2017-07-28 11:59:11 +02:00
|
|
|
public static final class Factory implements SmackDebuggerFactory {
|
|
|
|
|
|
|
|
public static final SmackDebuggerFactory INSTANCE = new Factory();
|
|
|
|
|
|
|
|
private Factory() {
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public SmackDebugger create(XMPPConnection connection) throws IllegalArgumentException {
|
|
|
|
return new ConsoleDebugger(connection);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2005-06-07 22:43:02 +02:00
|
|
|
}
|