From a4ce0da953df619e2ef969bff587671a0074978a Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Wed, 31 Jul 2024 14:54:16 +0200 Subject: [PATCH] sys/log: Add API to get log entry count This adds log_entry_count() function that can be used to retrieve number of entries in log. Signed-off-by: Jerzy Kasenberg --- sys/log/full/include/log/log.h | 12 +++++++++++ sys/log/full/src/log.c | 16 ++++++++++++++ sys/log/full/src/log_fcb.c | 39 +++++++++++++++++++++++----------- 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/sys/log/full/include/log/log.h b/sys/log/full/include/log/log.h index 7e874ceb06..4030e094de 100644 --- a/sys/log/full/include/log/log.h +++ b/sys/log/full/include/log/log.h @@ -96,6 +96,7 @@ typedef int (*lh_append_mbuf_body_func_t)(struct log *log, struct os_mbuf *om); typedef int (*lh_walk_func_t)(struct log *, log_walk_func_t walk_func, struct log_offset *log_offset); +typedef int (*lh_get_entry_count_func_t)(struct log *log, uint32_t *entry_count); typedef int (*lh_flush_func_t)(struct log *); #if MYNEWT_VAL(LOG_STORAGE_INFO) typedef int (*lh_storage_info_func_t)(struct log *, struct log_storage_info *); @@ -116,6 +117,7 @@ struct log_handler { lh_walk_func_t log_walk; lh_walk_func_t log_walk_sector; lh_flush_func_t log_flush; + lh_get_entry_count_func_t log_get_entry_count; #if MYNEWT_VAL(LOG_STORAGE_INFO) lh_storage_info_func_t log_storage_info; #endif @@ -592,6 +594,16 @@ int log_walk_body(struct log *log, log_walk_body_func_t walk_body_func, struct log_offset *log_offset); int log_flush(struct log *log); +/** + * Returns number of entries in log. + * + * @param log - The log to return number of entries from + * @param entry_count - The pointer to variable to store number of entries. + * + * @return 0 on success, error code otherwise + */ +int log_get_entry_count(struct log *log, uint32_t *entry_count); + /** * @brief Walking a section of FCB. * diff --git a/sys/log/full/src/log.c b/sys/log/full/src/log.c index 68c04d6f00..ee9631b7b3 100644 --- a/sys/log/full/src/log.c +++ b/sys/log/full/src/log.c @@ -1014,6 +1014,22 @@ log_flush(struct log *log) return (rc); } +int +log_get_entry_count(struct log *log, uint32_t *entry_count) +{ + int rc; + + if (entry_count == NULL) { + rc = SYS_EINVAL; + } else if (log->l_log->log_get_entry_count == NULL) { + rc = SYS_ENOTSUP; + } else { + rc = log->l_log->log_get_entry_count(log, entry_count); + } + + return rc; +} + #if MYNEWT_VAL(LOG_STORAGE_INFO) int log_storage_info(struct log *log, struct log_storage_info *info) diff --git a/sys/log/full/src/log_fcb.c b/sys/log/full/src/log_fcb.c index 82bd1a415b..cd72a94bd5 100644 --- a/sys/log/full/src/log_fcb.c +++ b/sys/log/full/src/log_fcb.c @@ -655,6 +655,20 @@ log_fcb_flush(struct log *log) return fcb_clear(fcb); } +static int +log_fcb_get_entry_count(struct log *log, uint32_t *entry_count) +{ + struct fcb_log *fcb_log; + struct fcb *fcb; + + fcb_log = (struct fcb_log *)log->l_arg; + fcb = &fcb_log->fl_fcb; + + *entry_count = fcb_get_entry_count(fcb, NULL); + + return 0; +} + static int log_fcb_registered(struct log *log) { @@ -972,23 +986,24 @@ log_fcb_rtr_erase(struct log *log) } const struct log_handler log_fcb_handler = { - .log_type = LOG_TYPE_STORAGE, - .log_read = log_fcb_read, - .log_read_mbuf = log_fcb_read_mbuf, - .log_append = log_fcb_append, - .log_append_body = log_fcb_append_body, - .log_append_mbuf = log_fcb_append_mbuf, + .log_type = LOG_TYPE_STORAGE, + .log_read = log_fcb_read, + .log_read_mbuf = log_fcb_read_mbuf, + .log_append = log_fcb_append, + .log_append_body = log_fcb_append_body, + .log_append_mbuf = log_fcb_append_mbuf, .log_append_mbuf_body = log_fcb_append_mbuf_body, - .log_walk = log_fcb_walk, - .log_walk_sector = log_fcb_walk_area, - .log_flush = log_fcb_flush, + .log_walk = log_fcb_walk, + .log_walk_sector = log_fcb_walk_area, + .log_flush = log_fcb_flush, + .log_get_entry_count = log_fcb_get_entry_count, #if MYNEWT_VAL(LOG_STORAGE_INFO) - .log_storage_info = log_fcb_storage_info, + .log_storage_info = log_fcb_storage_info, #endif #if MYNEWT_VAL(LOG_STORAGE_WATERMARK) - .log_set_watermark = log_fcb_set_watermark, + .log_set_watermark = log_fcb_set_watermark, #endif - .log_registered = log_fcb_registered, + .log_registered = log_fcb_registered, }; #endif