|
@ -34,27 +34,41 @@ enum LoggingLevels { |
|
|
Questions/Suggestions mail nick@astrant.net |
|
|
Questions/Suggestions mail nick@astrant.net |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
struct Logger { |
|
|
struct LoggingBase { |
|
|
Logger(LoggingLevels level, std::ostream& logging_stream_, std::string prefix_ = std::string("")) |
|
|
protected: |
|
|
: logging_stream(&logging_stream_) |
|
|
LoggingBase(std::ostream & out, LoggingLevels level) |
|
|
, prefix(prefix_) |
|
|
: out(out) |
|
|
, level(level) |
|
|
, level(level) |
|
|
{} |
|
|
{} |
|
|
|
|
|
|
|
|
|
|
|
bool shouldSkip(){ |
|
|
|
|
|
return verbose < level; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
std::ostream & out; |
|
|
|
|
|
LoggingLevels level; |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
struct Logger : public LoggingBase { |
|
|
|
|
|
Logger(std::ostream& logging_stream_, LoggingLevels level, std::string prefix_ = "") |
|
|
|
|
|
: LoggingBase(logging_stream_, level) |
|
|
|
|
|
, prefix(prefix_) |
|
|
|
|
|
{} |
|
|
|
|
|
|
|
|
void log(std::string what){ |
|
|
void log(std::string what){ |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
*logging_stream << get_prefix() << "(" << what << ") took place at (" << boost::posix_time::microsec_clock::local_time() << std::endl; |
|
|
out << get_prefix() << "(" << what << ") took place at (" << boost::posix_time::microsec_clock::local_time() << std::endl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void start(std::string what){ |
|
|
void start(std::string what){ |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
Event e; |
|
|
Event e; |
|
|
e.start = boost::posix_time::microsec_clock::local_time(); |
|
|
e.start = boost::posix_time::microsec_clock::local_time(); |
|
|
e.name = what; |
|
|
e.name = what; |
|
|
|
|
|
|
|
|
EventMap::iterator it = event_map.find(what); |
|
|
EventMap::iterator it = event_map.find(what); |
|
|
if(it != event_map.end()){ |
|
|
if(it != event_map.end()){ |
|
|
*logging_stream << get_prefix() << "WARNING: Overwriting event(" << e.name << "), did you forget to call stop()?"; |
|
|
out << get_prefix() << "WARNING: Overwriting event(" << e.name << "), did you forget to call stop()?"; |
|
|
} else { |
|
|
} else { |
|
|
event_name_stack.push(what); |
|
|
event_name_stack.push(what); |
|
|
} |
|
|
} |
|
@ -63,7 +77,7 @@ struct Logger { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
void stop() { |
|
|
void stop() { |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
assert(!event_name_stack.empty()); |
|
|
assert(!event_name_stack.empty()); |
|
|
stop(event_name_stack.top()); |
|
|
stop(event_name_stack.top()); |
|
|
} |
|
|
} |
|
@ -72,7 +86,7 @@ private: |
|
|
void stop(std::string what){ |
|
|
void stop(std::string what){ |
|
|
EventMap::iterator it = event_map.find(what); |
|
|
EventMap::iterator it = event_map.find(what); |
|
|
if(it == event_map.end()){ |
|
|
if(it == event_map.end()){ |
|
|
*logging_stream << get_prefix() << "WARNING: No such, or already stopped, event(" << what << "), did you forget to call start()?"; |
|
|
out << get_prefix() << "WARNING: No such, or already stopped, event(" << what << "), did you forget to call start()?"; |
|
|
} else { |
|
|
} else { |
|
|
it->second.end = boost::posix_time::microsec_clock::local_time(); |
|
|
it->second.end = boost::posix_time::microsec_clock::local_time(); |
|
|
log_event(it->second); |
|
|
log_event(it->second); |
|
@ -95,12 +109,10 @@ private: |
|
|
// A stack used by 0-parameter stop() to stop the last start()ed event.
|
|
|
// A stack used by 0-parameter stop() to stop the last start()ed event.
|
|
|
std::stack<std::string> event_name_stack; |
|
|
std::stack<std::string> event_name_stack; |
|
|
|
|
|
|
|
|
std::ostream* logging_stream; |
|
|
|
|
|
|
|
|
|
|
|
std::string prefix; |
|
|
std::string prefix; |
|
|
|
|
|
|
|
|
void log_event(Event const& e){ |
|
|
void log_event(Event const& e){ |
|
|
*logging_stream << get_prefix() << "Event(" << e.name << ") started at (" << e.start << ") ended at (" << e.end << ") duration (" << e.end - e.start << " ms) " << std::endl; |
|
|
out << get_prefix() << "Event(" << e.name << ") started at (" << e.start << ") ended at (" << e.end << ") duration (" << e.end - e.start << " ms) " << std::endl; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
std::string get_prefix() const { |
|
|
std::string get_prefix() const { |
|
@ -110,44 +122,30 @@ private: |
|
|
return prefix + ": "; |
|
|
return prefix + ": "; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
LoggingLevels level; |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
/*
|
|
|
/*
|
|
|
My progressbar class (as seen on github) |
|
|
My progressbar class (as seen on github) |
|
|
*/ |
|
|
*/ |
|
|
|
|
|
|
|
|
struct Progressbar { |
|
|
struct Progressbar : public LoggingBase { |
|
|
Progressbar(LoggingLevels level, std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "") |
|
|
Progressbar(std::ostream & out, LoggingLevels level, std::string prefix = "") |
|
|
: out(out) |
|
|
: LoggingBase(out, level) |
|
|
, begin(begin) |
|
|
|
|
|
, prefix(prefix) |
|
|
, prefix(prefix) |
|
|
, end(end) |
|
|
|
|
|
, level(level) |
|
|
|
|
|
{ |
|
|
{ |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
if (begin != "") { |
|
|
|
|
|
out << begin << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
show(0, 1, ' '); |
|
|
show(0, 1, ' '); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
~Progressbar(){ |
|
|
~Progressbar(){ |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
show(1, 1, '='); |
|
|
show(1, 1, '='); |
|
|
|
|
|
out << std::endl; |
|
|
if (end != "") { |
|
|
|
|
|
out << "\n" << end << std::endl; |
|
|
|
|
|
} else { |
|
|
|
|
|
out << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
template <typename T> |
|
|
template <typename T> |
|
|
void show(T const & progress, T const & max, char delim = '>', char filling = '='){ |
|
|
void show(T const & progress, T const & max, char delim = '>'){ |
|
|
if (verbose < level) return; |
|
|
if (shouldSkip()) return; |
|
|
out << "\r"; |
|
|
out << "\r"; |
|
|
|
|
|
|
|
|
size_t width = 79; // default terminal size :D
|
|
|
size_t width = 79; // default terminal size :D
|
|
@ -162,47 +160,64 @@ struct Progressbar { |
|
|
double ratio = (double) progress / (double) max; |
|
|
double ratio = (double) progress / (double) max; |
|
|
size_t length = width * ratio; |
|
|
size_t length = width * ratio; |
|
|
|
|
|
|
|
|
std::string fill(length, filling); |
|
|
std::string fill(length, '='); |
|
|
std::string empty(width - length, ' '); |
|
|
std::string empty(width - length, ' '); |
|
|
out << '[' << fill << delim << empty << ']' << std::flush; |
|
|
out << '[' << fill << delim << empty << ']' << std::flush; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
|
std::ostream & out; |
|
|
|
|
|
std::string begin; |
|
|
|
|
|
std::string prefix; |
|
|
std::string prefix; |
|
|
std::string end; |
|
|
|
|
|
|
|
|
|
|
|
LoggingLevels level; |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
struct ProgressIndicator : private Progressbar { |
|
|
struct ProgressIndicator : public LoggingBase { |
|
|
ProgressIndicator(LoggingLevels level, std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "") |
|
|
ProgressIndicator(std::ostream & out, LoggingLevels level, std::string prefix = "") |
|
|
: Progressbar(level, out, prefix, begin, end) |
|
|
: LoggingBase(out, level) |
|
|
, progress(0.0) |
|
|
, prefix(prefix) |
|
|
, backwards(false) |
|
|
, wave("`*-,_,-*`") |
|
|
{} |
|
|
, previous_time(boost::posix_time::microsec_clock::local_time() - boost::posix_time::seconds(1.0)) |
|
|
|
|
|
, progress(0) |
|
|
|
|
|
{ |
|
|
|
|
|
show(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void update(double dt = 0.037){ |
|
|
~ProgressIndicator(){ |
|
|
if(!backwards){ |
|
|
show("="); |
|
|
progress += dt; |
|
|
out << std::endl; |
|
|
if(progress >= 1.0){ |
|
|
} |
|
|
progress = 1.0; |
|
|
|
|
|
backwards = true; |
|
|
void show(std::string const & alt_wave = ""){ |
|
|
} |
|
|
if (shouldSkip()) return; |
|
|
} else { |
|
|
|
|
|
progress -= dt; |
|
|
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time(); |
|
|
if(progress <= 0.0){ |
|
|
if(now - previous_time < boost::posix_time::milliseconds(50.0)) return; |
|
|
progress = 0.0; |
|
|
|
|
|
backwards = false; |
|
|
out << "\r"; |
|
|
} |
|
|
|
|
|
|
|
|
size_t width = 79; // default terminal size :D
|
|
|
|
|
|
width -= 2; |
|
|
|
|
|
if(prefix != ""){ |
|
|
|
|
|
width -= prefix.size() + 1; |
|
|
|
|
|
out << prefix << ' '; |
|
|
} |
|
|
} |
|
|
show(progress, 1.0, backwards ? '<' : '>', ' '); |
|
|
|
|
|
|
|
|
std::string const & wave_to_use = (alt_wave == "") ? wave : alt_wave; |
|
|
|
|
|
out << '['; |
|
|
|
|
|
for(unsigned int i = 0; i < width; ++i){ |
|
|
|
|
|
out << wave_to_use[(i + progress) % wave_to_use.size()]; |
|
|
|
|
|
} |
|
|
|
|
|
out << ']' << std::flush; |
|
|
|
|
|
|
|
|
|
|
|
previous_time = now; |
|
|
|
|
|
++progress; |
|
|
|
|
|
if(progress >= wave.size()) |
|
|
|
|
|
progress = 0; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private: |
|
|
private: |
|
|
double progress; |
|
|
std::string prefix; |
|
|
bool backwards; |
|
|
std::string wave; |
|
|
|
|
|
boost::posix_time::ptime previous_time; |
|
|
|
|
|
unsigned int progress; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|