Solved

How to get vprintf to work?

Posted on 2006-06-14
7
329 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops 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.

770 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