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?

- 22,240
- 19
- 65
- 88
-
Make sure they are in the same namespace. This allows ADL to kick in. – Daniel Lidström Dec 02 '10 at 12:50
-
@Daniel: I do not see a back-end which can handle std::ostream or such ... ? – Danvil Dec 02 '10 at 13:12
2 Answers
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!

- 9,930
- 1
- 27
- 35
-
Thanks for your response! But if this is the only option, I will use a different framework which can use operator<< directly. No time to re-implement all these functions. – Danvil Dec 02 '10 at 14:54
-
Perhaps you can define template functions of the above that accept your types. Those functions could utilize your `operator<<` and a `std::ostringstream`. Put them inside your types namespace, at least I think you should, not sure. – Daniel Lidström Dec 02 '10 at 16:28
-
1Why not put in a feature request on the Pantheios site? The main author of Pantheios is the same guy who does STLSoft, and who came up with the Shims idea. If anyone can do it quickly, he's the one, and I think he's pretty responsive to requests. – dcw Dec 02 '10 at 20:39
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).

- 778
- 5
- 18