]> git.mxchange.org Git - flightgear.git/blob - src/Main/logger.cxx
Support for multiple data dirs.
[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 #ifdef HAVE_CONFIG_H
7 #  include "config.h"
8 #endif
9
10 #include "logger.hxx"
11
12 #include <fstream>
13 #include <string>
14
15 #include <simgear/debug/logstream.hxx>
16
17 #include "fg_props.hxx"
18
19 using std::string;
20 using std::endl;
21 \f
22 ////////////////////////////////////////////////////////////////////////
23 // Implementation of FGLogger
24 ////////////////////////////////////////////////////////////////////////
25
26 FGLogger::FGLogger ()
27 {
28 }
29
30 FGLogger::~FGLogger ()
31 {
32 }
33
34 void
35 FGLogger::init ()
36 {
37   SGPropertyNode * logging = fgGetNode("/logging");
38   if (logging == 0)
39     return;
40
41   std::vector<SGPropertyNode_ptr> children = logging->getChildren("log");
42   for (unsigned int i = 0; i < children.size(); i++) {
43
44     SGPropertyNode * child = children[i];
45
46     if (!child->getBoolValue("enabled", false))
47         continue;
48
49     _logs.push_back(Log());
50     Log &log = _logs[_logs.size()-1];
51     
52     string filename = child->getStringValue("filename");
53     if (filename.empty()) {
54         filename = "fg_log.csv";
55         child->setStringValue("filename", filename.c_str());
56     }
57
58     string delimiter = child->getStringValue("delimiter");
59     if (delimiter.empty()) {
60         delimiter = ",";
61         child->setStringValue("delimiter", delimiter.c_str());
62     }
63         
64     log.interval_ms = child->getLongValue("interval-ms");
65     log.last_time_ms = globals->get_sim_time_sec() * 1000;
66     log.delimiter = delimiter.c_str()[0];
67     log.output = new std::ofstream(filename.c_str());
68     if (!log.output) {
69       SG_LOG(SG_GENERAL, SG_ALERT, "Cannot write log to " << filename);
70       continue;
71     }
72
73     //
74     // Process the individual entries (Time is automatic).
75     //
76     std::vector<SGPropertyNode_ptr> entries = child->getChildren("entry");
77     (*log.output) << "Time";
78     for (unsigned int j = 0; j < entries.size(); j++) {
79       SGPropertyNode * entry = entries[j];
80
81       //
82       // Set up defaults.
83       //
84       if (!entry->hasValue("property")) {
85           entry->setBoolValue("enabled", false);
86           continue;
87       }
88
89       if (!entry->getBoolValue("enabled"))
90           continue;
91
92       SGPropertyNode * node =
93         fgGetNode(entry->getStringValue("property"), true);
94       log.nodes.push_back(node);
95       (*log.output) << log.delimiter
96                     << entry->getStringValue("title", node->getPath().c_str());
97     }
98     (*log.output) << endl;
99   }
100 }
101
102 void
103 FGLogger::reinit ()
104 {
105     _logs.clear();
106     init();
107 }
108
109 void
110 FGLogger::bind ()
111 {
112 }
113
114 void
115 FGLogger::unbind ()
116 {
117 }
118
119 void
120 FGLogger::update (double dt)
121 {
122     double sim_time_sec = globals->get_sim_time_sec();
123     double sim_time_ms = sim_time_sec * 1000;
124     for (unsigned int i = 0; i < _logs.size(); i++) {
125         while ((sim_time_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
126             _logs[i].last_time_ms += _logs[i].interval_ms;
127             (*_logs[i].output) << sim_time_sec;
128             for (unsigned int j = 0; j < _logs[i].nodes.size(); j++) {
129                 (*_logs[i].output) << _logs[i].delimiter
130                            << _logs[i].nodes[j]->getStringValue();
131             }
132             (*_logs[i].output) << endl;
133         }
134     }
135 }
136
137
138 \f
139 ////////////////////////////////////////////////////////////////////////
140 // Implementation of FGLogger::Log
141 ////////////////////////////////////////////////////////////////////////
142
143 FGLogger::Log::Log ()
144   : output(0),
145     interval_ms(0),
146     last_time_ms(-999999.0),
147     delimiter(',')
148 {
149 }
150
151 FGLogger::Log::~Log ()
152 {
153   delete output;
154 }
155
156 // end of logger.cxx