15129044dSC.J. Collier/*-
25129044dSC.J. Collier *   BSD LICENSE
35129044dSC.J. Collier *
45129044dSC.J. Collier *   Copyright(c) 2010-2014 Intel Corporation. All rights reserved.
55129044dSC.J. Collier *   All rights reserved.
65129044dSC.J. Collier *
75129044dSC.J. Collier *   Redistribution and use in source and binary forms, with or without
85129044dSC.J. Collier *   modification, are permitted provided that the following conditions
95129044dSC.J. Collier *   are met:
105129044dSC.J. Collier *
115129044dSC.J. Collier *     * Redistributions of source code must retain the above copyright
125129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer.
135129044dSC.J. Collier *     * Redistributions in binary form must reproduce the above copyright
145129044dSC.J. Collier *       notice, this list of conditions and the following disclaimer in
155129044dSC.J. Collier *       the documentation and/or other materials provided with the
165129044dSC.J. Collier *       distribution.
175129044dSC.J. Collier *     * Neither the name of Intel Corporation nor the names of its
185129044dSC.J. Collier *       contributors may be used to endorse or promote products derived
195129044dSC.J. Collier *       from this software without specific prior written permission.
205129044dSC.J. Collier *
215129044dSC.J. Collier *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
225129044dSC.J. Collier *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
235129044dSC.J. Collier *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
245129044dSC.J. Collier *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
255129044dSC.J. Collier *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
265129044dSC.J. Collier *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
275129044dSC.J. Collier *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
285129044dSC.J. Collier *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
295129044dSC.J. Collier *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
305129044dSC.J. Collier *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
315129044dSC.J. Collier *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
325129044dSC.J. Collier */
335129044dSC.J. Collier
345129044dSC.J. Collier#ifndef _RTE_LOG_H_
355129044dSC.J. Collier#define _RTE_LOG_H_
365129044dSC.J. Collier
375129044dSC.J. Collier/**
385129044dSC.J. Collier * @file
395129044dSC.J. Collier *
405129044dSC.J. Collier * RTE Logs API
415129044dSC.J. Collier *
425129044dSC.J. Collier * This file provides a log API to RTE applications.
435129044dSC.J. Collier */
445129044dSC.J. Collier
455129044dSC.J. Collier#ifdef __cplusplus
465129044dSC.J. Collierextern "C" {
475129044dSC.J. Collier#endif
485129044dSC.J. Collier
495129044dSC.J. Collier#include <stdint.h>
505129044dSC.J. Collier#include <stdio.h>
515129044dSC.J. Collier#include <stdarg.h>
525129044dSC.J. Collier
535129044dSC.J. Collier/** The rte_log structure. */
545129044dSC.J. Collierstruct rte_logs {
555129044dSC.J. Collier	uint32_t type;  /**< Bitfield with enabled logs. */
565129044dSC.J. Collier	uint32_t level; /**< Log level. */
573d9b7210SChristian Ehrhardt	FILE *file;     /**< Output file set by rte_openlog_stream, or NULL. */
585129044dSC.J. Collier};
595129044dSC.J. Collier
605129044dSC.J. Collier/** Global log informations */
615129044dSC.J. Collierextern struct rte_logs rte_logs;
625129044dSC.J. Collier
635129044dSC.J. Collier/* SDK log type */
645129044dSC.J. Collier#define RTE_LOGTYPE_EAL     0x00000001 /**< Log related to eal. */
655129044dSC.J. Collier#define RTE_LOGTYPE_MALLOC  0x00000002 /**< Log related to malloc. */
665129044dSC.J. Collier#define RTE_LOGTYPE_RING    0x00000004 /**< Log related to ring. */
675129044dSC.J. Collier#define RTE_LOGTYPE_MEMPOOL 0x00000008 /**< Log related to mempool. */
685129044dSC.J. Collier#define RTE_LOGTYPE_TIMER   0x00000010 /**< Log related to timers. */
695129044dSC.J. Collier#define RTE_LOGTYPE_PMD     0x00000020 /**< Log related to poll mode driver. */
705129044dSC.J. Collier#define RTE_LOGTYPE_HASH    0x00000040 /**< Log related to hash table. */
715129044dSC.J. Collier#define RTE_LOGTYPE_LPM     0x00000080 /**< Log related to LPM. */
725129044dSC.J. Collier#define RTE_LOGTYPE_KNI     0x00000100 /**< Log related to KNI. */
735129044dSC.J. Collier#define RTE_LOGTYPE_ACL     0x00000200 /**< Log related to ACL. */
745129044dSC.J. Collier#define RTE_LOGTYPE_POWER   0x00000400 /**< Log related to power. */
755129044dSC.J. Collier#define RTE_LOGTYPE_METER   0x00000800 /**< Log related to QoS meter. */
765129044dSC.J. Collier#define RTE_LOGTYPE_SCHED   0x00001000 /**< Log related to QoS port scheduler. */
775129044dSC.J. Collier#define RTE_LOGTYPE_PORT    0x00002000 /**< Log related to port. */
785129044dSC.J. Collier#define RTE_LOGTYPE_TABLE   0x00004000 /**< Log related to table. */
795129044dSC.J. Collier#define RTE_LOGTYPE_PIPELINE 0x00008000 /**< Log related to pipeline. */
805129044dSC.J. Collier#define RTE_LOGTYPE_MBUF    0x00010000 /**< Log related to mbuf. */
815129044dSC.J. Collier#define RTE_LOGTYPE_CRYPTODEV 0x00020000 /**< Log related to cryptodev. */
825129044dSC.J. Collier
835129044dSC.J. Collier/* these log types can be used in an application */
845129044dSC.J. Collier#define RTE_LOGTYPE_USER1   0x01000000 /**< User-defined log type 1. */
855129044dSC.J. Collier#define RTE_LOGTYPE_USER2   0x02000000 /**< User-defined log type 2. */
865129044dSC.J. Collier#define RTE_LOGTYPE_USER3   0x04000000 /**< User-defined log type 3. */
875129044dSC.J. Collier#define RTE_LOGTYPE_USER4   0x08000000 /**< User-defined log type 4. */
885129044dSC.J. Collier#define RTE_LOGTYPE_USER5   0x10000000 /**< User-defined log type 5. */
895129044dSC.J. Collier#define RTE_LOGTYPE_USER6   0x20000000 /**< User-defined log type 6. */
905129044dSC.J. Collier#define RTE_LOGTYPE_USER7   0x40000000 /**< User-defined log type 7. */
915129044dSC.J. Collier#define RTE_LOGTYPE_USER8   0x80000000 /**< User-defined log type 8. */
925129044dSC.J. Collier
935129044dSC.J. Collier/* Can't use 0, as it gives compiler warnings */
945129044dSC.J. Collier#define RTE_LOG_EMERG    1U  /**< System is unusable.               */
955129044dSC.J. Collier#define RTE_LOG_ALERT    2U  /**< Action must be taken immediately. */
965129044dSC.J. Collier#define RTE_LOG_CRIT     3U  /**< Critical conditions.              */
975129044dSC.J. Collier#define RTE_LOG_ERR      4U  /**< Error conditions.                 */
985129044dSC.J. Collier#define RTE_LOG_WARNING  5U  /**< Warning conditions.               */
995129044dSC.J. Collier#define RTE_LOG_NOTICE   6U  /**< Normal but significant condition. */
1005129044dSC.J. Collier#define RTE_LOG_INFO     7U  /**< Informational.                    */
1015129044dSC.J. Collier#define RTE_LOG_DEBUG    8U  /**< Debug-level messages.             */
1025129044dSC.J. Collier
1035129044dSC.J. Collier/**
1045129044dSC.J. Collier * Change the stream that will be used by the logging system.
1055129044dSC.J. Collier *
1065129044dSC.J. Collier * This can be done at any time. The f argument represents the stream
1075129044dSC.J. Collier * to be used to send the logs. If f is NULL, the default output is
1085129044dSC.J. Collier * used (stderr).
1095129044dSC.J. Collier *
1105129044dSC.J. Collier * @param f
1115129044dSC.J. Collier *   Pointer to the stream.
1125129044dSC.J. Collier * @return
1135129044dSC.J. Collier *   - 0 on success.
1145129044dSC.J. Collier *   - Negative on error.
1155129044dSC.J. Collier */
1165129044dSC.J. Collierint rte_openlog_stream(FILE *f);
1175129044dSC.J. Collier
1185129044dSC.J. Collier/**
1195129044dSC.J. Collier * Set the global log level.
1205129044dSC.J. Collier *
1215129044dSC.J. Collier * After this call, all logs that are lower or equal than level and
1225129044dSC.J. Collier * lower or equal than the RTE_LOG_LEVEL configuration option will be
1235129044dSC.J. Collier * displayed.
1245129044dSC.J. Collier *
1255129044dSC.J. Collier * @param level
1265129044dSC.J. Collier *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
1275129044dSC.J. Collier */
1285129044dSC.J. Colliervoid rte_set_log_level(uint32_t level);
1295129044dSC.J. Collier
1305129044dSC.J. Collier/**
1315129044dSC.J. Collier * Get the global log level.
1325129044dSC.J. Collier */
1335129044dSC.J. Collieruint32_t rte_get_log_level(void);
1345129044dSC.J. Collier
1355129044dSC.J. Collier/**
1365129044dSC.J. Collier * Enable or disable the log type.
1375129044dSC.J. Collier *
1385129044dSC.J. Collier * @param type
1395129044dSC.J. Collier *   Log type, for example, RTE_LOGTYPE_EAL.
1405129044dSC.J. Collier * @param enable
1415129044dSC.J. Collier *   True for enable; false for disable.
1425129044dSC.J. Collier */
1435129044dSC.J. Colliervoid rte_set_log_type(uint32_t type, int enable);
1445129044dSC.J. Collier
1455129044dSC.J. Collier/**
1465129044dSC.J. Collier * Get the global log type.
1475129044dSC.J. Collier */
1485129044dSC.J. Collieruint32_t rte_get_log_type(void);
1495129044dSC.J. Collier
1505129044dSC.J. Collier/**
1515129044dSC.J. Collier * Get the current loglevel for the message being processed.
1525129044dSC.J. Collier *
1535129044dSC.J. Collier * Before calling the user-defined stream for logging, the log
1545129044dSC.J. Collier * subsystem sets a per-lcore variable containing the loglevel and the
1555129044dSC.J. Collier * logtype of the message being processed. This information can be
1565129044dSC.J. Collier * accessed by the user-defined log output function through this
1575129044dSC.J. Collier * function.
1585129044dSC.J. Collier *
1595129044dSC.J. Collier * @return
1605129044dSC.J. Collier *   The loglevel of the message being processed.
1615129044dSC.J. Collier */
1625129044dSC.J. Collierint rte_log_cur_msg_loglevel(void);
1635129044dSC.J. Collier
1645129044dSC.J. Collier/**
1655129044dSC.J. Collier * Get the current logtype for the message being processed.
1665129044dSC.J. Collier *
1675129044dSC.J. Collier * Before calling the user-defined stream for logging, the log
1685129044dSC.J. Collier * subsystem sets a per-lcore variable containing the loglevel and the
1695129044dSC.J. Collier * logtype of the message being processed. This information can be
1705129044dSC.J. Collier * accessed by the user-defined log output function through this
1715129044dSC.J. Collier * function.
1725129044dSC.J. Collier *
1735129044dSC.J. Collier * @return
1745129044dSC.J. Collier *   The logtype of the message being processed.
1755129044dSC.J. Collier */
1765129044dSC.J. Collierint rte_log_cur_msg_logtype(void);
1775129044dSC.J. Collier
1785129044dSC.J. Collier/**
1795129044dSC.J. Collier * Generates a log message.
1805129044dSC.J. Collier *
1815129044dSC.J. Collier * The message will be sent in the stream defined by the previous call
1825129044dSC.J. Collier * to rte_openlog_stream().
1835129044dSC.J. Collier *
1845129044dSC.J. Collier * The level argument determines if the log should be displayed or
1855129044dSC.J. Collier * not, depending on the global rte_logs variable.
1865129044dSC.J. Collier *
1875129044dSC.J. Collier * The preferred alternative is the RTE_LOG() function because debug logs may
1885129044dSC.J. Collier * be removed at compilation time if optimization is enabled. Moreover,
1895129044dSC.J. Collier * logs are automatically prefixed by type when using the macro.
1905129044dSC.J. Collier *
1915129044dSC.J. Collier * @param level
1925129044dSC.J. Collier *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
1935129044dSC.J. Collier * @param logtype
1945129044dSC.J. Collier *   The log type, for example, RTE_LOGTYPE_EAL.
1955129044dSC.J. Collier * @param format
1965129044dSC.J. Collier *   The format string, as in printf(3), followed by the variable arguments
1975129044dSC.J. Collier *   required by the format.
1985129044dSC.J. Collier * @return
1995129044dSC.J. Collier *   - 0: Success.
2005129044dSC.J. Collier *   - Negative on error.
2015129044dSC.J. Collier */
2025129044dSC.J. Collierint rte_log(uint32_t level, uint32_t logtype, const char *format, ...)
2035129044dSC.J. Collier#ifdef __GNUC__
2045129044dSC.J. Collier#if (__GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2))
2055129044dSC.J. Collier	__attribute__((cold))
2065129044dSC.J. Collier#endif
2075129044dSC.J. Collier#endif
2085129044dSC.J. Collier	__attribute__((format(printf, 3, 4)));
2095129044dSC.J. Collier
2105129044dSC.J. Collier/**
2115129044dSC.J. Collier * Generates a log message.
2125129044dSC.J. Collier *
2135129044dSC.J. Collier * The message will be sent in the stream defined by the previous call
2145129044dSC.J. Collier * to rte_openlog_stream().
2155129044dSC.J. Collier *
2165129044dSC.J. Collier * The level argument determines if the log should be displayed or
2175129044dSC.J. Collier * not, depending on the global rte_logs variable. A trailing
2185129044dSC.J. Collier * newline may be added if needed.
2195129044dSC.J. Collier *
2205129044dSC.J. Collier * The preferred alternative is the RTE_LOG() because debug logs may be
2215129044dSC.J. Collier * removed at compilation time.
2225129044dSC.J. Collier *
2235129044dSC.J. Collier * @param level
2245129044dSC.J. Collier *   Log level. A value between RTE_LOG_EMERG (1) and RTE_LOG_DEBUG (8).
2255129044dSC.J. Collier * @param logtype
2265129044dSC.J. Collier *   The log type, for example, RTE_LOGTYPE_EAL.
2275129044dSC.J. Collier * @param format
2285129044dSC.J. Collier *   The format string, as in printf(3), followed by the variable arguments
2295129044dSC.J. Collier *   required by the format.
2305129044dSC.J. Collier * @param ap
2315129044dSC.J. Collier *   The va_list of the variable arguments required by the format.
2325129044dSC.J. Collier * @return
2335129044dSC.J. Collier *   - 0: Success.
2345129044dSC.J. Collier *   - Negative on error.
2355129044dSC.J. Collier */
2365129044dSC.J. Collierint rte_vlog(uint32_t level, uint32_t logtype, const char *format, va_list ap)
2375129044dSC.J. Collier	__attribute__((format(printf,3,0)));
2385129044dSC.J. Collier
2395129044dSC.J. Collier/**
2405129044dSC.J. Collier * Generates a log message.
2415129044dSC.J. Collier *
2425129044dSC.J. Collier * The RTE_LOG() is equivalent to rte_log() with two differences:
2435129044dSC.J. Collier
2445129044dSC.J. Collier * - RTE_LOG() can be used to remove debug logs at compilation time,
2455129044dSC.J. Collier *   depending on RTE_LOG_LEVEL configuration option, and compilation
2465129044dSC.J. Collier *   optimization level. If optimization is enabled, the tests
2475129044dSC.J. Collier *   involving constants only are pre-computed. If compilation is done
2485129044dSC.J. Collier *   with -O0, these tests will be done at run time.
2495129044dSC.J. Collier * - The log level and log type names are smaller, for example:
2505129044dSC.J. Collier *   RTE_LOG(INFO, EAL, "this is a %s", "log");
2515129044dSC.J. Collier *
2525129044dSC.J. Collier * @param l
2535129044dSC.J. Collier *   Log level. A value between EMERG (1) and DEBUG (8). The short name is
2545129044dSC.J. Collier *   expanded by the macro, so it cannot be an integer value.
2555129044dSC.J. Collier * @param t
2565129044dSC.J. Collier *   The log type, for example, EAL. The short name is expanded by the
2575129044dSC.J. Collier *   macro, so it cannot be an integer value.
2585129044dSC.J. Collier * @param ...
2595129044dSC.J. Collier *   The fmt string, as in printf(3), followed by the variable arguments
2605129044dSC.J. Collier *   required by the format.
2615129044dSC.J. Collier * @return
2625129044dSC.J. Collier *   - 0: Success.
2635129044dSC.J. Collier *   - Negative on error.
2645129044dSC.J. Collier */
2655129044dSC.J. Collier#define RTE_LOG(l, t, ...)					\
2665129044dSC.J. Collier	(void)((RTE_LOG_ ## l <= RTE_LOG_LEVEL) ?		\
2675129044dSC.J. Collier	 rte_log(RTE_LOG_ ## l,					\
2685129044dSC.J. Collier		 RTE_LOGTYPE_ ## t, # t ": " __VA_ARGS__) :	\
2695129044dSC.J. Collier	 0)
2705129044dSC.J. Collier
2715129044dSC.J. Collier#ifdef __cplusplus
2725129044dSC.J. Collier}
2735129044dSC.J. Collier#endif
2745129044dSC.J. Collier
2755129044dSC.J. Collier#endif /* _RTE_LOG_H_ */
276