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 ()
32 for (unsigned int i = 0; i < _logs.size(); i++) {
41 SGPropertyNode * logging = fgGetNode("/logging");
45 std::vector<SGPropertyNode_ptr> children = logging->getChildren("log");
46 for (unsigned int i = 0; i < children.size(); i++) {
48 SGPropertyNode * child = children[i];
50 if (!child->getBoolValue("enabled", false))
53 _logs.push_back(new Log());
54 Log &log = *_logs[_logs.size()-1];
56 string filename = child->getStringValue("filename");
57 if (filename.empty()) {
58 filename = "fg_log.csv";
59 child->setStringValue("filename", filename.c_str());
62 string delimiter = child->getStringValue("delimiter");
63 if (delimiter.empty()) {
65 child->setStringValue("delimiter", delimiter.c_str());
68 log.interval_ms = child->getLongValue("interval-ms");
69 log.last_time_ms = globals->get_sim_time_sec() * 1000;
70 log.delimiter = delimiter.c_str()[0];
71 log.output = new std::ofstream(filename.c_str());
73 SG_LOG(SG_GENERAL, SG_ALERT, "Cannot write log to " << filename);
78 // Process the individual entries (Time is automatic).
80 std::vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
81 (*log.output) << "Time";
82 for (unsigned int j = 0; j < entries.size(); j++) {
83 SGPropertyNode * entry = entries[j];
88 if (!entry->hasValue("property")) {
89 entry->setBoolValue("enabled", false);
93 if (!entry->getBoolValue("enabled"))
96 SGPropertyNode * node =
97 fgGetNode(entry->getStringValue("property"), true);
98 log.nodes.push_back(node);
99 (*log.output) << log.delimiter
100 << entry->getStringValue("title", node->getPath().c_str());
102 (*log.output) << endl;
109 for (unsigned int i = 0; i < _logs.size(); i++) {
127 FGLogger::update (double dt)
129 double sim_time_sec = globals->get_sim_time_sec();
130 double sim_time_ms = sim_time_sec * 1000;
131 for (unsigned int i = 0; i < _logs.size(); i++) {
132 while ((sim_time_ms - _logs[i]->last_time_ms) >= _logs[i]->interval_ms) {
133 _logs[i]->last_time_ms += _logs[i]->interval_ms;
134 (*_logs[i]->output) << sim_time_sec;
135 for (unsigned int j = 0; j < _logs[i]->nodes.size(); j++) {
136 (*_logs[i]->output) << _logs[i]->delimiter
137 << _logs[i]->nodes[j]->getStringValue();
139 (*_logs[i]->output) << endl;
146 ////////////////////////////////////////////////////////////////////////
147 // Implementation of FGLogger::Log
148 ////////////////////////////////////////////////////////////////////////
150 FGLogger::Log::Log ()
153 last_time_ms(-999999.0),
158 FGLogger::Log::~Log ()