Cleanups
[invirt/third/libt4.git] / tprintf.h
index c61626a..41539fe 100644 (file)
--- a/tprintf.h
+++ b/tprintf.h
@@ -14,44 +14,67 @@ extern std::map<void *, int> instance_name_map;
 extern int next_instance_num;
 extern char tprintf_thread_prefix;
 
+template <class A>
+struct iterator_pair : public std::pair<A, A> {
+    explicit iterator_pair(const A & first, const A & second) : std::pair<A, A>(first, second) {}
+};
+
+template <class A>
+const struct iterator_pair<A> make_iterator_pair(const A & first, const A & second) {
+    return iterator_pair<A>(first, second);
+}
+
+template <class A, class B>
+std::ostream & operator<<(std::ostream &o, const std::pair<A,B> &d) {
+    o << "<" << d.first << "," << d.second << ">";
+    return o;
+}
+
+template <class A>
+std::ostream & operator<<(std::ostream &o, const iterator_pair<A> &d) {
+    o << "[";
+    for (auto i=d.first; i!=d.second; i++) {
+        o << *i;
+        auto j(i);
+        if (++j != d.second)
+            o << ", ";
+    }
+    o << "]";
+    return o;
+}
+
 #define LOG_PREFIX { \
     cerr_mutex.lock(); \
-    auto self = std::this_thread::get_id(); \
-    int tid = thread_name_map[self]; \
-    if (tid==0) \
-        tid = thread_name_map[self] = ++next_thread_num; \
-    auto utime = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000000000; \
-    std::cerr << std::left << std::setw(9) << utime << " "; \
-    std::cerr << tprintf_thread_prefix << std::left << std::setw(2) << tid; \
-    std::cerr << " " << std::setw(24) << __FILE__ << " " << std::setw(18) << __func__; \
+    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_ = std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::system_clock::now().time_since_epoch()).count() % 1000000000; \
+    std::cerr << std::setfill('0') << std::dec << std::left << std::setw(9) << _utime_ << " "; \
+    std::cerr << tprintf_thread_prefix << std::left << std::setw(2) << _tid_; \
+    std::cerr << " " << std::setw(20) << __FILE__ << " " << std::setw(18) << __func__; \
 }
 #define LOG_THIS_POINTER { \
-    int self = instance_name_map[this]; \
-    if (self==0) \
-        self = instance_name_map[this] = ++next_instance_num; \
-    std::cerr << "#" << std::setw(2) << self; \
+    int _self_ = instance_name_map[this]; \
+    if (_self_==0) \
+        _self_ = instance_name_map[this] = ++next_instance_num; \
+    std::cerr << "#" << std::setw(2) << _self_; \
 }
 #define LOG_SUFFIX { \
     cerr_mutex.unlock(); \
 }
 
-#define LOG_NONMEMBER(x) { \
+#define LOG_NONMEMBER(_x_) { \
     LOG_PREFIX; \
-    std::cerr << x << std::endl; \
+    std::cerr << _x_ << std::endl; \
     LOG_SUFFIX; \
 }
-#define LOG(x) { \
+#define LOG(_x_) { \
     LOG_PREFIX; \
     LOG_THIS_POINTER; \
-    std::cerr << x << std::endl; \
+    std::cerr << _x_ << std::endl; \
     LOG_SUFFIX; \
 }
-#define JOIN(from,to,sep) ({ \
-    ostringstream oss; \
-    for(auto i=from;i!=to;i++) \
-        oss << *i << sep; \
-    oss.str(); \
-})
 #define LOG_FUNC_ENTER { \
     LOG_PREFIX; \
     LOG_THIS_POINTER; \
@@ -75,14 +98,13 @@ extern char tprintf_thread_prefix;
     LOG_SUFFIX; \
 }
 
-#define tprintf(args...) { \
-    int len = snprintf(NULL, 0, args); \
-    char buf[len+1]; \
-    buf[len] = '\0'; \
-    snprintf(buf, len+1, args); \
+#define tprintf(...) { \
+    char *buf = nullptr; \
+    int len = asprintf(&buf, __VA_ARGS__); \
     if (buf[len-1]=='\n') \
         buf[len-1] = '\0'; \
     LOG_NONMEMBER(buf); \
+    free(buf); \
 }
 
 #endif