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.
15 #include <simgear/debug/logstream.hxx>
17 #include "fg_props.hxx"
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of FGLogger
24 ////////////////////////////////////////////////////////////////////////
30 FGLogger::~FGLogger ()
37 SGPropertyNode * logging = fgGetNode("/logging");
41 std::vector<SGPropertyNode_ptr> children = logging->getChildren("log");
42 for (unsigned int i = 0; i < children.size(); i++) {
44 SGPropertyNode * child = children[i];
46 if (!child->getBoolValue("enabled", false))
49 _logs.push_back(Log());
50 Log &log = _logs[_logs.size()-1];
52 string filename = child->getStringValue("filename");
53 if (filename.empty()) {
54 filename = "fg_log.csv";
55 child->setStringValue("filename", filename.c_str());
58 string delimiter = child->getStringValue("delimiter");
59 if (delimiter.empty()) {
61 child->setStringValue("delimiter", delimiter.c_str());
64 log.interval_ms = child->getLongValue("interval-ms");
65 log.last_time_ms = globals->get_sim_time_sec() * 1000;
66 log.delimiter = delimiter.c_str()[0];
67 log.output = new std::ofstream(filename.c_str());
69 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot write log to " << filename);
74 // Process the individual entries (Time is automatic).
76 std::vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
77 (*log.output) << "Time";
78 for (unsigned int j = 0; j < entries.size(); j++) {
79 SGPropertyNode * entry = entries[j];
84 if (!entry->hasValue("property")) {
85 entry->setBoolValue("enabled", false);
89 if (!entry->getBoolValue("enabled"))
92 SGPropertyNode * node =
93 fgGetNode(entry->getStringValue("property"), true);
94 log.nodes.push_back(node);
95 (*log.output) << log.delimiter
96 << entry->getStringValue("title", node->getPath().c_str());
98 (*log.output) << endl;
120 FGLogger::update (double dt)
122 double sim_time_sec = globals->get_sim_time_sec();
123 double sim_time_ms = sim_time_sec * 1000;
124 for (unsigned int i = 0; i < _logs.size(); i++) {
125 while ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
126 _logs[i].last_time_ms += _logs[i].interval_ms;
127 (*_logs[i].output) << sim_time_sec;
128 for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
129 (*_logs[i].output) << _logs[i].delimiter
130 << _logs[i].nodes[j]->getStringValue();
132 (*_logs[i].output) << endl;
139 ////////////////////////////////////////////////////////////////////////
140 // Implementation of FGLogger::Log
141 ////////////////////////////////////////////////////////////////////////
143 FGLogger::Log::Log ()
146 last_time_ms(-999999.0),
151 FGLogger::Log::~Log ()