K
Keve Nagy
Hello Everyone,
I am trying to put together my ultimate TraceMessage function, but the
nature of difficulties I experience is beyound my C programming limits,
so I would like to request some community help to broaden that barrier.
I am only a hobby C programmer and I only do this out of curiosity, so
forgive me if I am saying, asking or assuming stupid things here!
For reference: This is a CLI tool, built using gcc.
Here is what I am trying to achieve:
Consider the standard printf() function. Throughout my sourcecodes I
use this to send out informative trace messages to standard output.
These messages are extremely useful during the development of the code,
and later again during debugging or enhancement. But their appearance is
not required for the normal operation of the finished program.
What I am looking for is a way to keep these trace-message printf()
lines in the code, but still be able to turn their action on or off at
run-time.
For example, if I start the program with a --debug argument it should
print out the trace messages, but should not display them otherwise.
My greatest difficulty is that I wanted to implement this traceMessage
function as a single call. Just like having the ability to call printf()
with an extra first int argument before the format string, and if that
argument has a value of 1 then the output should be printed or if it has
a value of 0 then no output should be printed.
Examples:
printfTraceMessage(1,"This is a format string with %d arguments.",1);
/* the line above should produce the equivalent of
printf("This is a format string with %d arguments.",1); */
printfTraceMessage(1,"Error %d occured in function %s where variable %s
had the value of %s.", holdingErrorcode, holdingFunctionname,
holdingVarname, var);
/* the line above should produce the equivalent of
printf("Error %d occured in function %s where variable %s had the
value of %s.", holdingErrorcode, holdingFunctionname, holdingVarname,
var); */
printfTraceMessage(0,"This is a format string with %d arguments.",1);
/* the line above should not produce any output, due to the 0 value
of the first argument */
So the extreme flexibility of variable arguments with printf() should
remain, but I need the additional ability to indicate if that printf()
should actually be carried out or should simply be ignored.
The way I currently do this is the following:
1., use sprintf(globalTraceMessageStr, "formatstring" ,x ,y, ...);
2., call my printTraceMsg() function.
void printTraceMsg(void) {
if (globalDebugIndicator==1) printf(globalTraceMessageStr);
}
So all I do is set the value of the global variable
"globalDebugIndicator" to control if I want to see the messages printed
or not.
But this method requires me to use two calls to achieve what I need. One
to sprintf(), and another to printTraceMsg(). So I alway need
double-instruction lines like:
sprintf(globTrcMsgStr, "fmt str", v1, v2, ...); printTraceMsg();
I alway wanted to replace this with a single instruction call, but so
far I was unable to implement that upgrade.
The idea was to use a function pointer, which could either point to
printf() or point to an empty function like:
void doNothing(void) { /* this intentionally does nothing */ };
Handling the extremely flexible variable argument list of printf() is
where I fail to implement this idea.
I also failed to implement another idea, creating a function that gets
an arbitrary printf() call as its only one parameter. Something like:
doWhenDebugIsOn( printf("fmt str",v1,v2,...,vN) );
Again, handling the extremely flexible way printf() processes variable
length arguments is where I fail.
So I believe I should somehow unite the use of a function pointer AND
variable length arguments, but how would I do that. I just wasn't able
to put this together using the stdarg.h samples and examples.
I would appreciate any suggestion on this topic or pointer to related
contents. Alternative ways to achieve my single-instruction-call
tracemessage function are also welcome!
Regards,
Keve
I am trying to put together my ultimate TraceMessage function, but the
nature of difficulties I experience is beyound my C programming limits,
so I would like to request some community help to broaden that barrier.
I am only a hobby C programmer and I only do this out of curiosity, so
forgive me if I am saying, asking or assuming stupid things here!
For reference: This is a CLI tool, built using gcc.
Here is what I am trying to achieve:
Consider the standard printf() function. Throughout my sourcecodes I
use this to send out informative trace messages to standard output.
These messages are extremely useful during the development of the code,
and later again during debugging or enhancement. But their appearance is
not required for the normal operation of the finished program.
What I am looking for is a way to keep these trace-message printf()
lines in the code, but still be able to turn their action on or off at
run-time.
For example, if I start the program with a --debug argument it should
print out the trace messages, but should not display them otherwise.
My greatest difficulty is that I wanted to implement this traceMessage
function as a single call. Just like having the ability to call printf()
with an extra first int argument before the format string, and if that
argument has a value of 1 then the output should be printed or if it has
a value of 0 then no output should be printed.
Examples:
printfTraceMessage(1,"This is a format string with %d arguments.",1);
/* the line above should produce the equivalent of
printf("This is a format string with %d arguments.",1); */
printfTraceMessage(1,"Error %d occured in function %s where variable %s
had the value of %s.", holdingErrorcode, holdingFunctionname,
holdingVarname, var);
/* the line above should produce the equivalent of
printf("Error %d occured in function %s where variable %s had the
value of %s.", holdingErrorcode, holdingFunctionname, holdingVarname,
var); */
printfTraceMessage(0,"This is a format string with %d arguments.",1);
/* the line above should not produce any output, due to the 0 value
of the first argument */
So the extreme flexibility of variable arguments with printf() should
remain, but I need the additional ability to indicate if that printf()
should actually be carried out or should simply be ignored.
The way I currently do this is the following:
1., use sprintf(globalTraceMessageStr, "formatstring" ,x ,y, ...);
2., call my printTraceMsg() function.
void printTraceMsg(void) {
if (globalDebugIndicator==1) printf(globalTraceMessageStr);
}
So all I do is set the value of the global variable
"globalDebugIndicator" to control if I want to see the messages printed
or not.
But this method requires me to use two calls to achieve what I need. One
to sprintf(), and another to printTraceMsg(). So I alway need
double-instruction lines like:
sprintf(globTrcMsgStr, "fmt str", v1, v2, ...); printTraceMsg();
I alway wanted to replace this with a single instruction call, but so
far I was unable to implement that upgrade.
The idea was to use a function pointer, which could either point to
printf() or point to an empty function like:
void doNothing(void) { /* this intentionally does nothing */ };
Handling the extremely flexible variable argument list of printf() is
where I fail to implement this idea.
I also failed to implement another idea, creating a function that gets
an arbitrary printf() call as its only one parameter. Something like:
doWhenDebugIsOn( printf("fmt str",v1,v2,...,vN) );
Again, handling the extremely flexible way printf() processes variable
length arguments is where I fail.
So I believe I should somehow unite the use of a function pointer AND
variable length arguments, but how would I do that. I just wasn't able
to put this together using the stdarg.h samples and examples.
I would appreciate any suggestion on this topic or pointer to related
contents. Alternative ways to achieve my single-instruction-call
tracemessage function are also welcome!
Regards,
Keve