Solved

How to get vprintf to work?

Posted on 2006-06-14
7
358 Views
Last Modified: 2010-04-15
Below is a chunk of code that doesn't seem to be working properly. It compiles fine but the vprintf does not output the text.
I basically want to make a custom debug function.

#include <stdarg.h>

void custom(ui8 priority, ui8 id_value, ...)
{
  va_list args;
  ui8 a,b;

  va_start(args, id_value);
  vprintf(id_value, args);
  va_end(args);
}

return_value testy(void)
{
  custom(1,1,"This is a %d %s.\n", 12, "test");
  return R_OK;
}
0
Comment
Question by:viperss
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
7 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 16906517
Hi viperss,
>  vprintf(id_value, args);

The code doesn't look right.
Check out the following code as an example:
void logEventMessage(char *format, ...)
{
    va_list marker;
    char buf[1024];
    va_start(marker,format);

    vsprintf(buf, format,marker);


David Maisonave (Axter)
Cheers!
0
 
LVL 30

Expert Comment

by:Axter
ID: 16906551
I believe your code should look more like the following:

 void custom(ui8 priority, ui8 id_value, format...)
{
  va_list args;
  ui8 a,b;

  va_start(args, format);
  vprintf(format, args);


I'm not sure what you want to do with priory and id_value arguments
0
 
LVL 30

Expert Comment

by:Axter
ID: 16906596
FYI:
I recommend you use macros for creating your custom debug function.
By using macros, you can pass additional data to your debug function, like __FILE__ and __LINE__, which will tell you source file and line number.
More modern compilers will also allow you to pass the calling function name via __FUNCTION__

Here's an example macro I use for driver debugging:
#define DbgPrintInfo                        DbgPrint( "%x: (%5.5i) [%s] [Info]", KeGetCurrentThread(), __LINE__, __FUNCTION__);DbgPrint

Since my driver only has one file, I've excluded __FILE__ from the debug log

The above debugging logic can easily be excluded from lease code via following method:
#if DBG
#define DbgPrintInfo                        DbgPrint( "%x: (%5.5i) [%s] [Info]", KeGetCurrentThread(), __LINE__, __FUNCTION__);DbgPrint
#else
#define DbgPrintInfo if (1);else DbgPrint
#endif //DBG

Instead of DBG, you should use the debug macro that is used for your compiler. (like _DEBUG, DEBUG, or _DEBUG_)
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

Author Comment

by:viperss
ID: 16907065
Thanks for the response.
What exactly is format in vprintf? Is it the last argument passed in to the function?
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
ID: 16907265
>>What exactly is format in vprintf? Is it the last argument passed in to the function?

format is the first argument in the VA list.
0
 
LVL 30

Expert Comment

by:Axter
ID: 16908271
Why the B grade?
0
 

Author Comment

by:viperss
ID: 16913608
Your answer was not 100% correct. I found out that the format string is the actual output string. Thus, you could pass it in separately. For example:

test_function(int priority, const char* format, ...)
{
  va_list args;
  va_start(args, format);
  vprintf(format, args);
  va_end(args);
}

test_function(1,"This is %s %s.\n", "a", "test");

Technically, format here is not the first argument in the variable list.
I'll really appreciate your help. It led me in the right direction.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

632 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question