Pass user context through libssh2_trace_sethandler() to callback

The libssh2_trace_sethandler() call allows the user to handle the output of libssh2 rather than having it written to stderr.  This patch updates libssh2_trace_sethandler() to allow a user-defined void* context value to be passed back to the output handler.
This commit is contained in:
Dave McCaldon 2010-01-20 12:02:13 -05:00 committed by Daniel Stenberg
parent 44eba0c993
commit f077984394
5 changed files with 16 additions and 6 deletions

View File

@ -8,10 +8,12 @@ libssh2_trace_sethandler - set a trace output handler
#include <libssh2.h> #include <libssh2.h>
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION *session, typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION *session,
void* context,
const char *data, const char *data,
size_t length); size_t length);
int libssh2_trace_sethandler(LIBSSH2_SESSION *session, int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
void* context,
libssh2_trace_handler_func callback); libssh2_trace_handler_func callback);
.SH DESCRIPTION .SH DESCRIPTION
libssh2_trace_sethandler installs a trace output handler for your application. libssh2_trace_sethandler installs a trace output handler for your application.
@ -22,5 +24,7 @@ libssh2 will call back as it generates trace output. This can be used to
capture the trace output and put it into a log file or diagnostic window. capture the trace output and put it into a log file or diagnostic window.
This function has no effect unless libssh2 was built to support this option, This function has no effect unless libssh2 was built to support this option,
and a typical "release build" might not. and a typical "release build" might not.
\fBcontext\fP can be used to pass arbitrary user defined data back into the callback when invoked.
.SH AVAILABILITY .SH AVAILABILITY
Added in libssh2 version 1.2.3 Added in libssh2 version 1.2.3

View File

@ -987,9 +987,12 @@ LIBSSH2_API int libssh2_trace(LIBSSH2_SESSION *session, int bitmask);
#define LIBSSH2_TRACE_PUBLICKEY (1<<8) #define LIBSSH2_TRACE_PUBLICKEY (1<<8)
#define LIBSSH2_TRACE_SOCKET (1<<9) #define LIBSSH2_TRACE_SOCKET (1<<9)
typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*, const char *, typedef void (*libssh2_trace_handler_func)(LIBSSH2_SESSION*,
void*,
const char *,
size_t); size_t);
LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session, LIBSSH2_API int libssh2_trace_sethandler(LIBSSH2_SESSION *session,
void* context,
libssh2_trace_handler_func callback); libssh2_trace_handler_func callback);
#ifdef __cplusplus #ifdef __cplusplus

View File

@ -736,6 +736,7 @@ struct _LIBSSH2_SESSION
#ifdef LIBSSH2DEBUG #ifdef LIBSSH2DEBUG
int showmask; /* what debug/trace messages to display */ int showmask; /* what debug/trace messages to display */
libssh2_trace_handler_func tracehandler; /* callback to display trace messages */ libssh2_trace_handler_func tracehandler; /* callback to display trace messages */
void* tracehandler_context; /* context for the trace handler */
#endif #endif
/* State variables used in libssh2_banner_send() */ /* State variables used in libssh2_banner_send() */

View File

@ -313,9 +313,10 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
} }
LIBSSH2_API int LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback) libssh2_trace_sethandler(LIBSSH2_SESSION *session, void* handler_context, libssh2_trace_handler_func callback)
{ {
session->tracehandler = callback; session->tracehandler = callback;
session->tracehandler_context = handler_context;
return 0; return 0;
} }
@ -362,7 +363,7 @@ _libssh2_debug(LIBSSH2_SESSION * session, int context, const char *format, ...)
va_end(vargs); va_end(vargs);
if (session->tracehandler) { if (session->tracehandler) {
(session->tracehandler)(session, buffer, len + 1); (session->tracehandler)(session, session->tracehandler_context, buffer, len + 1);
} else { } else {
write(2, buffer, len + 1); write(2, buffer, len + 1);
} }
@ -378,9 +379,10 @@ libssh2_trace(LIBSSH2_SESSION * session, int bitmask)
} }
LIBSSH2_API int LIBSSH2_API int
libssh2_trace_sethandler(LIBSSH2_SESSION *session, libssh2_trace_handler_func callback) libssh2_trace_sethandler(LIBSSH2_SESSION *session, void* handler_context, libssh2_trace_handler_func callback)
{ {
(void) session; (void) session;
(void) handler_context;
(void) callback; (void) callback;
return 0; return 0;
} }

View File

@ -71,7 +71,7 @@ debugdump(LIBSSH2_SESSION * session,
used = snprintf(buffer, sizeof(buffer), "=> %s (%d bytes)\n", used = snprintf(buffer, sizeof(buffer), "=> %s (%d bytes)\n",
desc, (int) size); desc, (int) size);
if (session->tracehandler) if (session->tracehandler)
(session->tracehandler)(session, buffer, used); (session->tracehandler)(session, session->tracehandler_context, buffer, used);
else else
write(2 /* stderr */, buffer, used); write(2 /* stderr */, buffer, used);
@ -104,7 +104,7 @@ debugdump(LIBSSH2_SESSION * session,
buffer[used] = 0; buffer[used] = 0;
if (session->tracehandler) if (session->tracehandler)
(session->tracehandler)(session, buffer, used); (session->tracehandler)(session, session->tracehandler_context, buffer, used);
else else
write(2, buffer, used); write(2, buffer, used);
} }