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

38
Canvas.cpp

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

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 <cassert>
@ -64,7 +64,7 @@ void Projector::finish_range() {
if ( factor * dist > (double)canvas->size[i] ) {
factor = (double)canvas->size[i] / dist;
//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 y = project_point[1]*factor + offset[1];
//cout << x << ", " << y << endl;
canvas->plot(x, y);
if ( even(point[2]*17) )
canvas->plot(x, y, 1);
@ -100,10 +98,10 @@ void Projector::plot(const double * point) {
void Projector::output(){
std::cout << "Projector properties: " << std::endl;
std::cout << " factor: " << factor << std::endl;
LogMoreInfo("Projector properties: \n");
LogMoreInfo(" factor: %f\n", factor);
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 <ctime>
#include <valarray>
@ -9,10 +10,25 @@
#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;
unsigned int iterations = DEFAULT_ITERATIONS;
unsigned int width = DEFAULT_WIDTH;
@ -20,8 +36,14 @@ int main(int argc, char *argv[]) {
for ( int i = 1; i < argc; ++i ) {
if ( strcmp(argv[i], "-v") == 0 ) {
verbose = true;
} else if ( strcmp(argv[i], "-w") == 0 ) {
verbose = 1;
} 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]);
} else if ( strcmp(argv[i], "-h") == 0 ) {
height = atoi(argv[++i]);
@ -32,6 +54,8 @@ int main(int argc, char *argv[]) {
}
}
LogInfo("Awesome Attractor, version %s\n", __DATE__);
// initialising stuff
Attractor myAttractor(attractorFile);
@ -44,6 +68,8 @@ int main(int argc, char *argv[]) {
projection.output();
LogInfo("\nRendering\n");
clock_t start, end;
start = clock();
for ( unsigned int j = 1; j <= 100; ++j ) {
@ -51,14 +77,16 @@ int main(int argc, char *argv[]) {
myAttractor.iterate();
myAttractor.plot();
}
std::cout << "\r" << j << "% done" << std::flush;
if ( verbose >= 0 ) {
std::cout << "\r" << j << "% done" << std::flush;
}
}
end = clock();
double totalIterations = 100.0*iterations;
double totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << std::endl << "total clock time: " << totalTime << std::endl;
std::cout << "average iterations per second: " << totalIterations/totalTime << std::endl << std::endl;
LogInfo("\nTotal clock time: %f\n", totalTime);
LogMoreInfo("Average iterations per second: %f\n\n", totalIterations/totalTime);
// saving output
start = clock();
@ -67,7 +95,7 @@ int main(int argc, char *argv[]) {
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);