summaryrefslogtreecommitdiff
path: root/accel-dp/log.c
blob: bd0d6bbd751bbad433b01eda10e8771e2693e124 (plain)
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <errno.h>
#include <string.h>
#include <stdint.h>
#include <signal.h>
#include <pthread.h>
#include <sys/time.h>

#include <rte_malloc.h>

#include "init.h"
#include "common.h"
#include "conf_file.h"

#include "log.h"

#define LOG_MSG   0
#define LOG_ERROR 1
#define LOG_WARN  2
#define LOG_INFO1 3
#define LOG_INFO2 4
#define LOG_DEBUG 5

struct _log_msg {
	struct list_head entry;
	int level;
	struct timeval timestamp;
	struct list_head chunks;
	unsigned int refs;
};

static int log_level;

static LIST_HEAD(targets);

static pthread_key_t pth_key;
static __thread struct _log_msg *cur_msg;
static __thread char *stat_buf;

static FILE *emerg_file;
static FILE *debug_file;

static void _log_free_msg(struct _log_msg *msg);
static struct log_msg *clone_msg(struct _log_msg *msg);
static int add_msg(struct _log_msg *msg, const char *buf, int len);
static void write_msg(FILE *f, struct _log_msg *msg);

static void stat_buf_free(void *ptr)
{
	rte_free(ptr);
}

void log_append(const char *str, int len)
{
	struct log_target *t;
	struct log_msg *m;

	if (!cur_msg)
		return;

	if (add_msg(cur_msg, str, len))
		goto out;

	if (str[len - 1] != '\n')
		return;

	if (debug_file)
		write_msg(debug_file, cur_msg);

	list_for_each_entry(t, &targets, entry) {
		m = clone_msg(cur_msg);
		if (!m)
			break;
		t->log(m);
	}

out:
	_log_free_msg(cur_msg);
	cur_msg = NULL;
}

static void do_log(int level, const char *fmt, va_list ap)
{
	if (!cur_msg) {
		cur_msg = rte_malloc(NULL, sizeof(*cur_msg), 0);
		if (!cur_msg)
			return;
		INIT_LIST_HEAD(&cur_msg->chunks);
		cur_msg->refs = 1;
		cur_msg->level = level;
		gettimeofday(&cur_msg->timestamp, NULL);
	}

	if (!stat_buf) {
		stat_buf = rte_malloc(NULL, LOG_MAX_SIZE + 1, 0);
		pthread_setspecific(pth_key, stat_buf);
	}

	vsnprintf(stat_buf, LOG_MAX_SIZE, fmt, ap);
	log_append(stat_buf, strlen(stat_buf));
}

void log_error(const char *fmt,...)
{
	if (log_level >= LOG_ERROR) {
		va_list ap;
		va_start(ap,fmt);
		do_log(LOG_ERROR, fmt, ap);
		va_end(ap);
	}
}

void log_warn(const char *fmt,...)
{
	if (log_level >= LOG_WARN) {
		va_list ap;
		va_start(ap,fmt);
		do_log(LOG_WARN, fmt, ap);
		va_end(ap);
	}
}

void log_info1(const char *fmt,...)
{
	if (log_level >= LOG_INFO1) {
		va_list ap;
		va_start(ap, fmt);
		do_log(LOG_INFO1, fmt, ap);
		va_end(ap);
	}
}

void log_info2(const char *fmt,...)
{
	if (log_level >= LOG_INFO2) {
		va_list ap;
		va_start(ap, fmt);
		do_log(LOG_INFO2, fmt, ap);
		va_end(ap);
	}
}

void log_debug(const char *fmt,...)
{
	if (log_level >= LOG_DEBUG) {
		va_list ap;
		va_start(ap, fmt);
		do_log(LOG_DEBUG, fmt, ap);
		va_end(ap);
	}
}

void log_debug2(const char *fmt,...)
{
	va_list ap;
	if (!debug_file)
		return;
	va_start(ap, fmt);
	vfprintf(debug_file, fmt, ap);
	va_end(ap);
	fflush(debug_file);
}
void log_msg(const char *fmt,...)
{
	va_list ap;
	va_start(ap, fmt);
	do_log(LOG_MSG, fmt, ap);
	va_end(ap);
}

void log_emerg(const char *fmt, ...)
{
	if (emerg_file) {
		va_list ap;
		va_start(ap, fmt);
		vfprintf(emerg_file, fmt, ap);
		va_end(ap);
		fflush(emerg_file);
	}
}

