Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d5f61f2 | 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 | d5f61f2 | 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 | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 12 | static char log_fmt_chars[LOGF_COUNT] = "clFLfm"; |
| 13 | |
Simon Glass | 0914011 | 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 | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 16 | { |
Heinrich Schuchardt | 77007f9 | 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 | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 27 | printf("Default log level: %d\n", gd->default_log_level); |
Heinrich Schuchardt | 77007f9 | 2020-05-31 13:44:51 +0000 | [diff] [blame] | 28 | } |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 29 | |
| 30 | return 0; |
| 31 | } |
| 32 | |
Simon Glass | 0914011 | 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 | ad0e503 | 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 | 3c21d77 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 42 | gd->log_fmt = log_get_default_format(); |
Simon Glass | ad0e503 | 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 | 0914011 | 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 | 3fd24fa | 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 | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 106 | #ifdef CONFIG_SYS_LONGHELP |
| 107 | static char log_help_text[] = |
| 108 | "level - get/set log level\n" |
Simon Glass | ad0e503 | 2017-12-28 13:14:19 -0700 | [diff] [blame] | 109 | "log format <fmt> - set log output format. <fmt> is a string where\n" |
| 110 | "\teach letter indicates something that should be displayed:\n" |
| 111 | "\tc=category, l=level, F=file, L=line number, f=function, m=msg\n" |
Heinrich Schuchardt | 3c21d77 | 2020-06-17 21:52:44 +0200 | [diff] [blame] | 112 | "\tor 'default', or 'all' for all\n" |
Simon Glass | 3fd24fa | 2017-12-28 13:14:20 -0700 | [diff] [blame] | 113 | "log rec <category> <level> <file> <line> <func> <message> - " |
| 114 | "output a log record" |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 115 | ; |
| 116 | #endif |
| 117 | |
Sean Anderson | f48b5b5 | 2020-10-27 19:55:32 -0400 | [diff] [blame^] | 118 | U_BOOT_CMD_WITH_SUBCMDS(log, "log system", log_help_text, |
| 119 | U_BOOT_SUBCMD_MKENT(level, 2, 1, do_log_level), |
| 120 | U_BOOT_SUBCMD_MKENT(format, 2, 1, do_log_format), |
| 121 | U_BOOT_SUBCMD_MKENT(rec, 7, 1, do_log_rec), |
Simon Glass | d5f61f2 | 2017-12-04 13:48:26 -0700 | [diff] [blame] | 122 | ); |