From 28701c3d8eb55875209892dd0c99c2823428dbbe Mon Sep 17 00:00:00 2001 From: Oliver Hahn Date: Sat, 14 Aug 2010 18:19:48 -0700 Subject: [PATCH] Added some logging infra structure. --- convolution_kernel.cc | 1 - densities.cc | 8 +-- general.hh | 1 + log.cc | 136 +++++++++++++++++++++++++++++++++++++ log.hh | 153 ++++++++++++++++++++++++++++++++++++++++++ main.cc | 29 ++++++-- mesh.hh | 2 +- 7 files changed, 318 insertions(+), 12 deletions(-) create mode 100644 log.cc create mode 100644 log.hh diff --git a/convolution_kernel.cc b/convolution_kernel.cc index 20e690f..ee5cc2e 100644 --- a/convolution_kernel.cc +++ b/convolution_kernel.cc @@ -253,7 +253,6 @@ namespace convolution{ }else{ rr2 = rr[0]*rr[0]+rr[1]*rr[1]+rr[2]*rr[2]; val += tfr->compute_real(rr2); - kdata_[idx] += (fftw_real)tfr->compute_real(rr2); } } } diff --git a/densities.cc b/densities.cc index 4e29f4b..9be052f 100644 --- a/densities.cc +++ b/densities.cc @@ -516,13 +516,13 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type top = new DensityGrid( nbase, nbase, nbase ); random_numbers *rc; - int x0[3] = { refh.offset_abs(levelmin,0)-lfac*shift[0], + /*int x0[3] = { refh.offset_abs(levelmin,0)-lfac*shift[0], refh.offset_abs(levelmin,1)-lfac*shift[1], refh.offset_abs(levelmin,2)-lfac*shift[2] }; int lx[3] = { refh.size(levelmin,0), refh.size(levelmin,1), - refh.size(levelmin,2) }; + refh.size(levelmin,2) };*/ if( randc[levelmin] == NULL ) { @@ -647,7 +647,7 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type //... restrict these contributions to the next level - for( int j=1; j<=levelmax-levelmin; ++j ) + for( int j=1; j<=(int)levelmax-(int)levelmin; ++j ) { int R = j; delta.add_patch( refh.offset(levelmin+j,0), refh.offset(levelmin+j,1), refh.offset(levelmin+j,2), @@ -723,7 +723,7 @@ void GenerateDensityHierarchy( config_file& cf, transfer_function *ptf, tf_type meshvar_bnd delta_longrange( *delta.get_grid(levelmin+i) ); coarse->copy_unpad( delta_longrange ); - for( int j=1; j<=levelmax-levelmin-i; ++j ) + for( int j=1; j<=(int)levelmax-(int)levelmin-i; ++j ) { int R = j; //delta.add_patch( refh.offset(levelmin+i+j,0), refh.offset(levelmin+i+j,1), refh.offset(levelmin+i+j,2), diff --git a/general.hh b/general.hh index a7e498e..a6ba046 100644 --- a/general.hh +++ b/general.hh @@ -24,6 +24,7 @@ #ifndef __GENERAL_HH #define __GENERAL_HH +#include "log.hh" #include diff --git a/log.cc b/log.cc new file mode 100644 index 0000000..5acdbdf --- /dev/null +++ b/log.cc @@ -0,0 +1,136 @@ +/* + + log.cc - This file is part of MUSIC - + a code to generate multi-scale initial conditions + for cosmological simulations + + Copyright (C) 2010 Oliver Hahn + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + */ + +#include "log.hh" +#include +#include + + +std::string MUSIC::log::outputFile_; +std::ofstream MUSIC::log::outputStream_; +std::list MUSIC::log::messages_; +void (*MUSIC::log::receiver)(const message&) = NULL; +MUSIC::log::messageType MUSIC::log::logLevel_; + + +std::string RemoveMultipleWhiteSpaces( std::string s ) +{ + std::string search = " "; // this is 2 spaces + size_t index; + + while( (index = s.find(search)) != std::string::npos ) + { // remove 1 character from the string at index + s.erase(index,1); + } + + return s; +} + +//void MUSIC::log::send(messageType type, const std::string& text) +void MUSIC::log::send(messageType type, std::stringstream& textstr) +{ + std::string text = textstr.str(); + // Skip logging if minimum level is higher + if (logLevel_) + if (type < logLevel_) return; + // log message + MUSIC::log::message m; + m.type = type; + m.text = text; + time_t t = time(NULL); + m.when = localtime(&t); + messages_.push_back(m); + + if( type==Info||type==Warning||type==Error||type==FatalError ) + { + std::cout << " - "; + if(type==Warning) + std::cout << "WARNING: "; + if(type==Error) + std::cout << "ERROR: "; + if(type==FatalError) + std::cout << "FATAL: "; + std::cout << text << std::endl; + } + + std::replace(text.begin(),text.end(),'\n',' '); + RemoveMultipleWhiteSpaces(text); + + // if enabled logging to file + if(outputStream_.is_open()) + { + // print time + char buffer[9]; + strftime(buffer, 9, "%X", m.when); + outputStream_ << buffer; + + // print type + switch(type) + { + case Info: outputStream_ << " | info | "; break; + case DebugInfo: outputStream_ << " | debug | "; break; + case Warning: outputStream_ << " | warning | "; break; + case Error: outputStream_ << " | ERROR | "; break; + case FatalError:outputStream_ << " | FATAL | "; break; + case User: outputStream_ << " | info | "; break; + default: outputStream_ << " | "; + } + + // print description + outputStream_ << text << std::endl; + } + + // if user wants to catch messages, send it to him + if(receiver) + receiver(m); +} + + +void MUSIC::log::setOutput(const std::string& filename) +{ + //logDebug("Setting output log file: " + filename); + outputFile_ = filename; + + // close old one + if(outputStream_.is_open()) + outputStream_.close(); + + // create file + outputStream_.open(filename.c_str()); + if(!outputStream_.is_open()) + LOGERR("Cannot create/open '" + filename + "' for logging"); + //send(Error, "Cannot create/open '" + filename + "' for logging"); +} + +void MUSIC::log::setLevel(const MUSIC::log::messageType level) +{ + logLevel_ = level; +} + + +MUSIC::log::~log() +{ + if(outputStream_.is_open()) + outputStream_.close(); +} + diff --git a/log.hh b/log.hh new file mode 100644 index 0000000..38caaa7 --- /dev/null +++ b/log.hh @@ -0,0 +1,153 @@ +/* + + log.hh - This file is part of MUSIC - + a code to generate multi-scale initial conditions + for cosmological simulations + + Copyright (C) 2010 Oliver Hahn + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + */ + +#ifndef __LOG_HH +#define __LOG_HH + +#include +#include +#include +#include +#include + +/*! + * \brief System for logging runtime library errors, warnings, etc. + * + * This is the class that catches every (debug) info, warning, error, or user message and + * processes it. Messages can be written to files and/or forwarded to user function for + * processing messages. + */ +namespace MUSIC +{ + +class log +{ +public: + log(){} + ~log(); + + /*! + * \brief Types of logged messages. + */ + enum messageType + { + Info, + DebugInfo, + Warning, + Error, + FatalError, + User + }; + + /*! + * \brief Logged message of type MessageType with some info. + */ + struct message + { + messageType type; + std::string text; + tm* when; + }; + + /*! + * \brief Open file where to log the messages. + */ + static void setOutput(const std::string& filename); + + /*! + * \brief Get the filename of log. + */ + static const std::string& output() { return outputFile_; } + + /*! + * \brief Add a new message to log. + * \param type Type of the new message. + * \param text Message. + * \remarks Message is directly passes to user reciever if one is set. + */ + //static void send(messageType type, const std::string& text); + static void send(messageType type, std::stringstream& text); + + /*! + * \brief Get the list of all of the logged messages. + */ + static const std::list& messages() { return messages_; } + + /*! + * \brief Get the last logged message. + */ + static const message& lastMessage() { return messages_.back(); } + + /*! + * \brief Set user function to receive newly sent messages to logger. + */ + static void setUserReceiver(void (*userFunc)(const message&)) { receiver = userFunc; } + + /*! + * \brief Set minimum level of message to be logged. + */ + static void setLevel(const log::messageType level); + +private: + + static std::string outputFile_; + static std::ofstream outputStream_; + static std::list messages_; + static messageType logLevel_; + static void (*receiver)(const message&); +}; + +} +#ifndef LOGERR +#define LOGERR(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Error, ss); } +#endif + +#ifndef LOGINFO +#define LOGINFO(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Info, ss); } +#endif + +#ifndef LOGWARN +#define LOGWARN(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::Warning, ss); } +#endif + +#ifndef LOGFATAL +#define LOGFATAL(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::FatalError, ss); } +#endif + +#ifndef LOGDEBUG +#define LOGDEBUG(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::DebugInfo, ss); } +#endif + +#ifndef LOGUSER +#define LOGUSER(x) { std::stringstream ss; ss<<(x); MUSIC::log::send(MUSIC::log::User, ss); } +#endif + +/*#define LOGINFO(x) MUSIC::log::send(MUSIC::log::Info, std::stringstream()<<(x)); +#define LOGWARN(x) MUSIC::log::send(MUSIC::log::Warning, std::stringstream()<<(x)); +#define LOGFATAL(x) MUSIC::log::send(MUSIC::log::FatalError, std::stringstream()<<(x)); +*/ + + +#endif //__LOG_HH + + diff --git a/main.cc b/main.cc index f31d63a..6bf5772 100644 --- a/main.cc +++ b/main.cc @@ -48,7 +48,7 @@ #include "transfer_function.hh" #define THE_CODE_NAME "music!" -#define THE_CODE_VERSION "0.4.0a" +#define THE_CODE_VERSION "0.5.0a" namespace music @@ -272,6 +272,11 @@ int main (int argc, const char * argv[]) exit(0); } + //... open log file + char logfname[128]; + sprintf(logfname,"%s_log.txt",argv[1]); + MUSIC::log::setOutput(logfname); + LOGINFO(std::string("Opening log file '")+logfname+"'.") /******************************************************************************************************/ /* read and interpret config file *********************************************************************/ @@ -285,7 +290,7 @@ int main (int argc, const char * argv[]) boxlength = cf.getValue( "setup", "boxlength" ); lbase = cf.getValue( "setup", "levelmin" ); lmax = cf.getValue( "setup", "levelmax" ); - lbaseTF = cf.getValueSafe( "setup", "levelminTF", lbase ); + lbaseTF = cf.getValueSafe( "setup", "levelmin_TF", lbase ); force_shift = cf.getValueSafe("setup", "force_shift", force_shift ); @@ -296,8 +301,11 @@ int main (int argc, const char * argv[]) { std::cout << " - WARNING: levelminTF < levelmin. This is not good!\n" << " I will set levelminTF = levelmin.\n"; + + LOGUSER("levelminTF < levelmin. set levelminTF = levelmin."); + lbaseTF = lbase; - cf.insertValue("setup","levelminTF",cf.getValue("setup","levelmin")); + cf.insertValue("setup","levelmin_TF",cf.getValue("setup","levelmin")); } temp = cf.getValue( "setup", "ref_offset" ); @@ -409,11 +417,13 @@ int main (int argc, const char * argv[]) try{ if( ! do_2LPT ) { + LOGUSER("Entering 1LPT branch") + //... cdm density and displacements std::cout << "=============================================================\n"; std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n"; std::cout << "-------------------------------------------------------------\n"; - + LOGUSER("Computing dark matter displacements...") grid_hierarchy f( nbnd ), u(nbnd); @@ -449,6 +459,7 @@ int main (int argc, const char * argv[]) std::cout << "=============================================================\n"; std::cout << " COMPUTING BARYON DENSITY\n"; std::cout << "-------------------------------------------------------------\n"; + LOGUSER("Computing baryon density...") GenerateDensityHierarchy( cf, the_transfer_function_plugin, baryon , rh_TF, f, false, true ); coarsen_density(rh_Poisson, f); @@ -468,6 +479,7 @@ int main (int argc, const char * argv[]) std::cout << "=============================================================\n"; std::cout << " COMPUTING VELOCITIES\n"; std::cout << "-------------------------------------------------------------\n"; + LOGUSER("Computing velocitites...") //... velocities if( do_baryons ) @@ -496,11 +508,14 @@ int main (int argc, const char * argv[]) }else { //.. use 2LPT ... + LOGUSER("Entering 2LPT branch") + grid_hierarchy f( nbnd ), u1(nbnd), u2(nbnd); std::cout << "=============================================================\n"; std::cout << " COMPUTING VELOCITIES\n"; std::cout << "-------------------------------------------------------------\n"; + LOGUSER("Computing velocities...") GenerateDensityHierarchy( cf, the_transfer_function_plugin, total , rh_TF, f, true, false ); coarsen_density(rh_Poisson, f); @@ -548,7 +563,8 @@ int main (int argc, const char * argv[]) std::cout << "=============================================================\n"; std::cout << " COMPUTING DARK MATTER DISPLACEMENTS\n"; std::cout << "-------------------------------------------------------------\n"; - + LOGUSER("Computing dark matter displacements...") + //... //u1 += u2; @@ -592,6 +608,7 @@ int main (int argc, const char * argv[]) std::cout << "=============================================================\n"; std::cout << " COMPUTING BARYON DENSITY\n"; std::cout << "-------------------------------------------------------------\n"; + LOGUSER("Computing baryon density...") GenerateDensityHierarchy( cf, the_transfer_function_plugin, cdm , rh_TF, f, false, true ); coarsen_density(rh_Poisson, f); @@ -650,7 +667,7 @@ int main (int argc, const char * argv[]) /** we are done ! **/ std::cout << " - Done!" << std::endl << std::endl; - + LOGUSER("Done"); ///*****************************************/// diff --git a/mesh.hh b/mesh.hh index 0474111..7ab98fb 100644 --- a/mesh.hh +++ b/mesh.hh @@ -1191,8 +1191,8 @@ public: << " size = (" << std::setw(5) << nx_[ilevel] << ", " << std::setw(5) << ny_[ilevel] << ", " << std::setw(5) << nz_[ilevel] << ")\n"; } std::cout << "-------------------------------------------------------------\n"; - } + }; #endif