X-Git-Url: http://xvm.mit.edu/gitweb/invirt/third/libt4.git/blobdiff_plain/5a5c578e2e358a121cdb9234a6cb11c4ecfbf323..c06ef44e7af1571710fd31dd0ab068dd77b1eb2d:/threaded_log.h diff --git a/threaded_log.h b/threaded_log.h index ebb2222..9a83bfb 100644 --- a/threaded_log.h +++ b/threaded_log.h @@ -1,61 +1,26 @@ #ifndef threaded_log_h #define threaded_log_h -#include "types.h" +#include +#include -extern mutex cerr_mutex; -extern map thread_name_map; -extern int next_thread_num; -extern map instance_name_map; -extern int next_instance_num; -extern char log_thread_prefix; +struct locked_ostream { + std::ostream & s; + lock l; + locked_ostream(locked_ostream &&) = default; + ~locked_ostream() { s << std::endl; } + template + locked_ostream & operator<<(U && u) { s << u; return *this; } +}; -namespace std { - // This... is an awful hack. But sticking this in std:: makes it possible for - // ostream_iterator to use it. - template - ostream & operator<<(ostream &o, const pair &d) { - return o << "<" << d.first << "," << d.second << ">"; - } -} +locked_ostream && _log_prefix(locked_ostream && f, const string & file, const string & func); +locked_ostream && _log_member(locked_ostream && f, const void *ptr); +int _log_debug_level(); +lock _log_lock(); -template -typename enable_if::value && !is_same::value, ostream>::type & -operator<<(ostream &o, const A &a) { - o << "["; - auto oit = ostream_iterator(o, ", "); - copy(a.begin(), a.end(), oit); - o << "]"; - return o; -} +#define LOG_NONMEMBER _log_prefix(locked_ostream{std::cerr, _log_lock()}, __FILE__, __func__) +#define LOG _log_member(LOG_NONMEMBER, (const void *)this) -#define LOG_PREFIX { \ - auto _thread_ = std::this_thread::get_id(); \ - int _tid_ = thread_name_map[_thread_]; \ - if (_tid_==0) \ - _tid_ = thread_name_map[_thread_] = ++next_thread_num; \ - auto _utime_ = duration_cast(system_clock::now().time_since_epoch()).count() % 1000000000; \ - cerr << setfill('0') << dec << left << setw(9) << _utime_ << " "; \ - cerr << setfill(' ') << log_thread_prefix << left << setw(2) << _tid_; \ - cerr << " " << setw(20) << __FILE__ << " " << setw(18) << __func__; \ -} -#define LOG_THIS_POINTER { \ - int _self_ = instance_name_map[this]; \ - if (_self_==0) \ - _self_ = instance_name_map[this] = ++next_instance_num; \ - cerr << "#" << setw(2) << _self_; \ -} - -#define LOG_NONMEMBER(_x_) { \ - lock _cel_(cerr_mutex); \ - LOG_PREFIX; \ - cerr << _x_ << endl; \ -} -#define LOG(_x_) { \ - lock _cel_(cerr_mutex); \ - LOG_PREFIX; \ - LOG_THIS_POINTER; \ - cerr << _x_ << endl; \ -} +#define IF_LEVEL(level) if(_log_debug_level() >= abs(level)) #endif