tailq: remove unneeded inclusions
[dpdk.git] / lib / librte_eal / linuxapp / eal / eal_log.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Intel Corporation nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <string.h>
35 #include <stdio.h>
36 #include <stdint.h>
37 #include <sys/types.h>
38 #include <syslog.h>
39 #include <sys/queue.h>
40
41 #include <rte_memory.h>
42 #include <rte_memzone.h>
43 #include <rte_eal.h>
44 #include <rte_launch.h>
45 #include <rte_per_lcore.h>
46 #include <rte_lcore.h>
47 #include <rte_spinlock.h>
48 #include <rte_log.h>
49
50 #include "eal_private.h"
51
52 /*
53  * default log function, used once mempool (hence log history) is
54  * available
55  */
56 static ssize_t
57 console_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
58 {
59         char copybuf[BUFSIZ + 1];
60         ssize_t ret;
61         uint32_t loglevel;
62
63         /* add this log in history */
64         rte_log_add_in_history(buf, size);
65
66         /* write on stdout */
67         ret = fwrite(buf, 1, size, stdout);
68         fflush(stdout);
69
70         /* truncate message if too big (should not happen) */
71         if (size > BUFSIZ)
72                 size = BUFSIZ;
73
74         /* Syslog error levels are from 0 to 7, so subtract 1 to convert */
75         loglevel = rte_log_cur_msg_loglevel() - 1;
76         memcpy(copybuf, buf, size);
77         copybuf[size] = '\0';
78
79         /* write on syslog too */
80         syslog(loglevel, "%s", copybuf);
81
82         return ret;
83 }
84
85 static cookie_io_functions_t console_log_func = {
86         .write = console_log_write,
87 };
88
89 /*
90  * set the log to default function, called during eal init process,
91  * once memzones are available.
92  */
93 int
94 rte_eal_log_init(const char *id, int facility)
95 {
96         FILE *log_stream;
97
98         log_stream = fopencookie(NULL, "w+", console_log_func);
99         if (log_stream == NULL)
100                 return -1;
101
102         openlog(id, LOG_NDELAY | LOG_PID, facility);
103
104         if (rte_eal_common_log_init(log_stream) < 0)
105                 return -1;
106
107         return 0;
108 }
109
110 /* early logs */
111
112 /*
113  * early log function, used during boot when mempool (hence log
114  * history) is not available
115  */
116 static ssize_t
117 early_log_write(__attribute__((unused)) void *c, const char *buf, size_t size)
118 {
119         ssize_t ret;
120         ret = fwrite(buf, size, 1, stdout);
121         fflush(stdout);
122         if (ret == 0)
123                 return -1;
124         return ret;
125 }
126
127 static cookie_io_functions_t early_log_func = {
128         .write = early_log_write,
129 };
130 static FILE *early_log_stream;
131
132 /*
133  * init the log library, called by rte_eal_init() to enable early
134  * logs
135  */
136 int
137 rte_eal_log_early_init(void)
138 {
139         early_log_stream = fopencookie(NULL, "w+", early_log_func);
140         if (early_log_stream == NULL) {
141                 printf("Cannot configure early_log_stream\n");
142                 return -1;
143         }
144         rte_openlog_stream(early_log_stream);
145         return 0;
146 }