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?

> 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.

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:

#include <iostream>
using namespace std;

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

int main()
{
	cout<<"this is the output\n";
	debug2("Output goes to here in debug\n");
}

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.

Am I missing something, or are you looking for some as simple (and ugly) as this:

#define debug2 printf

int main()
{
    debug2("testing %d,%d,%d", 1,2,3);
    return 0;
}

output:

Testing 1,2,3

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.

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:

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

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

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int Debug_Output(string input)
{
    ofstream logfile;
    logfile.open("c:\\log.log", ios::app);
    if (!logfile.is_open()) return 0;
    logfile << input << '\n';
    return 1;

}
int main()
{
    if (!Debug_Output("test")) cout << "writing failed";
    if (!Debug_Output("Next line")) cout << "writing failed";
    return 0;
}

> 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).

This article has been dead for over six months. Start a new discussion instead.