Browse Source

Added a logger, so one can choose from verbose/quiet mode etc

master
Joshua Moerman 14 years ago
parent
commit
941f6c6c2b
  1. 8
      Attractor.cpp
  2. 24
      AttractorKernel.cpp
  3. 38
      Canvas.cpp
  4. 21
      Logger.hpp
  5. 12
      Projector.cpp
  6. 46
      main.cpp

8
Attractor.cpp

@ -1,6 +1,6 @@
#include "Attractor.hpp" #include "Attractor.hpp"
#include <iostream> #include "Logger.hpp"
#include <fstream> #include <fstream>
#include <cstdlib> #include <cstdlib>
#include <string> #include <string>
@ -12,7 +12,7 @@
Attractor::Attractor(const std::string& filename) { Attractor::Attractor(const std::string& filename) {
// opening file // opening file
std::cout << "Reading file " << filename << "..." << std::endl; LogInfo("Reading file '%s'...\n", filename.c_str());
stfu::node system; stfu::node system;
system.read(filename.c_str()); system.read(filename.c_str());
@ -102,7 +102,7 @@ void Attractor::output() {
const double * point = myAttractor->vector(); const double * point = myAttractor->vector();
for ( unsigned int i = 0; i < dimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
std::cout << point[i] << " "; LogMoreInfo("%f, ", point[i]);
} }
std::cout << std::endl; LogMoreInfo("\n");
} }

24
AttractorKernel.cpp

@ -8,8 +8,8 @@
*/ */
#include "AttractorKernel.hpp" #include "AttractorKernel.hpp"
#include "Logger.hpp"
#include <algorithm> #include <algorithm>
#include <iostream>
#pragma mark - #pragma mark -
#pragma mark memory #pragma mark memory
@ -21,7 +21,7 @@ numberOfParameters(numberOfParameters), dimension(dimension){
allocate(); allocate();
} }
catch (std::exception& e) { catch (std::exception& e) {
std::cout << "Couldn't construct AttractorKernel: " << e.what() << std::endl; LogError("Couldn't construct Attractorkernel: %s\n", e.what());
dealloc(); dealloc();
} }
@ -105,8 +105,8 @@ AttractorKernel * AttractorKernel::createAttractorKernel(stfu::node& attractor){
// } // }
const unsigned int dimension = atoi(attractorDimension.c_str()); const unsigned int dimension = atoi(attractorDimension.c_str());
std::cout << " Formula: " << attractorType << std::endl; LogMoreInfo(" Formula: %s\n", attractorType.c_str());
std::cout << " Dimensions: " << dimension << std::endl; LogMoreInfo(" Dimensions: %d\n", dimension);
// depending on type, make the formula object // depending on type, make the formula object
@ -114,20 +114,20 @@ AttractorKernel * AttractorKernel::createAttractorKernel(stfu::node& attractor){
if ( dimension == 3 ) { if ( dimension == 3 ) {
myAttractor = new Lorenz3D(); myAttractor = new Lorenz3D();
} else { } else {
std::cerr << "something wrong"; LogError("something wrong\n");
exit(37); exit(37);
} }
} else if ( attractorType == "polynomial" ) { } else if ( attractorType == "polynomial" ) {
const std::string attractorOrde = attractor.getValue("orde"); const std::string attractorOrde = attractor.getValue("orde");
const unsigned int orde = atoi(attractorOrde.c_str()); const unsigned int orde = atoi(attractorOrde.c_str());
std::cout << " Orde: " << orde << std::endl; LogMoreInfo(" Orde: %d\n", orde);
myAttractor = new Polynomial(dimension, orde); myAttractor = new Polynomial(dimension, orde);
} else if ( attractorType == "polynomial a" ) { } else if ( attractorType == "polynomial a" ) {
if ( dimension == 3 ) { if ( dimension == 3 ) {
myAttractor = new PolynomialA3D(); myAttractor = new PolynomialA3D();
} else { } else {
std::cerr << "something wrong"; LogError("something wrong\n");
exit(37); exit(37);
} }
} else if ( attractorType == "logistic" ) { } else if ( attractorType == "logistic" ) {
@ -136,12 +136,12 @@ AttractorKernel * AttractorKernel::createAttractorKernel(stfu::node& attractor){
if ( dimension == 3 ) { if ( dimension == 3 ) {
myAttractor = new Unravel3D(); myAttractor = new Unravel3D();
} else { } else {
std::cerr << "something wrong"; LogError("something wrong\n");
exit(37); exit(37);
} }
} else { } else {
std::cout << "'" << attractorType << "' not recognized" << std::endl; LogError("'%s' not recognized\n", attractorType.c_str());
exit(3); exit(37);
} }
@ -151,10 +151,10 @@ AttractorKernel * AttractorKernel::createAttractorKernel(stfu::node& attractor){
for ( unsigned int i = 0; i < numberOfParameters; i++ ) { for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
stfu::node attractorParameters = attractor.getChild("parameters"); stfu::node attractorParameters = attractor.getChild("parameters");
(*myAttractor)[i] = atof(attractorParameters.getValue(i).c_str()); (*myAttractor)[i] = atof(attractorParameters.getValue(i).c_str());
std::cout << " Parameter " << i << " set to " << (*myAttractor)[i] << ", "; LogMoreInfo(" Parameter %d set to %f, ", i, (*myAttractor)[i]);
} }
std::cout << std::endl << " Reading file complete" << std::endl; LogMoreInfo("\n Reading file complete\n");
return myAttractor; return myAttractor;
} }

