New upstream version 18.02
[deb_dpdk.git] / lib / librte_eal / linuxapp / eal / 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_private.h"
21
22 /*
23  * default log function
24  */
25 static ssize_t
26 console_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
27 {
28         char copybuf[BUFSIZ + 1];
29         ssize_t ret;
30         uint32_t loglevel;
31
32         /* write on stdout */
33         ret = fwrite(buf, 1, size, stdout);
34         fflush(stdout);
35
36         /* truncate message if too big (should not happen) */
37         if (size > BUFSIZ)
38                 size = BUFSIZ;
39
40         /* Syslog error levels are from 0 to 7, so subtract 1 to convert */
41         loglevel = rte_log_cur_msg_loglevel() - 1;
42         memcpy(copybuf, buf, size);
43         copybuf[size] = '\0';
44
45         /* write on syslog too */
46         syslog(loglevel, "%s", copybuf);
47
48         return ret;
49 }
50
51 static cookie_io_functions_t console_log_func = {
52         .write = console_log_write,
53 };
54
55 /*
56  * set the log to default function, called during eal init process,
57  * once memzones are available.
58  */
59 int
60 rte_eal_log_init(const char *id, int facility)
61 {
62         FILE *log_stream;
63
64         log_stream = fopencookie(NULL, "w+", console_log_func);
65         if (log_stream == NULL)
66                 return -1;
67
68         openlog(id, LOG_NDELAY | LOG_PID, facility);
69
70         eal_log_set_default(log_stream);
71
72         return 0;
73 }