log: add ssh_vlog to save the stack space

and add LOG_SIZE macro to control the buffer size

Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
Change-Id: I3eaeea001fc531fdb55074fc3a9d140b27847c1f
This commit is contained in:
Xiang Xiao 2021-05-10 19:27:31 +08:00 committed by Jakub Jelen
parent 672c1f8a3a
commit 14276f0b51
4 changed files with 39 additions and 20 deletions

View File

@ -49,6 +49,8 @@
#endif
#endif
#include <stdarg.h>
#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);

View File

@ -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

View File

@ -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;

View File

@ -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);
}
}