38
Canvas.cpp

@ -1,4 +1,4 @@
#include <iostream> #include "Logger.hpp"
#include <fstream> #include <fstream>
#include <cmath> #include <cmath>
#include <cstdlib> #include <cstdlib>
@ -20,10 +20,8 @@ Canvas::Canvas(unsigned int width, unsigned int height, unsigned int num_colors)
assert(int_array != NULL); assert(int_array != NULL);
clear(); clear();
#ifdef HARDDEBUG LogDebug("New Canvas\n");
cout << "New canvas" << endl;
#endif
} }
void Canvas::clear() { void Canvas::clear() {
@ -98,9 +96,8 @@ void Canvas::plot(double x, double y, unsigned int c, double intensity) {
/* /*
I/O functions I/O functions
*/ */
void Canvas::output() { void Canvas::output() {
std::cout << "Canvas: " << std::endl; LogMoreInfo("Canvas: \n Dimensions: %d x %d x %d\n", width, height, num_colors);
std::cout << "Dimensions: " << width << " x " << height << " x " << num_colors << std::endl;
} }
void Canvas::output_file(const char * filename){ void Canvas::output_file(const char * filename){
@ -131,8 +128,8 @@ void Canvas::output_file(const char * filename){
power[i] = 1; power[i] = 1;
} }
if ( n <= 10 ) { if ( n <= 10 ) {
std::cout << "not enough data" << std::endl; LogInfo("not enough data\n");
} }
} }
@ -182,17 +179,16 @@ void Canvas::output_file(const char * filename){
delete[] max_int; delete[] max_int;
delete[] power; delete[] power;
std::cout << "ready for writing file i suppose: " << filename << std::endl; LogInfo("Writing %s\n", filename);
std::ofstream file(filename); std::ofstream file(filename);
if ( !file ) { if ( !file ) {
std::cout << "jij hebt pech, geen png voor jou" << std::endl; LogError("Couldn't write to file");
} }
std::cout << filename << std::endl; pngFile->close();
LogMoreInfo("File written");
pngFile->close();
} }
@ -230,16 +226,16 @@ void Canvas::output_raw(){
void Canvas::input_raw(const char * filename){ void Canvas::input_raw(const char * filename){
std::ifstream infile(filename, std::ifstream::binary); std::ifstream infile(filename, std::ifstream::binary);
if ( ! infile ) { if ( ! infile ) {
std::cout << "poep" << std::endl; LogError("Could not read file %s", filename);
return; return;
} }
infile.seekg (0, std::ios::end); infile.seekg (0, std::ios::end);
int length = infile.tellg(); int length = infile.tellg();
infile.seekg (0, std::ios::beg); infile.seekg (0, std::ios::beg);
std::cout << "length: " << length << " =? " << static_cast<int>(width*height*num_colors*sizeof(unsigned int)) << std::endl; LogDebug("Length: %d =? %d\n", length, static_cast<int>(width*height*num_colors*sizeof(unsigned int)));
infile.read (reinterpret_cast<char*>(int_array), sizeof (unsigned int)*width*height*num_colors); infile.read (reinterpret_cast<char*>(int_array), sizeof (unsigned int)*width*height*num_colors);
} }

21
Logger.hpp

@ -0,0 +1,21 @@
#ifndef LOGGER_HPP_INCLUDED
#define LOGGER_HPP_INCLUDED
#include <cstdio>
extern int verbose;
#define LogDebug(s, ...) \
if ( verbose >= 3 ) printf(s, ##__VA_ARGS__);
#define LogMoreInfo(s, ...) \
if ( verbose >= 2 ) printf(s, ##__VA_ARGS__);
#define LogInfo(s, ...) \
if ( verbose >= 1 ) printf(s, ##__VA_ARGS__);
#define LogError(s, ...) \
if ( verbose >= 0 ) { printf("%s, %d: ", __FILE__, __LINE__); printf(s, ##__VA_ARGS__); }
#endif // LOGGER_HPP_INCLUDED

12
Projector.cpp

@ -1,4 +1,4 @@
#include <iostream> #include "Logger.hpp"
#include <cmath> #include <cmath>
#include <cassert> #include <cassert>
@ -64,7 +64,7 @@ void Projector::finish_range() {
if ( factor * dist > (double)canvas->size[i] ) { if ( factor * dist > (double)canvas->size[i] ) {
factor = (double)canvas->size[i] / dist; factor = (double)canvas->size[i] / dist;
//teh_size = canvas->size[i]; //teh_size = canvas->size[i];
std::cout << "crap for dim" << i << std::endl; LogDebug("Crap for dimension %d\n", i);
} }
} }
@ -89,8 +89,6 @@ void Projector::plot(const double * point) {
const double x = project_point[0]*factor + offset[0]; const double x = project_point[0]*factor + offset[0];
const double y = project_point[1]*factor + offset[1]; const double y = project_point[1]*factor + offset[1];
//cout << x << ", " << y << endl;
canvas->plot(x, y); canvas->plot(x, y);
if ( even(point[2]*17) ) if ( even(point[2]*17) )
canvas->plot(x, y, 1); canvas->plot(x, y, 1);
@ -100,10 +98,10 @@ void Projector::plot(const double * point) {
void Projector::output(){ void Projector::output(){
std::cout << "Projector properties: " << std::endl; LogMoreInfo("Projector properties: \n");
std::cout << " factor: " << factor << std::endl; LogMoreInfo(" factor: %f\n", factor);
for ( unsigned int i = 0; i < intern_dim; i++ ) { for ( unsigned int i = 0; i < intern_dim; i++ ) {
std::cout << " dimension " << i << ": offset: " << offset[i] << ", range: [" << range_min[i] << ", " << range_max[i] << "]" << std::endl; LogMoreInfo(" dimension %d: offset: %f, range: [%f, %f]\n", i, offset[i], range_min[i], range_max[i]);
} }
} }

46
main.cpp

@ -1,3 +1,4 @@
#include "Logger.hpp"
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include <valarray> #include <valarray>
@ -9,10 +10,25 @@
#include "defines.hpp" #include "defines.hpp"
int main(int argc, char *argv[]) {
std::cout << "Awesome Attractor, version " << __DATE__ << std::endl;
bool verbose = false; int verbose;
void showHelpText(){
std::cout << "Awesome Attractor, version " << __DATE__ << std::endl;
std::cout << "Usage: AwesomeAttractor [OPTION]... FILE" << std::endl << std::endl;
std::cout << "Optons:" << std::endl;
std::cout << " --help Shows this help" << std::endl;
std::cout << " -q quiet mode" << std::endl;
std::cout << " -v verbose mode" << std::endl;
std::cout << " -V loud mode" << std::endl;
std::cout << " -w N Sets width of output image to N" << std::endl;
std::cout << " -h N Sets height of output image to N" << std::endl;
std::cout << " -i N Sets number of iterations to N" << std::endl;
exit(0);
}
int main(int argc, char *argv[]) {
verbose = 0;
std::string attractorFile = DEFAULT_ATTRACTOR_FILE; std::string attractorFile = DEFAULT_ATTRACTOR_FILE;
unsigned int iterations = DEFAULT_ITERATIONS; unsigned int iterations = DEFAULT_ITERATIONS;
unsigned int width = DEFAULT_WIDTH; unsigned int width = DEFAULT_WIDTH;
@ -20,8 +36,14 @@ int main(int argc, char *argv[]) {
for ( int i = 1; i < argc; ++i ) { for ( int i = 1; i < argc; ++i ) {
if ( strcmp(argv[i], "-v") == 0 ) { if ( strcmp(argv[i], "-v") == 0 ) {
verbose = true; verbose = 1;
} else if ( strcmp(argv[i], "-w") == 0 ) { } else if ( strcmp(argv[i], "-q") == 0 ) {
verbose = -1;
} else if ( strcmp(argv[i], "--help") == 0 ) {
showHelpText();
} else if ( strcmp(argv[i], "-V") == 0 ) {
verbose = 3;
} else if ( strcmp(argv[i], "-w") == 0 ) {
width = atoi(argv[++i]); width = atoi(argv[++i]);
} else if ( strcmp(argv[i], "-h") == 0 ) { } else if ( strcmp(argv[i], "-h") == 0 ) {
height = atoi(argv[++i]); height = atoi(argv[++i]);
@ -32,6 +54,8 @@ int main(int argc, char *argv[]) {
} }
} }
LogInfo("Awesome Attractor, version %s\n", __DATE__);
// initialising stuff // initialising stuff
Attractor myAttractor(attractorFile); Attractor myAttractor(attractorFile);
@ -44,6 +68,8 @@ int main(int argc, char *argv[]) {
projection.output(); projection.output();
LogInfo("\nRendering\n");
clock_t start, end; clock_t start, end;
start = clock(); start = clock();
for ( unsigned int j = 1; j <= 100; ++j ) { for ( unsigned int j = 1; j <= 100; ++j ) {
@ -51,14 +77,16 @@ int main(int argc, char *argv[]) {
myAttractor.iterate(); myAttractor.iterate();
myAttractor.plot(); myAttractor.plot();
} }
std::cout << "\r" << j << "% done" << std::flush; if ( verbose >= 0 ) {
std::cout << "\r" << j << "% done" << std::flush;
}
} }
end = clock(); end = clock();
double totalIterations = 100.0*iterations; double totalIterations = 100.0*iterations;
double totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); double totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << std::endl << "total clock time: " << totalTime << std::endl; LogInfo("\nTotal clock time: %f\n", totalTime);
std::cout << "average iterations per second: " << totalIterations/totalTime << std::endl << std::endl; LogMoreInfo("Average iterations per second: %f\n\n", totalIterations/totalTime);
// saving output // saving output
start = clock(); start = clock();
@ -67,7 +95,7 @@ int main(int argc, char *argv[]) {
totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << "total clock time for writing png: " << totalTime << std::endl; LogInfo("Total clock time for writing png: %f\n", totalTime);