c++pantheios

How to make use of existing operator<< overloads when logging with Pantheios?


If have a ton of user defined types which implement operator<< to write to an std::ostream. How can I uses these when logging my types with Pantheios?


Solution

  • Well there is a way you can reuse the operator<< but it ain't pretty. I personally use the boost::lexical_cast library to convert almost any data-type to the std::string data type, which Pantheios supports natively. So if you have the operator<< defined for the point class then you could simply type:

    pantheios::log_ERROR("Point: ", boost::lexical_cast<string>(point_object))

    There are some caveats with this of course. Many people complain that boost::lexical_cast is slow. You can Google it and find some articles that speak of same (http://stackoverflow.com/questions/1250795/very-poor-boostlexical-cast-performance, http://accu.org/index.php/journals/1375). Considering that Pantheios boasts superior performance, you may lose some of that advantage. And the most obvious, you could add a few hundred header files to your project when you add boost::lexical_cast. You also have to type in more letters (e.g. boost::lexical_cast) for each conversion (you could minimize this with a macro - #define BLCS boost::lexical_cast<string> - but thats more indirection than some people may be comfortable with).