]> git.mxchange.org Git - flightgear.git/blob - src/Main/logger.cxx
Depreciate NetworkOLK. A big thanks goes to Oliver Delise for implementing it in...
[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 STL_FSTREAM
9 #include <string>
10
11 SG_USING_STD(ofstream);
12 SG_USING_STD(endl);
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_ptr> children = logging->getChildren("log");
41   for (unsigned int i = 0; i < children.size(); i++) {
42
43     SGPropertyNode * child = children[i];
44
45     if (!child->getBoolValue("enabled", false))
46         continue;
47
48     _logs.push_back(Log());
49     Log &log = _logs[_logs.size()-1];
50     
51     string filename = child->getStringValue("filename");
52     if (filename.size() == 0) {
53         filename = "fg_log.csv";
54         child->setStringValue("filename", filename.c_str());
55     }
56
57     string delimiter = child->getStringValue("delimiter");
58     if (delimiter.size() == 0) {
59         delimiter = ",";
60         child->setStringValue("delimiter", delimiter.c_str());
61     }
62         
63     log.interval_ms = child->getLongValue("interval-ms");
64     log.delimiter = delimiter.c_str()[0];
65     log.output = new ofstream(filename.c_str());
66     if (!log.output) {
67       SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
68       continue;
69     }
70
71     //
72     // Process the individual entries (Time is automatic).
73     //
74     vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
75     (*log.output) << "Time";
76     for (unsigned int j = 0; j < entries.size(); j++) {
77       SGPropertyNode * entry = entries[j];
78
79       //
80       // Set up defaults.
81       //
82       if (!entry->hasValue("property")) {
83           entry->setBoolValue("enabled", false);
84           continue;
85       }
86
87       if (!entry->getBoolValue("enabled"))
88           continue;
89
90       SGPropertyNode * node =
91         fgGetNode(entry->getStringValue("property"), true);
92       log.nodes.push_back(node);
93       (*log.output) << log.delimiter
94                     << entry->getStringValue("title", node->getPath());
95     }
96     (*log.output) << endl;
97   }
98 }
99
100 void
101 FGLogger::reinit ()
102 {
103     _logs.clear();
104     init();
105 }
106
107 void
108 FGLogger::bind ()
109 {
110 }
111
112 void
113 FGLogger::unbind ()
114 {
115 }
116
117 void
118 FGLogger::update (double dt)
119 {
120   double sim_time_ms = globals->get_sim_time_sec() * 1000;
121   for (unsigned int i = 0; i < _logs.size(); i++) {
122     if ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
123       _logs[i].last_time_ms = sim_time_ms;
124       (*_logs[i].output) << sim_time_ms;
125       for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
126         (*_logs[i].output) << _logs[i].delimiter
127                            << _logs[i].nodes[j]->getStringValue();
128       }
129       (*_logs[i].output) << endl;
130     }
131   }
132 }
133
134
135 \f
136 ////////////////////////////////////////////////////////////////////////
137 // Implementation of FGLogger::Log
138 ////////////////////////////////////////////////////////////////////////
139
140 FGLogger::Log::Log ()
141   : output(0),
142     interval_ms(0),
143     last_time_ms(-999999.0),
144     delimiter(',')
145 {
146 }
147
148 FGLogger::Log::~Log ()
149 {
150   delete output;
151 }
152
153 // end of logger.cxx