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