int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args)
{
- enum_name_t *ed = *((enum_name_t**)(args[0]));
- int val = *((int*)(args[1]));
+ enum_name_t *e;
+ int val;
+ char *name;
- char *name = enum_to_name(ed, val);
+ e = *((enum_name_t**)(args[0]));
+ val = *((int*)(args[1]));
+ name = enum_to_name(e, val);
+ if (name == NULL)
+ {
+ return print_in_hook(data, "(%d)", val);
+ }
+ else
+ {
+ return print_in_hook(data, "%s", name);
+ }
+}
+/**
+ * Described in header.
+ */
+int enum_dynamic_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
+ const void *const *args)
+{
+ enum_name_t *e;
+ enum_name_get_t e_get;
+ int val, id = 0;
+ char *name = NULL;
+
+ e_get = *((enum_name_get_t*)(args[0]));
+ id = *((int*)(args[1]));
+ val = *((int*)(args[2]));
+
+ e = e_get(id);
+ if (e)
+ {
+ name = enum_to_name(e, val);
+ }
if (name == NULL)
{
return print_in_hook(data, "(%d)", val);
typedef struct enum_name_t enum_name_t;
+/**
+ * Callback function to get an enum name using an index variable.
+ *
+ * @param index variable to select enum_name_t
+ * @return enum name
+ */
+typedef enum_name_t* (*enum_name_get_t)(int id);
+
/**
* Struct to store names for enums.
*
* character %N is replaced by the enum string. Printf needs two arguments to
* resolve a %N, the enum_name_t* (the defined name in ENUM_BEGIN) followed
* by the numerical enum value.
+ *
+ * To select an enum name dynamically, the %M printf format specifier can be
+ * used. Instead of an enum_name_t, the printf hook expects a enum_name_get_t
+ * followed by an index, followed by the enum value. The enum_name_t is looked
+ * up using the function and the index argument.
*/
struct enum_name_t {
/** value of the first enum string */
int enum_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
const void *const *args);
+/**
+ * printf hook function for dynamically selected enum_names_t.
+ *
+ * Arguments are:
+ * enum_name_get_t getter, int index, int value
+ */
+int enum_dynamic_printf_hook(printf_hook_data_t *data, printf_hook_spec_t *spec,
+ const void *const *args);
+
#endif /** ENUM_H_ @}*/
pfh->add_handler(pfh, 'N', enum_printf_hook,
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
PRINTF_HOOK_ARGTYPE_END);
+ pfh->add_handler(pfh, 'M', enum_dynamic_printf_hook,
+ PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
+ PRINTF_HOOK_ARGTYPE_INT, PRINTF_HOOK_ARGTYPE_END);
pfh->add_handler(pfh, 'T', time_printf_hook,
PRINTF_HOOK_ARGTYPE_POINTER, PRINTF_HOOK_ARGTYPE_INT,
PRINTF_HOOK_ARGTYPE_END);