Big assert / debug clean up

This commit is contained in:
Dario
2023-07-12 14:05:51 +01:00
parent 27350f71b8
commit 6da6666559
26 changed files with 108 additions and 218 deletions

View File

@@ -0,0 +1,143 @@
/**
* @file
* @brief Assertion handling.
*
* Configuration:
* * STA_ASSERT_FORCE: Ignore debug defines and always enable assertions
* * DEBUG: Enables assertions when defined
* * NDEBUG: Disables assertions when defined (overrides DEBUG)
*/
#ifndef STA_CORE_ASSERT_HPP
#define STA_CORE_ASSERT_HPP
/**
* @defgroup sta_core Core
* @brief STA Core library
*/
/**
* @defgroup sta_core_platforms Platforms
* @ingroup sta_core
* @brief Platform specific implementations.
*/
#include <sta/config.hpp>
// Determine if module should be enabled
// Condition:
// STA_ASSERT_FORCE is defined
// or
// DEBUG is defined but not NDEBUG
#ifdef STA_ASSERT_FORCE
# define STA_ASSERT_ENABLED
#else // !STA_ASSERT_FORCE
# if defined(DEBUG) && !defined(NDEBUG)
# define STA_ASSERT_ENABLED
# endif // DEBUG && !NDEBUG
#endif // !STA_ASSERT_FORCE
#if defined(STA_ASSERT_ENABLED) || defined(DOXYGEN)
#include <cstdint>
/**
* @defgroup sta_core_assert Assert
* @ingroup sta_core
* @brief Assertion handling.
*/
namespace sta
{
/**
* @ingroup sta_core_assert
* @{
*/
/**
* @brief Handle failed assertions.
*
* The default implementation uses **STA_DEBUG_PRINT** and will not
* generate any output if the **DEBUG_SERIAL** module is disabled.
*
* Weak implementation can be overridden.
*
* @param expr Asserted expression or message
* @param file File name
* @param line Line number
*/
void assert_failed(const char * expr, const char * file, uint32_t line);
/**
* @brief Stop execution.
*
* Weak implementation can be overridden.
*/
void assert_halt();
/** @} */
} // namespace sta
/**
* @ingroup sta_core_assert
* @{
*/
/**
* @brief Assert expression.
*
* @param expr Expression
*/
# define STA_ASSERT(expr) ( (void)( !(expr) && ( sta::assert_failed(#expr, __FILE__, __LINE__), 1 ) && ( sta::assert_halt(), 1 ) ) )
/**
* @brief Assert expression.
*
* @param expr Expression
* @param msg Message shown on failure
*/
# define STA_ASSERT_MSG(expr, msg) ( (void)( !(expr) && ( sta::assert_failed(msg, __FILE__, __LINE__), 1 ) && ( sta::assert_halt(), 1 ) ) )
/**
* @brief Assert expression if condition is true.
*
* @param cond Condition
* @param expr Expression
*/
# define STA_ASSERT_COND(cond, expr) ( (void)( (cond) ? STA_ASSERT(expr) : 1 ) )
/**
* @brief Assert expression if condition is true.
*
* @param cond Condition
* @param expr Expression
* @param msg Message shown on failure
*/
# define STA_ASSERT_COND_MSG(cond, expr, msg) ( (void)( (cond) ? STA_ASSERT_MSG(expr, msg) ) )
/**
* @brief Expression only evaluated when assertions are enabled.
*
* @param expr Expression
*/
# define STA_ASSERT_EXTRA(expr) expr
/** @} */
#else // !STA_ASSERT_ENABLED
# define STA_ASSERT(expr) ((void)0)
# define STA_ASSERT_MSG(expr, msg) ((void)0)
# define STA_ASSERT_COND(cond, expr) ((void)0)
# define STA_ASSERT_COND_MSG(cond, expr, msg) ((void)0)
# define STA_ASSERT_EXTRA(expr) ((void)0)
#endif // !STA_ASSERT_ENABLED
#endif // STA_CORE_ASSERT_HPP

View File

@@ -0,0 +1,40 @@
#ifndef STA_CORE_DEBUGGING_HPP
#define STA_CORE_DEBUGGING_HPP
#include <sta/config.hpp>
#ifdef STA_DEBUGGING_ENABLED
#include <sta/debug/printing/printable.hpp>
namespace sta
{
extern Printable * Debug;
} // namespace sta
/**
* @brief Debug print message.
*
* @param ... See @ref sta::PrintableUART::print
*
* @ingroup sta_core_debug
*/
# define STA_DEBUG_PRINT(...) sta::Debug->print(__VA_ARGS__)
/**
* @brief Debug print message followed by new-line to UART.
*
* @param ... See @ref sta::PrintableUART::println
*
* @ingroup sta_core_debug
*/
# define STA_DEBUG_PRINTLN(...) sta::Debug->println(__VA_ARGS__)
#else // !STA_DEBUGGING_ENABLED
# define STA_DEBUG_PRINT(...) ((void)0)
# define STA_DEBUG_PRINTLN(...) ((void)0)
#endif // STA_DEBUGGING_ENABLED
#endif // STA_CORE_DEBUGGING_HPP

View File

@@ -0,0 +1,192 @@
/**
* @file
* @brief Printable UART interface definition.
*/
#ifndef STA_CORE_PRINTABLE_HPP
#define STA_CORE_PRINTABLE_HPP
#include <cstddef>
#include <cstdint>
namespace sta
{
/**
* @brief Integer representation.
*
* @ingroup sta_core
*/
enum class IntegerBase
{
DEC, /**< Decimal */
BIN, /**< Binary */
HEX /**< Hexadecimal */
};
class Printable
{
public:
/**
* @brief Print single character.
*
* @param c Character to print
*/
void print(char c);
/**
* @brief Print boolean value.
*
* @param b Boolean value
*/
void print(bool b);
/**
* @brief Print floating point value.
*
* @param d Floating point value
*/
void print(double d);
/**
* @brief Print integer in selected base.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void print(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void print(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void print(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string.
*
* @param str Null terminated string
*/
void print(const char * str);
/**
* @brief Print string.
*
* @param str String buffer
* @param length String length
*/
virtual void print(const char * str, size_t length) = 0;
/**
* @brief Print new-line.
*/
void println();
/**
* @brief Print single character followed by a new-line.
*
* @param c Character to print
*/
void println(char c);
/**
* @brief Print boolean value followed by a new-line.
*
* @param b Boolean value
*/
void println(bool b);
/**
* @brief Print floating point value followed by a new-line.
*
* @param d Floating point value
*/
void println(double d);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 8-bit unsigned integer
* @param base Integer base
*/
void println(uint8_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 16-bit unsigned integer
* @param base Integer base
*/
void println(uint16_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print integer in selected base followed by a new-line.
*
* @param num 32-bit unsigned integer
* @param base Integer base
*/
void println(uint32_t num, IntegerBase base = IntegerBase::DEC);
/**
* @brief Print c-string followed by a new-line.
*
* @param str Null terminated string
*/
void println(const char * str);
/**
* @brief Print string followed by a new-line.
*
* @param str String buffer
* @param length String length
*/
void println(const char * str, size_t length);
private:
/**
* @brief Print unsigned integer in selected base.
*
* @param value Unsigned integer value
* @param base Integer base
* @param fmt printf format string for base 10
* @param size Size of value in bytes
*/
void printBase(uintmax_t value, IntegerBase base, const char * fmt, size_t size);
/**
* @brief Print unsigned integer in base 10.
*
* @param value Unsigned integer value
* @param fmt printf format string
*/
void printDec(uintmax_t value, const char * fmt);
/**
* @brief Print unsigned integer in base 2.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printBin(uintmax_t value, size_t digits);
/**
* @brief Print unsigned integer in base 16.
*
* @param value Unsigned integer value
* @param digits Number of digits to print
*/
void printHex(uintmax_t value, size_t digits);
};
} // namespace sta
#endif // STA_CORE_PRINTABLE_HPP

View File

@@ -0,0 +1,28 @@
#ifndef STA_CORE_PRINTABLE_PRINTF_HPP
#define STA_CORE_PRINTABLE_PRINTF_HPP
#include <sta/debug/printing/printable.hpp>
namespace sta
{
class PrintablePrintf : public Printable
{
public:
/**
* @brief Construct a new Printable Printf object
*/
PrintablePrintf();
/**
* @brief Print string.
*
* @param str String buffer
* @param length String length
*/
void print(const char * str, size_t length) override;
};
} // namespace sta
#endif // STA_CORE_PRINTABLE_PRINTF_HPP

View File

@@ -0,0 +1,44 @@
/**
* @file
* @brief Printable UART interface definition.
*/
#ifndef STA_CORE_PRINTABLE_UART_HPP
#define STA_CORE_PRINTABLE_UART_HPP
#include <sta/bus/uart/uart.hpp>
#include <sta/debug/printing/printable.hpp>
#include <cstddef>
#include <cstdint>
namespace sta
{
/**
* @brief Printable interface for UART.
*
* @ingroup sta_core
*/
class PrintableUART : public Printable
{
public:
/**
* @param intf UART instance
*/
PrintableUART(UART * intf);
/**
* @brief Print string.
*
* @param str String buffer
* @param length String length
*/
void print(const char * str, size_t length) override;
private:
UART * intf_;
};
} // namespace sta
#endif // STA_CORE_PRINTABLE_UART_HPP