Solved

How to get vprintf to work?

Posted on 2006-06-14
7
319 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

Question has a verified solution.

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

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…
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 create, access, and change arrays in the C programming language.

863 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

19 Experts available now in Live!

Get 1:1 Help Now