xref: /btstack/src/btstack_debug.h (revision 1fa044644eacd05211406efa9b19dce991c8a634)
116ece135SMatthias Ringwald /*
216ece135SMatthias Ringwald  * Copyright (C) 2014 BlueKitchen GmbH
316ece135SMatthias Ringwald  *
416ece135SMatthias Ringwald  * Redistribution and use in source and binary forms, with or without
516ece135SMatthias Ringwald  * modification, are permitted provided that the following conditions
616ece135SMatthias Ringwald  * are met:
716ece135SMatthias Ringwald  *
816ece135SMatthias Ringwald  * 1. Redistributions of source code must retain the above copyright
916ece135SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer.
1016ece135SMatthias Ringwald  * 2. Redistributions in binary form must reproduce the above copyright
1116ece135SMatthias Ringwald  *    notice, this list of conditions and the following disclaimer in the
1216ece135SMatthias Ringwald  *    documentation and/or other materials provided with the distribution.
1316ece135SMatthias Ringwald  * 3. Neither the name of the copyright holders nor the names of
1416ece135SMatthias Ringwald  *    contributors may be used to endorse or promote products derived
1516ece135SMatthias Ringwald  *    from this software without specific prior written permission.
1616ece135SMatthias Ringwald  * 4. Any redistribution, use, or modification is done solely for
1716ece135SMatthias Ringwald  *    personal benefit and not for any commercial purpose or for
1816ece135SMatthias Ringwald  *    monetary gain.
1916ece135SMatthias Ringwald  *
2016ece135SMatthias Ringwald  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
2116ece135SMatthias Ringwald  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2216ece135SMatthias Ringwald  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
232fca4dadSMilanka Ringwald  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL BLUEKITCHEN
242fca4dadSMilanka Ringwald  * GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
2516ece135SMatthias Ringwald  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
2616ece135SMatthias Ringwald  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
2716ece135SMatthias Ringwald  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
2816ece135SMatthias Ringwald  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
2916ece135SMatthias Ringwald  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
3016ece135SMatthias Ringwald  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3116ece135SMatthias Ringwald  * SUCH DAMAGE.
3216ece135SMatthias Ringwald  *
3316ece135SMatthias Ringwald  * Please inquire about commercial licensing options at
3416ece135SMatthias Ringwald  * [email protected]
3516ece135SMatthias Ringwald  *
3616ece135SMatthias Ringwald  */
3716ece135SMatthias Ringwald 
38fe5a6c4eSMilanka Ringwald /**
39fe5a6c4eSMilanka Ringwald  * @title Debug Messages
4016ece135SMatthias Ringwald  *
41fe5a6c4eSMilanka Ringwald  * Allow to funnel debug and error messages.
42fe5a6c4eSMilanka Ringwald  *
4316ece135SMatthias Ringwald  */
4416ece135SMatthias Ringwald 
4580e33422SMatthias Ringwald #ifndef DEBUG_H
4680e33422SMatthias Ringwald #define DEBUG_H
4716ece135SMatthias Ringwald 
487907f069SMatthias Ringwald #include "btstack_config.h"
498314c363SMatthias Ringwald #include "btstack_defines.h"
5016ece135SMatthias Ringwald #include "hci_dump.h"
5116ece135SMatthias Ringwald 
524902524cSMatthias Ringwald #if defined __cplusplus
534902524cSMatthias Ringwald extern "C" {
544902524cSMatthias Ringwald #endif
554902524cSMatthias Ringwald 
5616ece135SMatthias Ringwald #ifdef __AVR__
5716ece135SMatthias Ringwald #include <avr/pgmspace.h>
5816ece135SMatthias Ringwald #endif
5916ece135SMatthias Ringwald 
6050ca9754SMatthias Ringwald #ifdef HAVE_ASSERT
614b246621SMatthias Ringwald #ifndef btstack_assert
6250ca9754SMatthias Ringwald #include <assert.h>
6350ca9754SMatthias Ringwald #endif
644b246621SMatthias Ringwald #endif
6550ca9754SMatthias Ringwald 
66dd807209SMatthias Ringwald // fallback to __FILE__ for untagged files
67a981810dSMatthias Ringwald #ifndef BTSTACK_FILE__
68a981810dSMatthias Ringwald #define BTSTACK_FILE__ __FILE__
69dd807209SMatthias Ringwald #endif
70dd807209SMatthias Ringwald 
7150ca9754SMatthias Ringwald #ifdef HAVE_ASSERT
724b246621SMatthias Ringwald // allow to override btstack_assert in btstack_config.h
734b246621SMatthias Ringwald #ifndef btstack_assert
7450ca9754SMatthias Ringwald // map to libc assert
75*ad2dfd72SMatthias Ringwald #ifdef NDEBUG
76*ad2dfd72SMatthias Ringwald #define btstack_assert(condition)  {(void)(condition);}
77*ad2dfd72SMatthias Ringwald #else
7850ca9754SMatthias Ringwald #define btstack_assert(condition)  assert(condition)
79*ad2dfd72SMatthias Ringwald #endif
804b246621SMatthias Ringwald #endif /* btstack_assert */
8150ca9754SMatthias Ringwald #else /* HAVE_ASSERT */
8250ca9754SMatthias Ringwald #ifdef ENABLE_BTSTACK_ASSERT
838f8c6422SDirk Helbig #include <stdnoreturn.h>
848f8c6422SDirk Helbig noreturn void btstack_assert_failed(const char * file, uint16_t line_nr);
8550ca9754SMatthias Ringwald #ifndef btstack_assert
8650ca9754SMatthias Ringwald // use btstack macro that calls btstack_assert_failed() - provided by port
8750ca9754SMatthias Ringwald #define btstack_assert(condition)         if (condition) {} else { btstack_assert_failed(BTSTACK_FILE__, __LINE__);  }
8850ca9754SMatthias Ringwald #endif
894b246621SMatthias Ringwald #else /* ENABLE_BTSTACK_ASSERT */
9050ca9754SMatthias Ringwald // asserts off
916fbaff2fSDirk Helbig #define btstack_assert(condition)         {(void)(condition);}
924b246621SMatthias Ringwald #endif /* btstack_assert */
934b246621SMatthias Ringwald #endif /* HAVE_ASSERT */
9450ca9754SMatthias Ringwald 
952eea7a35SMatthias Ringwald // mark code that should not be reached. Similar to assert, but mapped to NOP for coverage
962eea7a35SMatthias Ringwald #ifdef UNIT_TEST
972eea7a35SMatthias Ringwald #define btstack_unreachable()
982eea7a35SMatthias Ringwald #else
992eea7a35SMatthias Ringwald #define btstack_unreachable() btstack_assert(false)
1002eea7a35SMatthias Ringwald #endif
1012eea7a35SMatthias Ringwald 
102cd4b3424SMatthias Ringwald // allow to provide port specific printf
103cd4b3424SMatthias Ringwald #ifndef BTSTACK_PRINTF
10416ece135SMatthias Ringwald #ifdef __AVR__
10516ece135SMatthias Ringwald #define BTSTACK_PRINTF(format, ...)          printf_P(PSTR(format), ## __VA_ARGS__)
10616ece135SMatthias Ringwald #else
107a02d959eSDirk Helbig #define BTSTACK_PRINTF(...)          printf( __VA_ARGS__)
10816ece135SMatthias Ringwald #endif
109cd4b3424SMatthias Ringwald #endif
110cd4b3424SMatthias Ringwald 
111cd4b3424SMatthias Ringwald #ifdef __AVR__
112a02d959eSDirk Helbig #define HCI_DUMP_LOG_PRINTF(log_level, format, ...) hci_dump_log_P(log_level, PSTR("%S.%u: " format), PSTR(BTSTACK_FILE__), __LINE__, ## __VA_ARGS__)
113a02d959eSDirk Helbig #define HCI_DUMP_LOG_PUTS(log_level, format)        hci_dump_log_P(log_level, PSTR("%S.%u: " format), PSTR(BTSTACK_FILE__), __LINE__)
114cd4b3424SMatthias Ringwald #else
115a02d959eSDirk Helbig #define HCI_DUMP_LOG_PRINTF(log_level, format, ...) hci_dump_log(log_level, "%s.%u: " format, BTSTACK_FILE__, __LINE__, ## __VA_ARGS__)
116a02d959eSDirk Helbig #define HCI_DUMP_LOG_PUTS(log_level, format)        hci_dump_log(log_level, "%s.%u: " format, BTSTACK_FILE__, __LINE__);
117cd4b3424SMatthias Ringwald #endif
11816ece135SMatthias Ringwald 
119e65dd92cSMatthias Ringwald #ifdef _MSC_VER
120e65dd92cSMatthias Ringwald 
121e65dd92cSMatthias Ringwald // original version that requires GNU Macro extensions, but works with Visual Studio 2022
122e65dd92cSMatthias Ringwald 
123e65dd92cSMatthias Ringwald #define HCI_DUMP_LOG HCI_DUMP_LOG_PRINTF
124e65dd92cSMatthias Ringwald 
125e65dd92cSMatthias Ringwald #ifdef ENABLE_LOG_DEBUG
126e65dd92cSMatthias Ringwald #define log_debug(format, ...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_DEBUG, format,  ## __VA_ARGS__)
127e65dd92cSMatthias Ringwald #else
128e65dd92cSMatthias Ringwald #define log_debug(...) (void)(0)
129e65dd92cSMatthias Ringwald #endif
130e65dd92cSMatthias Ringwald 
131e65dd92cSMatthias Ringwald #ifdef ENABLE_LOG_INFO
132e65dd92cSMatthias Ringwald #define log_info(format, ...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_INFO, format,  ## __VA_ARGS__)
133e65dd92cSMatthias Ringwald #else
134e65dd92cSMatthias Ringwald #define log_info(...) (void)(0)
135e65dd92cSMatthias Ringwald #endif
136e65dd92cSMatthias Ringwald 
137e65dd92cSMatthias Ringwald #ifdef ENABLE_LOG_ERROR
138e65dd92cSMatthias Ringwald #define log_error(format, ...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_ERROR, format,  ## __VA_ARGS__)
139e65dd92cSMatthias Ringwald #else
140e65dd92cSMatthias Ringwald #define log_error(...) (void)(0)
141e65dd92cSMatthias Ringwald #endif
142e65dd92cSMatthias Ringwald 
143e65dd92cSMatthias Ringwald #else  /* _MSC_VER */
144e65dd92cSMatthias Ringwald 
145e65dd92cSMatthias Ringwald // C99 Pedantic version - does not work for Visual Studio 2022
146e65dd92cSMatthias Ringwald 
147a02d959eSDirk Helbig #define GET_LOGGER_TYPE_FOR_ARG_COUNT( _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, NAME, ... ) NAME
148a02d959eSDirk Helbig 
149a02d959eSDirk Helbig #define HCI_DUMP_LOG( ... ) GET_LOGGER_TYPE_FOR_ARG_COUNT(__VA_ARGS__, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PRINTF, HCI_DUMP_LOG_PUTS, UNUSED)( __VA_ARGS__ )
150a02d959eSDirk Helbig 
15116ece135SMatthias Ringwald #ifdef ENABLE_LOG_DEBUG
152a02d959eSDirk Helbig #define log_debug(...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_DEBUG, ## __VA_ARGS__)
15316ece135SMatthias Ringwald #else
15445e83228SMatthias Ringwald #define log_debug(...) (void)(0)
15516ece135SMatthias Ringwald #endif
15616ece135SMatthias Ringwald 
15716ece135SMatthias Ringwald #ifdef ENABLE_LOG_INFO
158a02d959eSDirk Helbig #define log_info(...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_INFO, ## __VA_ARGS__)
15916ece135SMatthias Ringwald #else
16045e83228SMatthias Ringwald #define log_info(...) (void)(0)
16116ece135SMatthias Ringwald #endif
16216ece135SMatthias Ringwald 
16316ece135SMatthias Ringwald #ifdef ENABLE_LOG_ERROR
164a02d959eSDirk Helbig #define log_error(...)  HCI_DUMP_LOG(HCI_DUMP_LOG_LEVEL_ERROR, ## __VA_ARGS__)
16516ece135SMatthias Ringwald #else
16645e83228SMatthias Ringwald #define log_error(...) (void)(0)
16716ece135SMatthias Ringwald #endif
16816ece135SMatthias Ringwald 
169e65dd92cSMatthias Ringwald #endif /* _MSC_VER */
170e65dd92cSMatthias Ringwald 
171e65dd92cSMatthias Ringwald 
172fe5a6c4eSMilanka Ringwald /* API_START */
173fe5a6c4eSMilanka Ringwald 
1748314c363SMatthias Ringwald /**
1758314c363SMatthias Ringwald  * @brief Log Security Manager key via log_info
176fe5a6c4eSMilanka Ringwald  * @param name
1778314c363SMatthias Ringwald  * @param key to log
1788314c363SMatthias Ringwald  */
1798314c363SMatthias Ringwald void log_info_key(const char * name, sm_key_t key);
1808314c363SMatthias Ringwald 
1818314c363SMatthias Ringwald /**
1828314c363SMatthias Ringwald  * @brief Hexdump via log_info
1838314c363SMatthias Ringwald  * @param data
1848314c363SMatthias Ringwald  * @param size
1858314c363SMatthias Ringwald  */
1868314c363SMatthias Ringwald void log_info_hexdump(const void *data, int size);
1878314c363SMatthias Ringwald 
188cb42147aSMatthias Ringwald /**
189fe5a6c4eSMilanka Ringwald  * @brief Hexdump via log_debug
190fe5a6c4eSMilanka Ringwald  * @param data
191cb42147aSMatthias Ringwald  * @param size
192cb42147aSMatthias Ringwald  */
193cb42147aSMatthias Ringwald void log_debug_hexdump(const void *data, int size);
194cb42147aSMatthias Ringwald 
195fe5a6c4eSMilanka Ringwald /* API_END */
196fe5a6c4eSMilanka Ringwald 
1974902524cSMatthias Ringwald #if defined __cplusplus
1984902524cSMatthias Ringwald }
1994902524cSMatthias Ringwald #endif
2004902524cSMatthias Ringwald 
20180e33422SMatthias Ringwald #endif // DEBUG_H
202