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