pre defined variables


 
Thread Tools Search this Thread
Top Forums Programming pre defined variables
# 8  
Old 11-30-2005
I've seen constructs like these in a lot of programs:
Code:
/**
 * mydebug.h -- freeware
 */

/* Prevent mydebug.h from being included more than once */
#ifndef __MYDEBUG_H__
#define __MYDEBUG_H__

/* Makes these functions work even if you include and use this in a .cpp file */
#ifdef __cplusplus
extern "C" {
#endif

/* Don't call directly, use DEBUG_PRINT(("something")) macros */
void _debug_print(const char *cmdstr, ...);
void _info_print(const char *cmdstr, ...);

#ifdef __cplusplus
}
#endif

/**
 * INFO_PRINT always calls _info_print, even if it might NOT actually print anything,
 * so use it sparingly, it'll bloat the final program.
 */
#define INFO_PRINT(X) _info_print X

/**
 * Will ONLY be called when DEBUG is defined, should magically disappear
 * otherwise.  Use all you want, will not bloat non-DEBUG builds.
 */
#ifdef DEBUG
#define DEBUG_PRINT(X) _debug_print X
#else
#define DEBUG_PRINT(X)
#endif

#endif/*__MYDEBUG_H__*/

Code:
/**
 * mydebug.c -- freeware
 */
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include "mydebug.h"

void _debug_print(const char *cmdstr, ...)
{
  va_list ap;

  va_start(ap,cmdstr);
    vfprintf(stderr,cmdstr,ap);
  va_end(ap);
}

void _info_print(const char *cmdstr, ...)
{
  va_list ap;

  /**
   * When DEBUG is undefined, it checks for the DEBUG environment variable.
   * Otherwise, it always prints.
   */

#ifndef DEBUG
  if(getenv("DEBUG") == NULL) return;
#endif

  va_start(ap,cmdstr);
    vfprintf(stderr,cmdstr,ap);
  va_end(ap);
}

This moves all the crazy preprocessor things into a header file and source file so you don't have to worry about whether they should be printing or not.

These functions also behave exactly like printf, because they just pass all their arguments to printf.
Code:
/**
 * debugtest.c -- freeware
 */
#include "mydebug.h"

int main()
{
  DEBUG_PRINT(("This should only print when DEBUG's defined in preprocessor %d\n",5));
  INFO_PRINT(("If DEBUG is defined, this always prints\n"));
  INFO_PRINT(("If DEBUG isn't defined, it only prints if DEBUG is in the environment\n"));
  return(0);
}

Note the crazy double brackets. These are there because most compilers cannot handle variable numbers of arguments inside preprocessor macros; the whole bracketed mess becomes one paramater, so DEBUG_PRINT(("something")) becomes _debug_print ("something") .

That I've seen this reimplimented time and time again in so many programs suggests to me there's no "standard" way.

Last edited by Corona688; 11-30-2005 at 12:22 PM.. Reason: Removing 'untested' from code 'cause I tested it :)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How are environment variables defined in a Gnome terminal session?

Hello... and thanks in advance for any help anyone can offer me with my question! I'm hoping someone will have a little patience with me and walk me through this! I'm trying to understand a user login process on Centos 7 and I've gotten a bit confused trying to figure out when/how a Gnome... (4 Replies)
Discussion started by: bodisha
4 Replies

2. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

3. Shell Programming and Scripting

How to write bash script to subdivide a given subnet into a pre-defined number of smaller subnets?

Bash script to subdivide a given subnet into a pre-defined number of smaller subnets. Show network/broadcast address, number of hosts and assign gateway. For example: Input: ./subnetter.sh 192.168.0.0/24 3 Output: 192.168.0.0/128 subnet 192.168.0.0 broadcast 192.168.0.127 gateway... (1 Reply)
Discussion started by: mail2rias
1 Replies

4. Shell Programming and Scripting

Doing math using user defined input and system environment variables

Hi, I need some help to setup some environmental variables. for example...Get A -> userdefined/user input B -> a number. c -> system variable...for example $GETCONF PAGE_SIZE E = do some math using bc display a message "The value is E" setup the system/kernel paramter sysctl -p... (3 Replies)
Discussion started by: saravanapandi
3 Replies

5. Shell Programming and Scripting

Need an automated pre-defined file pickup method

Hi Gurus, I need to develop a script which picks the files in a pre-defined order. The files from monday-friday will be named as abc_01_20130923 as monday's file and abc_02_20130924 as tuesday's..so..so forth till friday's which will be named as abc_05_20130927.It repeats over for the... (3 Replies)
Discussion started by: vikramgk9
3 Replies

6. AIX

Harddisk1 => Defined

I have an IBM 9110-51A p Series Server with AIX 5.3. It has 2 scsi hard disks hdisk0 and hdisk1 (and 4 on disk array). hdisk1 for few days ago is "Defined". When hdisk1 began "defined" i have a new one unknown disk "available" which called hdisk6. The left led of the case of hdisk1 on the... (4 Replies)
Discussion started by: stetsip
4 Replies

7. UNIX for Dummies Questions & Answers

user defined commands

Hi, i would like to create user defined commands. e,g: if an user executes , mkdircd test then a directory called test should be created and it should be cd to test. How i can create the command mkdircd with below action: mkdir $1 && cd $1. Please help me in achieving this (7 Replies)
Discussion started by: pandeesh
7 Replies

8. Shell Programming and Scripting

Crontab jobs don't see variables defined in /etc/profile

Is it true that Crontab jobs don't see variables defined in /etc/profile? How to get around that? (4 Replies)
Discussion started by: proactiveaditya
4 Replies

9. Shell Programming and Scripting

Ramdisk already defined

I am trying to mount the ramdisk for sorting, and i get the message "ramdisk already defined." What exactly is /dev/ramdisk0 and is it safe to remove it? # command sudo -S mktd 60 <<EOF initiate EOF # block of code for mktd ... && { print "ramdisk already defined." ... (1 Reply)
Discussion started by: ChicagoBlues
1 Replies

10. Shell Programming and Scripting

awk printf for user defined variables

I am working on a SunFire 480 - uname -a gives: SunOS bsmdb02 5.9 Generic_112233-08 sun4u sparc SUNW,Sun-Fire-480R I am tyring to sum up the total size of all the directories for each running database using awk: #!/usr/bin/ksh for Database in `ps -efl | grep "ora_pmon" | grep -v grep |... (1 Reply)
Discussion started by: jabberwocky
1 Replies
Login or Register to Ask a Question