-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.c
115 lines (95 loc) · 2.15 KB
/
log.c
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
114
115
#include <errno.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <event2/util.h>
#include "log.h"
static void socks_log(int serverity, const char *msg);
void log_ex(int eval, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_output(SOCKS_LOG_ERROR, NULL, fmt, ap);
va_end(ap);
exit(eval);
}
void log_e(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_output(SOCKS_LOG_ERROR, strerror(errno), fmt, ap);
va_end(ap);
}
void log_warn(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_output(SOCKS_LOG_WARN, strerror(errno), fmt, ap);
va_end(ap);
}
void log_d(int v, const char *fmt, ...)
{
va_list ap;
if (v > 0) {
va_start(ap, fmt);
log_output(SOCKS_LOG_DEBUG, NULL, fmt, ap);
va_end(ap);
}
}
void log_i(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
log_output(SOCKS_LOG_INFO, NULL, fmt, ap);
va_end(ap);
}
void log_output(int serverity, const char *errstr, const char *fmt, va_list ap)
{
char buf[512];
size_t len;
if (fmt != NULL)
evutil_vsnprintf(buf, sizeof(buf), fmt, ap);
else
// null-terminated
buf[0] = '\0';
if (errstr) {
len = strlen(buf);
if (len < sizeof(buf) - 3) {
evutil_snprintf(buf + len, sizeof(buf) - len, ": %s", errstr);
}
}
socks_log(serverity, buf);
}
static void socks_log(int serverity, const char *msg)
{
time_t t;
struct timeval now;
struct tm tm;
int ms;
const char *serverity_str = NULL;
char timebuf[64];
size_t n;
evutil_gettimeofday(&now, NULL);
t = (time_t)now.tv_sec;
ms = (int)now.tv_usec / 1000;
n = strftime(timebuf, sizeof(timebuf), "%b %d %H:%M:%S",
localtime_r(&t, &tm));
evutil_snprintf(timebuf+n, sizeof(timebuf)-n, ".%.3i", ms);
switch (serverity) {
case SOCKS_LOG_DEBUG:
serverity_str = "debug";
break;
case SOCKS_LOG_INFO:
serverity_str = "info";
break;
case SOCKS_LOG_WARN:
serverity_str = "warn";
break;
case SOCKS_LOG_ERROR:
serverity_str = "error";
break;
}
(void)fprintf(stderr, "%s [%s] %s\n", timebuf, serverity_str, msg);
}