c++ - Locking ostream output in macro -
i have code proprietary logger:
#define log getstream() where getstream returns std::ostream. user do:
log << "text"; i need thread safe avoid this:
#define end unlock(); #define log lock(); getstream() << "text" << end; since user need add "end":
log << "text" << end; any ideas?
remark: handle carriage return using this.
one way solve use function-style macros, incorporate locking/unlocking using c++ block , scoping:
#define log(output) \ \ { \ lockingclass lock; \ getstream() << output; \ } while (0) the lockingclass (or whatever want name it) scoped lock, locks stream on construction, , unlocks in on destruction.
could used e.g.
log("hello variable " << variable); can't use expressions containing comma though, preprocessor interpret commas argument separator macro. solved variadic macros.
Comments
Post a Comment