diff --git a/include/libssh/libssh.h b/include/libssh/libssh.h index 4f401056..4bfd81ef 100644 --- a/include/libssh/libssh.h +++ b/include/libssh/libssh.h @@ -49,6 +49,8 @@ #endif #endif +#include + #ifdef _MSC_VER /* Visual Studio hasn't inttypes.h so it doesn't know uint32_t */ typedef int int32_t; @@ -587,6 +589,10 @@ LIBSSH_API int ssh_set_log_level(int level); LIBSSH_API int ssh_get_log_level(void); LIBSSH_API void *ssh_get_log_userdata(void); LIBSSH_API int ssh_set_log_userdata(void *data); +LIBSSH_API void ssh_vlog(int verbosity, + const char *function, + const char *format, + va_list *va) PRINTF_ATTRIBUTE(3, 0); LIBSSH_API void _ssh_log(int verbosity, const char *function, const char *format, ...) PRINTF_ATTRIBUTE(3, 4); diff --git a/include/libssh/libsshpp.hpp b/include/libssh/libsshpp.hpp index 75c9c7a1..f792465c 100644 --- a/include/libssh/libsshpp.hpp +++ b/include/libssh/libsshpp.hpp @@ -369,13 +369,11 @@ public: return state; } void log(int priority, const char *format, ...){ - char buffer[1024]; va_list va; va_start(va, format); - vsnprintf(buffer, sizeof(buffer), format, va); + ssh_vlog(priority, "libsshpp", format, &va); va_end(va); - _ssh_log(priority, "libsshpp", "%s", buffer); } /** @brief copies options from a session to another diff --git a/src/libssh.map b/src/libssh.map index e30c2449..82cdfa21 100644 --- a/src/libssh.map +++ b/src/libssh.map @@ -405,6 +405,7 @@ LIBSSH_4_5_0 # Released ssh_userauth_publickey_auto_get_current_identity; ssh_userauth_try_publickey; ssh_version; + ssh_vlog; ssh_write_knownhost; string_burn; string_copy; diff --git a/src/log.c b/src/log.c index a8664b16..8ce1e71e 100644 --- a/src/log.c +++ b/src/log.c @@ -38,6 +38,10 @@ #include "libssh/misc.h" #include "libssh/session.h" +#ifndef LOG_SIZE +#define LOG_SIZE 1024 +#endif + static LIBSSH_THREAD int ssh_log_level; static LIBSSH_THREAD ssh_logging_callback ssh_log_cb; static LIBSSH_THREAD void *ssh_log_userdata; @@ -94,38 +98,52 @@ static void ssh_log_stderr(int verbosity, fprintf(stderr, " %s\n", buffer); } +static void ssh_log_custom(ssh_logging_callback log_fn, + int verbosity, + const char *function, + const char *buffer) +{ + char buf[LOG_SIZE + 64]; + + snprintf(buf, sizeof(buf), "%s: %s", function, buffer); + log_fn(verbosity, function, buf, ssh_get_log_userdata()); +} + void ssh_log_function(int verbosity, const char *function, const char *buffer) { ssh_logging_callback log_fn = ssh_get_log_callback(); + if (log_fn) { - char buf[1024]; - - snprintf(buf, sizeof(buf), "%s: %s", function, buffer); - - log_fn(verbosity, - function, - buf, - ssh_get_log_userdata()); + ssh_log_custom(log_fn, verbosity, function, buffer); return; } ssh_log_stderr(verbosity, function, buffer); } +void ssh_vlog(int verbosity, + const char *function, + const char *format, + va_list *va) +{ + char buffer[LOG_SIZE]; + + vsnprintf(buffer, sizeof(buffer), format, *va); + ssh_log_function(verbosity, function, buffer); +} + void _ssh_log(int verbosity, const char *function, const char *format, ...) { - char buffer[1024]; va_list va; if (verbosity <= ssh_get_log_level()) { va_start(va, format); - vsnprintf(buffer, sizeof(buffer), format, va); + ssh_vlog(verbosity, function, format, &va); va_end(va); - ssh_log_function(verbosity, function, buffer); } } @@ -135,14 +153,12 @@ void ssh_log(ssh_session session, int verbosity, const char *format, ...) { - char buffer[1024]; va_list va; if (verbosity <= session->common.log_verbosity) { va_start(va, format); - vsnprintf(buffer, sizeof(buffer), format, va); + ssh_vlog(verbosity, "", format, &va); va_end(va); - ssh_log_function(verbosity, "", buffer); } } @@ -157,14 +173,12 @@ void ssh_log_common(struct ssh_common_struct *common, const char *function, const char *format, ...) { - char buffer[1024]; va_list va; if (verbosity <= common->log_verbosity) { va_start(va, format); - vsnprintf(buffer, sizeof(buffer), format, va); + ssh_vlog(verbosity, function, format, &va); va_end(va); - ssh_log_function(verbosity, function, buffer); } }