1 // logger.cxx - log properties.
2 // Written by David Megginson, started 2002.
4 // This file is in the Public Domain, and comes with no warranty.
11 SG_USING_STD(ofstream);
15 #include <simgear/debug/logstream.hxx>
17 #include "fg_props.hxx"
21 ////////////////////////////////////////////////////////////////////////
22 // Implementation of FGLogger
23 ////////////////////////////////////////////////////////////////////////
29 FGLogger::~FGLogger ()
36 SGPropertyNode * logging = fgGetNode("/logging");
40 vector<SGPropertyNode_ptr> children = logging->getChildren("log");
41 for (unsigned int i = 0; i < children.size(); i++) {
43 SGPropertyNode * child = children[i];
45 if (!child->getBoolValue("enabled", false))
48 _logs.push_back(Log());
49 Log &log = _logs[_logs.size()-1];
51 string filename = child->getStringValue("filename");
52 if (filename.size() == 0) {
53 filename = "fg_log.csv";
54 child->setStringValue("filename", filename.c_str());
57 string delimiter = child->getStringValue("delimiter");
58 if (delimiter.size() == 0) {
60 child->setStringValue("delimiter", delimiter.c_str());
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());
68 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
73 // Process the individual entries (Time is automatic).
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];
83 if (!entry->hasValue("property")) {
84 entry->setBoolValue("enabled", false);
88 if (!entry->getBoolValue("enabled"))
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());
97 (*log.output) << endl;
119 FGLogger::update (double dt)
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();
131 (*_logs[i].output) << endl;
138 ////////////////////////////////////////////////////////////////////////
139 // Implementation of FGLogger::Log
140 ////////////////////////////////////////////////////////////////////////
142 FGLogger::Log::Log ()
145 last_time_ms(-999999.0),
150 FGLogger::Log::~Log ()