xref: /DragonOS/user/apps/dmesg/dmesg.c (revision 196b75dc17b5cc2ed84301bce776e496ddfe1ed1)
1 #include "dmesg.h"
2 
3 /**
4  * @brief 识别dmesg程序的第一个选项参数
5  *
6  * @param arg dmesg命令第一个选项参数
7  * @return int 有效时返回对应选项码,无效时返回 -1
8  */
9 int getopt(char *arg)
10 {
11     if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
12         return 0;
13     else if (!strcmp(arg, "-c") || !strcmp(arg, "--read-clear"))
14         return 4;
15     else if (!strcmp(arg, "-C") || !strcmp(arg, "--clear"))
16         return 5;
17     else if (!strcmp(arg, "-l") || !strcmp(arg, "--level"))
18         return 8;
19 
20     return -1;
21 }
22 
23 /**
24  * @brief 识别dmesg程序的第二个选项参数
25  *
26  * @param arg dmesg命令第一个选项参数
27  * @return int 有效时返回设置的日志级别,无效时返回 -1
28  */
29 int getlevel(char *arg)
30 {
31     if (!strcmp(arg, "EMERG") || !strcmp(arg, "emerg"))
32         return 0;
33     else if (!strcmp(arg, "ALERT") || !strcmp(arg, "alert"))
34         return 1;
35     else if (!strcmp(arg, "CRIT") || !strcmp(arg, "crit"))
36         return 2;
37     else if (!strcmp(arg, "ERR") || !strcmp(arg, "err"))
38         return 3;
39     else if (!strcmp(arg, "WARN") || !strcmp(arg, "warn"))
40         return 4;
41     else if (!strcmp(arg, "NOTICE") || !strcmp(arg, "notice"))
42         return 5;
43     else if (!strcmp(arg, "INFO") || !strcmp(arg, "info"))
44         return 6;
45     else if (!strcmp(arg, "DEBUG") || !strcmp(arg, "debug"))
46         return 7;
47     else
48     {
49         printf("dmesg: unknown level '%s'\n", arg);
50     }
51     return -2;
52 }
53 
54 /**
55  * @brief 打印dmesg手册
56  */
57 void print_help_msg()
58 {
59     const char *help_msg = "Usage:\n"
60                            " dmesg [options]\n\n"
61                            "Display or control the kernel ring buffer.\n\n"
62                            "Options:\n"
63                            " -C, --clear                 clear the kernel ring buffer\n"
64                            " -c, --read-clear            read and clear all messages\n"
65                            " -l, --level <list>          restrict output to defined levels\n"
66                            " -h, --help                  display this help\n\n"
67                            "Supported log levels (priorities):\n"
68                            "   emerg - system is unusable\n"
69                            "   alert - action must be taken immediately\n"
70                            "    crit - critical conditions\n"
71                            "     err - error conditions\n"
72                            "    warn - warning conditions\n"
73                            "  notice - normal but significant condition\n"
74                            "    info - informational\n"
75                            "   debug - debug-level messages\n";
76     printf("%s\n", help_msg);
77 }
78 
79 /**
80  * @brief 打印dmesg错误使用的信息
81  */
82 void print_bad_usage_msg()
83 {
84     const char *bad_usage_msg = "dmesg: bad usage\nTry 'dmesg --help' for more information.";
85     printf("%s\n", bad_usage_msg);
86 }