开发者

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

If have a ton of user defined types which implement operator<< to w开发者_JAVA百科rite to an std::ostream. How can I uses these when logging my types with Pantheios?


You need to provide "shims" for your own data types. Here's what seems to be the documentation on how to do this: http://www.pantheios.org/tutorials_code.html#types_without_shims. Example:

namespace stlsoft
{
  inline stlsoft::shim_string<char> c_str_data_a(Point const& point)
  {
    stlsoft::shim_string<char> s(101);

    int cch = ::sprintf(s, "{%d, %d; area=%d}",
                        point.x, point.y, point.x * point.y);

    s.truncate(static_cast<size_t>(cch));

    return s;
  }
  inline size_t c_str_len_a(Point const& point)
  {
    char buff[101];

    return static_cast<size_t>(::sprintf(&buff[0], "{%d, %d; area=%d}",
                               point.x, point.y, point.x * point.y));
  }

} // namespace stlsoft

In this case, the type can be passed directly to the log statement:

pantheios::log_ERROR("Point: ", point);

Good luck!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