5a795ac9ebe3f9f695be4d881f31979aa3c1d637
[dpdk.git] / lib / eal / linux / eal_log.c
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_log.h"
21
22 /*
23  * default log function
24  */
25 static ssize_t
26 console_log_write(__rte_unused void *c, const char *buf, size_t size)
27 {
28         ssize_t ret;
29
30         /* write on stderr */
31         ret = fwrite(buf, 1, size, stderr);
32         fflush(stderr);
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 int
41 console_log_close(__rte_unused void *c)
42 {
43         closelog();
44         return 0;
45 }
46
47 static cookie_io_functions_t console_log_func = {
48         .write = console_log_write,
49         .close = console_log_close,
50 };
51
52 /*
53  * set the log to default function, called during eal init process,
54  * once memzones are available.
55  */
56 int
57 eal_log_init(const char *id, int facility)
58 {
59         FILE *log_stream;
60
61         log_stream = fopencookie(NULL, "w+", console_log_func);
62         if (log_stream == NULL)
63                 return -1;
64
65         openlog(id, LOG_NDELAY | LOG_PID, facility);
66
67         eal_log_set_default(log_stream);
68
69         return 0;
70 }