Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

121 lines (100 sloc) 3.106 kb
#undef TIP
#include <ctime>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <ostream>
#include <set>
#include <sstream>
#include <string>
#include <vector>
namespace tip {
template<class InputIterator> void print_range(InputIterator first, InputIterator last, std::ostream& stream) {
stream << "(";
for (InputIterator iit = first; iit != last; ++iit)
stream << ((iit != first) ? ", " : "") << *iit;
stream << ")";
}
template<class F, class S> std::ostream& operator << (std::ostream& out, const std::pair<F, S>& p) {
return out << "<" << p.first << " : " << p.second << ">";
}
template<class T> std::ostream& operator << (std::ostream& out, const std::vector<T>& v) {
out << "v";
print_range(v.begin(), v.end(), out);
return out;
}
template<class T> std::ostream& operator << (std::ostream& out, const std::list<T>& l) {
out << "l";
print_range(l.begin(), l.end(), out);
return out;
}
template<class T> std::ostream& operator << (std::ostream& out, const std::set<T>& s) {
out << "s";
print_range(s.begin(), s.end(), out);
return out;
}
template<class T1, class T2> std::ostream& operator << (std::ostream& out, const std::map<T1, T2>& m) {
out << "m";
print_range(m.begin(), m.end(), out);
return out;
}
class Log {
private:
std::ostringstream stream_;
bool finished_;
Log();
static double clock(void) {
timespec res;
clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &res);
return res.tv_sec * 1.e-0 + res.tv_nsec * 1.e-9;
}
public:
Log(int line, const char* function) {
stream_ << std::fixed << std::setprecision(3) << clock() << " ";
stream_ << function << "[" << line << "]";
finished_ = false;
}
~Log() {
stream_ << std::endl;
std::cerr << stream_.str() << std::flush;
}
Log& value() {
return *this;
}
/*
template<class T, int N> Log& value(const T (&v)[N]) {
if (!finished_) {
stream_ << "a";
print_range(&v[0], &v[N], stream_);
}
return *this;
}
*/
template<class T> Log& value(const T& v) {
if (!finished_) stream_ << v;
return *this;
}
Log& name(const std::string& n) {
if (n.empty())
finished_ = true;
if (!finished_) {
stream_ << ", ";
if (n[0] != '"')
stream_ << n << " = ";
}
return *this;
}
};
}; // end of namespace tip
#define TIP_0(x, ...) .name(#x).value(x)
#define TIP_1(x, ...) .name(#x).value(x) TIP_0(__VA_ARGS__)
#define TIP_2(x, ...) .name(#x).value(x) TIP_1(__VA_ARGS__)
#define TIP_3(x, ...) .name(#x).value(x) TIP_2(__VA_ARGS__)
#define TIP_4(x, ...) .name(#x).value(x) TIP_3(__VA_ARGS__)
#define TIP_5(x, ...) .name(#x).value(x) TIP_4(__VA_ARGS__)
#define TIP_6(x, ...) .name(#x).value(x) TIP_5(__VA_ARGS__)
#define TIP_7(x, ...) .name(#x).value(x) TIP_6(__VA_ARGS__)
#define TIP_8(x, ...) .name(#x).value(x) TIP_7(__VA_ARGS__)
#define TIP_9(x, ...) .name(#x).value(x) TIP_8(__VA_ARGS__)
#define TIP(...) (tip::Log(__LINE__, __FUNCTION__) TIP_9(__VA_ARGS__))
Jump to Line
Something went wrong with that request. Please try again.