CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Debug / trace macro

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
gribas



Joined: 21 Feb 2008
Posts: 21

View user's profile Send private message

Debug / trace macro
PostPosted: Thu Aug 30, 2012 11:20 am     Reply with quote

Hi,

Is there a better way to print debug info? I wasn't able to hide the stream information inside the macro (RS232_STREAM), so I end up with this:

Code:

#define DEBUG 1

#define TRACE(x) \
        do { if (DEBUG) fprintf x ; } while (0)

void main() {
   TRACE((RS232_STREAM,"Test %d %s \r\n",1,"..."));
}



Regards,
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Aug 30, 2012 1:39 pm     Reply with quote

This thread may have some tips:
http://www.ccsinfo.com/forum/viewtopic.php?t=47206
gribas



Joined: 21 Feb 2008
Posts: 21

View user's profile Send private message

PostPosted: Thu Aug 30, 2012 2:53 pm     Reply with quote

Thanks for the info! After reading that thread I came up with some alternatives that seem to work:


If you want to be able to switch to/from debug mode (debug will be present in production code)
Code:

#define TRACE(x) \
         do { if (debug_var) fprintf x ; } while (0)

void main() {
  debug_var = 1;
  TRACE((DBG,"Debug information: a=%u, b=%u",
                         variable_name_a,
                         variable_name_b));
  debug_var = 0;
  TRACE((DBG,"No Output"));
}



If you want to remove all debug info from production code
Code:

#ifdef DEBUG
    #define TRACE(x)   fprintf x
#else
    #define TRACE(x)
#endif

void main() {
  TRACE((DBG,"Debug information: a=%u, b=%u %s",
                         variable_name_a,
                         variable_name_b,
                         "\n"));
}



If you want to create switchable debug blocks
Code:

#define _D if(debug_var)

void main() {
  debug_var = 1;
 
  _D printf("This is a debug statement");
  _D {
     a += 10;
     b += 11;
  }
}



If you want to create debug blocks that will go away when in production
Code:

#ifdef DEBUG
    #define _D if (1)   
#else
    #define _D if (0)
#endif

void main() {
  _D {
     a += 10; printf("Test");
  }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group