Skip to content

Commit

Permalink
sys/log: Add option to limit number of printed logs
Browse files Browse the repository at this point in the history
This adds -n option that can be used to specify number of
logs to print with log command.

Usage:
log -n <number>

i.e.:
log -n 10

will print at most 10 entries from each log

Signed-off-by: Jerzy Kasenberg <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer committed Sep 6, 2024
1 parent 97a90d5 commit 23b6eeb
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions sys/log/full/src/log_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,26 @@

static uint32_t shell_log_count;


struct walk_arg {
uint32_t count_limit;
uint32_t count;
};

static int
shell_log_count_entry(struct log *log, struct log_offset *log_offset,
const struct log_entry_hdr *ueh, const void *dptr, uint16_t len)
{
struct walk_arg *arg = (struct walk_arg *)log_offset->lo_arg;

shell_log_count++;
if (arg) {
arg->count++;
if (arg->count >= arg->count_limit) {
return 1;
}
}

return 0;
}

Expand All @@ -59,6 +74,7 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset,
struct CborParser cbor_parser;
struct CborValue cbor_value;
struct log_cbor_reader cbor_reader;
struct walk_arg *arg = (struct walk_arg *)log_offset->lo_arg;
char tmp[32 + 1];
int off;
int blksz;
Expand Down Expand Up @@ -108,6 +124,12 @@ shell_log_dump_entry(struct log *log, struct log_offset *log_offset,
}

console_write("\n", 1);
if (arg) {
arg->count++;
if (arg->count >= arg->count_limit) {
return 1;
}
}
return 0;
}

Expand All @@ -124,6 +146,7 @@ shell_log_dump_cmd(int argc, char **argv)
bool partial_match = false;
bool clear_log;
bool dump_logs = true;
struct walk_arg arg = {};
int i;
int rc;

Expand All @@ -133,6 +156,17 @@ shell_log_dump_cmd(int argc, char **argv)
list_only = true;
break;
}
if (0 == strcmp(argv[i], "-n")) {
if (i + 1 < argc) {
arg.count_limit = parse_ll_bounds(argv[i + 1], 1, 1000000, &rc);
if (rc) {
arg.count_limit = 1;
}
log_offset.lo_arg = &arg;
}
++i;
continue;
}
if (0 == strcmp(argv[i], "-t")) {
dump_logs = false;
continue;
Expand Down Expand Up @@ -241,8 +275,10 @@ shell_log_storage_cmd(int argc, char **argv)
if (log_storage_info(log, &info)) {
console_printf("Storage info not supported for %s\n", log->l_name);
} else {
console_printf("%s: %d of %d used\n", log->l_name,
(unsigned)info.used, (unsigned)info.size);
uint32_t entry_count = 0;
log_get_entry_count(log, &entry_count);
console_printf("%s: %d of %d used; %d entries\n", log->l_name,
(unsigned)info.used, (unsigned)info.size, (int)entry_count);
#if MYNEWT_VAL(LOG_STORAGE_WATERMARK)
console_printf("%s: %d of %d used by unread entries\n", log->l_name,
(unsigned)info.used_unread, (unsigned)info.size);
Expand Down

0 comments on commit 23b6eeb

Please sign in to comment.