Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2017 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <command.h> |
| 9 | #include <dm.h> |
| 10 | #include <log.h> |
| 11 | |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 12 | static char log_fmt_chars[LOGF_COUNT] = "clFLfm"; |
| 13 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 14 | static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc, |
| 15 | char *const argv[]) |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 16 | { |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 17 | if (argc > 1) { |
| 18 | long log_level = simple_strtol(argv[1], NULL, 10); |
| 19 | |
| 20 | if (log_level < 0 || log_level > _LOG_MAX_LEVEL) { |
| 21 | printf("Only log levels <= %d are supported\n", |
| 22 | _LOG_MAX_LEVEL); |
| 23 | return CMD_RET_FAILURE; |
| 24 | } |
| 25 | gd->default_log_level = log_level; |
| 26 | } else { |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 27 | printf("Default log level: %d\n", gd->default_log_level); |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 28 | } |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 33 | static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc, |
| 34 | char *const argv[]) |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 35 | { |
| 36 | int i; |
| 37 | |
| 38 | if (argc > 1) { |
| 39 | const char *str = argv[1]; |
| 40 | |
| 41 | if (!strcmp(str, "default")) { |
Heinrich Schuchardt | 2d349c7 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 42 | gd->log_fmt = log_get_default_format(); |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 43 | } else if (!strcmp(str, "all")) { |
| 44 | gd->log_fmt = LOGF_ALL; |
| 45 | } else { |
| 46 | gd->log_fmt = 0; |
| 47 | for (; *str; str++) { |
| 48 | char *ptr = strchr(log_fmt_chars, *str); |
| 49 | |
| 50 | if (!ptr) { |
| 51 | printf("Invalid log char '%c'\n", *str); |
| 52 | return CMD_RET_FAILURE; |
| 53 | } |
| 54 | gd->log_fmt |= 1 << (ptr - log_fmt_chars); |
| 55 | } |
| 56 | } |
| 57 | } else { |
| 58 | printf("Log format: "); |
| 59 | for (i = 0; i < LOGF_COUNT; i++) { |
| 60 | if (gd->log_fmt & (1 << i)) |
| 61 | printf("%c", log_fmt_chars[i]); |
| 62 | } |
| 63 | printf("\n"); |
| 64 | } |
| 65 | |
| 66 | return 0; |
| 67 | } |
| 68 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 69 | static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc, |
| 70 | char *const argv[]) |
Simon Glass | df12ab5 | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 71 | { |
| 72 | enum log_category_t cat; |
| 73 | enum log_level_t level; |
| 74 | const char *file; |
| 75 | uint line; |
| 76 | const char *func; |
| 77 | const char *msg; |
| 78 | char *end; |
| 79 | |
| 80 | if (argc < 7) |
| 81 | return CMD_RET_USAGE; |
| 82 | cat = log_get_cat_by_name(argv[1]); |
| 83 | level = simple_strtoul(argv[2], &end, 10); |
| 84 | if (end == argv[2]) { |
| 85 | level = log_get_level_by_name(argv[2]); |
| 86 | |
| 87 | if (level == LOGL_NONE) { |
| 88 | printf("Invalid log level '%s'\n", argv[2]); |
| 89 | return CMD_RET_USAGE; |
| 90 | } |
| 91 | } |
| 92 | if (level >= LOGL_MAX) { |
| 93 | printf("Invalid log level %u\n", level); |
| 94 | return CMD_RET_USAGE; |
| 95 | } |
| 96 | file = argv[3]; |
| 97 | line = simple_strtoul(argv[4], NULL, 10); |
| 98 | func = argv[5]; |
| 99 | msg = argv[6]; |
| 100 | if (_log(cat, level, file, line, func, "%s\n", msg)) |
| 101 | return CMD_RET_FAILURE; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 106 | static struct cmd_tbl log_sub[] = { |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 107 | U_BOOT_CMD_MKENT(level, CONFIG_SYS_MAXARGS, 1, do_log_level, "", ""), |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 108 | #ifdef CONFIG_LOG_TEST |
| 109 | U_BOOT_CMD_MKENT(test, 2, 1, do_log_test, "", ""), |
| 110 | #endif |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 111 | U_BOOT_CMD_MKENT(format, CONFIG_SYS_MAXARGS, 1, do_log_format, "", ""), |
Simon Glass | df12ab5 | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 112 | U_BOOT_CMD_MKENT(rec, CONFIG_SYS_MAXARGS, 1, do_log_rec, "", ""), |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 113 | }; |
| 114 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 115 | static int do_log(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 116 | { |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 117 | struct cmd_tbl *cp; |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 118 | |
| 119 | if (argc < 2) |
| 120 | return CMD_RET_USAGE; |
| 121 | |
| 122 | /* drop initial "log" arg */ |
| 123 | argc--; |
| 124 | argv++; |
| 125 | |
| 126 | cp = find_cmd_tbl(argv[0], log_sub, ARRAY_SIZE(log_sub)); |
| 127 | if (cp) |
| 128 | return cp->cmd(cmdtp, flag, argc, argv); |
| 129 | |
| 130 | return CMD_RET_USAGE; |
| 131 | } |
| 132 | |
| 133 | #ifdef CONFIG_SYS_LONGHELP |
| 134 | static char log_help_text[] = |
| 135 | "level - get/set log level\n" |
Simon Glass | 14c8a06 | 2017-12-04 13:48:27 -0700 | [diff] [blame] | 136 | #ifdef CONFIG_LOG_TEST |
| 137 | "log test - run log tests\n" |
| 138 | #endif |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 139 | "log format <fmt> - set log output format. <fmt> is a string where\n" |
| 140 | "\teach letter indicates something that should be displayed:\n" |
| 141 | "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n" |
Heinrich Schuchardt | 2d349c7 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 142 | "\tor 'default', or 'all' for all\n" |
Simon Glass | df12ab5 | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 143 | "log rec <category> <level> <file> <line> <func> <message> - " |
| 144 | "output a log record" |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 145 | ; |
| 146 | #endif |
| 147 | |
| 148 | U_BOOT_CMD( |
| 149 | log, CONFIG_SYS_MAXARGS, 1, do_log, |
| 150 | "log system", log_help_text |
| 151 | ); |