Skip to content

Commit

Permalink
Use Slf4j for logs
Browse files Browse the repository at this point in the history
Closes #72
  • Loading branch information
ibauersachs committed Sep 2, 2019
1 parent 512b61a commit 9de972d
Show file tree
Hide file tree
Showing 15 changed files with 87 additions and 120 deletions.
11 changes: 11 additions & 0 deletions pom.xml
Expand Up @@ -198,6 +198,17 @@
</build>

<dependencies>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.28</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.8</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
Expand Down
7 changes: 3 additions & 4 deletions src/main/java/org/xbill/DNS/Cache.java
Expand Up @@ -9,6 +9,7 @@
import java.util.List;
import java.util.Map;
import java.util.Set;
import lombok.extern.slf4j.Slf4j;

/**
* A cache of DNS records. The cache obeys TTLs, so items are purged after their validity period is
Expand All @@ -20,6 +21,7 @@
* @see Credibility
* @author Brian Wellington
*/
@Slf4j
public class Cache {

private interface Element {
Expand Down Expand Up @@ -604,7 +606,6 @@ public SetResponse addMessage(Message in) {
boolean completed = false;
RRset[] answers, auth, addl;
SetResponse response = null;
boolean verbose = Options.check("verbosecache");
HashSet<Name> additionalNames;

if ((rcode != Rcode.NOERROR && rcode != Rcode.NXDOMAIN) || question == null) {
Expand Down Expand Up @@ -720,9 +721,7 @@ public SetResponse addMessage(Message in) {
cred = getCred(Section.ADDITIONAL, isAuth);
addRRset(rRset, cred);
}
if (verbose) {
System.out.println("addMessage: " + response);
}
log.debug("addMessage: {}", response);
return (response);
}

Expand Down
6 changes: 4 additions & 2 deletions src/main/java/org/xbill/DNS/Client.java
Expand Up @@ -8,8 +8,10 @@
import java.nio.channels.SelectableChannel;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import lombok.extern.slf4j.Slf4j;
import org.xbill.DNS.utils.hexdump;

@Slf4j
class Client {

protected long endTime;
Expand Down Expand Up @@ -52,8 +54,8 @@ protected static void blockUntil(SelectionKey key, long endTime) throws IOExcept

protected static void verboseLog(
String prefix, SocketAddress local, SocketAddress remote, byte[] data) {
if (Options.check("verbosemsg")) {
System.err.println(hexdump.dump(prefix, data));
if (log.isDebugEnabled()) {
log.debug(hexdump.dump(prefix, data));
}
if (packetLogger != null) {
packetLogger.log(prefix, local, remote, data);
Expand Down
12 changes: 5 additions & 7 deletions src/main/java/org/xbill/DNS/Compression.java
Expand Up @@ -2,13 +2,16 @@

package org.xbill.DNS;

import lombok.extern.slf4j.Slf4j;

/**
* DNS Name Compression object.
*
* @see Message
* @see Name
* @author Brian Wellington
*/
@Slf4j
public class Compression {

private static class Entry {
Expand All @@ -20,7 +23,6 @@ private static class Entry {
private static final int TABLE_SIZE = 17;
private static final int MAX_POINTER = 0x3FFF;
private Entry[] table;
private boolean verbose = Options.check("verbosecompression");

/** Creates a new Compression object. */
public Compression() {
Expand All @@ -43,9 +45,7 @@ public void add(int pos, Name name) {
entry.pos = pos;
entry.next = table[row];
table[row] = entry;
if (verbose) {
System.err.println("Adding " + name + " at " + pos);
}
log.debug("Adding {} at {}", name, pos);
}

/**
Expand All @@ -62,9 +62,7 @@ public int get(Name name) {
pos = entry.pos;
}
}
if (verbose) {
System.err.println("Looking for " + name + ", found " + pos);
}
log.debug("Looking for {}, found {}", name, pos);
return pos;
}
}
10 changes: 4 additions & 6 deletions src/main/java/org/xbill/DNS/ExtendedResolver.java
Expand Up @@ -8,6 +8,7 @@
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
* An implementation of Resolver that can send queries to multiple servers, sending the queries
Expand All @@ -16,6 +17,7 @@
* @see Resolver
* @author Brian Wellington
*/
@Slf4j
public class ExtendedResolver implements Resolver {

private static class Resolution implements ResolverListener {
Expand Down Expand Up @@ -137,9 +139,7 @@ public void startAsync(ResolverListener listener) {
*/
@Override
public void receiveMessage(Object id, Message m) {
if (Options.check("verbose")) {
System.err.println("ExtendedResolver: " + "received message");
}
log.debug("received message");
synchronized (this) {
if (done) {
return;
Expand All @@ -160,9 +160,7 @@ public void receiveMessage(Object id, Message m) {
*/
@Override
public void handleException(Object id, Exception e) {
if (Options.check("verbose")) {
System.err.println("ExtendedResolver: got " + e);
}
log.debug("resolving failed", e);
synchronized (this) {
outstanding--;
if (done) {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/org/xbill/DNS/Lookup.java
Expand Up @@ -9,6 +9,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import lombok.extern.slf4j.Slf4j;

/**
* The Lookup object issues queries to caching DNS servers. The input consists of a name, an
Expand All @@ -25,6 +26,7 @@
* @see ResolverConfig
* @author Brian Wellington
*/
@Slf4j
public final class Lookup {

private static Resolver defaultResolver;
Expand All @@ -40,7 +42,6 @@ public final class Lookup {
private Name name;
private int type;
private int dclass;
private boolean verbose;
private int iterations;
private boolean foundAlias;
private boolean done;
Expand Down Expand Up @@ -232,7 +233,6 @@ public Lookup(Name name, int type, int dclass) {
this.cache = getDefaultCache(dclass);
}
this.credibility = Credibility.NORMAL;
this.verbose = Options.check("verbose");
this.result = -1;
}

Expand Down Expand Up @@ -436,9 +436,9 @@ private void processResponse(Name name, SetResponse response) {

private void lookup(Name current) {
SetResponse sr = cache.lookupRecords(current, type, credibility);
if (verbose) {
System.err.println("lookup " + current + " " + Type.string(type));
System.err.println(sr);
if (log.isDebugEnabled()) {
log.debug("lookup {} {}", current, Type.string(type));
log.debug(sr.toString());
}
processResponse(current, sr);
if (done || doneCurrent) {
Expand All @@ -451,6 +451,8 @@ private void lookup(Name current) {
try {
response = resolver.send(query);
} catch (IOException e) {
log.debug("Lookup failed", e);

// A network error occurred. Press on.
if (e instanceof InterruptedIOException) {
timedout = true;
Expand Down Expand Up @@ -479,9 +481,9 @@ private void lookup(Name current) {
if (sr == null) {
sr = cache.lookupRecords(current, type, credibility);
}
if (verbose) {
System.err.println("queried " + current + " " + Type.string(type));
System.err.println(sr);
if (log.isDebugEnabled()) {
log.debug("queried {} {}", current, Type.string(type));
log.debug(sr.toString());
}
processResponse(current, sr);
}
Expand Down
10 changes: 4 additions & 6 deletions src/main/java/org/xbill/DNS/Name.java
Expand Up @@ -5,12 +5,14 @@
import java.io.IOException;
import java.io.Serializable;
import java.text.DecimalFormat;
import lombok.extern.slf4j.Slf4j;

/**
* A representation of a domain name. It may either be absolute (fully qualified) or relative.
*
* @author Brian Wellington
*/
@Slf4j
public class Name implements Comparable, Serializable {

private static final long serialVersionUID = -7257019940971525644L;
Expand Down Expand Up @@ -369,9 +371,7 @@ public Name(DNSInput in) throws WireParseException {
case LABEL_COMPRESSION:
pos = in.readU8();
pos += ((len & ~LABEL_MASK) << 8);
if (Options.check("verbosecompression")) {
System.err.println("currently " + in.current() + ", pointer to " + pos);
}
log.debug("currently {}, pointer to {}", in.current(), pos);

if (pos >= in.current() - 2) {
throw new WireParseException("bad compression");
Expand All @@ -381,9 +381,7 @@ public Name(DNSInput in) throws WireParseException {
savedState = true;
}
in.jump(pos);
if (Options.check("verbosecompression")) {
System.err.println("current name '" + this + "', seeking to " + pos);
}
log.debug("current name '{}', seeking to {}", this, pos);
break;
default:
throw new WireParseException("bad label type");
Expand Down
9 changes: 2 additions & 7 deletions src/main/java/org/xbill/DNS/Options.java
Expand Up @@ -8,14 +8,9 @@

/**
* Boolean options:<br>
* bindttl - Print TTLs in BIND format<br>
* BINDTTL - Print TTLs in BIND format<br>
* multiline - Print records in multiline format<br>
* noprintin - Don't print the class of a record if it's IN<br>
* verbose - Turn on general debugging statements<br>
* verbosemsg - Print all messages sent or received by SimpleResolver<br>
* verbosecompression - Print messages related to name compression<br>
* verbosesec - Print messages related to signature verification<br>
* verbosecache - Print messages related to cache lookups<br>
* noPrintIN - Don't print the class of a record if it's IN<br>
* <br>
* Valued options:<br>
* tsigfudge=n - Sets the default TSIG fudge value (in seconds)<br>
Expand Down
14 changes: 5 additions & 9 deletions src/main/java/org/xbill/DNS/ResolverConfig.java
Expand Up @@ -15,6 +15,7 @@
import java.util.Locale;
import java.util.ResourceBundle;
import java.util.StringTokenizer;
import lombok.extern.slf4j.Slf4j;

/**
* A class that tries to locate name servers and the search path to be appended to unqualified
Expand All @@ -40,6 +41,7 @@
* @author <a href="mailto:yannick@meudal.net">Yannick Meudal</a>
* @author <a href="mailto:arnt@gulbrandsen.priv.no">Arnt Gulbrandsen</a>
*/
@Slf4j
public class ResolverConfig {

public static final String DNS_SERVER_PROP = "dns.server";
Expand Down Expand Up @@ -81,17 +83,13 @@ private void addServer(String server, List<String> list) {
if (list.contains(server)) {
return;
}
if (Options.check("verbose")) {
System.out.println("adding server " + server);
}
log.debug("adding server {}", server);
list.add(server);
}

private void addSearch(String search, List<Name> list) {
Name name;
if (Options.check("verbose")) {
System.out.println("adding search " + search);
}
log.debug("adding search {}", search);
try {
name = Name.fromString(search, Name.root);
} catch (TextParseException e) {
Expand All @@ -108,9 +106,7 @@ private int parseNdots(String token) {
try {
int ndots = Integer.parseInt(token);
if (ndots >= 0) {
if (Options.check("verbose")) {
System.out.println("setting ndots " + token);
}
log.debug("setting ndots {}", token);
return ndots;
}
} catch (NumberFormatException e) {
Expand Down
18 changes: 5 additions & 13 deletions src/main/java/org/xbill/DNS/SimpleResolver.java
Expand Up @@ -7,6 +7,7 @@
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.List;
import lombok.extern.slf4j.Slf4j;

/**
* An implementation of Resolver that sends one query to one server. SimpleResolver handles TCP
Expand All @@ -17,6 +18,7 @@
* @see OPTRecord
* @author Brian Wellington
*/
@Slf4j
public class SimpleResolver implements Resolver {

/** The default port to send queries to */
Expand Down Expand Up @@ -178,9 +180,6 @@ private Message parseMessage(byte[] b) throws WireParseException {
try {
return (new Message(b));
} catch (IOException e) {
if (Options.check("verbose")) {
e.printStackTrace();
}
if (!(e instanceof WireParseException)) {
e = new WireParseException("Error parsing message");
}
Expand All @@ -193,9 +192,7 @@ private void verifyTSIG(Message query, Message response, byte[] b, TSIG tsig) {
return;
}
int error = tsig.verify(response, b, query.getTSIG());
if (Options.check("verbose")) {
System.err.println("TSIG verify: " + Rcode.TSIGstring(error));
}
log.debug("TSIG verify: {}", Rcode.TSIGstring(error));
}

private void applyEDNS(Message query) {
Expand Down Expand Up @@ -224,10 +221,7 @@ private int maxUDPSize(Message query) {
*/
@Override
public Message send(Message query) throws IOException {
if (Options.check("verbose")) {
System.err.println(
"Sending to " + address.getAddress().getHostAddress() + ":" + address.getPort());
}
log.debug("Sending to {}:{}", address.getAddress().getHostAddress(), address.getPort());

if (query.getHeader().getOpcode() == Opcode.QUERY) {
Record question = query.getQuestion();
Expand Down Expand Up @@ -277,9 +271,7 @@ public Message send(Message query) throws IOException {
if (tcp) {
throw new WireParseException(error);
} else {
if (Options.check("verbose")) {
System.err.println(error);
}
log.debug(error);
continue;
}
}
Expand Down

0 comments on commit 9de972d

Please sign in to comment.