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 Aug 5, 2024
1 parent 4b4fc89 commit 7850aa3
Showing 1 changed file with 43 additions and 5 deletions.
48 changes: 43 additions & 5 deletions sys/log/full/src/log_shell.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,25 @@

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++;
return 0;
if (arg) {
arg->count++;
if (arg->count >= arg->count_limit) {
return 1;
}
} return 0;
}

static int
Expand All @@ -59,6 +72,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 +122,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 +144,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 +154,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 @@ -187,8 +219,12 @@ shell_log_dump_cmd(int argc, char **argv)
console_printf("Dumping log %s\n", log->l_name);
}

log_offset.lo_arg = NULL;
log_offset.lo_ts = 0;
if (reverse) {
log_offset.lo_ts = -1;
log_offset.lo_walk_backward = true;
} else {
log_offset.lo_ts = 0;
}
log_last_index = log_get_last_index(log);
if (log_limit == 0 || log_last_index < log_limit) {
log_offset.lo_index = 0;
Expand Down Expand Up @@ -241,8 +277,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 7850aa3

Please sign in to comment.