From e78bb6664382c6928d21f898817944619e2e8135 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=98yvind=20R=C3=B8nningstad?= Date: Wed, 22 Nov 2023 14:24:37 +0100 Subject: [PATCH] Rename zcbor_print() to zcbor_log() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit and zcbor_print_assert() to zcbor_log_assert() Signed-off-by: Øyvind Rønningstad --- README.md | 2 +- include/zcbor_print.h | 12 +++++------ samples/pet/src/pet_decode.c | 2 +- samples/pet/src/pet_encode.c | 2 +- src/zcbor_common.c | 8 ++++---- src/zcbor_decode.c | 24 +++++++++++----------- src/zcbor_encode.c | 12 +++++------ tests/decode/test2_suit/src/main.c | 8 ++++---- tests/decode/test7_suit9_simple/src/main.c | 8 ++++---- tests/encode/test1_suit/src/main.c | 6 +++--- zcbor/zcbor.py | 2 +- 11 files changed, 43 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index cec61a89..1cdf705a 100644 --- a/README.md +++ b/README.md @@ -93,7 +93,7 @@ If using zcbor with Zephyr, use the [Kconfig options](https://github.com/zephyrp Name | Description ------------------------- | ----------- `ZCBOR_CANONICAL` | Assume canonical encoding (AKA "deterministically encoded CBOR"). When encoding lists and maps, do not use indefinite length encoding. Enabling `ZCBOR_CANONICAL` increases code size and makes the encoding library more often use state backups. When decoding, ensure that the incoming data conforms to canonical encoding, i.e. no indefinite length encoding, and always using minimal length encoding (e.g. not using 16 bits to encode a value < 256). Note: the map ordering constraint in canonical encoding is not checked. -`ZCBOR_VERBOSE` | Print messages on encoding/decoding errors (`zcbor_print()`), and also a trace message (`zcbor_trace()`) for each decoded value, and in each generated function (when using code generation). Requires `printk` as found in Zephyr. +`ZCBOR_VERBOSE` | Print log messages on encoding/decoding errors (`zcbor_log()`), and also a trace message (`zcbor_trace()`) for each decoded value, and in each generated function (when using code generation). Requires `printk` as found in Zephyr. `ZCBOR_ASSERTS` | Enable asserts (`zcbor_assert()`). When they fail, the assert statements instruct the current function to return a `ZCBOR_ERR_ASSERTION` error. If `ZCBOR_VERBOSE` is enabled, a message is printed. `ZCBOR_STOP_ON_ERROR` | Enable the `stop_on_error` functionality. This makes all functions abort their execution if called when an error has already happened. `ZCBOR_BIG_ENDIAN` | All decoded values are returned as big-endian. The default is little-endian. diff --git a/include/zcbor_print.h b/include/zcbor_print.h index 13a8d90b..eefbd8e3 100644 --- a/include/zcbor_print.h +++ b/include/zcbor_print.h @@ -24,32 +24,32 @@ extern "C" { (size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \ state->constant_state ? state->constant_state->error : 0, __FILE__, __LINE__)) -#define zcbor_print_assert(expr, ...) \ +#define zcbor_log_assert(expr, ...) \ do { \ zcbor_do_print("ASSERTION \n \"" #expr \ "\"\nfailed at %s:%d with message:\n ", \ __FILE__, __LINE__); \ zcbor_do_print(__VA_ARGS__);\ } while(0) -#define zcbor_print(...) zcbor_do_print(__VA_ARGS__) +#define zcbor_log(...) zcbor_do_print(__VA_ARGS__) #else #define zcbor_trace() ((void)state) -#define zcbor_print_assert(...) -#define zcbor_print(...) +#define zcbor_log_assert(...) +#define zcbor_log(...) #endif #ifdef ZCBOR_ASSERTS #define zcbor_assert(expr, ...) \ do { \ if (!(expr)) { \ - zcbor_print_assert(expr, __VA_ARGS__); \ + zcbor_log_assert(expr, __VA_ARGS__); \ ZCBOR_FAIL(); \ } \ } while(0) #define zcbor_assert_state(expr, ...) \ do { \ if (!(expr)) { \ - zcbor_print_assert(expr, __VA_ARGS__); \ + zcbor_log_assert(expr, __VA_ARGS__); \ ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \ } \ } while(0) diff --git a/samples/pet/src/pet_decode.c b/samples/pet/src/pet_decode.c index 417a39a2..207dd5b3 100644 --- a/samples/pet/src/pet_decode.c +++ b/samples/pet/src/pet_decode.c @@ -25,7 +25,7 @@ static bool decode_Pet(zcbor_state_t *state, struct Pet *result); static bool decode_Pet( zcbor_state_t *state, struct Pet *result) { - zcbor_print("%s\r\n", __func__); + zcbor_log("%s\r\n", __func__); bool tmp_result = (((zcbor_list_start_decode(state) && ((((zcbor_list_start_decode(state) && ((zcbor_multi_decode(1, 3, &(*result).names_count, (zcbor_decoder_t *)zcbor_tstr_decode, state, (&(*result).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_decode(state), false)) && zcbor_list_end_decode(state))) && ((zcbor_bstr_decode(state, (&(*result).birthday))) diff --git a/samples/pet/src/pet_encode.c b/samples/pet/src/pet_encode.c index 9accf9d8..6c71ba02 100644 --- a/samples/pet/src/pet_encode.c +++ b/samples/pet/src/pet_encode.c @@ -25,7 +25,7 @@ static bool encode_Pet(zcbor_state_t *state, const struct Pet *input); static bool encode_Pet( zcbor_state_t *state, const struct Pet *input) { - zcbor_print("%s\r\n", __func__); + zcbor_log("%s\r\n", __func__); bool tmp_result = (((zcbor_list_start_encode(state, 3) && ((((zcbor_list_start_encode(state, 3) && ((zcbor_multi_encode_minmax(1, 3, &(*input).names_count, (zcbor_encoder_t *)zcbor_tstr_encode, state, (&(*input).names), sizeof(struct zcbor_string))) || (zcbor_list_map_end_force_encode(state), false)) && zcbor_list_end_encode(state, 3))) && (((((((*input).birthday.len >= 8) diff --git a/src/zcbor_common.c b/src/zcbor_common.c index e90beca5..0e8e6acb 100644 --- a/src/zcbor_common.c +++ b/src/zcbor_common.c @@ -49,7 +49,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, zcbor_state_t local_copy = *state; if (state->constant_state->current_backup == 0) { - zcbor_print("No backups available.\r\n"); + zcbor_log("No backups available.\r\n"); ZCBOR_ERR(ZCBOR_ERR_NO_BACKUP_ACTIVE); } @@ -60,7 +60,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, if (!(flags & ZCBOR_FLAG_KEEP_PAYLOAD)) { if (state->constant_state->backup_list[i].payload_moved) { - zcbor_print("Payload pointer out of date.\r\n"); + zcbor_log("Payload pointer out of date.\r\n"); ZCBOR_ERR(ZCBOR_ERR_PAYLOAD_OUTDATED); } } @@ -73,7 +73,7 @@ bool zcbor_process_backup(zcbor_state_t *state, uint32_t flags, } if (local_copy.elem_count > max_elem_count) { - zcbor_print("elem_count: %" PRIuFAST32 " (expected max %" PRIuFAST32 ")\r\n", + zcbor_log("elem_count: %" PRIuFAST32 " (expected max %" PRIuFAST32 ")\r\n", local_copy.elem_count, max_elem_count); ZCBOR_ERR(ZCBOR_ERR_HIGH_ELEM_COUNT); } @@ -289,7 +289,7 @@ int zcbor_entry_function(const uint8_t *payload, size_t payload_len, int err = zcbor_pop_error(state); err = (err == ZCBOR_SUCCESS) ? ZCBOR_ERR_UNKNOWN : err; - zcbor_print("Return error: %d\r\n", err); + zcbor_log("Return error: %d\r\n", err); return err; } diff --git a/src/zcbor_decode.c b/src/zcbor_decode.c index a7538d99..8f0d2688 100644 --- a/src/zcbor_decode.c +++ b/src/zcbor_decode.c @@ -211,7 +211,7 @@ bool zcbor_uint_decode(zcbor_state_t *state, void *result, size_t result_size) INITIAL_CHECKS_WITH_TYPE(ZCBOR_MAJOR_TYPE_PINT); if (!value_extract(state, result, result_size)) { - zcbor_print("uint with size %d failed.\r\n", result_size); + zcbor_log("uint with size %d failed.\r\n", result_size); ZCBOR_FAIL(); } return true; @@ -281,7 +281,7 @@ bool zcbor_int64_expect(zcbor_state_t *state, int64_t expected) } if (actual != expected) { - zcbor_print("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected); + zcbor_log("%" PRIi64 " != %" PRIi64 "\r\n", actual, expected); ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE); } return true; @@ -328,7 +328,7 @@ bool zcbor_uint64_expect(zcbor_state_t *state, uint64_t expected) ZCBOR_FAIL(); } if (actual != expected) { - zcbor_print("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected); + zcbor_log("%" PRIu64 " != %" PRIu64 "\r\n", actual, expected); ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE); } return true; @@ -380,7 +380,7 @@ static bool str_start_decode_with_overflow_check(zcbor_state_t *state, /* Casting to size_t is safe since str_start_decode() checks that * payload_end is bigger that payload. */ if (result->len > (size_t)(state->payload_end - state->payload)) { - zcbor_print("error: 0x%zu > 0x%zu\r\n", + zcbor_log("error: 0x%zu > 0x%zu\r\n", result->len, (state->payload_end - state->payload)); ERR_RESTORE(ZCBOR_ERR_NO_PAYLOAD); @@ -471,7 +471,7 @@ void zcbor_next_fragment(zcbor_state_t *state, result->fragment.len = result->total_len - result->offset; partition_fragment(state, result); - zcbor_print("New fragment length %zu\r\n", result->fragment.len); + zcbor_log("New fragment length %zu\r\n", result->fragment.len); state->payload += result->fragment.len; } @@ -487,7 +487,7 @@ void zcbor_bstr_next_fragment(zcbor_state_t *state, result->fragment.len = result->total_len - result->offset; partition_fragment(state, result); - zcbor_print("fragment length %zu\r\n", result->fragment.len); + zcbor_log("fragment length %zu\r\n", result->fragment.len); state->payload_end = state->payload + result->fragment.len; } @@ -837,7 +837,7 @@ static bool try_key(zcbor_state_t *state, void *key_result, zcbor_decoder_t key_ return false; } - zcbor_print("Found element at index %d.\n", get_current_index(state, 1)); + zcbor_log("Found element at index %d.\n", get_current_index(state, 1)); return true; } @@ -978,7 +978,7 @@ bool zcbor_unordered_map_end_decode(zcbor_state_t *state) for (size_t i = 0; i < zcbor_flags_to_bytes(state->decode_state.map_elem_count); i++) { if (state->decode_state.map_search_elem_state[i] != 0) { - zcbor_print("unprocessed element(s) in map: [%d] = 0x%02x\n", + zcbor_log("unprocessed element(s) in map: [%d] = 0x%02x\n", i, state->decode_state.map_search_elem_state[i]); ZCBOR_ERR(ZCBOR_ERR_ELEMS_NOT_PROCESSED); } @@ -1030,7 +1030,7 @@ bool zcbor_simple_expect(zcbor_state_t *state, uint8_t expected) } if (actual != expected) { - zcbor_print("simple value %u != %u\r\n", actual, expected); + zcbor_log("simple value %u != %u\r\n", actual, expected); ERR_RESTORE(ZCBOR_ERR_WRONG_VALUE); } @@ -1071,7 +1071,7 @@ bool zcbor_bool_decode(zcbor_state_t *state, bool *result) } *result = value; - zcbor_print("boolval: %u\r\n", *result); + zcbor_log("boolval: %u\r\n", *result); return true; } @@ -1455,11 +1455,11 @@ bool zcbor_multi_decode(size_t min_decode, state->payload = payload_bak; state->elem_count = elem_count_bak; ZCBOR_ERR_IF(i < min_decode, ZCBOR_ERR_ITERATIONS); - zcbor_print("Found %" PRIuFAST32 " elements.\r\n", i); + zcbor_log("Found %" PRIuFAST32 " elements.\r\n", i); return true; } } - zcbor_print("Found %" PRIuFAST32 " elements.\r\n", max_decode); + zcbor_log("Found %" PRIuFAST32 " elements.\r\n", max_decode); *num_decode = max_decode; return true; } diff --git a/src/zcbor_encode.c b/src/zcbor_encode.c index 46b15f4b..f22e00d7 100644 --- a/src/zcbor_encode.c +++ b/src/zcbor_encode.c @@ -28,7 +28,7 @@ static uint8_t log2ceil(size_t val) case 8: return 3; } - zcbor_print("Should not come here.\r\n"); + zcbor_log("Should not come here.\r\n"); return 0; } @@ -140,7 +140,7 @@ bool zcbor_int_encode(zcbor_state_t *state, const void *input_int, size_t int_si bool zcbor_uint_encode(zcbor_state_t *state, const void *input_uint, size_t uint_size) { if (!value_encode(state, ZCBOR_MAJOR_TYPE_PINT, input_uint, uint_size)) { - zcbor_print("uint with size %d failed.\r\n", uint_size); + zcbor_log("uint with size %d failed.\r\n", uint_size); ZCBOR_FAIL(); } return true; @@ -389,14 +389,14 @@ static bool list_map_end_encode(zcbor_state_t *state, size_t max_num, ZCBOR_FAIL(); } - zcbor_print("list_count: %" PRIuFAST32 "\r\n", list_count); + zcbor_log("list_count: %" PRIuFAST32 "\r\n", list_count); /** If max_num is smaller than the actual number of encoded elements, * the value_encode() below will corrupt the data if the encoded * header is larger than the previously encoded header. */ if (header_len > max_header_len) { - zcbor_print("max_num too small.\r\n"); + zcbor_log("max_num too small.\r\n"); ZCBOR_ERR(ZCBOR_ERR_HIGH_ELEM_COUNT); } @@ -455,7 +455,7 @@ bool zcbor_list_map_end_force_encode(zcbor_state_t *state) bool zcbor_simple_encode(zcbor_state_t *state, uint8_t *input) { if (!value_encode(state, ZCBOR_MAJOR_TYPE_SIMPLE, input, sizeof(*input))) { - zcbor_print("Error encoding %u (0x%p)\r\n", *input, input); + zcbor_log("Error encoding %u (0x%p)\r\n", *input, input); ZCBOR_FAIL(); } return true; @@ -596,7 +596,7 @@ bool zcbor_multi_encode(const size_t num_encode, zcbor_encoder_t encoder, ZCBOR_FAIL(); } } - zcbor_print("Encoded %" PRIuFAST32 " elements.\n", num_encode); + zcbor_log("Encoded %" PRIuFAST32 " elements.\n", num_encode); return true; } diff --git a/tests/decode/test2_suit/src/main.c b/tests/decode/test2_suit/src/main.c index 1eba39e3..abde652f 100644 --- a/tests/decode/test2_suit/src/main.c +++ b/tests/decode/test2_suit/src/main.c @@ -44,8 +44,8 @@ ZTEST(cbor_decode_test2, test_5) uint8_t expected_component1[] = {0x00, 0x34, 0x01}; int res; - zcbor_print("test_vector at: 0x%zu\r\n", (size_t)test_vector1); - zcbor_print("test_vector end at: 0x%zu\r\n", + zcbor_log("test_vector at: 0x%zu\r\n", (size_t)test_vector1); + zcbor_log("test_vector end at: 0x%zu\r\n", ((size_t)test_vector1) + sizeof(test_vector1)); memset(&outerwrapper1, 0, sizeof(outerwrapper1)); res = cbor_decode_SUIT_Outer_Wrapper(test_vector1, sizeof(test_vector1), @@ -124,7 +124,7 @@ ZTEST(cbor_decode_test2, test_5) sizeof(expected_component1), "component elem 1 doesn't match."); - zcbor_print("\r\n"); + zcbor_log("\r\n"); zassert_equal(1, manifest ->SUIT_Manifest_suit_common .SUIT_Manifest_suit_common_cbor @@ -165,7 +165,7 @@ ZTEST(cbor_decode_test2, test_5) .SUIT_Command_SUIT_Directive_m .suit_directive_set_parameters_m_l_map_SUIT_Parameters_m_count, "Should be two vars (parameters)."); - zcbor_print("\r\n"); + zcbor_log("\r\n"); memset(&sequence, 0, sizeof(sequence)); res = cbor_decode_SUIT_Command_Sequence( diff --git a/tests/decode/test7_suit9_simple/src/main.c b/tests/decode/test7_suit9_simple/src/main.c index 531fe44c..c8fd8372 100644 --- a/tests/decode/test7_suit9_simple/src/main.c +++ b/tests/decode/test7_suit9_simple/src/main.c @@ -95,8 +95,8 @@ ZTEST(cbor_decode_test7, test_suit9_simple2) { int res; - zcbor_print("test_vector at: 0x%zx\r\n", (size_t)test_vector2); - zcbor_print("test_vector end at: 0x%zx\r\n", + zcbor_log("test_vector at: 0x%zx\r\n", (size_t)test_vector2); + zcbor_log("test_vector end at: 0x%zx\r\n", ((size_t)test_vector2) + sizeof(test_vector2)); memset(&envelope1, 0, sizeof(envelope1)); res = cbor_decode_SUIT_Envelope(test_vector2, @@ -115,8 +115,8 @@ ZTEST(cbor_decode_test7, test_suit9_simple5) { int res; - zcbor_print("test_vector at: 0x%zx\r\n", (size_t)test_vector5); - zcbor_print("test_vector end at: 0x%zx\r\n", + zcbor_log("test_vector at: 0x%zx\r\n", (size_t)test_vector5); + zcbor_log("test_vector end at: 0x%zx\r\n", ((size_t)test_vector5) + sizeof(test_vector5)); memset(&envelope1, 0, sizeof(envelope1)); res = cbor_decode_SUIT_Envelope(test_vector5, diff --git a/tests/encode/test1_suit/src/main.c b/tests/encode/test1_suit/src/main.c index 46b271c7..2f639dac 100644 --- a/tests/encode/test1_suit/src/main.c +++ b/tests/encode/test1_suit/src/main.c @@ -243,7 +243,7 @@ void test_command_sequence(struct zcbor_string *sequence_str, return; } - zcbor_print("\r\ntest %s\r\n", name); + zcbor_log("\r\ntest %s\r\n", name); memset(&sequence1, 0, sizeof(sequence1)); int res = cbor_decode_SUIT_Command_Sequence(sequence_str->value, @@ -337,8 +337,8 @@ void test_manifest(const uint8_t *input, uint32_t len) int res; size_t out_len; - zcbor_print("test_vector at: 0x%zx\r\n", (size_t)input); - zcbor_print("test_vector end at: 0x%zx\r\n", + zcbor_log("test_vector at: 0x%zx\r\n", (size_t)input); + zcbor_log("test_vector end at: 0x%zx\r\n", ((size_t)input) + len); res = cbor_decode_SUIT_Outer_Wrapper(input, len, &outerwrapper1, NULL); zassert_equal(ZCBOR_SUCCESS, res, "top-level decoding failed."); diff --git a/zcbor/zcbor.py b/zcbor/zcbor.py index bb45b4d7..5509029e 100755 --- a/zcbor/zcbor.py +++ b/zcbor/zcbor.py @@ -2651,7 +2651,7 @@ def render_function(self, xcoder, mode): xcoder.type_name if struct_ptr_name(mode) in body else "void"} *{struct_ptr_name(mode)}) {{ - zcbor_print("%s\\r\\n", __func__); + zcbor_log("%s\\r\\n", __func__); {"struct zcbor_string tmp_str;" if "tmp_str" in body else ""} {"bool int_res;" if "int_res" in body else ""}