Browse Source

Packet logging for JGit

Imitate the packet tracing feature from C Git v1.7.5-rc0~58^2~1 (add
packet tracing debug code, 2011-02-24).  Unlike C Git, use the log4j
log level setting instead of the GIT_TRACE_PACKET environment variable
to enable tracing.

Tested as follows:

 1. Enable tracing by adding the lines

	log4j.logger.org.eclipse.jgit.transport=DEBUG, stderr
	log4j.additivity.org.eclipse.jgit.transport=false

    to org.eclipse.jgit.pgm/resources/log4j.properties.

 2. mvn package

 3. org.eclipse.jgit.pgm/target/jgit \
	ls-remote git://git.kernel.org/pub/scm/git/git 2>&1 |less

Then the output provides a trace of packets sent and received over
the wire:

  2016-08-24 16:36:42 DEBUG PacketLineOut:145 - git> git-upload-pack /pub/scm/git/git^@host=git.kernel.org^@
  2016-08-24 16:36:42 DEBUG PacketLineIn:165 - git< 2632c897f74b1cc9b5533f467da459b9ec725538 HEAD^@multi_ack thin-pack side-band side-band-64k ofs-delta shallow no-progress include-tag multi_ack_detailed symref=HEAD:refs/heads/master agent=git/2.8.4
  2016-08-24 16:36:42 DEBUG PacketLineIn:165 - git< e0c1ceafc5bece92d35773a75fff59497e1d9bd5 refs/heads/maint

Change-Id: I5028c064f3ac090510386057cb4e6d30d4eae232
Signed-off-by: Dan Wang <dwwang@google.com>
stable-4.5
Dan Wang 8 years ago committed by Jonathan Nieder
parent
commit
9974f30702
  1. 26
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java
  2. 10
      org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java

26
org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java

@ -55,6 +55,8 @@ import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.MutableObjectId;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Read Git style pkt-line formatting from an input stream.
@ -67,6 +69,8 @@ import org.eclipse.jgit.util.RawParseUtils;
* against the underlying InputStream.
*/
public class PacketLineIn {
private static final Logger log = LoggerFactory.getLogger(PacketLineIn.class);
/** Magic return from {@link #readString()} when a flush packet is found. */
public static final String END = new StringBuilder(0).toString(); /* must not string pool */
@ -136,12 +140,16 @@ public class PacketLineIn {
*/
public String readString() throws IOException {
int len = readLength();
if (len == 0)
if (len == 0) {
log.debug("git< 0000"); //$NON-NLS-1$
return END;
}
len -= 4; // length header (4 bytes)
if (len == 0)
if (len == 0) {
log.debug("git< "); //$NON-NLS-1$
return ""; //$NON-NLS-1$
}
byte[] raw;
if (len <= lineBuffer.length)
@ -152,7 +160,10 @@ public class PacketLineIn {
IO.readFully(in, raw, 0, len);
if (raw[len - 1] == '\n')
len--;
return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
log.debug("git< " + s); //$NON-NLS-1$
return s;
}
/**
@ -167,8 +178,10 @@ public class PacketLineIn {
*/
public String readStringRaw() throws IOException {
int len = readLength();
if (len == 0)
if (len == 0) {
log.debug("git< 0000"); //$NON-NLS-1$
return END;
}
len -= 4; // length header (4 bytes)
@ -179,7 +192,10 @@ public class PacketLineIn {
raw = new byte[len];
IO.readFully(in, raw, 0, len);
return RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
String s = RawParseUtils.decode(Constants.CHARSET, raw, 0, len);
log.debug("git< " + s); //$NON-NLS-1$
return s;
}
void discardUntilEnd() throws IOException {

10
org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java

@ -49,6 +49,9 @@ import java.io.IOException;
import java.io.OutputStream;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.util.RawParseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Write Git style pkt-line formatting to an output stream.
@ -61,6 +64,8 @@ import org.eclipse.jgit.lib.Constants;
* against the underlying OutputStream.
*/
public class PacketLineOut {
private static final Logger log = LoggerFactory.getLogger(PacketLineOut.class);
private final OutputStream out;
private final byte[] lenbuffer;
@ -135,6 +140,10 @@ public class PacketLineOut {
formatLength(len + 4);
out.write(lenbuffer, 0, 4);
out.write(buf, pos, len);
if (log.isDebugEnabled()) {
String s = RawParseUtils.decode(Constants.CHARSET, buf, pos, len);
log.debug("git> " + s); //$NON-NLS-1$
}
}
/**
@ -153,6 +162,7 @@ public class PacketLineOut {
public void end() throws IOException {
formatLength(0);
out.write(lenbuffer, 0, 4);
log.debug("git> 0000"); //$NON-NLS-1$
if (flushOnEnd)
flush();
}

Loading…
Cancel
Save