root/library/bdm/base/loggers.h @ 565

Revision 565, 3.4 kB (checked in by vbarta, 15 years ago)

using own error macros (basically copied from IT++, but never aborting)

  • Property svn:eol-style set to native
Line 
1/*!
2  \file
3  \brief Loggers for storing results of experiments
4  \author Vaclav Smidl.
5
6  -----------------------------------
7  BDM++ - C++ library for Bayesian Decision Making under Uncertainty
8
9  Using IT++ for numerical operations
10  -----------------------------------
11*/
12
13#ifndef LGR_H
14#define LGR_H
15
16#include "bdmbase.h"
17#include "user_info.h"
18
19namespace bdm {
20using std::string;
21
22/*!
23 * Logging into matrices in data format in memory, optionally persisted into a file.
24 */
25class memlog : public logger {
26
27protected:
28        //! Maximum length of vectors stored in memory
29        int maxlen;
30        //! Currect record to be written
31        int ind;
32        //! Storage
33        Array<mat> vectors;
34        //! name of it file to save results
35        string itfilename;
36public:
37        memlog ( int maxlen0, string itf = "" ) : maxlen ( maxlen0 ), ind ( 0 ), vectors ( 0 ), itfilename ( itf ) {}
38
39        //!Default constructor
40        memlog() : maxlen ( 0 ), ind ( 0 ), vectors ( 0 ) {}
41
42        //! Initialize storage
43        void init();
44
45        void step() {
46                if ( ind < maxlen ) ind++;
47                else bdm_error ( "memlog::ind is too high;" );
48        }
49
50        void logit ( int id, const vec &v ) {
51                bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
52                if ( id >= 0 ) {
53                        vectors ( id ).set_row ( ind, v );
54                }
55        }
56        void logit ( int id, const double &d ) {
57                bdm_assert_debug ( id < vectors.length(), "Logger was not initialized, run init()." );
58                bdm_assert_debug ( vectors ( id ).cols() == 1, "Vector expected" );
59                if ( id >= 0 ) {
60                        vectors ( id ) ( ind ) = d;
61                }
62        }
63        //! Save values into an itfile named after \c fname.
64        void itsave ( const char* fname );
65        //!
66        void finalize() {
67                if ( itfilename.length() > 0 ) itsave ( itfilename.c_str() );
68        };
69
70
71        /*! \brief UI for memlog
72
73        TODO dat tam kam patri, a to celej blok
74
75        \code
76        logger = {
77                class = "memlog";
78                filename = "file_name.it"; // resulting filename with results in it format
79                maxlen = 100;          // size of memory buffer
80        }
81        \endcode
82         */
83        void from_setting ( const Setting &set );
84
85        // TODO dodelat void to_setting( Setting &set ) const;
86};
87
88UIREGISTER ( memlog );
89SHAREDPTR ( memlog );
90
91/*!
92* \brief Logging into dirfile with buffer in memory
93
94* Dirfile is a special format used by the kst program. See documentation of kst for description.
95*
96* This format is used to store scalars, hence multivariate RVs must be separated.
97*/
98class dirfilelog : public memlog {
99
100protected:
101        //!name of the directory
102        string dirname;
103        //! Automatically generated
104        Array<string> scalarnames;
105public:
106        /*!\brief Default constructor
107        @param dirname0 name of the directory in which to store the results
108        @param maxlen0 length of the memory buffers, when full the buffers will be dumped to HDD and returned to the beginning. */
109        dirfilelog ( string dirname0, int maxlen0 ) : memlog ( maxlen0 ), dirname ( dirname0 ), scalarnames ( 0 ) {}
110
111        dirfilelog() {}
112
113        //! Initialize storage
114        void init();
115        void step();
116        void finalize();
117        /*! \brief Write memory storage to disk.
118        @param Len length of buffer to be written, if 0 the file is truncated at 0.
119        */
120        void write_buffers ( int Len );
121
122        /*! \brief UI for dirfilelog (Kst file format)
123        \code
124        logger = {
125                class = "dirfilelog";
126                dirname = "directory_for_files"; // resulting files will be stored there
127                maxlen = 100;                    // size of memory buffer, when full results are written to disk
128        }
129        \endcode
130        */
131        void from_setting ( const Setting &set );
132
133        // TODO dodelat void to_setting( Setting &set ) const;
134};
135
136UIREGISTER ( dirfilelog );
137SHAREDPTR ( dirfilelog );
138
139};
140#endif // LGR_H
Note: See TracBrowser for help on using the browser.