3030#include "log.h"
3131#include "session_p.h"
3232
33+ #define NC_MSG_SIZE 256
34+
3335/**
3436 * @brief libnetconf verbose level variable
3537 */
@@ -57,9 +59,36 @@ struct {
5759
5860#ifdef NC_ENABLED_SSH_TLS
5961
62+ static void
63+ nc_libssh_log_cb (int priority , const char * UNUSED (function ), const char * buffer , void * UNUSED (userdata ))
64+ {
65+ static char last_msg [NC_MSG_SIZE ] = {0 };
66+ static struct timespec last_print = {0 }, cur_time ;
67+
68+ /* check for repeated messages and do not print them */
69+ if (!strncmp (last_msg , buffer , NC_MSG_SIZE - 1 )) {
70+ nc_realtime_get (& cur_time );
71+ if (!last_print .tv_sec || (nc_time_diff (& cur_time , & last_print ) >= 1000 )) {
72+ /* print another repeated message every 1s */
73+ fprintf (stderr , "%s: SSH: -||-\n" , verb [priority ].label );
74+ }
75+
76+ last_print = cur_time ;
77+ return ;
78+ }
79+
80+ /* store the last message */
81+ strncpy (last_msg , buffer , NC_MSG_SIZE - 1 );
82+ memset (& last_print , 0 , sizeof last_print );
83+
84+ /* print the message */
85+ fprintf (stderr , "%s: SSH: %s\n" , verb [priority ].label , buffer );
86+ }
87+
6088API void
6189nc_libssh_thread_verbosity (int level )
6290{
91+ ssh_set_log_callback (nc_libssh_log_cb );
6392 ssh_set_log_level (level );
6493}
6594
@@ -68,23 +97,22 @@ nc_libssh_thread_verbosity(int level)
6897static void
6998prv_vprintf (const struct nc_session * session , NC_VERB_LEVEL level , const char * format , va_list args )
7099{
71- #define PRV_MSG_INIT_SIZE 256
72100 va_list args2 ;
73101 char * prv_msg ;
74102 void * mem ;
75103 int req_len ;
76104
77- prv_msg = malloc (PRV_MSG_INIT_SIZE );
105+ prv_msg = malloc (NC_MSG_SIZE );
78106 if (!prv_msg ) {
79107 return ;
80108 }
81109
82110 va_copy (args2 , args );
83111
84- req_len = vsnprintf (prv_msg , PRV_MSG_INIT_SIZE - 1 , format , args );
112+ req_len = vsnprintf (prv_msg , NC_MSG_SIZE - 1 , format , args );
85113 if (req_len == -1 ) {
86114 goto cleanup ;
87- } else if (req_len >= PRV_MSG_INIT_SIZE - 1 ) {
115+ } else if (req_len >= NC_MSG_SIZE - 1 ) {
88116 /* the length is not enough */
89117 ++ req_len ;
90118 mem = realloc (prv_msg , req_len );
@@ -110,7 +138,6 @@ prv_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *f
110138
111139cleanup :
112140 free (prv_msg );
113- #undef PRV_MSG_INIT_SIZE
114141}
115142
116143void
0 commit comments