Move logging functions to separate file

This commit is contained in:
Kim Woelders
2019-08-30 09:37:25 +02:00
committed by Kim Woelders
parent e3675b9e89
commit 9db6c5d83c
4 changed files with 130 additions and 95 deletions
+1 -1
View File
@@ -5,7 +5,7 @@ CFLAGS_WARN = -Wall -Wextra -Werror -Wno-unused-parameter
bin_PROGRAMS = ttynvt
ttynvt_SOURCES = \
ttynvt.c \
ttynvt.c nvt_log.c nvt_log.h \
telnet_basic.c telnet_rfc2217.c telnet.h telnet_param.h
ttynvt_CPPFLAGS = -I$(top_builddir) $(FUSE_CFLAGS) $(CFLAGS_OPT) $(CFLAGS_WARN)
+84
View File
@@ -0,0 +1,84 @@
/*
* Logging functions
*/
#include <stdarg.h>
#include <stdio.h>
#include <syslog.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>
#include "nvt_log.h"
#include <sys/syscall.h>
#define gettid() (pid_t)syscall(__NR_gettid)
char log_level = 0;
char log_stdout = 0;
static const char *_hms_txt(char *buf, unsigned int len)
{
struct timeval tv;
unsigned int sod, hh, mm, ss;
gettimeofday(&tv, NULL);
sod = tv.tv_sec % (24 * 3600);
hh = sod / 3600;
mm = (sod - hh * 3600) / 60;
ss = sod - hh * 3600 - mm * 60;
snprintf(buf, len, "%02d:%02d:%02d.%06d",
hh, mm, ss, (unsigned int)tv.tv_usec);
return buf;
}
void nvt_log(int prio, const char *fmt, ...)
{
va_list arg;
char buf[256], buft[64];
const char *eol;
int len;
va_start(arg, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, arg);
if (log_stdout)
{
eol = (len > 0 && buf[len - 1] != '\n') ? "\n" : "";
printf("%s: [%d] %d: %s%s",
_hms_txt(buft, sizeof(buft)), gettid(), prio, buf, eol);
}
else
{
syslog(prio, "[%d] %s", gettid(), buf);
}
va_end(arg);
}
#define BPL 16 /* Bytes per line */
void nvt_log_buf(int prio, const char *txt, const void *ptr_, unsigned int len)
{
unsigned int ix, i, imax;
int l, n;
char buf[128];
const unsigned char *ptr = ptr_;
for (ix = 0; ix < len || (len == 0 && ix == 0); ix += BPL)
{
n = snprintf(buf, sizeof(buf), "%s: %04x:", txt, ix);
if (n <= 0)
return; /* Just playing it safe */
imax = (len - ix < BPL) ? len - ix : BPL;
for (i = 0; i < imax; i++)
{
l = snprintf(buf + n, sizeof(buf) - n, " %02x", *ptr++);
if (l <= 0)
return; /* Just playing it safe */
n += l;
}
nvt_log(prio, "%s\n", buf);
}
}
+26
View File
@@ -0,0 +1,26 @@
/*
* Logging functions
*/
#ifndef NVT_LOG_H
#define NVT_LOG_H
#include <syslog.h>
#define _PRF_N_(no) __attribute__((__format__(__printf__, (no), (no) + 1)))
#define _PRF2_ _PRF_N_(2)
_PRF2_ void nvt_log(int prio, const char *fmt, ...);
void nvt_log_buf(int prio, const char *txt,
const void *ptr, unsigned int len);
extern char log_level;
extern char log_stdout;
#define DBG(...) \
if (log_level > 0) nvt_log(LOG_DEBUG, __VA_ARGS__)
#define DBG2(...) \
if (log_level > 1) nvt_log(LOG_DEBUG, __VA_ARGS__)
#define DBG2_BUF(txt, ptr, len) \
if (log_level > 1) nvt_log_buf(LOG_DEBUG, txt, ptr, len)
#endif /* NVT_LOG_H */
+19 -94
View File
@@ -24,9 +24,8 @@
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include "nvt_log.h"
#include "telnet.h"
#define NET_BUF_SIZE 1024
@@ -36,9 +35,6 @@
#define FD_MASTER 1
#define FD_SLAVE 2
#define _PRF_N_(no) __attribute__((__format__(__printf__, (no), (no) + 1)))
#define _PRF2_ _PRF_N_(2)
static struct ttynvt_param {
unsigned int major;
@@ -84,80 +80,6 @@ typedef struct {
tn_ctx_t *tn;
} ttynvt_t;
#include <sys/syscall.h>
#define gettid() (pid_t)syscall(__NR_gettid)
static const char *_hms_txt(char *buf, unsigned int len)
{
struct timeval tv;
unsigned int sod, hh, mm, ss;
gettimeofday(&tv, NULL);
sod = tv.tv_sec % (24 * 3600);
hh = sod / 3600;
mm = (sod - hh * 3600) / 60;
ss = sod - hh * 3600 - mm * 60;
snprintf(buf, len, "%02d:%02d:%02d.%06d",
hh, mm, ss, (unsigned int)tv.tv_usec);
return buf;
}
_PRF2_ static void _log(int prio, const char *fmt, ...)
{
va_list arg;
char buf[256], buft[64];
va_start(arg, fmt);
vsnprintf(buf, sizeof(buf), fmt, arg);
if (ttynvt_param.logstd)
{
printf("%s: [%d] %d: %s",
_hms_txt(buft, sizeof(buft)), gettid(), prio, buf);
}
else
{
syslog(prio, "[%d] %s", gettid(), buf);
}
va_end(arg);
}
#define BPL 16 /* Bytes per line */
void _log_buf(int prio, const char *txt, const void *ptr_, unsigned int len)
{
unsigned int ix, i, imax;
int l, n;
char buf[128];
const unsigned char *ptr = ptr_;
for (ix = 0; ix < len || (len == 0 && ix == 0); ix += BPL)
{
n = snprintf(buf, sizeof(buf), "%s: %04x:", txt, ix);
if (n <= 0)
return; /* Just playing it safe */
imax = (len - ix < BPL) ? len - ix : BPL;
for (i = 0; i < imax; i++)
{
l = snprintf(buf + n, sizeof(buf) - n, " %02x", *ptr++);
if (l <= 0)
return; /* Just playing it safe */
n += l;
}
_log(prio, "%s\n", buf);
}
}
#define DBG(...) \
if (ttynvt_param.debug > 0) _log(LOG_DEBUG, __VA_ARGS__)
#define DBG2(...) \
if (ttynvt_param.debug > 1) _log(LOG_DEBUG, __VA_ARGS__)
#define DBG2_BUF(txt, ptr, len) \
if (ttynvt_param.debug > 1) _log_buf(LOG_DEBUG, txt, ptr, len)
static void _fd_close(int fd)
{
if (fd >= 0)
@@ -213,7 +135,7 @@ static void ttynvt_release(fuse_req_t req, struct fuse_file_info *info)
{
ttynvt_t *tty = (ttynvt_t *) (uintptr_t) info->fh;
_log(LOG_INFO, "connection closed\n");
nvt_log(LOG_INFO, "connection closed\n");
tty->rel_pending = 1;
_fd_close(tty->fds[FD_NET].fd);
@@ -309,12 +231,12 @@ static void *_read_net(void *arg)
NET_BUF_SIZE - tty->net_cnt);
if (res < 0)
{
_log(LOG_ERR, "net read error: %m\n");
nvt_log(LOG_ERR, "net read error: %m\n");
break;
}
if (res == 0)
{
_log(LOG_INFO, "Connection closed by remote host\n");
nvt_log(LOG_INFO, "Connection closed by remote host\n");
break;
}
tty->net_cnt += res;
@@ -328,7 +250,7 @@ static void *_read_net(void *arg)
write(tty->fds[FD_MASTER].fd, tty->net_buf, tty->net_cnt);
if (res < 0)
{
_log(LOG_ERR, "master write error: %m\n");
nvt_log(LOG_ERR, "master write error: %m\n");
break;
}
tty->net_cnt = 0;
@@ -343,7 +265,7 @@ static void *_read_net(void *arg)
DBG("%s: read res=%d pollout=%d\n", __func__, res, tty->pollout);
if (res < 0)
{
_log(LOG_ERR, "master read error: %m\n");
nvt_log(LOG_ERR, "master read error: %m\n");
break;
}
if (tty->pollout == 0)
@@ -382,14 +304,14 @@ static int _srv_connect(const char *host, unsigned int port)
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
_log(LOG_ERR, "Socket open failed: %m\n");
nvt_log(LOG_ERR, "Socket open failed: %m\n");
return sockfd;
}
server = gethostbyname(host);
if (server == NULL)
{
_log(LOG_ERR, "Cannot resolve host name: %m\n");
nvt_log(LOG_ERR, "Cannot resolve host name: %m\n");
close(sockfd);
return -1;
}
@@ -403,7 +325,7 @@ static int _srv_connect(const char *host, unsigned int port)
err = connect(sockfd, (struct sockaddr *)&serveraddr, sizeof(serveraddr));
if (err < 0)
{
_log(LOG_ERR, "Failed to connect to: %s:%u: %m\n", host, port);
nvt_log(LOG_ERR, "Failed to connect to: %s:%u: %m\n", host, port);
close(sockfd);
return -1;
}
@@ -588,8 +510,8 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
info->nonseekable = 1;
info->direct_io = 1;
_log(LOG_INFO, "Connected to server: %s:%d\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_INFO, "Connected to server: %s:%d\n",
ttynvt_param.host, ttynvt_param.port);
fuse_reply_open(req, info);
@@ -597,8 +519,8 @@ static void ttynvt_open(fuse_req_t req, struct fuse_file_info *info)
open_err:
_log(LOG_INFO, "Connection to server: %s:%d failed: %m\n",
ttynvt_param.host, ttynvt_param.port);
nvt_log(LOG_INFO, "Connection to server: %s:%d failed: %m\n",
ttynvt_param.host, ttynvt_param.port);
for (n = 0; n < 3; n++)
_fd_close(tty->fds[n].fd);
@@ -623,7 +545,7 @@ ttynvt_read(fuse_req_t req, size_t size, off_t off,
DBG2("%s: tty->error=%d\n", __func__, tty->error);
if (tty->epipe == 0)
{
_log(LOG_WARNING, "Rx EPIPE\n");
nvt_log(LOG_WARNING, "Rx EPIPE\n");
tty->epipe = 1;
fuse_reply_buf(req, NULL, 0);
}
@@ -1179,6 +1101,9 @@ int main(int argc, char *argv[])
return 1;
}
log_level = ttynvt_param.debug;
log_stdout = ttynvt_param.logstd;
if (!ttynvt_param.dev_name)
ttynvt_param.dev_name = "ttyNVT0";
@@ -1221,14 +1146,14 @@ int main(int argc, char *argv[])
switch (cpid)
{
case -1:
_log(LOG_ERR, "fork(): %m\n");
nvt_log(LOG_ERR, "fork(): %m\n");
return -1;
case 0: /* Child */
DBG("Child proceeding...\n");
rc = cuse_lowlevel_main(args.argc, args.argv, &ci, &ttynvt_op, NULL);
if (rc != 0)
_log(LOG_ERR, "ttynvt failed rc=%d\n", rc);
nvt_log(LOG_ERR, "ttynvt failed rc=%d\n", rc);
else
DBG("Child exited\n");
return rc;