I have some struct that I need to print frequently. For now, I am using a classical print wrapper around this struct :
void printf_mystruct(struct* my_struct)
if (my_struct==NULL) return;
printf("[value1:%d value2:%d]", struct->value1, struct->value2);
printf("[%l] Struct is incorrect : %mys\n", log_level, my_struct);
Sorry, but some answers are incorrect on Linux with Glibc
On Linux with a GNU Glibc, you can customize printf: you would call
register_printf_function to e.g. define the meaning of
%Y in your
printf format strings.
However, this behavior is Glibc specific, and might even become obsolete... I'm not sure I would recommend this approach!
If coding in C++, the C++ stream library has manipulators which you could extend, and you can also overload for your types the
operator << etc.