-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Print overhaul #373
Merged
oyvindronningstad
merged 5 commits into
NordicSemiconductor:main
from
oyvindronningstad:print
Jan 2, 2024
Merged
Print overhaul #373
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7ad302e
zcbor_print.h: Move printing code to a new file zcbor_print.h
oyvindronningstad 98ee098
Rename zcbor_print() to zcbor_log()
oyvindronningstad 4abdf4b
zcbor_print.h: Move from printk to printf
oyvindronningstad 54c2b7b
zcbor_print.h: Improve zcbor_trace() and other tracing
oyvindronningstad 6eb5d5b
Refactor printing of errors from generated functions
oyvindronningstad File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright (c) 2023 Nordic Semiconductor ASA | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#ifndef ZCBOR_PRINT_H__ | ||
#define ZCBOR_PRINT_H__ | ||
|
||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#ifndef ZCBOR_PRINT_FUNC | ||
#include <stdio.h> | ||
#define zcbor_do_print(...) printf(__VA_ARGS__) | ||
#else | ||
#define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__) | ||
#endif | ||
|
||
#ifdef ZCBOR_VERBOSE | ||
#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)) | ||
#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 { \ | ||
zcbor_do_print("ASSERTION \n \"" #expr \ | ||
"\"\nfailed at %s:%d with message:\n ", \ | ||
__FILE__, __LINE__); \ | ||
zcbor_do_print(__VA_ARGS__);\ | ||
} while(0) | ||
#define zcbor_log(...) zcbor_do_print(__VA_ARGS__) | ||
#else | ||
#define zcbor_trace(state, appendix) | ||
#define zcbor_trace_file(state) ((void)state) | ||
#define zcbor_log_assert(...) | ||
#define zcbor_log(...) | ||
#endif | ||
|
||
#ifdef ZCBOR_ASSERTS | ||
#define zcbor_assert(expr, ...) \ | ||
do { \ | ||
if (!(expr)) { \ | ||
zcbor_log_assert(expr, __VA_ARGS__); \ | ||
ZCBOR_FAIL(); \ | ||
} \ | ||
} while(0) | ||
#define zcbor_assert_state(expr, ...) \ | ||
do { \ | ||
if (!(expr)) { \ | ||
zcbor_log_assert(expr, __VA_ARGS__); \ | ||
ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \ | ||
} \ | ||
} while(0) | ||
#else | ||
#define zcbor_assert(expr, ...) | ||
#define zcbor_assert_state(expr, ...) | ||
#endif | ||
|
||
__attribute__((used)) | ||
static void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, uint32_t size) | ||
{ | ||
for (uint32_t j = 0; j < size; j++) { | ||
zcbor_do_print("%x ", str1[j]); | ||
} | ||
zcbor_do_print("\r\n"); | ||
for (uint32_t j = 0; j < size; j++) { | ||
zcbor_do_print("%x ", str2[j]); | ||
} | ||
zcbor_do_print("\r\n"); | ||
for (uint32_t j = 0; j < size; j++) { | ||
zcbor_do_print("%x ", str1[j] != str2[j]); | ||
} | ||
zcbor_do_print("\r\n"); | ||
zcbor_do_print("\r\n"); | ||
} | ||
|
||
__attribute__((used)) | ||
static void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, uint32_t size) | ||
{ | ||
for (uint32_t i = 0; i <= size / 16; i++) { | ||
zcbor_do_print("line %d (char %d)\r\n", i, i*16); | ||
zcbor_print_compare_lines(&str1[i*16], &str2[i*16], | ||
MIN(16, (size - i*16))); | ||
} | ||
zcbor_do_print("\r\n"); | ||
} | ||
|
||
__attribute__((used)) | ||
static void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, uint32_t size) | ||
{ | ||
bool printed = false; | ||
for (uint32_t i = 0; i <= size / 16; i++) { | ||
if (memcmp(&str1[i*16], &str2[i*16], MIN(16, (size - i*16)) != 0)) { | ||
zcbor_do_print("line %d (char %d)\r\n", i, i*16); | ||
zcbor_print_compare_lines(&str1[i*16], &str2[i*16], | ||
MIN(16, (size - i*16))); | ||
printed = true; | ||
} | ||
} | ||
if (printed) { | ||
zcbor_do_print("\r\n"); | ||
} | ||
} | ||
|
||
__attribute__((used)) | ||
static const char *zcbor_error_str(int error) | ||
{ | ||
#define ZCBOR_ERR_CASE(err) case err: \ | ||
return #err; /* The literal is static per C99 6.4.5 paragraph 5. */\ | ||
|
||
switch(error) { | ||
ZCBOR_ERR_CASE(ZCBOR_SUCCESS) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_MEM) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_NO_BACKUP_ACTIVE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_LOW_ELEM_COUNT) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_HIGH_ELEM_COUNT) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_INT_SIZE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_FLOAT_SIZE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_ADDITIONAL_INVAL) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_NO_PAYLOAD) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_PAYLOAD_NOT_CONSUMED) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_TYPE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_VALUE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_WRONG_RANGE) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_ITERATIONS) | ||
ZCBOR_ERR_CASE(ZCBOR_ERR_ASSERTION) | ||
} | ||
#undef ZCBOR_ERR_CASE | ||
|
||
return "ZCBOR_ERR_UNKNOWN"; | ||
} | ||
|
||
__attribute__((used)) | ||
static void zcbor_print_error(int error) | ||
{ | ||
zcbor_do_print("%s\r\n", zcbor_error_str(error)); | ||
} | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* ZCBOR_PRINT_H__ */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does it intentionally print
"\r\n"
twice?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, to make it visually separate from the next thing to be printed.