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