LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

log file function combining DebugPrintf and fprintf with varargin

Hi,

I want to combine DebugPrintf and fprintf into one function, say LogPrintf. The idea is that everywhere in my code that I use a DebugPrintf I could also be fprintf'ing to a log file.
Suppose my code says this:

int i = 0;
DebugPrintf("%s%d\n", "Hello world. Counter = ", i);

Instead I want to have it say:

int i = 0;
LogPrintf("%s%d\n", "Hello world. Counter = ", i);

The interface to LogPrintf would be exactly like the interface to DebugPrintf:

int LogPrintf (const char formatString[], ...);

Then within LogPrintf, and assuming I've already opened the log file to use for fprintf, I would put:

DebugPrintf(input formatting string, input arguments list);
fprintf(already opened log file pointer, input formatting string, input arguments list);

My problem is with the varargin (...) in the argument lists of DebugPrintf and fprintf. How do I pass the variable-length argument list as a parameter into the DebugPrintf and fprintf calls within LogPrintf ?

Any ideas?

Thanks!
0 Kudos
Message 1 of 5
(4,451 Views)

Are you looking for something like this? 

int LogPrintf(char *format, ...)
{  va_list arguments;
 
 va_start(arguments, format);
 fprintf(logFileStream, format, arguments);
 va_end(arguments);
 return(0);
}

0 Kudos
Message 2 of 5
(4,449 Views)
Thanks, this got me started! One problem is you can't pass format and arguments directly to fprintf like you suggested.

But I found some ANSI C functions that might do the trick:

#include
#include
int vprintf( char *format, va_list arg_ptr );
int vfprintf( FILE *stream, const char *format, va_list arg_ptr );
int vsprintf( char *buffer, char *format, va_list arg_ptr );

Example usage of vprintf:

void error( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
fprintf( stderr, "Error: " );
vfprintf( stderr, fmt, args );
fprintf( stderr, "\n" );
va_end( args );
exit( 1 );
}

So, I could make my life really easy by going:

int LogPrintf( char *fmt, ... ) {
va_list args;
va_start( args, fmt );
vfprintf( [pointer to log file here], fmt, args );
vfprintf( [pointer to CVI's debug output pane here], fmt, args );
va_end( args );
return 0;
}

How do I obtain a pointer to CVI's output pane to redirect my vfprintf output there?
0 Kudos
Message 3 of 5
(4,429 Views)
Even better, vsprintf formats the variable length argument list into a string that can then be easily passed to DebugPrintf and fprintf:

int EventLogPrintf (const char formatString[], ...)
{
static char buffer[EVENTLOG_MAXBUFFER];
int status;

va_list args;
va_start( args, formatString );
status = vsprintf( buffer, formatString, args );
DebugPrintf("%s", buffer);
fprintf([pointer to logfile stream],"%s", buffer);
va_end( args );

return status;
}

Thanks for your help getting me started on this!
0 Kudos
Message 4 of 5
(4,423 Views)

I think you have the right answer.  I was working from memory a bit and did not have a compiler in front of me to try it out.

Happy to hear you found a solution.

0 Kudos
Message 5 of 5
(4,419 Views)