Quantlib C++:如何將 QL_TRACE 輸出到日誌文件
我正在嘗試使用 Quantlib C++ 將程序中的一些中間變數值輸出到文本文件,以便我可以嘗試調試我的程式碼以查看發生了什麼,我設置程式碼的方式是這樣的:
在方法的開頭:
std::ofstream outfile ("C:\\temp\\MY DEBUG FILE", std::ofstream::out); QL_TRACE_ON(outfile);
然後在我想看到列印出來的值的地方:
QL_TRACE(var1, var2, var3, ....) //many more variable here
然後到最後:
outfile.close();
但是,每次我執行此程式碼時,都會創建文本文件,但它根本不包含任何內容。(0字節文件)
我也嘗試在 hpp 開始時這樣做:
#define QL_ENABLE_TRACING
我已經包含了 ql/quantlib.hpp ,它又將包含跟踪 hpp。
我在這裡還缺少什麼嗎?
除了你已經做的,你還必須添加語句
QL_TRACE_ENABLE;
to your method before starting to emit tracing messages; see the documentation at http://quantlib.org/reference/group__debug_macros.html.
Also, note that your statement
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.