Skip to content

Commit

Permalink
zcbor_print.h: Improve zcbor_trace() and other tracing
Browse files Browse the repository at this point in the history
Make printout more terse, and make the most printed instance
(value_extract) not print the whole path.
Pass state parameter to macro.
Add more trace printing to zcbor_decode.c, and to errors.

Signed-off-by: Øyvind Rønningstad <oyvind.ronningstad@nordicsemi.no>
  • Loading branch information
oyvindronningstad committed Dec 19, 2023
1 parent 4abdf4b commit 028dea9
Show file tree
Hide file tree
Showing 8 changed files with 108 additions and 14 deletions.
6 changes: 5 additions & 1 deletion include/zcbor_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,26 +169,30 @@ typedef enum
*/
#define ZCBOR_FAIL() \
do {\
zcbor_trace(); \
zcbor_log("ZCBOR_FAIL "); \
zcbor_trace_file(state); \
return false; \
} while(0)

#define ZCBOR_FAIL_IF(expr) \
do {\
if (expr) { \
zcbor_log("ZCBOR_FAIL_IF(" #expr ") "); \
ZCBOR_FAIL(); \
} \
} while(0)

#define ZCBOR_ERR(err) \
do { \
zcbor_log("ZCBOR_ERR(%d) ", err); \
zcbor_error(state, err); \
ZCBOR_FAIL(); \
} while(0)

#define ZCBOR_ERR_IF(expr, err) \
do {\
if (expr) { \
zcbor_log("ZCBOR_ERR_IF(" #expr ", %d) ", err); \
ZCBOR_ERR(err); \
} \
} while(0)
Expand Down
15 changes: 12 additions & 3 deletions include/zcbor_print.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,17 @@ extern "C" {
#endif

#ifdef ZCBOR_VERBOSE
#define zcbor_trace() (zcbor_do_print("bytes left: %zu, byte: 0x%x, elem_count: 0x%zx, err: %d, %s:%d\n",\
#define zcbor_trace_raw(state) (zcbor_do_print("rem: %zu, cur: 0x%x, ec: 0x%zx, err: %d",\
(size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
state->constant_state ? state->constant_state->error : 0, __FILE__, __LINE__))
state->constant_state ? state->constant_state->error : 0))
#define zcbor_trace(state, appendix) do { \
zcbor_trace_raw(state); \
zcbor_do_print(", %s\n", appendix); \
} while(0)
#define zcbor_trace_file(state) do { \
zcbor_trace_raw(state); \
zcbor_do_print(", %s:%d\n", __FILE__, __LINE__); \
} while(0)

#define zcbor_log_assert(expr, ...) \
do { \
Expand All @@ -33,7 +41,8 @@ do { \
} while(0)
#define zcbor_log(...) zcbor_do_print(__VA_ARGS__)
#else
#define zcbor_trace() ((void)state)
#define zcbor_trace(state, appendix)
#define zcbor_trace_file(state) ((void)state)
#define zcbor_log_assert(...)
#define zcbor_log(...)
#endif
Expand Down
2 changes: 1 addition & 1 deletion samples/pet/src/pet_decode.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ static bool decode_Pet(
|| (((*result).species_choice == Pet_species_other_c) && ((1)))) || (zcbor_error(state, ZCBOR_ERR_WRONG_VALUE), false)))))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state))));

if (!tmp_result)
zcbor_trace();
zcbor_trace_file(state);

return tmp_result;
}
Expand Down
2 changes: 1 addition & 1 deletion samples/pet/src/pet_encode.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static bool encode_Pet(
: false))))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3))));

if (!tmp_result)
zcbor_trace();
zcbor_trace_file(state);

return tmp_result;
}
Expand Down
12 changes: 8 additions & 4 deletions src/zcbor_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ bool zcbor_new_backup(zcbor_state_t *state, size_t new_elem_count)

state->elem_count = new_elem_count;

zcbor_log("New backup (level %zu)\n", i);

return true;
}

Expand All @@ -54,11 +56,13 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags,
ZCBOR_ERR(ZCBOR_ERR_NO_BACKUP_ACTIVE);
}

if (flags & ZCBOR_FLAG_RESTORE) {
/* use the backup at current_backup - 1, since otherwise, the
* 0th backup would be unused. */
size_t i = state->constant_state->current_backup - 1;
/* use the backup at current_backup - 1, since otherwise, the
* 0th backup would be unused. */
size_t i = state->constant_state->current_backup - 1;

zcbor_log("Process backup (level %d, flags 0x%x)\n", i, flags);

if (flags & ZCBOR_FLAG_RESTORE) {
if (!(flags & ZCBOR_FLAG_KEEP_PAYLOAD)) {
if (state->constant_state->backup_list[i].payload_moved) {
zcbor_log("Payload pointer out of date.\r\n");
Expand Down
Loading

0 comments on commit 028dea9

Please sign in to comment.