]> git.mxchange.org Git - flightgear.git/blob - src/Main/logger.cxx
Added a new 'delimiter' property to allow an alternative delimiter to
[flightgear.git] / src / Main / logger.cxx
1 // logger.cxx - log properties.
2 // Written by David Megginson, started 2002.
3 //
4 // This file is in the Public Domain, and comes with no warranty.
5
6 #include "logger.hxx"
7
8 #include <fstream>
9 SG_USING_STD(ofstream);
10 SG_USING_STD(endl);
11
12 #include <string>
13 SG_USING_STD(string);
14
15 #include <simgear/debug/logstream.hxx>
16
17 #include "fg_props.hxx"
18
19
20 \f
21 ////////////////////////////////////////////////////////////////////////
22 // Implementation of FGLogger
23 ////////////////////////////////////////////////////////////////////////
24
25 FGLogger::FGLogger ()
26 {
27 }
28
29 FGLogger::~FGLogger ()
30 {
31 }
32
33 void
34 FGLogger::init ()
35 {
36   SGPropertyNode * logging = fgGetNode("/logging");
37   if (logging == 0)
38     return;
39
40   vector<SGPropertyNode *> children = logging->getChildren("log");
41   for (int i = 0; i < children.size(); i++) {
42     _logs.push_back(Log());
43     Log &log = _logs[_logs.size()-1];
44     SGPropertyNode * child = children[i];
45     string filename = child->getStringValue("filename", "fg_log.csv");
46     log.interval_ms = child->getLongValue("interval-ms", 0);
47     log.delimiter = child->getStringValue("delimiter", ",")[0];
48     log.output = new ofstream(filename.c_str());
49     if (!log.output) {
50       SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
51       continue;
52     }
53     vector<SGPropertyNode *> entries = child->getChildren("entry");
54     (*log.output) << "Time";
55     for (int j = 0; j < entries.size(); j++) {
56       SGPropertyNode * entry = entries[j];
57       SGPropertyNode * node =
58         fgGetNode(entry->getStringValue("property"), true);
59       log.nodes.push_back(node);
60       (*log.output) << log.delimiter
61                     << entry->getStringValue("title", node->getPath());
62     }
63     (*log.output) << endl;
64   }
65 }
66
67 void
68 FGLogger::bind ()
69 {
70 }
71
72 void
73 FGLogger::unbind ()
74 {
75 }
76
77 void
78 FGLogger::update (int dt)
79 {
80   long elapsed_ms = globals->get_elapsed_time_ms();
81   for (int i = 0; i < _logs.size(); i++) {
82     if ((elapsed_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
83       _logs[i].last_time_ms = elapsed_ms;
84       (*_logs[i].output) << globals->get_elapsed_time_ms();
85       for (int j = 0; j < _logs[i].nodes.size(); j++) {
86         (*_logs[i].output) << _logs[i].delimiter
87                            << _logs[i].nodes[j]->getStringValue();
88       }
89       (*_logs[i].output) << endl;
90     }
91   }
92 }
93
94
95 \f
96 ////////////////////////////////////////////////////////////////////////
97 // Implementation of FGLogger::Log
98 ////////////////////////////////////////////////////////////////////////
99
100 FGLogger::Log::Log ()
101   : output(0),
102     interval_ms(0),
103     last_time_ms(-999999L),
104     delimiter(',')
105 {
106 }
107
108 FGLogger::Log::~Log ()
109 {
110   delete output;
111 }
112
113 // end of logger.cxx