C++ debug macro

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 16
Reputation: welles is an unknown quantity at this point 
Solved Threads: 0
welles welles is offline Offline
Newbie Poster

C++ debug macro

 
0
  #1
Sep 2nd, 2008
Dear all,
I am trying to print out some outputs defined by my own in different function calls and classes into a debug file, I saw in one source code using macros like:

#ifdef DEBUG_OUTPUT
#define debug2 printf
#else
#define debug2(arg1,...)
#endif

and with a call to this macro like: debug2("This is the debug output\n");
Can anyone please tell me where is the printed output go? I wish to write some of my own defined outputs from different functions(which from different classes) and write them into a file so that I can follow the order of the function calls and hence how the program works.
So is it possible to use this type of debug macro or anyone please suggest me an alternative?
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ debug macro

 
0
  #2
Sep 3rd, 2008
> Can anyone please tell me where is the printed output go?
In this example, it would go to the console. In debug, it would expand to
printf ("This is the debug output\n");

It would be pretty easy to replace that with say
void myLoggingPrintf( const char *fmt, ... );

And it could do whatever you wanted.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 16
Reputation: welles is an unknown quantity at this point 
Solved Threads: 0
welles welles is offline Offline
Newbie Poster

Re: C++ debug macro

 
0
  #3
Sep 3rd, 2008
Hi, Salem,
Thank you for your reply, but I am still not sure about how to use this debug macro. I did a very simple test in VS2008 with a new console application like this:

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #ifdef DEBUG_OUTPUT
  5. #define debug2 printf
  6. #else
  7. #define debug2(arg1,...)
  8. #endif
  9.  
  10. int main()
  11. {
  12. cout<<"this is the output\n";
  13. debug2("Output goes to here in debug\n");
  14. }

however only the first line goes to the console output. So what have I missed out? If possible, can you please give me an example?
Also you mentioned to replace this with "void myLoggingPrintf( const char *fmt, ... );"
so is the first parameter the file name that the output will go? can you please explain a bit more on this?
Also I want to test an existing program with a number of files in it, and get some printouts from a number of functions, so do I need to define this "myLoggingPrintf" function call within each individual C++ file and then call it at where ever I want? And if the files are from different directories, do I need to specify the location of the output file so that all the printouts that I want to see goes into only one file at the end?

Thank you very much for your help. Any comment would be highly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,853
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: C++ debug macro

 
0
  #4
Sep 3rd, 2008
Am I missing something, or are you looking for some as simple (and ugly) as this:
  1. #define debug2 printf
  2.  
  3. int main()
  4. {
  5. debug2("testing %d,%d,%d", 1,2,3);
  6. return 0;
  7. }
output:
  1. Testing 1,2,3
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 16
Reputation: welles is an unknown quantity at this point 
Solved Threads: 0
welles welles is offline Offline
Newbie Poster

Re: C++ debug macro

 
0
  #5
Sep 3rd, 2008
hi, niek_e, thank you for your reply,
the example you gave works but if I do the following it does not work, no output in console:

#ifdef DEBUG_OUTPUT
#define debug2 printf
#else
#define debug2(arg1,...)
#endif

actually the second line "#define debug2 printf" becomes shaded and in gray in VS2008 text editor. So why does this happen? Also (and more importantly) how can I printout the things that I want into a text file from a number of different function calls (which from different classes in different C++ files) so I can trace how the program works, like which part/class/function is executed first and so on....
Thank you.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,853
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 301
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: C++ debug macro

 
0
  #6
Sep 3rd, 2008
Originally Posted by welles View Post
actually the second line "#define debug2 printf" becomes shaded and in gray in VS2008 text editor. So why does this happen?
The gray code means that visual studio has discovered that the code will not be executed. So the if-statement is probably true.
I'll make some comments in your code:

  1. #ifdef DEBUG_OUTPUT // if the symbol DEBUG_OUTPUT is defined
  2. #define debug2 printf // define debug2 to be the same as printf
  3. #else // if DEBUG_OUTPUT is NOT defined
  4. #define debug2(arg1,...) // define something else
  5. #endif // endif :)

Originally Posted by welles View Post
Also (and more importantly) how can I printout the things that I want into a text file from a number of different function calls (which from different classes in different C++ files) so I can trace how the program works, like which part/class/function is executed first and so on....
Thank you.
Here's a small example of a textfile-based-log-function

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int Debug_Output(string input)
  8. {
  9. ofstream logfile;
  10. logfile.open("c:\\log.log", ios::app);
  11. if (!logfile.is_open()) return 0;
  12. logfile << input << '\n';
  13. return 1;
  14.  
  15. }
  16. int main()
  17. {
  18. if (!Debug_Output("test")) cout << "writing failed";
  19. if (!Debug_Output("Next line")) cout << "writing failed";
  20. return 0;
  21. }
Last edited by niek_e; Sep 3rd, 2008 at 6:41 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: C++ debug macro

 
0
  #7
Sep 3rd, 2008
> actually the second line "#define debug2 printf" becomes shaded and in gray in VS2008 text
> editor. So why does this happen?
Because it's conditional code.

You can add (at the top of the file)
#define DEBUG_OUTPUT
But that's hard work if you have to do it to lots of files.

Typically, in project settings -> compiler -> pre-processor, you would add
DEBUG_OUTPUT
in the list of additional defined. You might see NDEBUG in the release build (this is a standard one).
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC