Spare more Resources for non-verbose Errors (#1214)

* Spare more Resources for non-verbose Errors

At the moment we only remove the __FILE__ in case ETL_VERBOSE_ERRORS is
not set. However, we also want to remove the error text which can
consume quite some resources and without the file name the line number
is also not very useful.

* Introduce separate ETL_MINIMAL_ERRORS to keep backwards compatability

To not break runtime backwards compatability, instead of removing the
text in case of ETL_VERBOSE_ERRORS not defined, we introduce a second
flag ETL_MINIMAL_ERRORS which is mutually exclusive. The semantic is as
follows:

- ETL_VERBOSE_ERRORS: We use verbose text, file names and line numbers.
- ETL_MINIMAL_ERRORS: We do not use anything.
- ETL_VERBOSE_ERRORS and ETL_MINIMAL_ERRORS: Issue an error.
- non defined: We use terse text without file names nor line numbers.

---------

Co-authored-by: John Wellbelove <jwellbelove@users.noreply.github.com>
This commit is contained in:
Christoph Rüthing 2025-12-13 09:26:25 +01:00 committed by GitHub
parent 558c04b0a0
commit 20486b11cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 45 additions and 19 deletions

View File

@ -397,15 +397,23 @@ namespace etl
//*************************************
#if defined(ETL_VERBOSE_ERRORS)
#define ETL_ERROR(e) (e(__FILE__, __LINE__)) // Make an exception with the file name and line number.
#define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v))) // Make an exception with the file name, line number and value.
#define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text) // Use the verbose text.
#define ETL_ERROR_GENERIC(text) (etl::exception((text),__FILE__, __LINE__)) // Make a generic exception with a message, file name and line number.
// include everything, file name, line number, verbose text
#define ETL_ERROR(e) (e(__FILE__, __LINE__))
#define ETL_ERROR_WITH_VALUE(e, v) (e(__FILE__, __LINE__, (v)))
#define ETL_ERROR_TEXT(verbose_text, terse_text) (verbose_text)
#define ETL_ERROR_GENERIC(text) (etl::exception((text), __FILE__, __LINE__))
#elif defined(ETL_MINIMAL_ERRORS)
// include nothing, no file name, no line number, no text
#define ETL_ERROR(e) (e("", -1))
#define ETL_ERROR_WITH_VALUE(e, v) (e("", -1, (v)))
#define ETL_ERROR_TEXT(verbose_text, terse_text) ("")
#define ETL_ERROR_GENERIC(text) (etl::exception("", "", -1))
#else
#define ETL_ERROR(e) (e("", __LINE__)) // Make an exception with the line number.
#define ETL_ERROR_WITH_VALUE(e, v) (e("", __LINE__, (v))) // Make an exception with the file name, line number and value.
#define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text) // Use the terse text.
#define ETL_ERROR_GENERIC(text) (etl::exception((text),"", __LINE__)) // Make a generic exception with a message and line number.
// include only terse text, no file name, no line number
#define ETL_ERROR(e) (e("", -1))
#define ETL_ERROR_WITH_VALUE(e, v) (e("", -1, (v)))
#define ETL_ERROR_TEXT(verbose_text, terse_text) (terse_text)
#define ETL_ERROR_GENERIC(text) (etl::exception((text),"", -1))
#endif
#endif

View File

@ -49,11 +49,11 @@ namespace etl
typedef const char* string_type;
typedef int numeric_type;
#if defined(ETL_VERBOSE_ERRORS)
//*************************************************************************
/// Constructor.
//*************************************************************************
#if defined(ETL_VERBOSE_ERRORS)
ETL_CONSTEXPR
exception(string_type reason_, string_type file_, numeric_type line_)
: reason_text(reason_),
@ -61,14 +61,15 @@ namespace etl
line(line_)
{
}
#else
//*************************************************************************
/// Constructor.
//*************************************************************************
#elif defined(ETL_MINIMAL_ERRORS)
ETL_CONSTEXPR
exception(string_type reason_, string_type /*file_*/, numeric_type line_)
: reason_text(reason_),
line(line_)
exception(string_type /*reason_*/, string_type /*file_*/, numeric_type /*line_*/)
{
}
#else
ETL_CONSTEXPR
exception(string_type reason_, string_type /*file_*/, numeric_type /*line_*/)
: reason_text(reason_)
{
}
#endif
@ -80,7 +81,11 @@ namespace etl
ETL_CONSTEXPR
string_type what() const
{
#if !defined(ETL_MINIMAL_ERRORS)
return reason_text;
#else
return "";
#endif
}
@ -105,16 +110,23 @@ namespace etl
ETL_CONSTEXPR
numeric_type line_number() const
{
#if defined(ETL_VERBOSE_ERRORS)
return line;
#else
return -1;
#endif
}
private:
string_type reason_text; ///< The reason for the exception.
#if !defined(ETL_MINIMAL_ERRORS)
string_type reason_text; ///< The reason for the exception.
#endif
#if defined(ETL_VERBOSE_ERRORS)
string_type file_text; ///< The file for the exception.
#endif
numeric_type line; ///< The line for the exception.
#endif
};
}

View File

@ -83,6 +83,12 @@ SOFTWARE.
#define ETL_IS_DEBUG_BUILD 0
#endif
//*************************************
// Do a validity check for error settings, only one is allowed.
#if defined(ETL_VERBOSE_ERRORS) && defined(ETL_MINIMAL_ERRORS)
#error "ETL_VERBOSE_ERRORS and ETL_MINIMAL_ERRORS are mutually exclusive"
#endif
//*************************************
// Helper macros, so we don't have to use double negatives.
// The ETL will use the STL, unless ETL_NO_STL is defined.