Print date in the log.

git-svn-id: http://svn.igniterealtime.org/svn/repos/smack/trunk@2500 b35dd754-fafc-0310-a699-88a17e54d16e
This commit is contained in:
Gaston Dombiak 2005-06-08 22:00:08 +00:00 committed by gaston
parent 3c4441db96
commit d64eb0ffdb
1 changed files with 33 additions and 13 deletions

View File

@ -8,12 +8,14 @@ import org.jivesoftware.smack.util.*;
import java.io.Reader; import java.io.Reader;
import java.io.Writer; import java.io.Writer;
import java.text.SimpleDateFormat;
import java.util.Date;
/** /**
* Very simple debugger that prints to the console (stdout) the sent and received stanzas. Use * 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 * this debugger with caution since printing to the console is an expensive operation that may
* even block the thread since only one thread may print at a time.<p> * even block the thread since only one thread may print at a time.<p>
* * <p/>
* It is possible to not only print the raw sent and received stanzas but also the interpreted * 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 * 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>. * just change the <tt>printInterpreted</tt> static variable to <tt>true</tt>.
@ -23,6 +25,7 @@ import java.io.Writer;
public class ConsoleDebugger implements SmackDebugger { public class ConsoleDebugger implements SmackDebugger {
public static boolean printInterpreted = false; public static boolean printInterpreted = false;
private SimpleDateFormat dateFormatter = new SimpleDateFormat("hh:mm:ss aaa");
private XMPPConnection connection = null; private XMPPConnection connection = null;
@ -48,19 +51,25 @@ public class ConsoleDebugger implements SmackDebugger {
// Create a special Reader that wraps the main Reader and logs data to the GUI. // Create a special Reader that wraps the main Reader and logs data to the GUI.
ObservableReader debugReader = new ObservableReader(reader); ObservableReader debugReader = new ObservableReader(reader);
readerListener = new ReaderListener() { readerListener = new ReaderListener() {
public void read(String str) { public void read(String str) {
System.out.println("RCV (" + connection.hashCode() + "): " + str); System.out.println(
} dateFormatter.format(new Date()) + " RCV (" + connection.hashCode() +
}; "): " +
str);
}
};
debugReader.addReaderListener(readerListener); debugReader.addReaderListener(readerListener);
// Create a special Writer that wraps the main Writer and logs data to the GUI. // Create a special Writer that wraps the main Writer and logs data to the GUI.
ObservableWriter debugWriter = new ObservableWriter(writer); ObservableWriter debugWriter = new ObservableWriter(writer);
writerListener = new WriterListener() { writerListener = new WriterListener() {
public void write(String str) { public void write(String str) {
System.out.println("SENT (" + connection.hashCode() + "): " + str); System.out.println(
} dateFormatter.format(new Date()) + " SENT (" + connection.hashCode() +
}; "): " +
str);
}
};
debugWriter.addWriterListener(writerListener); debugWriter.addWriterListener(writerListener);
// Assign the reader/writer objects to use the debug versions. The packet reader // Assign the reader/writer objects to use the debug versions. The packet reader
@ -74,18 +83,29 @@ public class ConsoleDebugger implements SmackDebugger {
listener = new PacketListener() { listener = new PacketListener() {
public void processPacket(Packet packet) { public void processPacket(Packet packet) {
if (printInterpreted) { if (printInterpreted) {
System.out.println("RCV PKT (" + connection.hashCode() + "): " + packet.toXML()); System.out.println(
dateFormatter.format(new Date()) + " RCV PKT (" +
connection.hashCode() +
"): " +
packet.toXML());
} }
} }
}; };
connListener = new ConnectionListener() { connListener = new ConnectionListener() {
public void connectionClosed() { public void connectionClosed() {
System.out.println("Connection closed (" + connection.hashCode() + ")"); System.out.println(
dateFormatter.format(new Date()) + " Connection closed (" +
connection.hashCode() +
")");
} }
public void connectionClosedOnError(Exception e) { public void connectionClosedOnError(Exception e) {
System.out.println("Connection closed due to an exception (" + connection.hashCode() + ")"); System.out.println(
dateFormatter.format(new Date()) +
" Connection closed due to an exception (" +
connection.hashCode() +
")");
e.printStackTrace(); e.printStackTrace();
} }
}; };
@ -94,7 +114,7 @@ public class ConsoleDebugger implements SmackDebugger {
public void userHasLogged(String user) { public void userHasLogged(String user) {
boolean isAnonymous = "".equals(StringUtils.parseName(user)); boolean isAnonymous = "".equals(StringUtils.parseName(user));
String title = String title =
"User logged (" + connection.hashCode() + "): " "User logged (" + connection.hashCode() + "): "
+ (isAnonymous ? "" : StringUtils.parseBareAddress(user)) + (isAnonymous ? "" : StringUtils.parseBareAddress(user))
+ "@" + "@"
+ connection.getHost() + connection.getHost()