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

泄露秘密 提交于 2019-12-08 19:09:29

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

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!

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!