Solved

How to get vprintf to work?

Posted on 2006-06-14
7
306 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 and use switch statements in the C programming language.

744 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now