M
Martin
hi
i have written a logger with the following calling syntax:
rlog << "bla" << var << ....
i.e. just like cout. it is done by inheriting from std::streambuf
and creating an ostream object (rlog_ostream) with such a custom
streambuf.
rlog is now a macro defined as
#define rlog (rlog_ostream<<"<"__FUNCTION__">")
so it'll log the caller's function name automatically.
now i want to make this whole thing thread-safe, preferably on a
per-log-call basis, i.e. one rlog << ... statement may not be interrupted.
how would i do that ? i thought about locking a mutex in the macro
somehow, but then it cant be released after the logging is completed.
i cant lock in the ostream object either, since i did not inherit from
that, and if i do it in the streambuf then "atomic" logs might get
interrupted.
i'd even switch to a function-like calling syntax, like
rlog ( "bla", var1, "blubb", var2 )
then it'd be easy to lock and unlock for each log operation. i could
overload the function a few times to support different numbers of arguments,
and use templates to support all kinds of types. but how would i
get the __FUNCTION__ thing in there then ? a macro again would
be fine, but macros can't be overloaded (afaik), so it won't support
different numbers of arguments then!
does anyone have an idea or know a trick how to solve this ?
sorry this has gotten so long
thanks
martin
i have written a logger with the following calling syntax:
rlog << "bla" << var << ....
i.e. just like cout. it is done by inheriting from std::streambuf
and creating an ostream object (rlog_ostream) with such a custom
streambuf.
rlog is now a macro defined as
#define rlog (rlog_ostream<<"<"__FUNCTION__">")
so it'll log the caller's function name automatically.
now i want to make this whole thing thread-safe, preferably on a
per-log-call basis, i.e. one rlog << ... statement may not be interrupted.
how would i do that ? i thought about locking a mutex in the macro
somehow, but then it cant be released after the logging is completed.
i cant lock in the ostream object either, since i did not inherit from
that, and if i do it in the streambuf then "atomic" logs might get
interrupted.
i'd even switch to a function-like calling syntax, like
rlog ( "bla", var1, "blubb", var2 )
then it'd be easy to lock and unlock for each log operation. i could
overload the function a few times to support different numbers of arguments,
and use templates to support all kinds of types. but how would i
get the __FUNCTION__ thing in there then ? a macro again would
be fine, but macros can't be overloaded (afaik), so it won't support
different numbers of arguments then!
does anyone have an idea or know a trick how to solve this ?
sorry this has gotten so long
thanks
martin