]> git.mxchange.org Git - flightgear.git/blob - src/Main/logger.cxx
Change the timing semantics slightly so that you get much closer to the
[flightgear.git] / src / Main / logger.cxx
1 // logger.cxx - log properties.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #include "logger.hxx"
7
8 #include STL_FSTREAM
9 #include <string>
10
11 SG_USING_STD(ofstream);
12 SG_USING_STD(endl);
13 SG_USING_STD(string);
14
15 #include <simgear/debug/logstream.hxx>
16
17 #include "fg_props.hxx"
18
19
20 \f
21 ////////////////////////////////////////////////////////////////////////
22 // Implementation of FGLogger
23 ////////////////////////////////////////////////////////////////////////
24
25 FGLogger::FGLogger ()
26 {
27 }
28
29 FGLogger::~FGLogger ()
30 {
31 }
32
33 void
34 FGLogger::init ()
35 {
36   SGPropertyNode * logging = fgGetNode("/logging");
37   if (logging == 0)
38     return;
39
40   vector<SGPropertyNode_ptr> children = logging->getChildren("log");
41   for (unsigned int i = 0; i < children.size(); i++) {
42
43     SGPropertyNode * child = children[i];
44
45     if (!child->getBoolValue("enabled", false))
46         continue;
47
48     _logs.push_back(Log());
49     Log &log = _logs[_logs.size()-1];
50     
51     string filename = child->getStringValue("filename");
52     if (filename.size() == 0) {
53         filename = "fg_log.csv";
54         child->setStringValue("filename", filename.c_str());
55     }
56
57     string delimiter = child->getStringValue("delimiter");
58     if (delimiter.size() == 0) {
59         delimiter = ",";
60         child->setStringValue("delimiter", delimiter.c_str());
61     }
62         
63     log.interval_ms = child->getLongValue("interval-ms");
64     log.last_time_ms = globals->get_sim_time_sec() * 1000;
65     log.delimiter = delimiter.c_str()[0];
66     log.output = new ofstream(filename.c_str());
67     if (!log.output) {
68       SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
69       continue;
70     }
71
72     //
73     // Process the individual entries (Time is automatic).
74     //
75     vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
76     (*log.output) << "Time";
77     for (unsigned int j = 0; j < entries.size(); j++) {
78       SGPropertyNode * entry = entries[j];
79
80       //
81       // Set up defaults.
82       //
83       if (!entry->hasValue("property")) {
84           entry->setBoolValue("enabled", false);
85           continue;
86       }
87
88       if (!entry->getBoolValue("enabled"))
89           continue;
90
91       SGPropertyNode * node =
92         fgGetNode(entry->getStringValue("property"), true);
93       log.nodes.push_back(node);
94       (*log.output) << log.delimiter
95                     << entry->getStringValue("title", node->getPath());
96     }
97     (*log.output) << endl;
98   }
99 }
100
101 void
102 FGLogger::reinit ()
103 {
104     _logs.clear();
105     init();
106 }
107
108 void
109 FGLogger::bind ()
110 {
111 }
112
113 void
114 FGLogger::unbind ()
115 {
116 }
117
118 void
119 FGLogger::update (double dt)
120 {
121     double sim_time_sec = globals->get_sim_time_sec();
122     double sim_time_ms = sim_time_sec * 1000;
123     for (unsigned int i = 0; i < _logs.size(); i++) {
124         while ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
125             _logs[i].last_time_ms += _logs[i].interval_ms;
126             (*_logs[i].output) << sim_time_sec;
127             for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
128                 (*_logs[i].output) << _logs[i].delimiter
129                            << _logs[i].nodes[j]->getStringValue();
130             }
131             (*_logs[i].output) << endl;
132         }
133     }
134 }
135
136
137 \f
138 ////////////////////////////////////////////////////////////////////////
139 // Implementation of FGLogger::Log
140 ////////////////////////////////////////////////////////////////////////
141
142 FGLogger::Log::Log ()
143   : output(0),
144     interval_ms(0),
145     last_time_ms(-999999.0),
146     delimiter(',')
147 {
148 }
149
150 FGLogger::Log::~Log ()
151 {
152   delete output;
153 }
154
155 // end of logger.cxx