Skip to content

Commit 43f16ce

Browse files
committed
log UPDATE use user log cb for all the messages
1 parent 38f1c42 commit 43f16ce

2 files changed

Lines changed: 35 additions & 32 deletions

File tree

src/log.c

Lines changed: 26 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
22
* @file log.c
33
* @author Radek Krejci <rkrejci@cesnet.cz>
4+
* @author Michal Vasko <mvasko@cesnet.cz>
45
* @brief libnetconf2 - log functions
56
*
67
* @copyright
7-
* Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
8+
* Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
89
*
910
* This source code is licensed under BSD 3-Clause License (the "License").
1011
* You may not use this file except in compliance with the License.
@@ -15,6 +16,8 @@
1516

1617
#define _GNU_SOURCE /* pthread_rwlock_t */
1718

19+
#include "log_p.h"
20+
1821
#include <stdarg.h>
1922
#include <stdio.h>
2023
#include <stdlib.h>
@@ -68,21 +71,20 @@ nc_libssh_log_cb(int priority, const char *UNUSED(function), const char *buffer,
6871
/* check for repeated messages and do not print them */
6972
if (!strncmp(last_msg, buffer, NC_MSG_SIZE - 1)) {
7073
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+
if (last_print.tv_sec && (nc_time_diff(&cur_time, &last_print) < 1000)) {
75+
/* print another repeated message only after 1s */
76+
return;
7477
}
7578

7679
last_print = cur_time;
77-
return;
80+
} else {
81+
/* store the last message */
82+
strncpy(last_msg, buffer, NC_MSG_SIZE - 1);
83+
memset(&last_print, 0, sizeof last_print);
7884
}
7985

80-
/* store the last message */
81-
strncpy(last_msg, buffer, NC_MSG_SIZE - 1);
82-
memset(&last_print, 0, sizeof last_print);
83-
8486
/* print the message */
85-
fprintf(stderr, "%s: SSH: %s\n", verb[priority].label, buffer);
87+
nc_log_printf(NULL, priority, "SSH: %s", buffer);
8688
}
8789

8890
API void
@@ -95,58 +97,58 @@ nc_libssh_thread_verbosity(int level)
9597
#endif /* NC_ENABLED_SSH_TLS */
9698

9799
static void
98-
prv_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, va_list args)
100+
nc_log_vprintf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, va_list args)
99101
{
100102
va_list args2;
101-
char *prv_msg;
103+
char *msg;
102104
void *mem;
103105
int req_len;
104106

105-
prv_msg = malloc(NC_MSG_SIZE);
106-
if (!prv_msg) {
107+
msg = malloc(NC_MSG_SIZE);
108+
if (!msg) {
107109
return;
108110
}
109111

110112
va_copy(args2, args);
111113

112-
req_len = vsnprintf(prv_msg, NC_MSG_SIZE - 1, format, args);
114+
req_len = vsnprintf(msg, NC_MSG_SIZE - 1, format, args);
113115
if (req_len == -1) {
114116
goto cleanup;
115117
} else if (req_len >= NC_MSG_SIZE - 1) {
116118
/* the length is not enough */
117119
++req_len;
118-
mem = realloc(prv_msg, req_len);
120+
mem = realloc(msg, req_len);
119121
if (!mem) {
120122
goto cleanup;
121123
}
122-
prv_msg = mem;
124+
msg = mem;
123125

124126
/* now print the full message */
125-
req_len = vsnprintf(prv_msg, req_len, format, args2);
127+
req_len = vsnprintf(msg, req_len, format, args2);
126128
if (req_len == -1) {
127129
goto cleanup;
128130
}
129131
}
130132

131133
if (print_clb) {
132-
print_clb(session, level, prv_msg);
134+
print_clb(session, level, msg);
133135
} else if (session && session->id) {
134-
fprintf(stderr, "Session %" PRIu32 " %s: %s\n", session->id, verb[level].label, prv_msg);
136+
fprintf(stderr, "Session %" PRIu32 " %s: %s\n", session->id, verb[level].label, msg);
135137
} else {
136-
fprintf(stderr, "%s: %s\n", verb[level].label, prv_msg);
138+
fprintf(stderr, "%s: %s\n", verb[level].label, msg);
137139
}
138140

139141
cleanup:
140-
free(prv_msg);
142+
free(msg);
141143
}
142144

143145
void
144-
prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...)
146+
nc_log_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...)
145147
{
146148
va_list ap;
147149

148150
va_start(ap, format);
149-
prv_vprintf(session, level, format, ap);
151+
nc_log_vprintf(session, level, format, ap);
150152
va_end(ap);
151153
}
152154

src/log_p.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
/**
2-
* @file log.h
2+
* @file log_p.h
33
* @author Radek Krejci <rkrejci@cesnet.cz>
4+
* @author Michal Vasko <mvasko@cesnet.cz>
45
* @brief libnetconf2 logger
56
*
67
* @copyright
7-
* Copyright (c) 2015 - 2021 CESNET, z.s.p.o.
8+
* Copyright (c) 2015 - 2024 CESNET, z.s.p.o.
89
*
910
* This source code is licensed under BSD 3-Clause License (the "License").
1011
* You may not use this file except in compliance with the License.
@@ -32,7 +33,7 @@
3233
* @param[in] level Verbose level
3334
* @param[in] format Formatting string
3435
*/
35-
void prv_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...);
36+
void nc_log_printf(const struct nc_session *session, NC_VERB_LEVEL level, const char *format, ...);
3637

3738
/**
3839
* @brief Verbose level variable
@@ -42,11 +43,11 @@ extern ATOMIC_T verbose_level;
4243
/*
4344
* Verbose printing macros
4445
*/
45-
#define ERR(session, ...) prv_printf(session, NC_VERB_ERROR, __VA_ARGS__)
46-
#define WRN(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_WARNING){prv_printf(session, NC_VERB_WARNING, __VA_ARGS__);}
47-
#define VRB(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_VERBOSE){prv_printf(session, NC_VERB_VERBOSE, __VA_ARGS__);}
48-
#define DBG(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG){prv_printf(session, NC_VERB_DEBUG, __VA_ARGS__);}
49-
#define DBL(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG_LOWLVL){prv_printf(session, NC_VERB_DEBUG_LOWLVL, __VA_ARGS__);}
46+
#define ERR(session, ...) nc_log_printf(session, NC_VERB_ERROR, __VA_ARGS__)
47+
#define WRN(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_WARNING){nc_log_printf(session, NC_VERB_WARNING, __VA_ARGS__);}
48+
#define VRB(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_VERBOSE){nc_log_printf(session, NC_VERB_VERBOSE, __VA_ARGS__);}
49+
#define DBG(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG){nc_log_printf(session, NC_VERB_DEBUG, __VA_ARGS__);}
50+
#define DBL(session, ...) if(ATOMIC_LOAD_RELAXED(verbose_level)>=NC_VERB_DEBUG_LOWLVL){nc_log_printf(session, NC_VERB_DEBUG_LOWLVL, __VA_ARGS__);}
5051

5152
#define ERRMEM ERR(NULL, "%s: memory reallocation failed (%s:%d).", __func__, __FILE__, __LINE__)
5253
#define ERRINITSRV ERR(NULL, "%s: server not initialized.", __func__)

0 commit comments

Comments
 (0)