void log_free_msg(struct log_msg *m)
{
	struct _log_msg *msg = (struct _log_msg *)m->lpd;

	//printf("free msg %p\n", m);

	rte_free(m->hdr);
	_log_free_msg(msg);

	rte_free(m);
}


static void _log_free_msg(struct _log_msg *msg)
{
	struct log_chunk *chunk;

	if (__sync_sub_and_fetch(&msg->refs, 1))
		return;

	while(!list_empty(&msg->chunks)) {
		chunk = list_entry(msg->chunks.next, typeof(*chunk), entry);
		list_del(&chunk->entry);
		rte_free(chunk);
	}

	rte_free(msg);
}

static struct log_msg *clone_msg(struct _log_msg *msg)
{
	struct log_msg *m = rte_malloc(NULL, sizeof(*m), 0);
	if (!m) {
		log_emerg("log: out of memory\n");
		return NULL;
	}

	m->hdr = rte_malloc(NULL, sizeof(*m->hdr), 0);
	if (!m->hdr) {
		log_emerg("log: out of memory\n");
		rte_free(m);
		return NULL;
	}

	m->hdr->len = 0;
	m->lpd = msg;
	m->chunks = &msg->chunks;
	m->timestamp = msg->timestamp;
	m->level = msg->level;

	__sync_add_and_fetch(&msg->refs, 1);

	//printf("clone msg %p\n", m);
	return m;
}

static int add_msg(struct _log_msg *msg, const char *buf, int len)
{
	struct log_chunk *chunk;
	int i, chunk_cnt, n;

	if (!list_empty(&msg->chunks)) {
		chunk = list_entry(msg->chunks.prev, typeof(*chunk), entry);
		if (chunk->len != LOG_CHUNK_SIZE) {
			n = LOG_CHUNK_SIZE - chunk->len;
			if (n > len)
				n = len;
			memcpy(chunk->msg + chunk->len, buf, n);
			chunk->len += n;
			chunk->msg[chunk->len] = 0;
			buf += n;
			len -= n;
			if (len == 0)
				return 0;
		}
	}

	chunk_cnt = (len - 1)/LOG_CHUNK_SIZE + 1;

	for (i = 0; i < chunk_cnt; i++) {
		chunk = rte_malloc(NULL, sizeof(*chunk), 0);
		if (!chunk)
			return -1;

		chunk->len = i == chunk_cnt -1 ? len - i * LOG_CHUNK_SIZE : LOG_CHUNK_SIZE;
		memcpy(chunk->msg, buf + i * LOG_CHUNK_SIZE, chunk->len);
		chunk->msg[chunk->len] = 0;

		list_add_tail(&chunk->entry, &msg->chunks);
	}

	return 0;
}

static void write_msg(FILE *f, struct _log_msg *msg)
{
	struct log_chunk *chunk;

	fprintf(f, "[%u.%03u] ", (unsigned)msg->timestamp.tv_sec, (unsigned)msg->timestamp.tv_usec/1000);

	list_for_each_entry(chunk, &msg->chunks, entry)
		fwrite(chunk->msg, chunk->len, 1, f);

	fflush(f);
}

void log_register_target(struct log_target *t)
{
	list_add_tail(&t->entry, &targets);
}

static void sighup(int n)
{
	struct log_target *t;

	list_for_each_entry(t, &targets, entry)
		if (t->reopen)
			t->reopen();
}

static void config_load(void)
{
	const char *opt;

	opt = conf_get_opt("log", "level");
	if (opt && atoi(opt) >= 0)
		log_level = atoi(opt);

	opt = conf_get_opt("log", "log-emerg");
	if (opt) {
		if (emerg_file)
			emerg_file = freopen(opt, "a", emerg_file);
		else
			emerg_file = fopen(opt, "a");
		if (!emerg_file)
			fprintf(stderr, "log:open: %s\n", strerror(errno));
	} else if (emerg_file) {
		fclose(emerg_file);
		emerg_file = NULL;
	}

	opt = conf_get_opt("log", "log-debug");
	if (opt) {
		if (debug_file)
			debug_file = freopen(opt, "a", debug_file);
		else
			debug_file = fopen(opt, "a");
		if (!debug_file)
			fprintf(stderr, "log:open: %s\n", strerror(errno));
	} else if (debug_file) {
		fclose(debug_file);
		debug_file = NULL;
	}
}

static void log_init(void)
{
	pthread_key_create(&pth_key, stat_buf_free);

	config_load();

	signal(SIGHUP, sighup);
}

DEFINE_INIT(0, log_init);