Imported Upstream version 16.07-rc1
[deb_dpdk.git] / lib / librte_eal / common / eal_common_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 <stdio.h>
35 #include <stdint.h>
36 #include <stdarg.h>
37 #include <stdlib.h>
38
39 #include <rte_log.h>
40 #include <rte_per_lcore.h>
41
42 #include "eal_private.h"
43
44 /* global log structure */
45 struct rte_logs rte_logs = {
46         .type = ~0,
47         .level = RTE_LOG_DEBUG,
48         .file = NULL,
49 };
50
51 static FILE *default_log_stream;
52
53 /**
54  * This global structure stores some informations about the message
55  * that is currently beeing processed by one lcore
56  */
57 struct log_cur_msg {
58         uint32_t loglevel; /**< log level - see rte_log.h */
59         uint32_t logtype;  /**< log type  - see rte_log.h */
60 };
61
62  /* per core log */
63 static RTE_DEFINE_PER_LCORE(struct log_cur_msg, log_cur_msg);
64
65 /* default logs */
66
67 int
68 rte_log_add_in_history(const char *buf __rte_unused, size_t size __rte_unused)
69 {
70         return 0;
71 }
72
73 void
74 rte_log_set_history(int enable)
75 {
76         if (enable)
77                 RTE_LOG(WARNING, EAL, "The log history is deprecated.\n");
78 }
79
80 /* Change the stream that will be used by logging system */
81 int
82 rte_openlog_stream(FILE *f)
83 {
84         if (f == NULL)
85                 rte_logs.file = default_log_stream;
86         else
87                 rte_logs.file = f;
88         return 0;
89 }
90
91 /* Set global log level */
92 void
93 rte_set_log_level(uint32_t level)
94 {
95         rte_logs.level = (uint32_t)level;
96 }
97
98 /* Get global log level */
99 uint32_t
100 rte_get_log_level(void)
101 {
102         return rte_logs.level;
103 }
104
105 /* Set global log type */
106 void
107 rte_set_log_type(uint32_t type, int enable)
108 {
109         if (enable)
110                 rte_logs.type |= type;
111         else
112                 rte_logs.type &= (~type);
113 }
114
115 /* Get global log type */
116 uint32_t
117 rte_get_log_type(void)
118 {
119         return rte_logs.type;
120 }
121
122 /* get the current loglevel for the message beeing processed */
123 int rte_log_cur_msg_loglevel(void)
124 {
125         return RTE_PER_LCORE(log_cur_msg).loglevel;
126 }
127
128 /* get the current logtype for the message beeing processed */
129 int rte_log_cur_msg_logtype(void)
130 {
131         return RTE_PER_LCORE(log_cur_msg).logtype;
132 }
133
134 /* Dump log history to file */
135 void
136 rte_log_dump_history(FILE *out __rte_unused)
137 {
138 }
139
140 /*
141  * Generates a log message The message will be sent in the stream
142  * defined by the previous call to rte_openlog_stream().
143  */
144 int
145 rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
146 {
147         int ret;
148         FILE *f = rte_logs.file;
149
150         if ((level > rte_logs.level) || !(logtype & rte_logs.type))
151                 return 0;
152
153         /* save loglevel and logtype in a global per-lcore variable */
154         RTE_PER_LCORE(log_cur_msg).loglevel = level;
155         RTE_PER_LCORE(log_cur_msg).logtype = logtype;
156
157         ret = vfprintf(f, format, ap);
158         fflush(f);
159         return ret;
160 }
161
162 /*
163  * Generates a log message The message will be sent in the stream
164  * defined by the previous call to rte_openlog_stream().
165  * No need to check level here, done by rte_vlog().
166  */
167 int
168 rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
169 {
170         va_list ap;
171         int ret;
172
173         va_start(ap, format);
174         ret = rte_vlog(level, logtype, format, ap);
175         va_end(ap);
176         return ret;
177 }
178
179 /*
180  * called by environment-specific log init function
181  */
182 int
183 rte_eal_common_log_init(FILE *default_log)
184 {
185         default_log_stream = default_log;
186         rte_openlog_stream(default_log);
187
188 #if RTE_LOG_LEVEL >= RTE_LOG_DEBUG
189         RTE_LOG(NOTICE, EAL, "Debug logs available - lower performance\n");
190 #endif
191
192         return 0;
193 }