Question: How to redirect BOOST_SPIRIT_DEBUG messages to a set file (e.g. foo.log)
In 2004 the following code was suggested to redirect the debugging output from the std::cout to a file:
>struct MyDebugOut {
> bool Output;
> ofstream* where_to;
> MyDebug() : Output(true) {};
>};
>
>template MyDebugOut& operator<<(MyDebugOut& _mdo,
>const T& _data)
>{
> if(Output)
> where_to << _data;
>
> return *this;
>};
>
>MyDebugOut mdo;
>#DEFINE BOOST_SPIRIT_DEBUG_OUT mdo
I do not believe this example would work today. I get messages from Visual Studio that there is a missing type specifier for the generic template function. Here is a sample grammar I figured we could work from for this discussion. What I would like to do is discover the proper technique for redirecting output to a file and add this as example code to the Boost Spirit debug page documentation.
Stephen
----------- CODE -----------------
#include
<string>
#define
BOOST_SPIRIT_DEBUG
#include
<boost/spirit/core.hpp>
namespace
mystuff
{
class grammar : public boost::spirit::grammar< grammar >
{
public:
// ---[ THE ACTUAL GRAMMAR DEFINITION ]-------------------------------------------------------------------
template <typename SCANNER>
class definition
{
// Grammar rules
boost::spirit::rule< SCANNER > m_equation;
boost::spirit::rule< SCANNER > m_number;
public:
boost::spirit::rule< SCANNER >
const & start() const { return m_equation; }
// - -[ create the definition ] - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
definition(grammar
const & self)
{
using namespace boost::spirit;
// 1: an object is an empty set or set of members...
m_equation
= m_number >> ch_p(
'+') >> m_number;
m_number
= strict_real_p
| int_p
;
BOOST_SPIRIT_DEBUG_RULE(m_equation);
BOOST_SPIRIT_DEBUG_RULE(m_number);
}
};
};
// ==========================================================================================================
// === T H E F I N A L P A R S I N G R O U T I N E ===
// ==========================================================================================================
}
int
main (int, char**)
{
// 1. parse the input
mystuff::grammar gr;
std::string input =
"3+3";
if ( boost::spirit::parse(input.begin(), input.end(), gr, boost::spirit::space_p).full)
{
std::cout <<
"parsing succeeded" << std::endl;
}
else
{
std::cout <<
"parsing failed" << std::endl;
}
}