Add libssh2_trace_sethandler() to the API (even more)

This commit is contained in:
Dave McCaldon
2010-01-15 22:58:44 +01:00
committed by Daniel Stenberg
parent 474e38119b
commit 44eba0c993
7 changed files with 80 additions and 23 deletions

View File

@@ -735,6 +735,7 @@ struct _LIBSSH2_SESSION
struct transportpacket packet;
#ifdef LIBSSH2DEBUG
int showmask; /* what debug/trace messages to display */
libssh2_trace_handler_func tracehandler; /* callback to display trace messages */
#endif
/* State variables used in libssh2_banner_send() */

View File

@@ -312,6 +312,13 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
return 0;
}
LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback)
{
session->tracehandler = callback;
return 0;
}
void
_libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
{
@@ -353,8 +360,12 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
len += vsnprintf(buffer + len, 1535 - len, format, vargs);
buffer[len] = '\n';
va_end(vargs);
write(2, buffer, len + 1);
if (session->tracehandler) {
(session->tracehandler)(session, buffer, len + 1);
} else {
write(2, buffer, len + 1);
}
}
#else
@@ -365,6 +376,14 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
(void) bitmask;
return 0;
}
LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback)
{
(void) session;
(void) callback;
return 0;
}
#endif
/* init the list head */

View File

@@ -41,6 +41,7 @@
#include "libssh2_priv.h"
#include <errno.h>
#include <fcntl.h>
#include <ctype.h>
#include <assert.h>
@@ -57,36 +58,56 @@ debugdump(LIBSSH2_SESSION * session,
{
size_t i;
size_t c;
FILE *stream = stderr;
unsigned int width = 0x10;
char buffer[256]; /* Must be enough for width*4 + about 30 or so */
size_t used;
static const char* hex_chars = "0123456789ABCDEF";
if (!(session->showmask & LIBSSH2_TRACE_TRANS)) {
/* not asked for, bail out */
return;
}
fprintf(stream, "=> %s (%d bytes)\n", desc, (int) size);
used = snprintf(buffer, sizeof(buffer), "=> %s (%d bytes)\n",
desc, (int) size);
if (session->tracehandler)
(session->tracehandler)(session, buffer, used);
else
write(2 /* stderr */, buffer, used);
for(i = 0; i < size; i += width) {
fprintf(stream, "%04lx: ", (long)i);
used = snprintf(buffer, sizeof(buffer), "%04lx: ", (long)i);
/* hex not disabled, show it */
for(c = 0; c < width; c++) {
if (i + c < size)
fprintf(stream, "%02x ", ptr[i + c]);
else
fputs(" ", stream);
if (i + c < size) {
buffer[used++] = hex_chars[(ptr[i+c] >> 4) & 0xF];
buffer[used++] = hex_chars[ptr[i+c] & 0xF];
}
else {
buffer[used++] = ' ';
buffer[used++] = ' ';
}
buffer[used++] = (c == (width/2)-1) ? ':' : ' ';
}
buffer[used++] = ':';
buffer[used++] = ' ';
for(c = 0; (c < width) && (i + c < size); c++) {
fprintf(stream, "%c",
(ptr[i + c] >= 0x20) &&
(ptr[i + c] < 0x80) ? ptr[i + c] : UNPRINTABLE_CHAR);
buffer[used++] = isprint(ptr[i + c]) ?
ptr[i + c] : UNPRINTABLE_CHAR;
}
fputc('\n', stream); /* newline */
buffer[used++] = '\n';
buffer[used] = 0;
if (session->tracehandler)
(session->tracehandler)(session, buffer, used);
else
write(2, buffer, used);
}
fflush(stream);
}
#else
#define debugdump(a,x,y,z)