1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
|
/*
* (C) 2014 by Pablo Neira Ayuso <pablo@netfilter.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*/
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <syslog.h>
#include "config.h"
#include "logging.h"
static struct {
const char *text;
const char *color;
} logging[NFTS_LOG_MAX] = {
[NFTS_LOG_DEBUG] = {
.text = "DEBUG",
.color = "\033[1;33m",
},
[NFTS_LOG_INFO] = {
.text = "INFO",
.color = "\033[1;32m",
},
[NFTS_LOG_NOTICE] = {
.text = "NOTICE",
.color = "\033[1;36m",
},
[NFTS_LOG_ERROR] = {
.text = "ERROR",
.color = "\033[1;33m",
},
[NFTS_LOG_FATAL] = {
.text = "FATAL",
.color = "\033[1;31m",
},
};
int nft_sync_log_init(struct nft_sync_inst *inst)
{
int ret = 0;
switch (inst->log.type) {
case NFTS_LOG_T_SYSLOG:
break;
case NFTS_LOG_T_FILE:
if (inst->log.fd == NULL)
inst->log.fd = stdout;
else {
inst->log.fd = fopen(inst->log.filename, "w+");
if (inst->log.fd == NULL)
return -1;
}
break;
}
return ret;
}
void nft_sync_log_fini(struct nft_sync_inst *inst)
{
switch (inst->log.type) {
case NFTS_LOG_T_SYSLOG:
break;
case NFTS_LOG_T_FILE:
if (inst->log.fd != NULL)
fclose(inst->log.fd);
break;
}
}
void nft_sync_log(struct nft_sync_inst *inst, int prio,
const char *format, ...)
{
time_t t;
char *timebuf = NULL;
va_list args;
switch (inst->log.type) {
case NFTS_LOG_T_FILE:
t = time(NULL);
timebuf = ctime(&t);
timebuf[strlen(timebuf) - 1]='\0';
break;
case NFTS_LOG_T_SYSLOG:
break;
}
switch (inst->log.type) {
case NFTS_LOG_T_FILE:
va_start(args, format);
fprintf(inst->log.fd, "%s[%s] [%s] ",
inst->log.color ? logging[prio].color : "", timebuf,
logging[prio].text);
vfprintf(inst->log.fd, format, args);
va_end(args);
fprintf(inst->log.fd, "%s\n",
inst->log.color ? "\033[1;0m" : "");
fflush(inst->log.fd);
break;
case NFTS_LOG_T_SYSLOG:
va_start(args, format);
vsyslog(prio, format, args);
va_end(args);
break;
}
}
|