Add toString() method to default connection
[xudocci.git] / src / main / java / net / pterodactylus / irc / DefaultConnection.java
index 876aefb..d352b55 100644 (file)
@@ -316,7 +316,7 @@ public class DefaultConnection extends AbstractExecutionThreadService implements
                        while (connected) {
                                Reply reply = connectionHandler.readReply();
                                eventBus.post(new ReplyReceived(this, reply));
-                               logger.trace(String.format("<< %s", reply));
+                               logger.trace(String.format("<< %s", addEscapeCharacters(reply.toString())));
                                String command = reply.command();
                                List<String> parameters = reply.parameters();
 
@@ -378,6 +378,18 @@ public class DefaultConnection extends AbstractExecutionThreadService implements
        // PRIVATE METHODS
        //
 
+       private String addEscapeCharacters(String line) {
+               StringBuilder escaped = new StringBuilder();
+               for (char c : line.toCharArray()) {
+                       if (c < 32) {
+                               escaped.append("\\CTRL[").append((int) c).append("]");
+                       } else {
+                               escaped.append(c);
+                       }
+               }
+               return escaped.toString();
+       }
+
        /**
         * Returns an item from the list, or {@link Optional#empty()} if the list is
         * shorter than required for the given index.
@@ -398,6 +410,11 @@ public class DefaultConnection extends AbstractExecutionThreadService implements
                return Optional.empty();
        }
 
+       @Override
+       public String toString() {
+               return String.format("→ %s:%d", hostname, port);
+       }
+
        /** Handles input and output for the connection. */
        private class ConnectionHandler implements Closeable {