logging: Some refactoring

This commit is contained in:
Kim Woelders
2023-02-09 17:00:41 +01:00
parent 05b9b13b04
commit 6e5c44eb29
3 changed files with 36 additions and 7 deletions
+26 -3
View File
@@ -14,7 +14,30 @@
#define gettid() (pid_t)syscall(__NR_gettid)
char log_level = 0;
char log_stdout = 0;
static FILE *log_dest = NULL;
void nvt_log_dest(int dest)
{
switch (dest)
{
default:
case NVT_LOG_SYSLOG:
log_dest = NULL;
break;
case NVT_LOG_STDOUT:
log_dest = stdout;
break;
case NVT_LOG_STDERR:
log_dest = stderr;
break;
}
}
void nvt_log_level(int level)
{
log_level = level;
}
static const char *_hms_txt(char *buf, unsigned int len)
{
@@ -43,10 +66,10 @@ void nvt_log(int prio, const char *fmt, ...)
va_start(arg, fmt);
len = vsnprintf(buf, sizeof(buf), fmt, arg);
if (log_stdout)
if (log_dest)
{
eol = (len > 0 && buf[len - 1] != '\n') ? "\n" : "";
printf("%s: [%d] %d: %s%s",
fprintf(log_dest, "%s: [%d] %d: %s%s",
_hms_txt(buft, sizeof(buft)), gettid(), prio, buf, eol);
}
else
+6 -1
View File
@@ -9,12 +9,17 @@
#define _PRF_N_(no) __attribute__((__format__(__printf__, (no), (no) + 1)))
#define _PRF2_ _PRF_N_(2)
void nvt_log_dest(int dest);
void nvt_log_level(int level);
_PRF2_ void nvt_log(int prio, const char *fmt, ...);
void nvt_log_buf(int prio, const char *txt,
const void *ptr, unsigned int len);
#define NVT_LOG_SYSLOG 0
#define NVT_LOG_STDOUT 1
#define NVT_LOG_STDERR 2
extern char log_level;
extern char log_stdout;
#define DBG(...) \
if (log_level > 0) nvt_log(LOG_DEBUG, __VA_ARGS__)
+3 -2
View File
@@ -1192,8 +1192,9 @@ int main(int argc, char *argv[])
return 1;
}
log_level = ttynvt_param.debug;
log_stdout = ttynvt_param.logstd;
nvt_log_level(ttynvt_param.debug);
if (ttynvt_param.logstd)
nvt_log_dest(NVT_LOG_STDOUT);
if (!ttynvt_param.dev_name)
ttynvt_param.dev_name = "ttyNVT0";