Browse Source

some logging shizzle

master
Joshua Moerman 13 years ago
parent
commit
6d3cf83dc0
  1. 6
      Attractor.hpp
  2. 58
      Logger.hpp
  3. 10
      main.cpp
  4. 2
      projectors/Normalizer.cpp

6
Attractor.hpp

@ -37,11 +37,12 @@ public:
} }
void init_range() { void init_range() {
ProgressIndicator p(LOG_VERBOSE, std::cout, "searching for parameters");
for(unsigned int i = 0; i < 1000000; i++) { for(unsigned int i = 0; i < 1000000; i++) {
iterate(); iterate();
if(kernel->convergent() || kernel->divergent()){ if(kernel->convergent() || kernel->divergent()){
kernel->generate_random_parameters(); kernel->generate_random_parameters();
LogDebug("Generating new parameters.\n"); p.update();
i = 0; i = 0;
} }
} }
@ -49,6 +50,9 @@ public:
void iterate() { void iterate() {
(*kernel)(); (*kernel)();
}
void project() {
projector->plot(kernel->vector()); projector->plot(kernel->vector());
} }

58
Logger.hpp

@ -22,22 +22,32 @@ extern int verbose;
#define LogError(s, ...) \ #define LogError(s, ...) \
if ( verbose >= 0 ) { printf("%s, %s(), %d: ", __FILE__, __func__, __LINE__); printf(s, ##__VA_ARGS__); } 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: Imported from Astrant:
Questions/Suggestions mail nick@astrant.net Questions/Suggestions mail nick@astrant.net
*/ */
struct Logger { 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_) : logging_stream(&logging_stream_)
, prefix(prefix_) , prefix(prefix_)
, level(level)
{} {}
void log(std::string what){ 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; *logging_stream << 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;
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;
@ -53,6 +63,7 @@ struct Logger {
} }
void stop() { void stop() {
if (verbose < level) return;
assert(!event_name_stack.empty()); assert(!event_name_stack.empty());
stop(event_name_stack.top()); stop(event_name_stack.top());
} }
@ -99,6 +110,8 @@ private:
return prefix + ": "; return prefix + ": ";
} }
} }
LoggingLevels level;
}; };
/* /*
@ -106,12 +119,14 @@ private:
*/ */
struct Progressbar { 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) : out(out)
, begin(begin) , begin(begin)
, prefix(prefix) , prefix(prefix)
, end(end) , end(end)
, level(level)
{ {
if (verbose < level) return;
if (begin != "") { if (begin != "") {
out << begin << std::endl; out << begin << std::endl;
} }
@ -120,6 +135,7 @@ struct Progressbar {
} }
~Progressbar(){ ~Progressbar(){
if (verbose < level) return;
show(1, 1, '='); show(1, 1, '=');
if (end != "") { if (end != "") {
@ -130,10 +146,11 @@ struct Progressbar {
} }
template <typename T> 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"; out << "\r";
size_t width = 80; // default terminal size :D size_t width = 79; // default terminal size :D
width -= prefix.size(); width -= prefix.size();
width -= 3; // [, > and ] width -= 3; // [, > and ]
@ -145,7 +162,7 @@ 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, '='); std::string fill(length, filling);
std::string empty(width - length, ' '); std::string empty(width - length, ' ');
out << '[' << fill << delim << empty << ']' << std::flush; out << '[' << fill << delim << empty << ']' << std::flush;
} }
@ -155,6 +172,37 @@ private:
std::string begin; std::string begin;
std::string prefix; std::string prefix;
std::string end; 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;
}; };

10
main.cpp

@ -17,7 +17,7 @@
#include <boost/program_options.hpp> #include <boost/program_options.hpp>
namespace po = boost::program_options; namespace po = boost::program_options;
int verbose = 3; int verbose = 4;
std::string generate_filename(){ std::string generate_filename(){
char filename[64]; char filename[64];
@ -29,10 +29,11 @@ std::string generate_filename(){
} }
void render(Attractor & myAttractor, Canvas2D & canvas, unsigned int iterations){ void render(Attractor & myAttractor, Canvas2D & canvas, unsigned int iterations){
Progressbar progress(std::cout, "rendering"); Progressbar progress(LOG_INFO, std::cout, "rendering");
for(unsigned int j = 1; j <= iterations; ++j) { for(unsigned int j = 1; j <= iterations; ++j) {
for(unsigned int i = 0; i < 1000000; ++i) { for(unsigned int i = 0; i < 1000000; ++i) {
myAttractor.iterate(); myAttractor.iterate();
myAttractor.project();
canvas.plot(myAttractor.projector->projectedPoint); canvas.plot(myAttractor.projector->projectedPoint);
} }
progress.show(j, iterations); progress.show(j, iterations);
@ -40,9 +41,6 @@ void render(Attractor & myAttractor, Canvas2D & canvas, unsigned int iterations)
} }
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
LogInfo("Awesome Attractor, version %s\n", __DATE__);
verbose = 0;
std::string attractorFile; std::string attractorFile;
std::string output_path; std::string output_path;
bool generate_random = false; bool generate_random = false;
@ -79,7 +77,7 @@ int main(int argc, char* argv[]) {
std::string filename = output_path + generate_filename(); std::string filename = output_path + generate_filename();
Logger logger(std::cout); Logger logger(LOG_VERBOSE, std::cout);
Canvas2D canvas(width, height); Canvas2D canvas(width, height);
{ {
Attractor* my_attractor_ptr = 0; Attractor* my_attractor_ptr = 0;

2
projectors/Normalizer.cpp

@ -97,8 +97,6 @@ void Normalizer::finish_range() {
double dist = range_max[i] - range_min[i]; double dist = range_max[i] - range_min[i];
if(factor * dist > 1.0) { if(factor * dist > 1.0) {
factor = 1.0 / dist; factor = 1.0 / dist;
//teh_size = canvas->size[i];
LogDebug("Crap for dimension %d\n", i);
} }
} }