Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
August 17, 2017, 04:36:28 AM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: multilevel debug implementation with macros  (Read 1630 times)
zolfaghar
Newbie
*
Posts: 1


Email
« on: June 21, 2016, 06:52:56 PM »

Hi Steve,
On page 396 of chapter 17 of the fourth edition of C programming book you provided snippets of code to create a multilevel debug method. I added everything you had in the book in a file, and tried to run it, but I do not get the desired result. According to the content, once all the code is in and it is compiled, we should be able to type a.out -d3 and see a more verbose logging result than when we type in a.out -d1. Here is what I have:

Code: (Objective-C)
#include <stdio.h>
#include <stdlib.h>
int Debug;
//#define DEBUG(fmt, ...)  fprintf (stderr, fmt, __VA_ARGS__ )
#ifdef DEBON
#define DEBUG(level,fmt, ...)        if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__)
#else
#    define DEBUG(level, fmt, ...)
#endif
int process ( int i1, int i2)
{
  int val;

  DEBUG (1, "process (%i, %i)\n", i1, i2);
  val = i1 * i2;
  DEBUG (1, "return %i\n", val);
  return val;
}
int main ( int argc, char *argv[])
{
int arg1 = 0, arg2 = 0;
if (argc > 1)
arg1 = atoi (argv[1]);
if ( argc == 3)
  arg2 = atoi (argv[2]);

DEBUG (1, "processed %i arguments\n", argc -1 );
DEBUG (1, "arg1 = %i, arg2 = %i\n" , arg1, arg2);
printf ("%d\n", process (arg1, arg2));
return 0;
}

The only thing I added to what you advised was a
Code: (Objective-C)
int Debug;
declaration. Could you elaborate on what else is needed, and why the code does not work?
Logged
djt
Newbie
*
Posts: 7



Email
« Reply #1 on: June 28, 2016, 05:41:02 PM »

Although I've only read the first 10 chapters of the book "Programming in C 4th edition" this may be of use:

The program has to process the command-line arguments and store the debug level in the variable Debug.

Here's a simplified example which uses the third command-line argument as the debug level (it just uses a number for the debug level, not "-d1" or "-d3"):

Code: (Objective-C)
/* test.c

   debug level test program
*/

#include <stdio.h>  
#include <stdlib.h>  

int Debug;  

//#define DEBUG(fmt, ...)  fprintf (stderr, fmt, __VA_ARGS__ )  

#ifdef DEBON  
#    define DEBUG(level, fmt, ...) if (Debug >= level) fprintf (stderr, fmt, __VA_ARGS__)  
#else  
#    define DEBUG(level, fmt, ...)  
#endif  

int process (int i1, int i2)  
{  
    int val;  
  
    DEBUG (1, "process (%i, %i)\n", i1, i2);  
    
    val = i1 * i2;  
    
    DEBUG (1, "return %i\n", val);  
    
    return val;  
}  

int main (int argc, char *argv[])  
{  
    int arg1 = 0, arg2 = 0;  
    
    if (argc > 1)  
        arg1 = atoi (argv[1]);  
    
    if (argc > 2)  
        arg2 = atoi (argv[2]);  
  
    if (argc > 3)
        Debug = atoi (argv[3]);
    
    DEBUG (1, "processed %i arguments\n", argc -1 );  
    DEBUG (1, "arg1 = %i, arg2 = %i, Debug = %i\n" , arg1, arg2, Debug);  
    
    printf ("%d\n", process (arg1, arg2));  

    return 0;  
}

compile the program test.c without DEBON:
gcc test.c -o test

then run the program:
test 5 10

output:
50

--------

then recompile the program with the debug macro included:
gcc test.c -D DEBON -o test

then run the program with the debug macro included but without a command line debug level argument:
test 5 10

output:
50

then re-run the program with the debug macro included and the debug level = 0:
test 5 10 0

output:
50

then re-run the program with the debug macro included and the debug level = 1:
test 5 10 1

output:
processed 3 arguments
arg1 = 5, arg2 = 10, Debug = 1
process (5, 10)
return 50
50

--------

The debugging output is generated when the debug level is 1 or greater.

djt
« Last Edit: June 30, 2016, 09:46:34 AM by djt » Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents ゥ 2009 classroomM.com. All rights reserved.