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 | |
Sean Anderson | 0a3b694 | 2020-10-27 19:55:33 -0400 | [diff] [blame^] | 14 | static enum log_level_t parse_log_level(char *const arg) |
| 15 | { |
| 16 | enum log_level_t ret; |
| 17 | ulong level; |
| 18 | |
| 19 | if (!strict_strtoul(arg, 10, &level)) { |
| 20 | if (level > _LOG_MAX_LEVEL) { |
| 21 | printf("Only log levels <= %d are supported\n", |
| 22 | _LOG_MAX_LEVEL); |
| 23 | return LOGL_NONE; |
| 24 | } |
| 25 | return level; |
| 26 | } |
| 27 | |
| 28 | ret = log_get_level_by_name(arg); |
| 29 | if (ret == LOGL_NONE) |
| 30 | printf("Unknown log level \"%s\"\n", arg); |
| 31 | return ret; |
| 32 | } |
| 33 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 34 | static int do_log_level(struct cmd_tbl *cmdtp, int flag, int argc, |
| 35 | char *const argv[]) |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 36 | { |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 37 | if (argc > 1) { |
Sean Anderson | 0a3b694 | 2020-10-27 19:55:33 -0400 | [diff] [blame^] | 38 | enum log_level_t log_level = parse_log_level(argv[1]); |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 39 | |
Sean Anderson | 0a3b694 | 2020-10-27 19:55:33 -0400 | [diff] [blame^] | 40 | if (log_level == LOGL_NONE) |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 41 | return CMD_RET_FAILURE; |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 42 | gd->default_log_level = log_level; |
| 43 | } else { |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 44 | printf("Default log level: %d\n", gd->default_log_level); |
Heinrich Schuchardt | 276bf92 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 45 | } |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 46 | |
Sean Anderson | 0a3b694 | 2020-10-27 19:55:33 -0400 | [diff] [blame^] | 47 | return CMD_RET_SUCCESS; |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 48 | } |
| 49 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 50 | static int do_log_format(struct cmd_tbl *cmdtp, int flag, int argc, |
| 51 | char *const argv[]) |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 52 | { |
| 53 | int i; |
| 54 | |
| 55 | if (argc > 1) { |
| 56 | const char *str = argv[1]; |
| 57 | |
| 58 | if (!strcmp(str, "default")) { |
Heinrich Schuchardt | 2d349c7 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 59 | gd->log_fmt = log_get_default_format(); |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 60 | } else if (!strcmp(str, "all")) { |
| 61 | gd->log_fmt = LOGF_ALL; |
| 62 | } else { |
| 63 | gd->log_fmt = 0; |
| 64 | for (; *str; str++) { |
| 65 | char *ptr = strchr(log_fmt_chars, *str); |
| 66 | |
| 67 | if (!ptr) { |
| 68 | printf("Invalid log char '%c'\n", *str); |
| 69 | return CMD_RET_FAILURE; |
| 70 | } |
| 71 | gd->log_fmt |= 1 << (ptr - log_fmt_chars); |
| 72 | } |
| 73 | } |
| 74 | } else { |
| 75 | printf("Log format: "); |
| 76 | for (i = 0; i < LOGF_COUNT; i++) { |
| 77 | if (gd->log_fmt & (1 << i)) |
| 78 | printf("%c", log_fmt_chars[i]); |
| 79 | } |
| 80 | printf("\n"); |
| 81 | } |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Simon Glass | ed38aef | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 86 | static int do_log_rec(struct cmd_tbl *cmdtp, int flag, int argc, |
| 87 | char *const argv[]) |
Simon Glass | df12ab5 | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 88 | { |
| 89 | enum log_category_t cat; |
| 90 | enum log_level_t level; |
| 91 | const char *file; |
| 92 | uint line; |
| 93 | const char *func; |
| 94 | const char *msg; |
| 95 | char *end; |
| 96 | |
| 97 | if (argc < 7) |
| 98 | return CMD_RET_USAGE; |
| 99 | cat = log_get_cat_by_name(argv[1]); |
| 100 | level = simple_strtoul(argv[2], &end, 10); |
| 101 | if (end == argv[2]) { |
| 102 | level = log_get_level_by_name(argv[2]); |
| 103 | |
| 104 | if (level == LOGL_NONE) { |
| 105 | printf("Invalid log level '%s'\n", argv[2]); |
| 106 | return CMD_RET_USAGE; |
| 107 | } |
| 108 | } |
| 109 | if (level >= LOGL_MAX) { |
| 110 | printf("Invalid log level %u\n", level); |
| 111 | return CMD_RET_USAGE; |
| 112 | } |
| 113 | file = argv[3]; |
| 114 | line = simple_strtoul(argv[4], NULL, 10); |
| 115 | func = argv[5]; |
| 116 | msg = argv[6]; |
| 117 | if (_log(cat, level, file, line, func, "%s\n", msg)) |
| 118 | return CMD_RET_FAILURE; |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 123 | #ifdef CONFIG_SYS_LONGHELP |
| 124 | static char log_help_text[] = |
| 125 | "level - get/set log level\n" |
Simon Glass | ea0049b | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 126 | "log format <fmt> - set log output format. <fmt> is a string where\n" |
| 127 | "\teach letter indicates something that should be displayed:\n" |
| 128 | "\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] | 129 | "\tor 'default', or 'all' for all\n" |
Simon Glass | df12ab5 | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 130 | "log rec <category> <level> <file> <line> <func> <message> - " |
| 131 | "output a log record" |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 132 | ; |
| 133 | #endif |
| 134 | |
Sean Anderson | dcabb4b | 2020-10-27 19:55:32 -0400 | [diff] [blame] | 135 | U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text, |
| 136 | U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level), |
| 137 | U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format), |
| 138 | U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec), |
Simon Glass | 5684f8f | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 139 | ); |