# Quantlib C++: How to output QL_TRACE to a log file

I am trying to output some intermediate variable values in a program using the Quantlib C++ to a text file so that I can try to debug my code to see what's going on, and the way I set up my code is like this:

At the beginning of the method:

std::ofstream outfile ("C:\\temp\\MY DEBUG FILE", std::ofstream::out);
QL_TRACE_ON(outfile);


then at where I'd like to see the values printed out:

QL_TRACE(var1, var2, var3, ....) //many more variable here


and then towards the end:

outfile.close();


However every time I run through this code the text file is created however it contains nothing at all. (0 byte file)

I have also tried to do this at the beginning of the hpp:

#define QL_ENABLE_TRACING


And my already includes ql/quantlib.hpp which will in turn include the tracing hpp.

Is there something else I am missing here?

• Why can't you just print to the std::cout yourself? – SmallChess Nov 8 '16 at 3:41

Besides what you already did, you also have to add the statement

QL_TRACE_ENABLE;


to your method before starting to emit tracing messages; see the documentation at http://quantlib.org/reference/group__debug_macros.html.

QL_TRACE(var1, var2, var3, ....);


is not going to work as is, since it will be expanded to something like

out << var1, var2, var3 << endl;


which is unlikely to compile (and even if it does, it won't do what you mean). If you want to trace multiple variables in a single statement, you can use

QL_TRACE(var1 << ", " << var2 << ", " << var3);


instead. The alternative is to use multiple statements, in which case you're even better off writing

QL_TRACE_VARIABLE(var1);
QL_TRACE_VARIABLE(var2);
QL_TRACE_VARIABLE(var3);
...


that will also output the name of the variable besides its value.

• Thanks @Luigi Ballabio, the code did compile but I guess that's why nothing was produced. – AZhu Nov 18 '16 at 3:01