1
0
Fork 0
flightgear/src/Main/logger.cxx

154 lines
3.3 KiB
C++
Raw Normal View History

2002-03-12 16:29:00 +00:00
// logger.cxx - log properties.
// Written by David Megginson, started 2002.
//
// This file is in the Public Domain, and comes with no warranty.
#include "logger.hxx"
#include STL_FSTREAM
#include <string>
2002-03-12 16:29:00 +00:00
SG_USING_STD(ofstream);
SG_USING_STD(endl);
SG_USING_STD(string);
#include <simgear/debug/logstream.hxx>
#include "fg_props.hxx"
////////////////////////////////////////////////////////////////////////
// Implementation of FGLogger
////////////////////////////////////////////////////////////////////////
FGLogger::FGLogger ()
{
}
FGLogger::~FGLogger ()
{
}
void
FGLogger::init ()
{
SGPropertyNode * logging = fgGetNode("/logging");
if (logging == 0)
return;
vector<SGPropertyNode_ptr> children = logging->getChildren("log");
for (unsigned int i = 0; i < children.size(); i++) {
SGPropertyNode * child = children[i];
if (!child->getBoolValue("enabled", false))
continue;
2002-03-12 16:29:00 +00:00
_logs.push_back(Log());
Log &log = _logs[_logs.size()-1];
string filename = child->getStringValue("filename");
if (filename.size() == 0) {
filename = "fg_log.csv";
child->setStringValue("filename", filename.c_str());
}
string delimiter = child->getStringValue("delimiter");
if (delimiter.size() == 0) {
delimiter = ",";
child->setStringValue("delimiter", delimiter.c_str());
}
log.interval_ms = child->getLongValue("interval-ms");
log.delimiter = delimiter.c_str()[0];
2002-03-12 16:29:00 +00:00
log.output = new ofstream(filename.c_str());
if (!log.output) {
SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
continue;
}
//
// Process the individual entries (Time is automatic).
//
vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
2002-03-12 16:29:00 +00:00
(*log.output) << "Time";
for (unsigned int j = 0; j < entries.size(); j++) {
2002-03-12 16:29:00 +00:00
SGPropertyNode * entry = entries[j];
//
// Set up defaults.
//
if (!entry->hasValue("property")) {
entry->setBoolValue("enabled", false);
continue;
}
if (!entry->getBoolValue("enabled"))
continue;
2002-03-12 16:29:00 +00:00
SGPropertyNode * node =
fgGetNode(entry->getStringValue("property"), true);
log.nodes.push_back(node);
(*log.output) << log.delimiter
2002-03-12 16:29:00 +00:00
<< entry->getStringValue("title", node->getPath());
}
(*log.output) << endl;
}
}
void
FGLogger::reinit ()
{
_logs.clear();
init();
}
2002-03-12 16:29:00 +00:00
void
FGLogger::bind ()
{
}
void
FGLogger::unbind ()
{
}
void
FGLogger::update (double dt)
2002-03-12 16:29:00 +00:00
{
double sim_time_ms = globals->get_sim_time_sec() * 1000;
for (unsigned int i = 0; i < _logs.size(); i++) {
if ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
_logs[i].last_time_ms = sim_time_ms;
(*_logs[i].output) << sim_time_ms;
for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
(*_logs[i].output) << _logs[i].delimiter
<< _logs[i].nodes[j]->getStringValue();
2002-03-12 16:29:00 +00:00
}
(*_logs[i].output) << endl;
}
}
}
////////////////////////////////////////////////////////////////////////
// Implementation of FGLogger::Log
////////////////////////////////////////////////////////////////////////
FGLogger::Log::Log ()
: output(0),
interval_ms(0),
last_time_ms(-999999.0),
delimiter(',')
2002-03-12 16:29:00 +00:00
{
}
FGLogger::Log::~Log ()
{
delete output;
}
// end of logger.cxx