Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > #iidef and #define to debug program

Reply
Thread Tools

#iidef and #define to debug program

 
 
FBM
Guest
Posts: n/a
 
      04-30-2006
Hi,

I am preparing project for submission and i'd like to allow the print
out of debug messages. I know that with something like

#define DEBUG

#ifdef DEBUG
print(X);
#endif

you can print out comments in an effective way.. however, can these
#define be sent through the command line, in form of a paramterer?

I mean, something like

%program_name param1 param2 (#define DEBUG)

thanks,
Fernando

 
Reply With Quote
 
 
 
 
Artie Gold
Guest
Posts: n/a
 
      04-30-2006
FBM wrote:
> Hi,
>
> I am preparing project for submission and i'd like to allow the print
> out of debug messages. I know that with something like
>
> #define DEBUG
>
> #ifdef DEBUG
> print(X);
> #endif
>
> you can print out comments in an effective way.. however, can these
> #define be sent through the command line, in form of a paramterer?



Erm, no. Preprocessin directives are a *compile* time thing.
You could, of course, pass an argument that indicates whether or not
you're debugging -- but that would have to be dealt with within the code
the compiler actually sees,

[snip]

HTH,
--ag
--
Artie Gold -- Austin, Texas
http://goldsays.blogspot.com
"You can't KISS* unless you MISS**"
[*-Keep it simple, stupid. **-Make it simple, stupid.]
 
Reply With Quote
 
 
 
 
Peter Shaggy Haywood
Guest
Posts: n/a
 
      05-02-2006
Groovy hepcat FBM was jivin' on 29 Apr 2006 21:47:45 -0700 in
comp.lang.c.
#iidef and #define to debug program's a cool scene! Dig it!

>I am preparing project for submission and i'd like to allow the print
>out of debug messages. I know that with something like
>
>#define DEBUG
>
>#ifdef DEBUG
> print(X);
>#endif
>
>you can print out comments in an effective way.. however, can these
>#define be sent through the command line, in form of a paramterer?


Sure, it's easy. I mean, you can't do it this way, of course. This
way is done at compile time. However, you can write a function that
can be switched on or off by a command line argument.
Create a new C source file containing something like the follwing
code:

#include <stdio.h>
#include <stdarg.h>
#include "debug.h"

static int debug_on = DBG_OFF;

void dbg_enable(int on_off)
{
debug_on = on_off;
}

int dbg_printf(const char *fmt, ...)
{
va_list ap;
int r = 0;

if(DBG_OFF != debug_on)
{
va_start(ap, fmt);
r = vfprintf(stderr, fmt, ap);
va_end(ap);
}

return r;
}

Save this as debug.c or something similar. Now, create another new C
source file, put the following code in it, and save it as debug.h or
similar:

#ifndef DEBUG_H
#define DEBUG_H

#define DBG_OFF 0
#define DBG_ON 1

void dbg_enable(int on_off);
int dbg_printf(const char *fmt, ...);

#endif

Now, in all your C source files in which you need this
functionality, include debug.h by putting the following line someplace
near the top of the file:

#include "debug.h"

You'll also have to compile and link debug.c with the rest of your
program. (See your compiler documentation for information on how to do
this.)
In your main() function, when you check for command line arguments,
if you find the argument to turn debugging on, you can easily do so by
passing DBG_ON to dbg_enable(). Then you call dbg_printf() to write
your debug messages. For example:

#include <stdio.h>
#include <string.h>
#include "debug.h"

int main(int argc, char **argv)
{
if(1 < argc && 0 == strcmp(argv[1], "-debug"))
{
dbg_enable(DBG_ON);
}

dbg_printf("Testing %d, %d, %d...\n", 1, 2, 3);

return 0;
}

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
netvmini.sys still not working on Windows 7 even after driver signing disabled ?! (Windows debug mode necessary for debug drivers ???) Skybuck Flying Windows 64bit 3 08-09-2009 05:54 AM
debug="false" in web.config and <%@ debug="true" ...%> in aspx file => true or false? André ASP .Net 3 08-28-2006 12:02 PM
Config Mgr Debug/Release and Web.config Compilation debug=true RonL ASP .Net 0 04-08-2006 03:50 PM
Debug (DLL MFC) -> Debug (Static MFC) ringos75 C++ 0 04-14-2005 01:50 PM
[Howto] Compiling debug Python extensions for non-debug Python Mike C. Fletcher Python 3 10-12-2003 09:37 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57