From 9974f3070262b92b06d582182d0f633155ed69d9 Mon Sep 17 00:00:00 2001 From: Dan Wang Date: Mon, 22 Aug 2016 19:16:51 -0700 Subject: [PATCH] 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 --- .../eclipse/jgit/transport/PacketLineIn.java | 26 +++++++++++++++---- .../eclipse/jgit/transport/PacketLineOut.java | 10 +++++++ 2 files changed, 31 insertions(+), 5 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java index 8d291b851..e142babd6 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineIn.java @@ -55,6 +55,8 @@ 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 @@ * 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 @@ else if (arg.equals(" ready")) //$NON-NLS-1$ */ 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 String readString() throws IOException { 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 String readString() throws IOException { */ 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 String readStringRaw() throws IOException { 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 { diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java index 2d4e9c717..a6bd34285 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/transport/PacketLineOut.java @@ -49,6 +49,9 @@ 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 @@ * 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 void writePacket(byte[] buf, int pos, int len) throws IOException { 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 void writePacket(byte[] buf, int pos, int len) throws IOException { public void end() throws IOException { formatLength(0); out.write(lenbuffer, 0, 4); + log.debug("git> 0000"); //$NON-NLS-1$ if (flushOnEnd) flush(); }