1
1

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
Этот коммит содержится в:
Xiang Xiao 2021-05-10 19:27:31 +08:00 коммит произвёл Jakub Jelen
родитель 672c1f8a3a
Коммит 14276f0b51
4 изменённых файлов: 39 добавлений и 20 удалений

Просмотреть файл

@ -49,6 +49,8 @@
#endif #endif
#endif #endif
#include <stdarg.h>
#ifdef _MSC_VER #ifdef _MSC_VER
/* Visual Studio hasn't inttypes.h so it doesn't know uint32_t */ /* Visual Studio hasn't inttypes.h so it doesn't know uint32_t */
typedef int int32_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 int ssh_get_log_level(void);
LIBSSH_API void *ssh_get_log_userdata(void); LIBSSH_API void *ssh_get_log_userdata(void);
LIBSSH_API int ssh_set_log_userdata(void *data); 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, LIBSSH_API void _ssh_log(int verbosity,
const char *function, const char *function,
const char *format, ...) PRINTF_ATTRIBUTE(3, 4); const char *format, ...) PRINTF_ATTRIBUTE(3, 4);

Просмотреть файл

@ -369,13 +369,11 @@ public:
return state; return state;
} }
void log(int priority, const char *format, ...){ void log(int priority, const char *format, ...){
char buffer[1024];
va_list va; va_list va;
va_start(va, format); va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va); ssh_vlog(priority, "libsshpp", format, &va);
va_end(va); va_end(va);
_ssh_log(priority, "libsshpp", "%s", buffer);
} }
/** @brief copies options from a session to another /** @brief copies options from a session to another

Просмотреть файл

@ -405,6 +405,7 @@ LIBSSH_4_5_0 # Released
ssh_userauth_publickey_auto_get_current_identity; ssh_userauth_publickey_auto_get_current_identity;
ssh_userauth_try_publickey; ssh_userauth_try_publickey;
ssh_version; ssh_version;
ssh_vlog;
ssh_write_knownhost; ssh_write_knownhost;
string_burn; string_burn;
string_copy; string_copy;

Просмотреть файл

@ -38,6 +38,10 @@
#include "libssh/misc.h" #include "libssh/misc.h"
#include "libssh/session.h" #include "libssh/session.h"
#ifndef LOG_SIZE
#define LOG_SIZE 1024
#endif
static LIBSSH_THREAD int ssh_log_level; static LIBSSH_THREAD int ssh_log_level;
static LIBSSH_THREAD ssh_logging_callback ssh_log_cb; static LIBSSH_THREAD ssh_logging_callback ssh_log_cb;
static LIBSSH_THREAD void *ssh_log_userdata; static LIBSSH_THREAD void *ssh_log_userdata;
@ -94,38 +98,52 @@ static void ssh_log_stderr(int verbosity,
fprintf(stderr, " %s\n", buffer); 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, void ssh_log_function(int verbosity,
const char *function, const char *function,
const char *buffer) const char *buffer)
{ {
ssh_logging_callback log_fn = ssh_get_log_callback(); ssh_logging_callback log_fn = ssh_get_log_callback();
if (log_fn) { if (log_fn) {
char buf[1024]; ssh_log_custom(log_fn, verbosity, function, buffer);
snprintf(buf, sizeof(buf), "%s: %s", function, buffer);
log_fn(verbosity,
function,
buf,
ssh_get_log_userdata());
return; return;
} }
ssh_log_stderr(verbosity, function, buffer); 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, void _ssh_log(int verbosity,
const char *function, const char *function,
const char *format, ...) const char *format, ...)
{ {
char buffer[1024];
va_list va; va_list va;
if (verbosity <= ssh_get_log_level()) { if (verbosity <= ssh_get_log_level()) {
va_start(va, format); va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va); ssh_vlog(verbosity, function, format, &va);
va_end(va); va_end(va);
ssh_log_function(verbosity, function, buffer);
} }
} }
@ -135,14 +153,12 @@ void ssh_log(ssh_session session,
int verbosity, int verbosity,
const char *format, ...) const char *format, ...)
{ {
char buffer[1024];
va_list va; va_list va;
if (verbosity <= session->common.log_verbosity) { if (verbosity <= session->common.log_verbosity) {
va_start(va, format); va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va); ssh_vlog(verbosity, "", format, &va);
va_end(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 *function,
const char *format, ...) const char *format, ...)
{ {
char buffer[1024];
va_list va; va_list va;
if (verbosity <= common->log_verbosity) { if (verbosity <= common->log_verbosity) {
va_start(va, format); va_start(va, format);
vsnprintf(buffer, sizeof(buffer), format, va); ssh_vlog(verbosity, function, format, &va);
va_end(va); va_end(va);
ssh_log_function(verbosity, function, buffer);
} }
} }