Skip to content

Commit b5ff0a6

Browse files
committed
sys/log: Add number of entries support in logs
- 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
1 parent 129d67e commit b5ff0a6

File tree

7 files changed

+715
-79
lines changed

7 files changed

+715
-79
lines changed

sys/log/full/include/log/log.h

+92-8
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ typedef int (*lh_append_mbuf_body_func_t)(struct log *log,
9797
typedef int (*lh_walk_func_t)(struct log *,
9898
log_walk_func_t walk_func, struct log_offset *log_offset);
9999
typedef int (*lh_flush_func_t)(struct log *);
100+
typedef uint16_t (*lh_read_entry_len_func_t)(struct log *, const void *dptr);
100101
#if MYNEWT_VAL(LOG_STORAGE_INFO)
101102
typedef int (*lh_storage_info_func_t)(struct log *, struct log_storage_info *);
102103
#endif
@@ -116,6 +117,7 @@ struct log_handler {
116117
lh_walk_func_t log_walk;
117118
lh_walk_func_t log_walk_sector;
118119
lh_flush_func_t log_flush;
120+
lh_read_entry_len_func_t log_read_entry_len;
119121
#if MYNEWT_VAL(LOG_STORAGE_INFO)
120122
lh_storage_info_func_t log_storage_info;
121123
#endif
@@ -129,25 +131,40 @@ struct log_handler {
129131
/* Image hash length to be looged */
130132
#define LOG_IMG_HASHLEN 4
131133

132-
/* Flags used to indicate type of data in reserved payload*/
133-
#define LOG_FLAGS_IMG_HASH (1 << 0)
134+
/* Flags used to indicate type of data in reserved payload */
135+
#define LOG_FLAGS_IMG_HASH (1 << 0)
136+
#define LOG_FLAGS_TLV_SUPPORT (1 << 1)
137+
138+
#define LOG_TLV_NUM_ENTRIES (1 << 0)
134139

135140
#if MYNEWT_VAL(LOG_VERSION) == 3
136141
struct log_entry_hdr {
137142
int64_t ue_ts;
138143
uint32_t ue_index;
139144
uint8_t ue_module;
140145
uint8_t ue_level;
141-
uint8_t ue_etype:4;
142-
uint8_t ue_flags:4;
146+
uint8_t ue_etype : 4;
147+
uint8_t ue_flags : 4;
143148
uint8_t ue_imghash[4];
144-
}__attribute__((__packed__));
149+
uint32_t ue_num_entries;
150+
} __attribute__((__packed__));
151+
152+
struct log_tlv {
153+
uint8_t tag;
154+
uint8_t len;
155+
/* Value is of variable size appended based on len,
156+
* val is logged after the tag and len are logged
157+
*/
158+
} __attribute__((__packed__));
159+
145160
#else
146161
#error "Unsupported log version"
147162
#endif
148163

149164
#define LOG_BASE_ENTRY_HDR_SIZE (15)
150165

166+
#define LOG_NUM_ENTRIES_SIZE (sizeof(((struct log *)0)->l_num_entries))
167+
151168
#define LOG_MODULE_STR(module) log_module_get_name(module)
152169

153170
#if MYNEWT_VAL(LOG_LEVEL) <= LOG_LEVEL_DEBUG
@@ -213,6 +230,7 @@ struct log {
213230
#if !MYNEWT_VAL(LOG_GLOBAL_IDX)
214231
uint32_t l_idx;
215232
#endif
233+
uint32_t l_num_entries;
216234
#if MYNEWT_VAL(LOG_STATS)
217235
STATS_SECT_DECL(logs) l_stats;
218236
#endif
@@ -511,6 +529,14 @@ void log_printf(struct log *log, uint8_t module, uint8_t level,
511529
int log_read(struct log *log, const void *dptr, void *buf, uint16_t off,
512530
uint16_t len);
513531

532+
/**
533+
* Reads entry length from the specified log.
534+
*
535+
* @return The number of bytes of entry length; 0 on failure.
536+
*/
537+
uint16_t
538+
log_read_entry_len(struct log *log, const void *dptr);
539+
514540
/**
515541
* @brief Reads a single log entry header.
516542
*
@@ -524,15 +550,37 @@ int log_read(struct log *log, const void *dptr, void *buf, uint16_t off,
524550
*/
525551
int log_read_hdr(struct log *log, const void *dptr, struct log_entry_hdr *hdr);
526552

553+
/**
554+
* @brief Reads a single log entry trailer.
555+
*
556+
* @param log The log to read from.
557+
* @param dptr Medium-specific data describing the area to
558+
* read from; typically obtained by a call to
559+
* `log_walk`.
560+
* @param tlv tlv type
561+
* @param buf Value buffer
562+
*
563+
* @return 0 on success; nonzero on failure.
564+
*/
565+
int log_read_trailer(struct log *log, const void *dptr, uint16_t tlv, void *buf);
566+
527567
/**
528568
* @brief Reads the header length
529569
*
530570
* @param hdr Ptr to the header
531-
*
571+
*
532572
* @return Length of the header
533573
*/
534-
uint16_t
535-
log_hdr_len(const struct log_entry_hdr *hdr);
574+
uint16_t log_hdr_len(const struct log_entry_hdr *hdr);
575+
576+
/**
577+
* @brief Reads the trailer length
578+
*
579+
* @param hdr Ptr to the header
580+
*
581+
* @return Length of the trailer
582+
*/
583+
uint16_t log_trailer_len(const struct log_entry_hdr *hdr);
536584

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

776+
/**
777+
* Fill number of entries
778+
*
779+
* @param log Ptr to log structure
780+
* @param dptr Ptr to data to be read
781+
* @param hdr Ptr to the header
782+
* @param offset Offset of the num of entries in the log entry
783+
*
784+
* @return 0 on success, non-zero on failure
785+
*/
786+
int log_fill_num_entries(struct log *log, const void *dptr,
787+
struct log_entry_hdr *hdr, uint16_t offset);
788+
728789
/**
729790
* Fill log current image hash
730791
*
@@ -735,6 +796,29 @@ int log_set_watermark(struct log *log, uint32_t index);
735796
int
736797
log_fill_current_img_hash(struct log_entry_hdr *hdr);
737798

799+
/**
800+
* Reads the log entry's header from the specified log and log index
801+
*
802+
* @param log The log to read from.
803+
* @param idx Index of the log entry to read header from
804+
* @param out_hdr On success, the last entry header gets written
805+
* here.
806+
*
807+
* @return 0 on success; nonzero on failure.
808+
*/
809+
int
810+
log_read_hdr_by_idx(struct log *log, uint32_t idx, struct log_entry_hdr *out_hdr);
811+
812+
/**
813+
* Get number of entries in log
814+
*
815+
* @param log The log to get number of entries for
816+
* @param idx The log index to read number of entries from
817+
* @param num_entries Ptr to fill up number of entries in log
818+
*/
819+
int
820+
log_get_entries(struct log *log, uint32_t idx, uint32_t *entries);
821+
738822
/* Handler exports */
739823
#if MYNEWT_VAL(LOG_CONSOLE)
740824
extern const struct log_handler log_console_handler;

0 commit comments

Comments
 (0)