7b1dd7574323c23d75f93f0ac6b950eedde8debd
[invirt/third/libt4.git] / threaded_log.h
1 #ifndef threaded_log_h
2 #define threaded_log_h
3
4 #include "types.h"
5
6 extern char log_thread_prefix;
7 extern int DEBUG_LEVEL;
8
9 struct locked_ostream {
10     std::ostream & s;
11     lock l;
12     locked_ostream(locked_ostream &&) = default;
13     ~locked_ostream() { s << std::endl; }
14     template <typename U>
15     locked_ostream & operator<<(U && u) { s << u; return *this; }
16 };
17
18 locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func);
19 locked_ostream && _log_member(locked_ostream && f, const void *ptr);
20 lock _log_lock();
21
22 #define LOG_NONMEMBER _log_prefix(locked_ostream{std::cerr, _log_lock()}, __FILE__, __func__)
23 #define LOG           _log_member(LOG_NONMEMBER, (const void *)this)
24
25 #define IF_LEVEL(level) if(DEBUG_LEVEL >= abs(level))
26
27 #endif