[262] | 1 | |
---|
[92] | 2 | #include "loggers.h" |
---|
[93] | 3 | #include "../osutils.h" |
---|
[92] | 4 | |
---|
| 5 | #include <fcntl.h> |
---|
[95] | 6 | #ifdef WIN32 |
---|
| 7 | #include <io.h> |
---|
| 8 | #endif |
---|
[92] | 9 | |
---|
[477] | 10 | namespace bdm { |
---|
[551] | 11 | |
---|
| 12 | void memlog::init() { |
---|
| 13 | int i; |
---|
| 14 | int n = entries.length(); |
---|
| 15 | vectors.set_size ( n ); |
---|
| 16 | for ( i = 0; i < n; i++ ) { |
---|
| 17 | vectors ( i ).set_size ( maxlen, entries ( i )._dsize() ); |
---|
| 18 | |
---|
| 19 | // avoid uninitialized memory |
---|
| 20 | for ( int j = 0; j < vectors ( i ).rows(); ++j ) { |
---|
| 21 | for (int k = 0; k < vectors ( i ).cols(); ++k ) { |
---|
| 22 | vectors ( i )(j, k) = 0; |
---|
| 23 | } |
---|
| 24 | } |
---|
| 25 | } |
---|
| 26 | } |
---|
| 27 | |
---|
[162] | 28 | void memlog::itsave ( const char* fname ) { |
---|
| 29 | it_file itf ( fname ); |
---|
[102] | 30 | int i; |
---|
[477] | 31 | string vec_name = ""; |
---|
[347] | 32 | int istart, iend; |
---|
[477] | 33 | for ( i = 0; i < entries.length(); i++ ) { |
---|
| 34 | istart = 0; |
---|
| 35 | for ( int j = 0; j < entries ( i ).length(); j++ ) { // same for as in add!!! |
---|
| 36 | vec_name = names ( i ) + entries ( i ).name ( j ); |
---|
| 37 | iend = istart + entries ( i ).size ( j ) - 1; |
---|
| 38 | itf << Name ( vec_name ) << vectors ( i ).get_cols ( istart, iend ); |
---|
[347] | 39 | // move start after current end... |
---|
[477] | 40 | istart = iend + 1; |
---|
[162] | 41 | } |
---|
[477] | 42 | |
---|
[102] | 43 | } |
---|
| 44 | } |
---|
| 45 | |
---|
[477] | 46 | void memlog::from_setting ( const Setting &set ) { |
---|
[357] | 47 | // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK? |
---|
[477] | 48 | string itfilename = ( const char* ) set["filename"]; |
---|
[377] | 49 | maxlen = set["maxlen"]; |
---|
[357] | 50 | } |
---|
| 51 | |
---|
[92] | 52 | void dirfilelog::init() { |
---|
[477] | 53 | int i, j, k; |
---|
| 54 | int nsc = 0; |
---|
| 55 | for ( i = 0; i < entries.length(); i++ ) { |
---|
| 56 | nsc += entries ( i )._dsize(); |
---|
| 57 | } |
---|
[92] | 58 | ; //all entries!! |
---|
| 59 | |
---|
| 60 | char filename[200]; |
---|
| 61 | char num[3]; |
---|
| 62 | |
---|
| 63 | // Initialize directory |
---|
[162] | 64 | makedir ( dirname ); // |
---|
[92] | 65 | |
---|
| 66 | // directory OK, or it can be overwritten |
---|
| 67 | |
---|
| 68 | //Create filenames |
---|
| 69 | scalarnames.set_length ( nsc ); |
---|
| 70 | // For all entries |
---|
[477] | 71 | int ii = 0; |
---|
| 72 | for ( i = 0; i < entries.length(); i++ ) { //for entries |
---|
| 73 | for ( j = 0; j < entries ( i ).length(); j++ ) { //for RVs in entries |
---|
[92] | 74 | int rvsize = entries ( i ).size ( j ); |
---|
[230] | 75 | // for non-empty names |
---|
[477] | 76 | if ( names ( i ).length() > 0 ) { |
---|
| 77 | scalarnames ( ii ) = names ( i ) + "_"; |
---|
| 78 | } |
---|
[230] | 79 | // add name |
---|
| 80 | scalarnames ( ii ) += entries ( i ).name ( j ); |
---|
| 81 | // add number when needed |
---|
[477] | 82 | if ( rvsize > 1 ) { |
---|
[230] | 83 | // copy the same name for th whole RV |
---|
[477] | 84 | for ( k = 1; k < rvsize; k++ ) { |
---|
| 85 | scalarnames ( ii + k ) = scalarnames ( ii ); |
---|
| 86 | } |
---|
[230] | 87 | // add numbers |
---|
[477] | 88 | for ( k = 0; k < rvsize; k++ ) { //for all scalars in given RV |
---|
| 89 | sprintf ( num, "%d", k ); |
---|
| 90 | scalarnames ( ii ) += ( std::string ) "_" + num; |
---|
[92] | 91 | ii++; |
---|
| 92 | } |
---|
[477] | 93 | } else { |
---|
| 94 | ii++; |
---|
| 95 | } |
---|
[92] | 96 | } |
---|
| 97 | } |
---|
| 98 | //Create format |
---|
[477] | 99 | string frm = "format"; |
---|
[92] | 100 | get_fname ( filename, dirname, frm ); |
---|
| 101 | |
---|
| 102 | FILE* fpf; |
---|
| 103 | fpf = fopen ( filename, "w" ); |
---|
[477] | 104 | for ( i = 0; i < ii; i++ ) { |
---|
[92] | 105 | fprintf ( fpf, "%s RAW d 1\n", scalarnames ( i ).c_str() ); |
---|
| 106 | } |
---|
| 107 | fclose ( fpf ); |
---|
| 108 | |
---|
| 109 | memlog::init(); |
---|
| 110 | |
---|
[162] | 111 | //Delete old data |
---|
[92] | 112 | write_buffers ( 0 ); |
---|
| 113 | } |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | void dirfilelog::write_buffers ( int Len ) { |
---|
| 117 | int fp; |
---|
[477] | 118 | int nen = entries.length(); //all entries!! |
---|
[92] | 119 | double *Dt; |
---|
| 120 | int DtRows; |
---|
[477] | 121 | int i, j, k; |
---|
[92] | 122 | int ii; //index in scalarnames; |
---|
| 123 | int jj; //index in vectors() |
---|
| 124 | char filename[200]; |
---|
| 125 | |
---|
[477] | 126 | it_assert_debug ( Len <= maxlen, "diffilelog" ); |
---|
[92] | 127 | |
---|
| 128 | ii = 0; |
---|
[477] | 129 | for ( i = 0; i < nen; i++ ) { //for entries |
---|
[92] | 130 | Dt = vectors ( i )._data(); |
---|
[162] | 131 | DtRows = vectors ( i ).rows(); |
---|
| 132 | |
---|
[477] | 133 | jj = 0; |
---|
| 134 | for ( j = 0; j < entries ( i ).length(); j++ ) { //for RVs in entries |
---|
[92] | 135 | int rvsize = entries ( i ).size ( j ); |
---|
[477] | 136 | for ( k = 0; k < rvsize; k++ ) { { //for all scalars in given RV |
---|
| 137 | get_fname ( filename, dirname, scalarnames ( ii ) ); |
---|
| 138 | if ( Len == 0 ) //initialization |
---|
[92] | 139 | fp = open ( filename, O_CREAT | O_WRONLY | O_TRUNC, 00644 ); |
---|
| 140 | else |
---|
| 141 | fp = open ( filename, O_CREAT | O_WRONLY | O_APPEND, 00644 ); |
---|
| 142 | |
---|
[477] | 143 | write ( fp, &Dt[jj* ( DtRows ) ], ( Len ) *sizeof ( double ) ); |
---|
[92] | 144 | close ( fp ); |
---|
| 145 | |
---|
| 146 | //next row in Dt |
---|
| 147 | jj++; |
---|
| 148 | //next scalarname |
---|
| 149 | ii++; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | } |
---|
| 154 | } |
---|
| 155 | |
---|
[162] | 156 | void dirfilelog::step ( ) { |
---|
[92] | 157 | |
---|
[477] | 158 | if ( ind == ( maxlen - 1 ) ) { |
---|
| 159 | write_buffers ( ind + 1 ); |
---|
[162] | 160 | ind = 0; |
---|
[477] | 161 | } else |
---|
[162] | 162 | ind++; |
---|
[92] | 163 | |
---|
| 164 | } |
---|
[162] | 165 | |
---|
| 166 | void dirfilelog::finalize ( ) { |
---|
[477] | 167 | if ( ind > 0 ) |
---|
[162] | 168 | write_buffers ( ind ); //assuming here that i+1 was not filled |
---|
| 169 | } |
---|
[254] | 170 | |
---|
[357] | 171 | |
---|
[477] | 172 | void dirfilelog::from_setting ( const Setting &set ) { |
---|
[357] | 173 | // TODO tady se natvrdo ocekava existence stringu, nejsou zadne defaulty.. je to tak OK? |
---|
[477] | 174 | dirname = ( const char* ) set["dirname"]; |
---|
[377] | 175 | maxlen = set["maxlen"]; |
---|
[477] | 176 | scalarnames.set_length ( 0 ); |
---|
[262] | 177 | } |
---|
[357] | 178 | |
---|
| 179 | } |
---|