CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Passing variable list of parameters to fprintf from own func

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
silelis



Joined: 12 Jun 2007
Posts: 68
Location: Poland, podlaskie district

View user's profile Send private message

Passing variable list of parameters to fprintf from own func
PostPosted: Sat Jun 24, 2017 3:40 am     Reply with quote

Hi,

I want to write my own log function which will take as few flash as it is possible.

At the moment I have something like this:
Code:

   #define dbg_printf(fmt,...)   fprintf(LOG_PORT,fmt,__VA_ARGS__)
   #define dbg_return_carriage() fprintf_return_carriage()

void fprintf_return_carriage(void)
{
   fprintf(LOG_PORT,"\r\n");
}

main()
{
  dbg_printf("logc text");
  dbg_return_carriage();
}


but it is flash space ineffective.

I want to write something like that:

Code:
void dbg_printf(fmt,...)
{
   fprintf(LOG_PORT,fmt,__VA_ARGS__);
   fprintf(LOG_PORT,"\r\n");
}


but I don't know how to pass variables from dbg_printf to 1st fprintf?

Any help will be welcomed.
Ttelmah



Joined: 11 Mar 2010
Posts: 19546

View user's profile Send private message

PostPosted: Sat Jun 24, 2017 10:31 am     Reply with quote

You can't.
All the standard CCS printf derivatives, work out what routines to use at compile time. They have to, since to have a printf, that can handle all the possible formats, would result in it being very much larger (huge...).
Now there has been code to handle some variables posted in the past, but such routines are very large compared to the standard implementation. You can create a 'half way house', by for example, creating three fixed printf routines to handle different numbers of variables, and then encapsulating these into three overloaded routines. So something like:
Code:

void print_var(int8 val)
{
   fprintf(STREAM,"%d",val);
}

void print_var(int8 val, int8 val2)
{
   fprintf(STREAM,"%d %d",val, val2);
}

void print_var(int8 val, int8 val2, int8 val3)
{
   fprintf(STREAM,"%d %d %d",val, val2, val3);
}


This obviously needs the correct stream name, and real formats, but you can then call

print_var(10);

and it'll use the first routine, while

print_var(10,20);

will use the second.

Full flexibility though would be bulky. I have in the past compiled part of a standard sprintf library. This came from stuff pointed to by some of the threads at sourceforge. However even quite a cut down version was enormous compared to the CCS routines.....

Have a look here for some limited sources:

<https://www.menie.org/georges/embedded/>
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group