|
|
@ -22,22 +22,32 @@ extern int verbose; |
|
|
|
#define LogError(s, ...) \ |
|
|
|
if ( verbose >= 0 ) { printf("%s, %s(), %d: ", __FILE__, __func__, __LINE__); printf(s, ##__VA_ARGS__); } |
|
|
|
|
|
|
|
enum LoggingLevels { |
|
|
|
LOG_ERROR, |
|
|
|
LOG_INFO, |
|
|
|
LOG_VERBOSE, |
|
|
|
LOG_DEBUG |
|
|
|
}; |
|
|
|
|
|
|
|
/*
|
|
|
|
Imported from Astrant: |
|
|
|
Questions/Suggestions mail nick@astrant.net |
|
|
|
*/ |
|
|
|
|
|
|
|
struct Logger { |
|
|
|
Logger(std::ostream& logging_stream_, std::string prefix_ = std::string("")) |
|
|
|
Logger(LoggingLevels level, std::ostream& logging_stream_, std::string prefix_ = std::string("")) |
|
|
|
: logging_stream(&logging_stream_) |
|
|
|
, prefix(prefix_) |
|
|
|
, level(level) |
|
|
|
{} |
|
|
|
|
|
|
|
void log(std::string what){ |
|
|
|
if (verbose < level) return; |
|
|
|
*logging_stream << get_prefix() << "(" << what << ") took place at (" << boost::posix_time::microsec_clock::local_time() << std::endl; |
|
|
|
} |
|
|
|
|
|
|
|
void start(std::string what){ |
|
|
|
if (verbose < level) return; |
|
|
|
Event e; |
|
|
|
e.start = boost::posix_time::microsec_clock::local_time(); |
|
|
|
e.name = what; |
|
|
@ -53,6 +63,7 @@ struct Logger { |
|
|
|
} |
|
|
|
|
|
|
|
void stop() { |
|
|
|
if (verbose < level) return; |
|
|
|
assert(!event_name_stack.empty()); |
|
|
|
stop(event_name_stack.top()); |
|
|
|
} |
|
|
@ -99,6 +110,8 @@ private: |
|
|
|
return prefix + ": "; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
LoggingLevels level; |
|
|
|
}; |
|
|
|
|
|
|
|
/*
|
|
|
@ -106,12 +119,14 @@ private: |
|
|
|
*/ |
|
|
|
|
|
|
|
struct Progressbar { |
|
|
|
Progressbar(std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "") |
|
|
|
Progressbar(LoggingLevels level, std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "") |
|
|
|
: out(out) |
|
|
|
, begin(begin) |
|
|
|
, prefix(prefix) |
|
|
|
, end(end) |
|
|
|
, level(level) |
|
|
|
{ |
|
|
|
if (verbose < level) return; |
|
|
|
if (begin != "") { |
|
|
|
out << begin << std::endl; |
|
|
|
} |
|
|
@ -120,6 +135,7 @@ struct Progressbar { |
|
|
|
} |
|
|
|
|
|
|
|
~Progressbar(){ |
|
|
|
if (verbose < level) return; |
|
|
|
show(1, 1, '='); |
|
|
|
|
|
|
|
if (end != "") { |
|
|
@ -130,10 +146,11 @@ struct Progressbar { |
|
|
|
} |
|
|
|
|
|
|
|
template <typename T> |
|
|
|
void show(T const & progress, T const & max, char delim = '>'){ |
|
|
|
void show(T const & progress, T const & max, char delim = '>', char filling = '='){ |
|
|
|
if (verbose < level) return; |
|
|
|
out << "\r"; |
|
|
|
|
|
|
|
size_t width = 80; // default terminal size :D
|
|
|
|
size_t width = 79; // default terminal size :D
|
|
|
|
width -= prefix.size(); |
|
|
|
width -= 3; // [, > and ]
|
|
|
|
|
|
|
@ -145,7 +162,7 @@ struct Progressbar { |
|
|
|
double ratio = (double) progress / (double) max; |
|
|
|
size_t length = width * ratio; |
|
|
|
|
|
|
|
std::string fill(length, '='); |
|
|
|
std::string fill(length, filling); |
|
|
|
std::string empty(width - length, ' '); |
|
|
|
out << '[' << fill << delim << empty << ']' << std::flush; |
|
|
|
} |
|
|
@ -155,6 +172,37 @@ private: |
|
|
|
std::string begin; |
|
|
|
std::string prefix; |
|
|
|
std::string end; |
|
|
|
|
|
|
|
LoggingLevels level; |
|
|
|
}; |
|
|
|
|
|
|
|
struct ProgressIndicator : private Progressbar { |
|
|
|
ProgressIndicator(LoggingLevels level, std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "") |
|
|
|
: Progressbar(level, out, prefix, begin, end) |
|
|
|
, progress(0.0) |
|
|
|
, backwards(false) |
|
|
|
{} |
|
|
|
|
|
|
|
void update(double dt = 0.037){ |
|
|
|
if(!backwards){ |
|
|
|
progress += dt; |
|
|
|
if(progress >= 1.0){ |
|
|
|
progress = 1.0; |
|
|
|
backwards = true; |
|
|
|
} |
|
|
|
} else { |
|
|
|
progress -= dt; |
|
|
|
if(progress <= 0.0){ |
|
|
|
progress = 0.0; |
|
|
|
backwards = false; |
|
|
|
} |
|
|
|
} |
|
|
|
show(progress, 1.0, backwards ? '<' : '>', ' '); |
|
|
|
} |
|
|
|
|
|
|
|
private: |
|
|
|
double progress; |
|
|
|
bool backwards; |
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|