Logger is more consistent
This commit is contained in:
parent
c0ac5c4023
commit
620b4c331c
3 changed files with 84 additions and 68 deletions
|
@ -37,12 +37,12 @@ public:
|
|||
}
|
||||
|
||||
void init_range() {
|
||||
ProgressIndicator p(LOG_VERBOSE, std::cout, "searching for parameters");
|
||||
ProgressIndicator p(std::cout, LOG_VERBOSE, "searching for parameters");
|
||||
for(unsigned int i = 0; i < 1000000; i++) {
|
||||
iterate();
|
||||
if(kernel->convergent() || kernel->divergent()){
|
||||
kernel->generate_random_parameters();
|
||||
p.update();
|
||||
p.show();
|
||||
i = 0;
|
||||
}
|
||||
}
|
||||
|
|
135
Logger.hpp
135
Logger.hpp
|
@ -34,27 +34,41 @@ enum LoggingLevels {
|
|||
Questions/Suggestions mail nick@astrant.net
|
||||
*/
|
||||
|
||||
struct Logger {
|
||||
Logger(LoggingLevels level, std::ostream& logging_stream_, std::string prefix_ = std::string(""))
|
||||
: logging_stream(&logging_stream_)
|
||||
, prefix(prefix_)
|
||||
struct LoggingBase {
|
||||
protected:
|
||||
LoggingBase(std::ostream & out, LoggingLevels level)
|
||||
: out(out)
|
||||
, 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){
|
||||
if (verbose < level) return;
|
||||
*logging_stream << get_prefix() << "(" << what << ") took place at (" << boost::posix_time::microsec_clock::local_time() << std::endl;
|
||||
if (shouldSkip()) return;
|
||||
out << get_prefix() << "(" << what << ") took place at (" << boost::posix_time::microsec_clock::local_time() << std::endl;
|
||||
}
|
||||
|
||||
void start(std::string what){
|
||||
if (verbose < level) return;
|
||||
if (shouldSkip()) return;
|
||||
Event e;
|
||||
e.start = boost::posix_time::microsec_clock::local_time();
|
||||
e.name = what;
|
||||
|
||||
EventMap::iterator it = event_map.find(what);
|
||||
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 {
|
||||
event_name_stack.push(what);
|
||||
}
|
||||
|
@ -63,7 +77,7 @@ struct Logger {
|
|||
}
|
||||
|
||||
void stop() {
|
||||
if (verbose < level) return;
|
||||
if (shouldSkip()) return;
|
||||
assert(!event_name_stack.empty());
|
||||
stop(event_name_stack.top());
|
||||
}
|
||||
|
@ -72,7 +86,7 @@ private:
|
|||
void stop(std::string what){
|
||||
EventMap::iterator it = event_map.find(what);
|
||||
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 {
|
||||
it->second.end = boost::posix_time::microsec_clock::local_time();
|
||||
log_event(it->second);
|
||||
|
@ -95,12 +109,10 @@ private:
|
|||
// A stack used by 0-parameter stop() to stop the last start()ed event.
|
||||
std::stack<std::string> event_name_stack;
|
||||
|
||||
std::ostream* logging_stream;
|
||||
|
||||
std::string prefix;
|
||||
|
||||
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 {
|
||||
|
@ -110,44 +122,30 @@ private:
|
|||
return prefix + ": ";
|
||||
}
|
||||
}
|
||||
|
||||
LoggingLevels level;
|
||||
};
|
||||
|
||||
/*
|
||||
My progressbar class (as seen on github)
|
||||
*/
|
||||
|
||||
struct Progressbar {
|
||||
Progressbar(LoggingLevels level, std::ostream & out, std::string prefix = "", std::string begin = "", std::string end = "")
|
||||
: out(out)
|
||||
, begin(begin)
|
||||
struct Progressbar : public LoggingBase {
|
||||
Progressbar(std::ostream & out, LoggingLevels level, std::string prefix = "")
|
||||
: LoggingBase(out, level)
|
||||
, prefix(prefix)
|
||||
, end(end)
|
||||
, level(level)
|
||||
{
|
||||
if (verbose < level) return;
|
||||
if (begin != "") {
|
||||
out << begin << std::endl;
|
||||
}
|
||||
|
||||
if (shouldSkip()) return;
|
||||
show(0, 1, ' ');
|
||||
}
|
||||
|
||||
~Progressbar(){
|
||||
if (verbose < level) return;
|
||||
if (shouldSkip()) return;
|
||||
show(1, 1, '=');
|
||||
|
||||
if (end != "") {
|
||||
out << "\n" << end << std::endl;
|
||||
} else {
|
||||
out << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template <typename T>
|
||||
void show(T const & progress, T const & max, char delim = '>', char filling = '='){
|
||||
if (verbose < level) return;
|
||||
void show(T const & progress, T const & max, char delim = '>'){
|
||||
if (shouldSkip()) return;
|
||||
out << "\r";
|
||||
|
||||
size_t width = 79; // default terminal size :D
|
||||
|
@ -162,47 +160,64 @@ struct Progressbar {
|
|||
double ratio = (double) progress / (double) max;
|
||||
size_t length = width * ratio;
|
||||
|
||||
std::string fill(length, filling);
|
||||
std::string fill(length, '=');
|
||||
std::string empty(width - length, ' ');
|
||||
out << '[' << fill << delim << empty << ']' << std::flush;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream & out;
|
||||
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)
|
||||
{}
|
||||
struct ProgressIndicator : public LoggingBase {
|
||||
ProgressIndicator(std::ostream & out, LoggingLevels level, std::string prefix = "")
|
||||
: LoggingBase(out, level)
|
||||
, prefix(prefix)
|
||||
, 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){
|
||||
if(!backwards){
|
||||
progress += dt;
|
||||
if(progress >= 1.0){
|
||||
progress = 1.0;
|
||||
backwards = true;
|
||||
~ProgressIndicator(){
|
||||
show("=");
|
||||
out << std::endl;
|
||||
}
|
||||
} else {
|
||||
progress -= dt;
|
||||
if(progress <= 0.0){
|
||||
progress = 0.0;
|
||||
backwards = false;
|
||||
|
||||
void show(std::string const & alt_wave = ""){
|
||||
if (shouldSkip()) return;
|
||||
|
||||
boost::posix_time::ptime now = boost::posix_time::microsec_clock::local_time();
|
||||
if(now - previous_time < boost::posix_time::milliseconds(50.0)) return;
|
||||
|
||||
out << "\r";
|
||||
|
||||
size_t width = 79; // default terminal size :D
|
||||
width -= 2;
|
||||
if(prefix != ""){
|
||||
width -= prefix.size() + 1;
|
||||
out << prefix << ' ';
|
||||
}
|
||||
|
||||
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()];
|
||||
}
|
||||
show(progress, 1.0, backwards ? '<' : '>', ' ');
|
||||
out << ']' << std::flush;
|
||||
|
||||
previous_time = now;
|
||||
++progress;
|
||||
if(progress >= wave.size())
|
||||
progress = 0;
|
||||
}
|
||||
|
||||
private:
|
||||
double progress;
|
||||
bool backwards;
|
||||
std::string prefix;
|
||||
std::string wave;
|
||||
boost::posix_time::ptime previous_time;
|
||||
unsigned int progress;
|
||||
};
|
||||
|
||||
|
||||
|
|
7
main.cpp
7
main.cpp
|
@ -28,8 +28,9 @@ std::string generate_filename(){
|
|||
return std::string(filename);
|
||||
}
|
||||
|
||||
void render(Attractor & myAttractor, Canvas2D & canvas, unsigned int iterations){
|
||||
Progressbar progress(LOG_INFO, std::cout, "rendering");
|
||||
template <typename C>
|
||||
void render(Attractor & myAttractor, C & canvas, unsigned int iterations){
|
||||
Progressbar progress(std::cout, LOG_INFO, "rendering");
|
||||
for(unsigned int j = 1; j <= iterations; ++j) {
|
||||
for(unsigned int i = 0; i < 1000000; ++i) {
|
||||
myAttractor.iterate();
|
||||
|
@ -66,8 +67,8 @@ int main(int argc, char* argv[]) try {
|
|||
|
||||
std::string filename = output_path + generate_filename();
|
||||
|
||||
Logger logger(LOG_VERBOSE, std::cout);
|
||||
Canvas2D canvas(width, height);
|
||||
Logger logger(std::cout, LOG_VERBOSE);
|
||||
{
|
||||
Attractor my_attractor(attractorFile);
|
||||
|
||||
|
|
Reference in a new issue