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.delimiter = delimiter.c_str()[0];
65 log.output = new ofstream(filename.c_str());
67 SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
72 // Process the individual entries (Time is automatic).
74 vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
75 (*log.output) << "Time";
76 for (unsigned int j = 0; j < entries.size(); j++) {
77 SGPropertyNode * entry = entries[j];
82 if (!entry->hasValue("property")) {
83 entry->setBoolValue("enabled", false);
87 if (!entry->getBoolValue("enabled"))
90 SGPropertyNode * node =
91 fgGetNode(entry->getStringValue("property"), true);
92 log.nodes.push_back(node);
93 (*log.output) << log.delimiter
94 << entry->getStringValue("title", node->getPath());
96 (*log.output) << endl;
118 FGLogger::update (double dt)
120 double sim_time_ms = globals->get_sim_time_sec() * 1000;
121 for (unsigned int i = 0; i < _logs.size(); i++) {
122 if ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
123 _logs[i].last_time_ms = sim_time_ms;
124 (*_logs[i].output) << sim_time_ms;
125 for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
126 (*_logs[i].output) << _logs[i].delimiter
127 << _logs[i].nodes[j]->getStringValue();
129 (*_logs[i].output) << endl;
136 ////////////////////////////////////////////////////////////////////////
137 // Implementation of FGLogger::Log
138 ////////////////////////////////////////////////////////////////////////
140 FGLogger::Log::Log ()
143 last_time_ms(-999999.0),
148 FGLogger::Log::~Log ()