/* * Copyright (C) 2014 Cloudius Systems, Ltd. */ #ifndef PRINT_HH_ #define PRINT_HH_ #include #include #include #include inline void apply_format(boost::format& fmt) { } template inline void apply_format(boost::format& fmt, A0&& a0, Arest&&... arest) { apply_format(fmt % std::forward(a0), std::forward(arest)...); } template std::ostream& fprint(std::ostream& os, boost::format& fmt, A&&... a) { apply_format(fmt, std::forward(a)...); return os << fmt; } template void print(boost::format& fmt, A&&... a) { fprint(std::cout, fmt, std::forward(a)...); } template std::ostream& fprint(std::ostream& os, const char* fmt, A&&... a) { boost::format bfmt(fmt); return fprint(os, bfmt, std::forward(a)...); } template void print(const char* fmt, A&&... a) { boost::format bfmt(fmt); return print(bfmt, std::forward(a)...); } template std::string sprint(const char* fmt, A&&... a) { boost::format bfmt(fmt); apply_format(bfmt, std::forward(a)...); return bfmt.str(); } template std::string format_separated(Iterator b, Iterator e, const char* sep = ", ") { std::string ret; if (b == e) { return ret; } ret += *b++; while (b != e) { ret += sep; ret += *b++; } return ret; } template struct usecfmt_wrapper { TimePoint val; }; template inline usecfmt_wrapper usecfmt(TimePoint tp) { return { tp }; }; template std::ostream& operator<<(std::ostream& os, usecfmt_wrapper>> tp) { auto usec = std::chrono::duration_cast(tp.val.time_since_epoch()).count(); std::ostream tmp(os.rdbuf()); tmp << std::setw(12) << (usec / 1000000) << "." << std::setw(6) << std::setfill('0') << (usec % 1000000); return os; } template void log(A&&... a) { std::cout << usecfmt(std::chrono::high_resolution_clock::now()) << " "; print(std::forward(a)...); } #endif /* PRINT_HH_ */