Solved

How to get vprintf to work?

Posted on 2006-06-14
7
339 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

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…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
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.

828 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