loggers.h
Go to the documentation of this file.00001 00013 #ifndef LGR_H 00014 #define LGR_H 00015 00016 #include "bdmbase.h" 00017 #include "user_info.h" 00018 00019 namespace bdm { 00020 using std::string; 00021 00024 class memlog : public logger { 00025 00026 protected: 00028 int maxlen; 00030 int ind; 00032 Array<mat> vectors; 00034 string filename; 00035 00037 bool file_saved; 00038 public: 00040 memlog ( int maxlen0, string fn = "" ) : logger ( "_" ), maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), filename ( fn ), file_saved(false) {} 00041 00043 memlog() : logger ( "_" ), maxlen ( 0 ), ind ( 0 ), vectors ( 0 ), file_saved(false) {} 00044 00046 void init(); 00047 00048 void step() { 00049 if ( ind < maxlen ) ind++; 00050 else bdm_error ( "memlog::ind is too high;" ); 00051 } 00052 00053 void log_vector ( int id, const vec &v ) { 00054 bdm_assert ( id < vectors.length(), "Logger was not initialized, run init()." ); 00055 if ( id >= 0 ) { 00056 vectors ( id ).set_row ( ind, v ); 00057 } 00058 } 00059 void log_double ( int id, const double &d ) { 00060 bdm_assert ( id < vectors.length(), "Logger was not initialized, run init()." ); 00061 if ( id >= 0 ) { 00062 vectors ( id )( ind, 0 )=d; 00063 } 00064 } 00065 void logit ( int id, const double &d ) { 00066 bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." ); 00067 bdm_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" ); 00068 if ( id >= 0 ) { 00069 vectors ( id ) ( ind ) = d; 00070 } 00071 } 00073 void itsave ( const char* fname ); 00075 void finalize() { 00076 if ( filename.length() > 0 && !file_saved ) { 00077 itsave ( ( filename + ".it" ).c_str() ); 00078 if ( settings.length() > 0 ) { 00079 setting_conf.writeFile ( ( filename + ".cfg" ).c_str() ); 00080 } 00081 file_saved = true; 00082 } 00083 00084 }; 00085 00086 00098 void from_setting ( const Setting &set ); 00099 00100 // TODO dodelat void to_setting( Setting &set ) const; 00101 }; 00102 00103 UIREGISTER ( memlog ); 00104 SHAREDPTR ( memlog ); 00105 00106 00110 class stdlog: public memlog { 00111 public: 00113 stdlog() : memlog ( 1 ) {}; 00114 00115 void init() { 00116 memlog::init(); 00117 for ( int i = 0; i < entries.length(); i++ ) { 00118 if ( entries ( i )._dsize() == 1 ) { 00119 cout << names ( i ) << separator << entries ( i ).name ( 0 ) << "\t"; 00120 } else 00121 for ( int j = 0; j < vectors ( i ).cols(); j++ ) { 00122 cout << names ( i ) << separator << entries ( i ).scalarname ( j ) << "\t"; 00123 } 00124 } 00125 cout << endl; 00126 00127 00128 } 00130 void step() { 00131 for ( int i = 0; i < vectors.length(); i++ ) { 00132 for ( int j = 0; j < vectors ( i ).cols(); j++ ) { 00133 cout << vectors ( i ) ( 0, j ) << "\t"; 00134 } 00135 } 00136 cout << endl; 00137 } 00143 void from_setting ( const Setting &set ) { 00144 } 00145 }; 00146 UIREGISTER ( stdlog ); 00147 00155 class dirfilelog : public memlog { 00156 00157 protected: 00159 string dirname; 00161 Array<string> scalarnames; 00162 00164 bool buffers_written; 00165 public: 00169 dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ), buffers_written(false) {} 00170 00171 dirfilelog() : buffers_written(false) {} 00172 00174 void init(); 00175 void step(); 00176 void finalize(); 00180 void write_buffers ( int Len ); 00181 00191 void from_setting ( const Setting &set ); 00192 00193 // TODO dodelat void to_setting( Setting &set ) const; 00194 }; 00195 00196 UIREGISTER ( dirfilelog ); 00197 SHAREDPTR ( dirfilelog ); 00198 00199 }; 00200 #endif // LGR_H
Generated on 2 Dec 2013 for mixpp by 1.4.7