Skip to content

Commit 4b9e5cb

Browse files
committed
log UPDATE use cb for libssh messages
1 parent 1ee3afb commit 4b9e5cb

3 files changed

Lines changed: 53 additions & 10 deletions

File tree

src/log.c

Lines changed: 32 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
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+
6088
API void
6189
nc_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)
6897
static void
6998
prv_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

111139
cleanup:
112140
free(prv_msg);
113-
#undef PRV_MSG_INIT_SIZE
114141
}
115142

116143
void

src/session.c

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,25 @@ nc_timeouttime_get(struct timespec *ts, uint32_t add_ms)
8080
assert((ts->tv_nsec >= 0) && (ts->tv_nsec < 1000000000L));
8181
}
8282

83+
int32_t
84+
nc_time_diff(const struct timespec *ts1, const struct timespec *ts2)
85+
{
86+
int64_t nsec_diff = 0;
87+
88+
nsec_diff += (((int64_t)ts1->tv_sec) - ((int64_t)ts2->tv_sec)) * 1000000000L;
89+
nsec_diff += ((int64_t)ts1->tv_nsec) - ((int64_t)ts2->tv_nsec);
90+
91+
return nsec_diff / 1000000L;
92+
}
93+
8394
int32_t
8495
nc_timeouttime_cur_diff(const struct timespec *ts)
8596
{
8697
struct timespec cur;
87-
int64_t nsec_diff = 0;
8898

8999
nc_timeouttime_get(&cur, 0);
90100

91-
nsec_diff += (((int64_t)ts->tv_sec) - ((int64_t)cur.tv_sec)) * 1000000000L;
92-
nsec_diff += ((int64_t)ts->tv_nsec) - ((int64_t)cur.tv_nsec);
93-
94-
return nsec_diff / 1000000L;
101+
return nc_time_diff(ts, &cur);
95102
}
96103

97104
void

src/session_p.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -935,6 +935,15 @@ NC_MSG_TYPE nc_send_msg_io(struct nc_session *session, int io_timeout, struct ly
935935
*/
936936
void nc_timeouttime_get(struct timespec *ts, uint32_t add_ms);
937937

938+
/**
939+
* @brief Get the time difference.
940+
*
941+
* @param[in] ts1 First timespec structure to compare.
942+
* @param[in] ts2 Second timespec structure to compare.
943+
* @return Time difference in milliseconds, positive if @p ts1 > @p ts2, negative if @p ts1 < @p ts2.
944+
*/
945+
int32_t nc_time_diff(const struct timespec *ts1, const struct timespec *ts2);
946+
938947
/**
939948
* @brief Get time difference based on the current time (uses COMPAT_CLOCK_ID).
940949
*

0 commit comments

Comments
 (0)