Skip to content

Commit

Permalink
[CONJ-756] better Trace format logging
Browse files Browse the repository at this point in the history
  • Loading branch information
rusher committed Jan 27, 2020
1 parent c95f232 commit 38c8a69
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -777,10 +777,9 @@ public ServerPrepareResult prepare(String sql, boolean executeOnMaster) throws S
cmdPrologue();
lock.lock();
try {
//search in cache first
if (options.cachePrepStmts && options.useServerPrepStmts) {

ServerPrepareResult pr = serverPrepareStatementCache.get(database + "-" + sql);

if (pr != null && pr.incrementShareCounter()) {
return pr;
}
Expand Down
48 changes: 36 additions & 12 deletions src/main/java/org/mariadb/jdbc/internal/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -711,17 +711,29 @@ public static String hexdump(int maxQuerySizeToLog, int offset, int length, byte
return outputBuilder.toString();

default:
StringBuilder sb = new StringBuilder();
sb.append("\n");
byte[] arr;
for (int i = 0; i < byteArr.length - 1; i++) {
arr = byteArr[i];
writeHex(arr, 0, arr.length, sb);

ByteArrayOutputStream outputStream = new ByteArrayOutputStream( );
try {
outputStream.write( byteArr[0] );
outputStream.write( byteArr[1], offset, length );
for (int i = 2; i< byteArr.length; i++) {
outputStream.write( byteArr[i] );
}
} catch (IOException ioe) {
// eat
}

byte[] concat = outputStream.toByteArray( );
if (concat.length <= offset) {
return "";
}
arr = byteArr[byteArr.length - 1];
int dataLength2 = Math.min(maxQuerySizeToLog, Math.min(arr.length - offset, length));
writeHex(arr, offset, dataLength2, sb);
return sb.toString();

int stlength = Math.min(maxQuerySizeToLog, outputStream.size());

StringBuilder out = new StringBuilder(stlength * 3 + 80);
out.append("\n");
writeHex(outputStream.toByteArray(), 0, outputStream.size(), out);
return out.toString();
}
}

Expand Down Expand Up @@ -755,6 +767,11 @@ private static void writeHex(
int pos = offset;
int posHexa = 0;

outputBuilder.append(
"+--------------------------------------------------+\n"
+ "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n"
+ "+--------------------------------------------------+------------------+\n| ");

while (pos < dataLength + offset) {
int byteValue = bytes[pos] & 0xFF;
outputBuilder
Expand All @@ -768,7 +785,7 @@ private static void writeHex(
outputBuilder.append(" ");
}
if (posHexa == 16) {
outputBuilder.append(" ").append(hexaValue).append("\n");
outputBuilder.append("| ").append(hexaValue).append(" |\n| ");
posHexa = 0;
}
pos++;
Expand All @@ -787,8 +804,15 @@ private static void writeHex(
outputBuilder.append(" ");
}

outputBuilder.append(" ").append(hexaValue, 0, posHexa).append("\n");
for (; posHexa < 16; posHexa++) {
hexaValue[posHexa] = ' ';
}

outputBuilder.append("| ")
.append(hexaValue)
.append(" |\n");
}
outputBuilder.append("+--------------------------------------------------+------------------+\n");
}

private static String getHex(final byte[] raw) {
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/mariadb/jdbc/SslTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ public void useSslForceTlsCombination() throws Exception {
if (isMariadbServer() && !Platform.isWindows()) {
useSslForceTls("TLSv1,TLSv1.1,TLSv1.2");
} else {
useSslForceTls("TLSv1,TLSv1");
useSslForceTls("TLSv1,TLSv1.1");
}
}

Expand All @@ -339,7 +339,7 @@ public void useSslForceTlsCombinationWithSpace() throws Exception {
if (isMariadbServer() && !Platform.isWindows()) {
useSslForceTls("TLSv1, TLSv1.1, TLSv1.2");
} else {
useSslForceTls("TLSv1, TLSv1");
useSslForceTls("TLSv1, TLSv1.1");
}
}

Expand All @@ -348,7 +348,7 @@ public void useSslForceTlsCombinationWithOnlySpace() throws Exception {
if (isMariadbServer() && !Platform.isWindows()) {
useSslForceTls("TLSv1 TLSv1.1 TLSv1.2");
} else {
useSslForceTls("TLSv1 TLSv1");
useSslForceTls("TLSv1 TLSv1.1");
}
}

Expand Down
15 changes: 9 additions & 6 deletions src/test/java/org/mariadb/jdbc/internal/util/UtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,15 @@ public void testByteDump() {
0x6C, 0x5F, 0x6D, 0x6F, 0x64, 0x65
};
String result =
"\n"
+ "4A 00 00 00 03 53 45 4C 45 43 54 20 40 40 6D 61 J....SELECT @@ma\n"
+ "78 5F 61 6C 6C 6F 77 65 64 5F 70 61 63 6B 65 74 x_allowed_packet\n"
+ "20 2C 20 40 40 73 79 73 74 65 6D 5F 74 69 6D 65 , @@system_time\n"
+ "5F 7A 6F 6E 65 2C 20 40 40 74 69 6D 65 5F 7A 6F _zone, @@time_zo\n"
+ "6E 65 2C 20 40 40 73 71 6C 5F 6D 6F 64 65 ne, @@sql_mode\n";
"\n+--------------------------------------------------+\n"
+ "| 0 1 2 3 4 5 6 7 8 9 a b c d e f |\n"
+ "+--------------------------------------------------+------------------+\n"
+ "| 4A 00 00 00 03 53 45 4C 45 43 54 20 40 40 6D 61 | J....SELECT @@ma |\n"
+ "| 78 5F 61 6C 6C 6F 77 65 64 5F 70 61 63 6B 65 74 | x_allowed_packet |\n"
+ "| 20 2C 20 40 40 73 79 73 74 65 6D 5F 74 69 6D 65 | , @@system_time |\n"
+ "| 5F 7A 6F 6E 65 2C 20 40 40 74 69 6D 65 5F 7A 6F | _zone, @@time_zo |\n"
+ "| 6E 65 2C 20 40 40 73 71 6C 5F 6D 6F 64 65 | ne, @@sql_mode |\n"
+ "+--------------------------------------------------+------------------+\n";
assertEquals(result, Utils.hexdump(bb));
}

Expand Down

0 comments on commit 38c8a69

Please sign in to comment.