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.
|
|
|
|
|
|
2008-07-31 12:04:32 +00:00
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
|
# include "config.h"
|
|
|
|
|
#endif
|
|
|
|
|
|
2002-03-12 16:29:00 +00:00
|
|
|
|
#include "logger.hxx"
|
|
|
|
|
|
2017-08-26 14:36:54 +00:00
|
|
|
|
#include <ios>
|
2002-12-31 18:26:02 +00:00
|
|
|
|
#include <string>
|
2017-08-26 14:36:54 +00:00
|
|
|
|
#include <cstdlib>
|
2002-12-31 18:26:02 +00:00
|
|
|
|
|
2002-03-12 16:29:00 +00:00
|
|
|
|
#include <simgear/debug/logstream.hxx>
|
2017-08-26 14:36:54 +00:00
|
|
|
|
#include <simgear/io/iostreams/sgstream.hxx>
|
|
|
|
|
#include <simgear/misc/sg_path.hxx>
|
2002-03-12 16:29:00 +00:00
|
|
|
|
|
|
|
|
|
#include "fg_props.hxx"
|
2017-08-26 14:36:54 +00:00
|
|
|
|
#include "globals.hxx"
|
|
|
|
|
#include "util.hxx"
|
2002-03-12 16:29:00 +00:00
|
|
|
|
|
2008-07-31 12:04:32 +00:00
|
|
|
|
using std::string;
|
|
|
|
|
using std::endl;
|
2002-03-12 16:29:00 +00:00
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGLogger
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGLogger::init ()
|
|
|
|
|
{
|
|
|
|
|
SGPropertyNode * logging = fgGetNode("/logging");
|
|
|
|
|
if (logging == 0)
|
|
|
|
|
return;
|
|
|
|
|
|
2011-10-17 16:41:59 +00:00
|
|
|
|
std::vector<SGPropertyNode_ptr> children = logging->getChildren("log");
|
2017-08-26 16:52:49 +00:00
|
|
|
|
_logs.reserve(children.size());
|
2003-02-01 17:59:52 +00:00
|
|
|
|
|
2020-09-29 21:19:20 +00:00
|
|
|
|
for (const auto& child: children) {
|
2003-02-01 17:59:52 +00:00
|
|
|
|
if (!child->getBoolValue("enabled", false))
|
|
|
|
|
continue;
|
|
|
|
|
|
2017-08-26 16:52:49 +00:00
|
|
|
|
_logs.emplace_back(new Log());
|
|
|
|
|
Log &log = *_logs.back();
|
|
|
|
|
|
2003-02-01 17:59:52 +00:00
|
|
|
|
string filename = child->getStringValue("filename");
|
2013-03-22 02:42:22 +00:00
|
|
|
|
if (filename.empty()) {
|
2003-02-01 17:59:52 +00:00
|
|
|
|
filename = "fg_log.csv";
|
|
|
|
|
child->setStringValue("filename", filename.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-26 14:36:54 +00:00
|
|
|
|
// Security: the path comes from the global Property Tree; it *must* be
|
|
|
|
|
// validated before we overwrite the file.
|
|
|
|
|
const SGPath authorizedPath = fgValidatePath(SGPath::fromUtf8(filename),
|
|
|
|
|
/* write */ true);
|
|
|
|
|
|
|
|
|
|
if (authorizedPath.isNull()) {
|
|
|
|
|
const string propertyPath = child->getChild("filename")
|
|
|
|
|
->getPath(/* simplify */ true);
|
|
|
|
|
const string msg =
|
|
|
|
|
"The FGLogger logging system, via the '" + propertyPath + "' property, "
|
|
|
|
|
"was asked to write to '" + filename + "', however this path is not "
|
|
|
|
|
"authorized for writing anymore for security reasons. " +
|
|
|
|
|
"Please choose another location, for instance in the $FG_HOME/Export "
|
|
|
|
|
"folder (" + (globals->get_fg_home() / "Export").utf8Str() + ").";
|
|
|
|
|
|
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, msg);
|
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-01 17:59:52 +00:00
|
|
|
|
string delimiter = child->getStringValue("delimiter");
|
2013-03-22 02:42:22 +00:00
|
|
|
|
if (delimiter.empty()) {
|
2003-02-01 17:59:52 +00:00
|
|
|
|
delimiter = ",";
|
|
|
|
|
child->setStringValue("delimiter", delimiter.c_str());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
log.interval_ms = child->getLongValue("interval-ms");
|
2003-11-24 01:50:48 +00:00
|
|
|
|
log.last_time_ms = globals->get_sim_time_sec() * 1000;
|
2003-02-01 17:59:52 +00:00
|
|
|
|
log.delimiter = delimiter.c_str()[0];
|
2017-08-26 14:36:54 +00:00
|
|
|
|
// Security: use the return value of fgValidatePath()
|
2017-08-26 17:28:17 +00:00
|
|
|
|
log.output.reset(new sg_ofstream(authorizedPath, std::ios_base::out));
|
2017-08-26 16:52:49 +00:00
|
|
|
|
if ( !(*log.output) ) {
|
2011-08-11 22:09:26 +00:00
|
|
|
|
SG_LOG(SG_GENERAL, SG_ALERT, "Cannot write log to " << filename);
|
2017-08-26 16:52:49 +00:00
|
|
|
|
_logs.pop_back();
|
2002-03-12 16:29:00 +00:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2003-02-01 17:59:52 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// Process the individual entries (Time is automatic).
|
|
|
|
|
//
|
2008-07-31 12:04:32 +00:00
|
|
|
|
std::vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
|
2002-03-12 16:29:00 +00:00
|
|
|
|
(*log.output) << "Time";
|
2002-03-13 17:06:17 +00:00
|
|
|
|
for (unsigned int j = 0; j < entries.size(); j++) {
|
2002-03-12 16:29:00 +00:00
|
|
|
|
SGPropertyNode * entry = entries[j];
|
2003-02-01 17:59:52 +00:00
|
|
|
|
|
|
|
|
|
//
|
|
|
|
|
// 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);
|
2002-03-12 19:55:49 +00:00
|
|
|
|
(*log.output) << log.delimiter
|
2009-12-18 06:15:48 +00:00
|
|
|
|
<< entry->getStringValue("title", node->getPath().c_str());
|
2002-03-12 16:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
(*log.output) << endl;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2003-02-01 17:59:52 +00:00
|
|
|
|
void
|
|
|
|
|
FGLogger::reinit ()
|
|
|
|
|
{
|
|
|
|
|
_logs.clear();
|
|
|
|
|
init();
|
|
|
|
|
}
|
|
|
|
|
|
2002-03-12 16:29:00 +00:00
|
|
|
|
void
|
|
|
|
|
FGLogger::bind ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
FGLogger::unbind ()
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void
|
2002-05-11 16:28:50 +00:00
|
|
|
|
FGLogger::update (double dt)
|
2002-03-12 16:29:00 +00:00
|
|
|
|
{
|
2003-11-24 01:50:48 +00:00
|
|
|
|
double sim_time_sec = globals->get_sim_time_sec();
|
|
|
|
|
double sim_time_ms = sim_time_sec * 1000;
|
|
|
|
|
for (unsigned int i = 0; i < _logs.size(); i++) {
|
2015-11-22 17:11:36 +00:00
|
|
|
|
while ((sim_time_ms - _logs[i]->last_time_ms) >= _logs[i]->interval_ms) {
|
|
|
|
|
_logs[i]->last_time_ms += _logs[i]->interval_ms;
|
|
|
|
|
(*_logs[i]->output) << sim_time_sec;
|
|
|
|
|
for (unsigned int j = 0; j < _logs[i]->nodes.size(); j++) {
|
|
|
|
|
(*_logs[i]->output) << _logs[i]->delimiter
|
|
|
|
|
<< _logs[i]->nodes[j]->getStringValue();
|
2003-11-24 01:50:48 +00:00
|
|
|
|
}
|
2015-11-22 17:11:36 +00:00
|
|
|
|
(*_logs[i]->output) << endl;
|
2003-11-24 01:50:48 +00:00
|
|
|
|
}
|
2002-03-12 16:29:00 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
// Implementation of FGLogger::Log
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
|
|
FGLogger::Log::Log ()
|
2017-08-26 17:28:17 +00:00
|
|
|
|
: interval_ms(0),
|
2002-04-20 14:52:43 +00:00
|
|
|
|
last_time_ms(-999999.0),
|
2002-03-12 19:55:49 +00:00
|
|
|
|
delimiter(',')
|
2002-03-12 16:29:00 +00:00
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 06:46:44 +00:00
|
|
|
|
|
|
|
|
|
// Register the subsystem.
|
|
|
|
|
SGSubsystemMgr::Registrant<FGLogger> registrantFGLogger;
|
|
|
|
|
|
2002-03-12 16:29:00 +00:00
|
|
|
|
// end of logger.cxx
|