Hi there. I am using a library called SOCI that has a method to set a
stream which it uses to log SQL queries. The signature is as follows:
void setLogStream(st d::ostream *s);
This works great when used with something like
setLogStream(&s td::cerr). However, I would like to add more context to
the query logged. Specifically, I'd like to add the thread ID. So,
instead of the query logged looking like:
select * from foo;
it would look like:
<thread-id>| select * from foo;
I already have the function to get the thread id
(boost::this_th read::get_id()) , but I need to find a way to pass an
object to setLogStream that will do the formatting for me. As far as I
can tell, the only way to do this is inheriting from std::ostream and
somehow overriding the default formatting. Except I'm not sure which
of its methods are virtual and meant to be overridden. Any clues?
Basically, I figure what I need might look something like:
struct ThreadLogger : public std::ostream {
// ...
virtual void someOverridenFu nction(const char *const buffer) {
std::cerr << boost::this_thr ead::get_id() << "| " << buffer;
}
// ...
};
and then:
ThreadLogger logger;
setLogStream(&l ogger);
Is what I'm looking to do possible? Should I look into IO manipulators
instead? Any help greatly appreciated :).
Thanks!