xref: /dpdk/lib/eal/linux/eal_log.c (revision 30a1de10)
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright(c) 2010-2014 Intel Corporation
3  */
4 
5 #include <stdio.h>
6 #include <sys/types.h>
7 #include <syslog.h>
8 
9 #include <rte_log.h>
10 
11 #include "eal_log.h"
12 
13 /*
14  * default log function
15  */
16 static ssize_t
console_log_write(__rte_unused void * c,const char * buf,size_t size)17 console_log_write(__rte_unused void *c, const char *buf, size_t size)
18 {
19 	ssize_t ret;
20 
21 	/* write on stderr */
22 	ret = fwrite(buf, 1, size, stderr);
23 	fflush(stderr);
24 
25 	/* Syslog error levels are from 0 to 7, so subtract 1 to convert */
26 	syslog(rte_log_cur_msg_loglevel() - 1, "%.*s", (int)size, buf);
27 
28 	return ret;
29 }
30 
31 static int
console_log_close(__rte_unused void * c)32 console_log_close(__rte_unused void *c)
33 {
34 	closelog();
35 	return 0;
36 }
37 
38 static cookie_io_functions_t console_log_func = {
39 	.write = console_log_write,
40 	.close = console_log_close,
41 };
42 
43 /*
44  * set the log to default function, called during eal init process,
45  * once memzones are available.
46  */
47 int
eal_log_init(const char * id,int facility)48 eal_log_init(const char *id, int facility)
49 {
50 	FILE *log_stream;
51 
52 	log_stream = fopencookie(NULL, "w+", console_log_func);
53 	if (log_stream == NULL)
54 		return -1;
55 
56 	openlog(id, LOG_NDELAY | LOG_PID, facility);
57 
58 	eal_log_set_default(log_stream);
59 
60 	return 0;
61 }
62