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);
}
This function is handy, but is also really limited. I cannot prepen or append some text without making a new wrapper. I know that I can use va_arg family to be able to prepend or apprend some text, but I feel like I would be re-implementing the wheel.
I am wondering if it's possible to write a customizing function to printf. I would like to be able to write something like this :
register2printf("%mys", &printf_mystruct);
...
if (incorrect)
printf("[%l] Struct is incorrect : %mys
", log_level, my_struct);
Is this possible ? How can I do this ?
NB: I am under Ubuntu Linux 10.04 and I use gcc.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…