]> git.mxchange.org Git - flightgear.git/blob - src/Main/logger.cxx
Added new logging module.
[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.output = new ofstream(filename.c_str());
48     if (!log.output) {
49       SG_LOG(SG_INPUT, SG_ALERT, "Cannot write log to " << filename);
50       continue;
51     }
52     vector<SGPropertyNode *> entries = child->getChildren("entry");
53     (*log.output) << "Time";
54     for (int j = 0; j < entries.size(); j++) {
55       SGPropertyNode * entry = entries[j];
56       SGPropertyNode * node =
57         fgGetNode(entry->getStringValue("property"), true);
58       log.nodes.push_back(node);
59       (*log.output) << ',' 
60                     << entry->getStringValue("title", node->getPath());
61     }
62     (*log.output) << endl;
63   }
64 }
65
66 void
67 FGLogger::bind ()
68 {
69 }
70
71 void
72 FGLogger::unbind ()
73 {
74 }
75
76 void
77 FGLogger::update (int dt)
78 {
79   long elapsed_ms = globals->get_elapsed_time_ms();
80   for (int i = 0; i < _logs.size(); i++) {
81     if ((elapsed_ms - _logs[i].last_time_ms) >= _logs[i].interval_ms) {
82       _logs[i].last_time_ms = elapsed_ms;
83       (*_logs[i].output) << globals->get_elapsed_time_ms();
84       for (int j = 0; j < _logs[i].nodes.size(); j++) {
85         (*_logs[i].output) << ',' << _logs[i].nodes[j]->getStringValue();
86       }
87       (*_logs[i].output) << endl;
88     }
89   }
90 }
91
92
93 \f
94 ////////////////////////////////////////////////////////////////////////
95 // Implementation of FGLogger::Log
96 ////////////////////////////////////////////////////////////////////////
97
98 FGLogger::Log::Log ()
99   : output(0),
100     interval_ms(0),
101     last_time_ms(-99999999999999L)
102 {
103 }
104
105 FGLogger::Log::~Log ()
106 {
107   delete output;
108 }
109
110 // end of logger.cxx