Skip to content

Commit

Permalink
sys/log: Add number of entries support in logs
Browse files Browse the repository at this point in the history
- Add number of entries support in logs
- Add TLV for number of entries support
- Also update log_shell and add syscfgs to support the same.
- Added support in fcb2 and cbmem for number of entries
- This makes it such that log entries are backwards and forwards
  compatible.
- Number of entries can be read using the console or newtmgr
- This is an optional feature enabled only if
  LOG_FLAGS_TLV_SUPPORT: 1 and LOG_TLV_NUM_ENTRIES: 1
  • Loading branch information
vrahane committed Jun 3, 2024
1 parent 129d67e commit b5ff0a6
Show file tree
Hide file tree
Showing 7 changed files with 715 additions and 79 deletions.
100 changes: 92 additions & 8 deletions sys/log/full/include/log/log.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ typedef int (*lh_append_mbuf_body_func_t)(struct log *log,
typedef int (*lh_walk_func_t)(struct log *,
log_walk_func_t walk_func, struct log_offset *log_offset);
typedef int (*lh_flush_func_t)(struct log *);
typedef uint16_t (*lh_read_entry_len_func_t)(struct log *, const void *dptr);
#if MYNEWT_VAL(LOG_STORAGE_INFO)
typedef int (*lh_storage_info_func_t)(struct log *, struct log_storage_info *);
#endif
Expand All @@ -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_read_entry_len_func_t log_read_entry_len;
#if MYNEWT_VAL(LOG_STORAGE_INFO)
lh_storage_info_func_t log_storage_info;
#endif
Expand All @@ -129,25 +131,40 @@ struct log_handler {
/* Image hash length to be looged */
#define LOG_IMG_HASHLEN 4

/* Flags used to indicate type of data in reserved payload*/
#define LOG_FLAGS_IMG_HASH (1 << 0)
/* Flags used to indicate type of data in reserved payload */
#define LOG_FLAGS_IMG_HASH (1 << 0)
#define LOG_FLAGS_TLV_SUPPORT (1 << 1)

#define LOG_TLV_NUM_ENTRIES (1 << 0)

#if MYNEWT_VAL(LOG_VERSION) == 3
struct log_entry_hdr {
int64_t ue_ts;
uint32_t ue_index;
uint8_t ue_module;
uint8_t ue_level;
uint8_t ue_etype:4;
uint8_t ue_flags:4;
uint8_t ue_etype : 4;
uint8_t ue_flags : 4;
uint8_t ue_imghash[4];
}__attribute__((__packed__));
uint32_t ue_num_entries;
} __attribute__((__packed__));

struct log_tlv {
uint8_t tag;
uint8_t len;
/* Value is of variable size appended based on len,
* val is logged after the tag and len are logged
*/
} __attribute__((__packed__));

#else
#error "Unsupported log version"
#endif

#define LOG_BASE_ENTRY_HDR_SIZE (15)

#define LOG_NUM_ENTRIES_SIZE (sizeof(((struct log *)0)->l_num_entries))

#define LOG_MODULE_STR(module) log_module_get_name(module)

#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_DEBUG
Expand Down Expand Up @@ -213,6 +230,7 @@ struct log {
#if !MYNEWT_VAL(LOG_GLOBAL_IDX)
uint32_t l_idx;
#endif
uint32_t l_num_entries;
#if MYNEWT_VAL(LOG_STATS)
STATS_SECT_DECL(logs) l_stats;
#endif
Expand Down Expand Up @@ -511,6 +529,14 @@ void log_printf(struct log *log, uint8_t module, uint8_t level,
int log_read(struct log *log, const void *dptr, void *buf, uint16_t off,
uint16_t len);

/**
* Reads entry length from the specified log.
*
* @return The number of bytes of entry length; 0 on failure.
*/
uint16_t
log_read_entry_len(struct log *log, const void *dptr);

/**
* @brief Reads a single log entry header.
*
Expand All @@ -524,15 +550,37 @@ int log_read(struct log *log, const void *dptr, void *buf, uint16_t off,
*/
int log_read_hdr(struct log *log, const void *dptr, struct log_entry_hdr *hdr);

/**
* @brief Reads a single log entry trailer.
*
* @param log The log to read from.
* @param dptr Medium-specific data describing the area to
* read from; typically obtained by a call to
* `log_walk`.
* @param tlv tlv type
* @param buf Value buffer
*
* @return 0 on success; nonzero on failure.
*/
int log_read_trailer(struct log *log, const void *dptr, uint16_t tlv, void *buf);

/**
* @brief Reads the header length
*
* @param hdr Ptr to the header
*
*
* @return Length of the header
*/
uint16_t
log_hdr_len(const struct log_entry_hdr *hdr);
uint16_t log_hdr_len(const struct log_entry_hdr *hdr);

/**
* @brief Reads the trailer length
*
* @param hdr Ptr to the header
*
* @return Length of the trailer
*/
uint16_t log_trailer_len(const struct log_entry_hdr *hdr);

/**
* @brief Reads data from the body of a log entry into a flat buffer.
Expand Down Expand Up @@ -725,6 +773,19 @@ log_set_rotate_notify_cb(struct log *log, log_notify_rotate_cb *cb);
int log_set_watermark(struct log *log, uint32_t index);
#endif

/**
* Fill number of entries
*
* @param log Ptr to log structure
* @param dptr Ptr to data to be read
* @param hdr Ptr to the header
* @param offset Offset of the num of entries in the log entry
*
* @return 0 on success, non-zero on failure
*/
int log_fill_num_entries(struct log *log, const void *dptr,
struct log_entry_hdr *hdr, uint16_t offset);

/**
* Fill log current image hash
*
Expand All @@ -735,6 +796,29 @@ int log_set_watermark(struct log *log, uint32_t index);
int
log_fill_current_img_hash(struct log_entry_hdr *hdr);

/**
* Reads the log entry's header from the specified log and log index
*
* @param log The log to read from.
* @param idx Index of the log entry to read header from
* @param out_hdr On success, the last entry header gets written
* here.
*
* @return 0 on success; nonzero on failure.
*/
int
log_read_hdr_by_idx(struct log *log, uint32_t idx, struct log_entry_hdr *out_hdr);

/**
* Get number of entries in log
*
* @param log The log to get number of entries for
* @param idx The log index to read number of entries from
* @param num_entries Ptr to fill up number of entries in log
*/
int
log_get_entries(struct log *log, uint32_t idx, uint32_t *entries);

/* Handler exports */
#if MYNEWT_VAL(LOG_CONSOLE)
extern const struct log_handler log_console_handler;
Expand Down
Loading

0 comments on commit b5ff0a6

Please sign in to comment.