forked from Netflix/dynomite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdyn_log.c
255 lines (202 loc) · 5.63 KB
/
dyn_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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
/*
* Dynomite - A thin, distributed replication layer for multi non-distributed
* storages. Copyright (C) 2014 Netflix, Inc.
*/
/*
* twemproxy - A fast and lightweight proxy for memcached protocol.
* Copyright (C) 2011 Twitter, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <ctype.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <time.h>
#include "dyn_core.h"
static struct logger logger;
/**
* Initialize logging including log level and output target. Logging output may
* be sent to standard error or to a log file.
* @param[in] level Log level.
* @param[in] name Full path to the log file.
* @return
*/
int log_init(int level, char *name) {
struct logger *l = &logger;
l->level = MAX(LOG_EMERG, MIN(level, LOG_PVERB));
l->name = name;
if (name == NULL || !strlen(name)) {
l->fd = STDERR_FILENO;
} else {
l->fd = open(name, O_WRONLY | O_APPEND | O_CREAT, 0666);
if (l->fd < 0) {
log_stderr("opening log file '%s' failed: %s", name, strerror(errno));
return -1;
}
}
return 0;
}
/**
* Close the logging file descriptor.
*/
void log_deinit(void) {
struct logger *l = &logger;
if (l->fd < 0 || l->fd == STDERR_FILENO) {
return;
}
close(l->fd);
}
void log_reopen(void) {
struct logger *l = &logger;
if (l->fd != STDERR_FILENO) {
close(l->fd);
l->fd = open(l->name, O_WRONLY | O_APPEND | O_CREAT, 0644);
if (l->fd < 0) {
log_stderr("reopening log file '%s' failed, ignored: %s", l->name,
strerror(errno));
}
}
}
void log_level_up(void) {
struct logger *l = &logger;
if (l->level < LOG_PVERB) {
l->level++;
loga("up log level to %d", l->level);
}
}
void log_level_down(void) {
struct logger *l = &logger;
if (l->level > LOG_EMERG) {
l->level--;
loga("down log level to %d", l->level);
}
}
void log_level_set(int level) {
struct logger *l = &logger;
l->level = MAX(LOG_EMERG, MIN(level, LOG_PVERB));
loga("set log level to %d", l->level);
}
int log_loggable(int level) {
struct logger *l = &logger;
if (level > l->level) {
return 0;
}
return 1;
}
void _log(const char *file, int line, int panic, const char *fmt, ...) {
struct logger *l = &logger;
int len, size, errno_save;
char buf[LOG_MAX_LEN];
va_list args;
ssize_t n;
if (l->fd < 0) {
return;
}
errno_save = errno;
len = 0; /* length of output buffer */
size = LOG_MAX_LEN; /* size of output buffer */
struct timeval curTime;
gettimeofday(&curTime, NULL);
char buffer[80];
strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", localtime(&curTime.tv_sec));
// May be not the perfect place to fix this
len +=
dn_scnprintf(buf + len, size - len, "[%.*s.%03d] %s:%d ", strlen(buffer),
buffer, (int64_t)curTime.tv_usec / 1000, file, line);
va_start(args, fmt);
len += dn_vscnprintf(buf + len, size - len, fmt, args);
va_end(args);
buf[len++] = '\n';
n = dn_write(l->fd, buf, len);
if (n < 0) {
l->nerror++;
}
errno = errno_save;
if (panic) {
fsync(l->fd);
close(l->fd);
abort();
}
}
void _log_stderr(const char *fmt, ...) {
struct logger *l = &logger;
int len, size, errno_save;
char buf[4 * LOG_MAX_LEN];
va_list args;
ssize_t n;
errno_save = errno;
len = 0; /* length of output buffer */
size = 4 * LOG_MAX_LEN; /* size of output buffer */
va_start(args, fmt);
len += dn_vscnprintf(buf, size, fmt, args);
va_end(args);
buf[len++] = '\n';
n = dn_write(STDERR_FILENO, buf, len);
if (n < 0) {
l->nerror++;
}
errno = errno_save;
}
/*
* Hexadecimal dump in the canonical hex + ascii display
* See -C option in man hexdump
*/
void _log_hexdump(const char *file, int line, char *data, int datalen,
const char *fmt, ...) {
struct logger *l = &logger;
char buf[8 * LOG_MAX_LEN];
int i, off, len, size, errno_save;
ssize_t n;
if (l->fd < 0) {
return;
}
/* log hexdump */
errno_save = errno;
off = 0; /* data offset */
len = 0; /* length of output buffer */
size = 8 * LOG_MAX_LEN; /* size of output buffer */
while (datalen != 0 && (len < size - 1)) {
char *save, *str;
unsigned char c;
int savelen;
len += dn_scnprintf(buf + len, size - len, "%08x ", off);
save = data;
savelen = datalen;
for (i = 0; datalen != 0 && i < 16; data++, datalen--, i++) {
c = (unsigned char)(*data);
str = (i == 7) ? " " : " ";
len += dn_scnprintf(buf + len, size - len, "%02x%s", c, str);
}
for (; i < 16; i++) {
str = (i == 7) ? " " : " ";
len += dn_scnprintf(buf + len, size - len, " %s", str);
}
data = save;
datalen = savelen;
len += dn_scnprintf(buf + len, size - len, " |");
for (i = 0; datalen != 0 && i < 16; data++, datalen--, i++) {
c = (unsigned char)(isprint(*data) ? *data : '.');
len += dn_scnprintf(buf + len, size - len, "%c", c);
}
len += dn_scnprintf(buf + len, size - len, "|\n");
off += 16;
}
n = dn_write(l->fd, buf, len);
if (n < 0) {
l->nerror++;
}
errno = errno_save;
}