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.
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
8890API void
@@ -95,58 +97,58 @@ nc_libssh_thread_verbosity(int level)
9597#endif /* NC_ENABLED_SSH_TLS */
9698
9799static 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
139141cleanup :
140- free (prv_msg );
142+ free (msg );
141143}
142144
143145void
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
0 commit comments