1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZCBOR_PRINT_H__
8 #define ZCBOR_PRINT_H__
9 
10 
11 #ifdef __cplusplus
12 extern "C" {
13 #endif
14 
15 #include <stdint.h>
16 #include <stdbool.h>
17 #include <stddef.h>
18 
19 #ifndef ZCBOR_PRINT_FUNC
20 #include <stdio.h>
21 #define zcbor_do_print(...) printf(__VA_ARGS__)
22 #else
23 #define zcbor_do_print(...) ZCBOR_PRINT_FUNC(__VA_ARGS__)
24 #endif
25 
26 #ifdef ZCBOR_VERBOSE
27 #define zcbor_trace_raw(state) (zcbor_do_print("rem: %zu, cur: 0x%x, ec: 0x%zx, err: %d",\
28 	(size_t)state->payload_end - (size_t)state->payload, *state->payload, state->elem_count, \
29 	state->constant_state ? state->constant_state->error : 0))
30 #define zcbor_trace(state, appendix) do { \
31 	zcbor_trace_raw(state); \
32 	zcbor_do_print(", %s\n", appendix); \
33 } while(0)
34 #define zcbor_trace_file(state) do { \
35 	zcbor_trace_raw(state); \
36 	zcbor_do_print(", %s:%d\n", __FILE__, __LINE__); \
37 } while(0)
38 
39 #define zcbor_log_assert(expr, ...) \
40 do { \
41 	zcbor_do_print("ASSERTION \n  \"" #expr \
42 		"\"\nfailed at %s:%d with message:\n  ", \
43 		__FILE__, __LINE__); \
44 	zcbor_do_print(__VA_ARGS__);\
45 } while(0)
46 #define zcbor_log(...) zcbor_do_print(__VA_ARGS__)
47 #else
48 #define zcbor_trace(state, appendix)
49 #define zcbor_trace_file(state) ((void)state)
50 #define zcbor_log_assert(...)
51 #define zcbor_log(...)
52 #endif
53 
54 #ifdef ZCBOR_ASSERTS
55 #define zcbor_assert(expr, ...) \
56 do { \
57 	if (!(expr)) { \
58 		zcbor_log_assert(expr, __VA_ARGS__); \
59 		ZCBOR_FAIL(); \
60 	} \
61 } while(0)
62 #define zcbor_assert_state(expr, ...) \
63 do { \
64 	if (!(expr)) { \
65 		zcbor_log_assert(expr, __VA_ARGS__); \
66 		ZCBOR_ERR(ZCBOR_ERR_ASSERTION); \
67 	} \
68 } while(0)
69 #else
70 #define zcbor_assert(expr, ...)
71 #define zcbor_assert_state(expr, ...)
72 #endif
73 
74 void zcbor_print_compare_lines(const uint8_t *str1, const uint8_t *str2, size_t size);
75 
76 void zcbor_print_compare_strings(const uint8_t *str1, const uint8_t *str2, size_t size);
77 
78 void zcbor_print_compare_strings_diff(const uint8_t *str1, const uint8_t *str2, size_t size);
79 
80 const char *zcbor_error_str(int error);
81 
82 void zcbor_print_error(int error);
83 
84 #ifdef __cplusplus
85 }
86 #endif
87 
88 #endif /* ZCBOR_PRINT_H__ */
89