-1

I'm working on a small Real-Time OS project and I ran into a small problem.

void printOutput(void)                                       
{                                                             
   Log_info2("Fib(%d) = %d", FIB_N , cur) ;               
   System_printf("Fib(%d) = %d", FIB_N , cur) ;                   
   System_flush() ;                                                            
}

As you can see this function calls both Log and printf functions with the exact same arguments. My question is - is there any way to make this piece of code more generalized?

I'd appreciate your ideas. Thank you

Jadenkun
  • 317
  • 2
  • 16
  • 1
    What does it mean - generalized? – Eugene Sh. Aug 17 '18 at 17:46
  • May or may not be appropriate to have a function that calls both `Log_info2` and `System_printf` (and `System_flush`?)? – Ry- Aug 17 '18 at 17:49
  • @Ry- That is a function that calls both Log and printf . – Jadenkun Aug 17 '18 at 17:56
  • @Jadenkun: I mean one that just forwards its arguments. Or even a macro, if you’re okay with varargs macros. – Ry- Aug 17 '18 at 18:00
  • @Ry-I don't mind using macros but that doesn't solve the problem of calling both Log and printf with the same arguments, does it? – Jadenkun Aug 17 '18 at 18:04
  • Sure it does, unless you consider `Log_info2(format, ...args); System_printf(format, ...args);` where `format` and `args` are inputs to be too repetitive. Not going to get around that. – Ry- Aug 17 '18 at 18:07
  • @Ry- I guess there's no workaround to this problem then, Thanks for your help though – Jadenkun Aug 17 '18 at 18:09

1 Answers1

2

A small simplification is all you might need.

void printOutput(void)                                       
{
   char const* format = "Fib(%d) = %d";
   Log_info2(format, FIB_N , cur);
   System_printf(format, FIB_N , cur) ;
   System_flush();
}

Further simplification to reduce code duplicaion.

void printOutput(void)
{
   char message[200];  // Make it large enough for your neeeds.
   sprintf(message, "Fib(%d) = %d", FIB_N , cur);

   Log_info2(message);
   System_printf(message) ;
   System_flush();
}
R Sahu
  • 204,454
  • 14
  • 159
  • 270