xref: /nrf52832-nimble/rt-thread/examples/ulog/ulog_example.c (revision 104654410c56c573564690304ae786df310c91fc)
1 /*
2  * Copyright (c) 2006-2018, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2018-08-02     armink       the first version
9  */
10 
11 #include <stdlib.h>
12 #include <rtthread.h>
13 
14 #ifndef ULOG_USING_SYSLOG
15 #define LOG_TAG              "example"
16 #define LOG_LVL              LOG_LVL_DBG
17 #include <ulog.h>
18 #else
19 #include <syslog.h>
20 #endif /* ULOG_USING_SYSLOG */
21 
ulog_example(void)22 void ulog_example(void)
23 {
24     int count = 0;
25 
26 #ifdef ULOG_USING_SYSLOG
27     openlog("example1", 0, 0);
28 #endif
29 
30     while (count++ < 50)
31     {
32 #ifndef ULOG_USING_SYSLOG
33         /* output different level log by LOG_X API */
34         LOG_D("LOG_D(%d): RT-Thread is an open source IoT operating system from China.", count);
35         LOG_I("LOG_I(%d): RT-Thread is an open source IoT operating system from China.", count);
36         LOG_W("LOG_W(%d): RT-Thread is an open source IoT operating system from China.", count);
37         LOG_E("LOG_E(%d): RT-Thread is an open source IoT operating system from China.", count);
38         ulog_d("test", "ulog_d(%d): RT-Thread is an open source IoT operating system from China.", count);
39         ulog_i("test", "ulog_i(%d): RT-Thread is an open source IoT operating system from China.", count);
40         ulog_w("test", "ulog_w(%d): RT-Thread is an open source IoT operating system from China.", count);
41         ulog_e("test", "ulog_e(%d): RT-Thread is an open source IoT operating system from China.", count);
42 
43 #ifdef ULOG_USING_FILTER
44         if (count == 20)
45         {
46             /* Set the global filer level is INFO. All of DEBUG log will stop output */
47             ulog_global_filter_lvl_set(LOG_LVL_INFO);
48             /* Set the test tag's level filter's level is ERROR. The DEBUG, INFO, WARNING log will stop output. */
49             ulog_tag_lvl_filter_set("test", LOG_LVL_ERROR);
50         }
51         else if (count == 30)
52         {
53             /* Set the example tag's level filter's level is LOG_FILTER_LVL_SILENT, the log enter silent mode. */
54             ulog_tag_lvl_filter_set("example", LOG_FILTER_LVL_SILENT);
55             /* Set the test tag's level filter's level is WARNING. The DEBUG, INFO log will stop output. */
56             ulog_tag_lvl_filter_set("test", LOG_LVL_WARNING);
57         }
58         else if (count == 40)
59         {
60             /* Set the test tag's level filter's level is LOG_FILTER_LVL_ALL. All level log will resume output. */
61             ulog_tag_lvl_filter_set("test", LOG_FILTER_LVL_ALL);
62             /* Set the global filer level is LOG_FILTER_LVL_ALL. All level log will resume output */
63             ulog_global_filter_lvl_set(LOG_FILTER_LVL_ALL);
64         }
65 #endif /* ULOG_USING_FILTER */
66 
67 #else
68         /* output different priority log by syslog API */
69         syslog(LOG_INFO, "syslog(%d) LOG_INFO: RT-Thread is an open source IoT operating system from China.", count);
70         syslog(LOG_DEBUG, "syslog(%d) LOG_DEBUG: RT-Thread is an open source IoT operating system from China.", count);
71         syslog(LOG_WARNING, "syslog(%d) LOG_WARNING: RT-Thread is an open source IoT operating system from China.", count);
72         syslog(LOG_ERR, "syslog(%d) LOG_ERR: RT-Thread is an open source IoT operating system from China.", count);
73         syslog(LOG_INFO | LOG_MAIL, "syslog(%d) LOG_INFO | LOG_MAIL: RT-Thread is an open source IoT operating system from China.", count);
74         syslog(LOG_DEBUG | LOG_DAEMON, "syslog(%d) LOG_DEBUG | LOG_DAEMON: RT-Thread is an open source IoT operating system from China.", count);
75         syslog(LOG_WARNING | LOG_AUTH, "syslog(%d) LOG_WARNING | LOG_AUTH: RT-Thread is an open source IoT operating system from China.", count);
76         syslog(LOG_ERR | LOG_SYSLOG, "syslog(%d) LOG_ERR | LOG_SYSLOG: RT-Thread is an open source IoT operating system from China.", count);
77 
78         if (count == 20)
79         {
80             /* Set log priority mask. Only output ERR and WARNING log. */
81             setlogmask(LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING));
82         }
83         else if (count == 40)
84         {
85             /* Set log priority mask. The log which level is less than ERROR will stop output. */
86             setlogmask(LOG_UPTO(LOG_ERR));
87         }
88 #endif /* ULOG_USING_SYSLOG */
89 
90         rt_thread_delay(rt_tick_from_millisecond(rand() % 1000));
91     }
92 }
93 MSH_CMD_EXPORT(ulog_example, run ulog example)
94