From b2a4b4410c006ea5d52ef60c76bd34d9628870d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoph=20R=C3=BCthing?= Date: Sat, 13 Dec 2025 09:26:25 +0100 Subject: [PATCH] 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 --- include/etl/error_handler.h | 24 ++++++++++++++++-------- include/etl/exception.h | 36 ++++++++++++++++++++++++------------ include/etl/platform.h | 6 ++++++ 3 files changed, 46 insertions(+), 20 deletions(-) diff --git a/include/etl/error_handler.h b/include/etl/error_handler.h index 425f68ef..2fb10011 100644 --- a/include/etl/error_handler.h +++ b/include/etl/error_handler.h @@ -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 diff --git a/include/etl/exception.h b/include/etl/exception.h index 4323df78..c73fef1c 100644 --- a/include/etl/exception.h +++ b/include/etl/exception.h @@ -60,26 +60,27 @@ namespace etl typedef const char* string_type; typedef int numeric_type; - -#if defined(ETL_VERBOSE_ERRORS) + //************************************************************************* /// Constructor. //************************************************************************* - ETL_EXCEPTION_CONSTEXPR +#if defined(ETL_VERBOSE_ERRORS) + ETL_CONSTEXPR exception(string_type reason_, string_type file_, numeric_type line_) : reason_text(reason_), file_text(file_), line(line_) { } +#elif defined(ETL_MINIMAL_ERRORS) + ETL_CONSTEXPR + exception(string_type /*reason_*/, string_type /*file_*/, numeric_type /*line_*/) + { + } #else - //************************************************************************* - /// Constructor. - //************************************************************************* - ETL_EXCEPTION_CONSTEXPR - exception(string_type reason_, string_type /*file_*/, numeric_type line_) - : reason_text(reason_), - line(line_) + ETL_CONSTEXPR + exception(string_type reason_, string_type /*file_*/, numeric_type /*line_*/) + : reason_text(reason_) { } #endif @@ -94,7 +95,11 @@ namespace etl override #endif { +#if !defined(ETL_MINIMAL_ERRORS) return reason_text; +#else + return ""; +#endif } @@ -119,16 +124,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 }; } diff --git a/include/etl/platform.h b/include/etl/platform.h index 62a5da60..116daeb4 100644 --- a/include/etl/platform.h +++ b/include/etl/platform.h @@ -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.