1a9de470cSBruce Richardson /* SPDX-License-Identifier: BSD-3-Clause
2a9de470cSBruce Richardson * Copyright(c) 2010-2014 Intel Corporation
3a9de470cSBruce Richardson */
4a9de470cSBruce Richardson
5a9de470cSBruce Richardson #include <stdio.h>
6a9de470cSBruce Richardson #include <stdint.h>
7a9de470cSBruce Richardson #include <stdarg.h>
8a9de470cSBruce Richardson #include <sys/queue.h>
9a9de470cSBruce Richardson
10a9de470cSBruce Richardson #include <rte_log.h>
11a9de470cSBruce Richardson #include <rte_memory.h>
12a9de470cSBruce Richardson #include <rte_launch.h>
13a9de470cSBruce Richardson #include <rte_eal.h>
14a9de470cSBruce Richardson #include <rte_per_lcore.h>
15a9de470cSBruce Richardson #include <rte_lcore.h>
16a9de470cSBruce Richardson
17a9de470cSBruce Richardson #include "test.h"
18a9de470cSBruce Richardson
19a9de470cSBruce Richardson /* for legacy log test */
20a9de470cSBruce Richardson #define RTE_LOGTYPE_TESTAPP1 RTE_LOGTYPE_USER1
21a9de470cSBruce Richardson #define RTE_LOGTYPE_TESTAPP2 RTE_LOGTYPE_USER2
22a9de470cSBruce Richardson
RTE_LOG_REGISTER(logtype3,logtype3,ERR)2339b57179SDavid Marchand RTE_LOG_REGISTER(logtype3, logtype3, ERR)
2439b57179SDavid Marchand
25a9de470cSBruce Richardson /*
26a9de470cSBruce Richardson * Logs
27a9de470cSBruce Richardson * ====
28a9de470cSBruce Richardson *
29a9de470cSBruce Richardson * - Enable log types.
30a9de470cSBruce Richardson * - Set log level.
31a9de470cSBruce Richardson * - Send logs with different types and levels, some should not be displayed.
32a9de470cSBruce Richardson */
33a9de470cSBruce Richardson static int
34a9de470cSBruce Richardson test_legacy_logs(void)
35a9de470cSBruce Richardson {
36a9de470cSBruce Richardson printf("== static log types\n");
37a9de470cSBruce Richardson
38a9de470cSBruce Richardson /* set logtype level low to so we can test global level */
39a9de470cSBruce Richardson rte_log_set_level(RTE_LOGTYPE_TESTAPP1, RTE_LOG_DEBUG);
40a9de470cSBruce Richardson rte_log_set_level(RTE_LOGTYPE_TESTAPP2, RTE_LOG_DEBUG);
41a9de470cSBruce Richardson
42a9de470cSBruce Richardson /* log in error level */
43a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_ERR);
44a9de470cSBruce Richardson RTE_LOG(ERR, TESTAPP1, "error message\n");
45a9de470cSBruce Richardson RTE_LOG(CRIT, TESTAPP1, "critical message\n");
46a9de470cSBruce Richardson
47a9de470cSBruce Richardson /* log in critical level */
48a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_CRIT);
49a9de470cSBruce Richardson RTE_LOG(ERR, TESTAPP2, "error message (not displayed)\n");
50a9de470cSBruce Richardson RTE_LOG(CRIT, TESTAPP2, "critical message\n");
51a9de470cSBruce Richardson
52a9de470cSBruce Richardson /* bump up single log type level above global to test it */
53a9de470cSBruce Richardson rte_log_set_level(RTE_LOGTYPE_TESTAPP2, RTE_LOG_EMERG);
54a9de470cSBruce Richardson
55a9de470cSBruce Richardson /* log in error level */
56a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_ERR);
57a9de470cSBruce Richardson RTE_LOG(ERR, TESTAPP1, "error message\n");
58a9de470cSBruce Richardson RTE_LOG(ERR, TESTAPP2, "error message (not displayed)\n");
59a9de470cSBruce Richardson
60a9de470cSBruce Richardson return 0;
61a9de470cSBruce Richardson }
62a9de470cSBruce Richardson
63a9de470cSBruce Richardson static int
test_logs(void)64a9de470cSBruce Richardson test_logs(void)
65a9de470cSBruce Richardson {
66a9de470cSBruce Richardson int logtype1, logtype2;
67a9de470cSBruce Richardson int ret;
68a9de470cSBruce Richardson
6939b57179SDavid Marchand #define CHECK_LEVELS(exp1, exp2, exp3) do \
7039b57179SDavid Marchand { \
7139b57179SDavid Marchand ret = rte_log_get_level(logtype1); \
7239b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, exp1, \
7339b57179SDavid Marchand "invalid level for logtype1 got %d, expecting %d\n", \
7439b57179SDavid Marchand ret, exp1); \
7539b57179SDavid Marchand ret = rte_log_get_level(logtype2); \
7639b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, exp2, \
7739b57179SDavid Marchand "invalid level for logtype2 got %d, expecting %d\n", \
7839b57179SDavid Marchand ret, exp2); \
7939b57179SDavid Marchand ret = rte_log_get_level(logtype3); \
8039b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, exp3, \
8139b57179SDavid Marchand "invalid level for logtype3 got %d, expecting %d\n", \
8239b57179SDavid Marchand ret, exp3); \
8339b57179SDavid Marchand } while (0)
8439b57179SDavid Marchand
85a9de470cSBruce Richardson printf("== dynamic log types\n");
86a9de470cSBruce Richardson
87a9de470cSBruce Richardson logtype1 = rte_log_register("logtype1");
88a9de470cSBruce Richardson if (logtype1 < 0) {
89a9de470cSBruce Richardson printf("Cannot register logtype1\n");
90a9de470cSBruce Richardson return -1;
91a9de470cSBruce Richardson }
92a9de470cSBruce Richardson logtype2 = rte_log_register("logtype2");
93a9de470cSBruce Richardson if (logtype2 < 0) {
94a9de470cSBruce Richardson printf("Cannot register logtype2\n");
95a9de470cSBruce Richardson return -1;
96a9de470cSBruce Richardson }
97a9de470cSBruce Richardson
9839b57179SDavid Marchand ret = rte_log_get_level(logtype1);
9939b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, RTE_LOG_INFO,
10039b57179SDavid Marchand "invalid default level for logtype1 got %d, expecting %d\n",
10139b57179SDavid Marchand ret, RTE_LOG_INFO);
10239b57179SDavid Marchand
10339b57179SDavid Marchand ret = rte_log_get_level(logtype2);
10439b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, RTE_LOG_INFO,
10539b57179SDavid Marchand "invalid default level for logtype2 got %d, expecting %d\n",
10639b57179SDavid Marchand ret, RTE_LOG_INFO);
10739b57179SDavid Marchand
10839b57179SDavid Marchand ret = rte_log_get_level(logtype3);
10939b57179SDavid Marchand TEST_ASSERT_EQUAL(ret, RTE_LOG_ERR,
11039b57179SDavid Marchand "invalid default level for logtype3 got %d, expecting %d\n",
11139b57179SDavid Marchand ret, RTE_LOG_ERR);
11239b57179SDavid Marchand
11339b57179SDavid Marchand rte_log_set_level(logtype1, RTE_LOG_ERR);
11439b57179SDavid Marchand CHECK_LEVELS(RTE_LOG_ERR, RTE_LOG_INFO, RTE_LOG_ERR);
11539b57179SDavid Marchand
1161570ab11SJie Zhou #ifndef RTE_EXEC_ENV_WINDOWS
11739b57179SDavid Marchand rte_log_set_level_regexp("type$", RTE_LOG_EMERG);
11839b57179SDavid Marchand CHECK_LEVELS(RTE_LOG_ERR, RTE_LOG_INFO, RTE_LOG_ERR);
11939b57179SDavid Marchand
12039b57179SDavid Marchand rte_log_set_level_regexp("type[23]", RTE_LOG_EMERG);
12139b57179SDavid Marchand CHECK_LEVELS(RTE_LOG_ERR, RTE_LOG_EMERG, RTE_LOG_EMERG);
12239b57179SDavid Marchand
12339b57179SDavid Marchand rte_log_set_level_pattern("logtype", RTE_LOG_DEBUG);
12439b57179SDavid Marchand CHECK_LEVELS(RTE_LOG_ERR, RTE_LOG_EMERG, RTE_LOG_EMERG);
1251570ab11SJie Zhou #else
1261570ab11SJie Zhou rte_log_set_level_pattern("logtype", RTE_LOG_DEBUG);
1271570ab11SJie Zhou CHECK_LEVELS(RTE_LOG_ERR, RTE_LOG_INFO, RTE_LOG_ERR);
1281570ab11SJie Zhou #endif
12939b57179SDavid Marchand
130a9de470cSBruce Richardson /* set logtype level low to so we can test global level */
13139b57179SDavid Marchand rte_log_set_level_pattern("logtype*", RTE_LOG_DEBUG);
13239b57179SDavid Marchand CHECK_LEVELS(RTE_LOG_DEBUG, RTE_LOG_DEBUG, RTE_LOG_DEBUG);
133a9de470cSBruce Richardson
134a9de470cSBruce Richardson /* log in error level */
135a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_ERR);
136a9de470cSBruce Richardson rte_log(RTE_LOG_ERR, logtype1, "error message\n");
137a9de470cSBruce Richardson rte_log(RTE_LOG_CRIT, logtype1, "critical message\n");
138a9de470cSBruce Richardson
139a9de470cSBruce Richardson /* log in critical level */
140a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_CRIT);
141a9de470cSBruce Richardson rte_log(RTE_LOG_ERR, logtype2, "error message (not displayed)\n");
142a9de470cSBruce Richardson rte_log(RTE_LOG_CRIT, logtype2, "critical message\n");
143a9de470cSBruce Richardson
144a9de470cSBruce Richardson /* bump up single log type level above global to test it */
145a9de470cSBruce Richardson rte_log_set_level(logtype2, RTE_LOG_EMERG);
146a9de470cSBruce Richardson
147a9de470cSBruce Richardson /* log in error level */
148a9de470cSBruce Richardson rte_log_set_global_level(RTE_LOG_ERR);
149a9de470cSBruce Richardson rte_log(RTE_LOG_ERR, logtype1, "error message\n");
150a9de470cSBruce Richardson rte_log(RTE_LOG_ERR, logtype2, "error message (not displayed)\n");
151a9de470cSBruce Richardson
152a9de470cSBruce Richardson ret = test_legacy_logs();
153a9de470cSBruce Richardson if (ret < 0)
154a9de470cSBruce Richardson return ret;
155a9de470cSBruce Richardson
15639b57179SDavid Marchand #undef CHECK_LEVELS
15739b57179SDavid Marchand
158a9de470cSBruce Richardson return 0;
159a9de470cSBruce Richardson }
160a9de470cSBruce Richardson
161*e0a8442cSBruce Richardson REGISTER_FAST_TEST(logs_autotest, true, true, test_logs);
162