Add logging macros that include the function name (#557)

* Added function name logging to mgba_logger

* Addendum to no-op defines

* Fixed compile errors

* Added "()" to function name in log to make it clearer it's a function

* Corrected documentation

* clang-format...?

* Removed MGBA_LOG_LEVEL_MASK magic number

* Added CHECK_NULL_ARG macros

* clang-format...?

* clang-format for real

* Added documentation for CHECK_NULL_ARG_VOID and CHECK_NULL_ARG_RET

* Fixed typo

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Fixed macro safety by wrappinh with do-while

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Fixed format string cut risk in mgba_func_printf

* clang-format

* clang-format some more

* clang-format for real

* Fixed DEBUG -> INFO in comment

Co-authored-by: Rickey <ric@rf3.xyz>

* Moved NULL-check macros to util.h and renamed them

* Small additions - macro rename + clang-format

* Truncate the string instead of the function name

* Cleaned up code from previous commit

* clang-format

* Added mgba_logger_available check to mgba_func_printf

* Updated documentation for latest change

* Changed CHECK_NULL_ARG functions to generic RETURN_ON_ERROR_VAL functions.

* clang-format

* Changed back to specifically check NULL

* clang-format

* Changed "=" into "==" in the error message

* clang-format

* Fixed forgotten "=="

---------

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Rickey <ric@rf3.xyz>
This commit is contained in:
MeirGavish
2026-06-28 06:54:47 +03:00
committed by GitHub
co-authored by Copilot Autofix powered by AI Rickey
parent 45eac259a5
commit 4d26f13c26
3 changed files with 110 additions and 5 deletions
+46
View File
@@ -8,6 +8,9 @@
#define UTIL_H
#include <stdint.h>
#ifdef MGBA_LOGGING
#include "mgba_logger.h"
#endif
/**
* @def GBAL_UNUSED
@@ -56,6 +59,49 @@
// so it needs at least this number of chars to be able to display any suffixed number
#define SUFFIXED_NUM_MIN_REQ_CHARS 4
#ifdef MGBA_LOGGING
#define LOG_ERROR(...) MGBA_FUNC_ERROR(__VA_ARGS__)
#else
// TODO: Add a define to conditionally compile print error to console and add it to the tests?
#define LOG_ERROR(...) ((void)(0))
#endif
/**
* @brief Checks if @p param is NULL and prints error message and returns in case it is.
* Useful for checking arguments to a function or errors during control flow.
*
* This version is for a void function, while @ref GBAL_RETURN_ON_ERROR_VAL_RET is for one with
* a return value.
*/
#define GBAL_RETURN_IF_NULL_VOID(param) \
do \
{ \
if ((param) == NULL) \
{ \
LOG_ERROR("Unexpected value: %s == NULL", #param); \
return; \
} \
} while (0)
/**
* @brief Checks if @p param is equal to NULL
* and prints error message and returns in case it is.
* Useful for checking arguments to a function or errors during control flow.
* @param ret_val The value to return in case @p param is equal to NULL.
*
* This version is for a function that returns a value while @ref GBAL_RETURN_ON_ERROR_VAL_VOID
* is for a void function.
*/
#define GBAL_RETURN_IF_NULL_RET(param, ret_val) \
do \
{ \
if ((param) == NULL) \
{ \
LOG_ERROR("Unexpected value: %s == NULL", #param); \
return (ret_val); \
} \
} while (0)
/**
* @brief Avoid overflow when adding two u32 integers
*