Skip to content

Commit 98c0333

Browse files
authored
Logging redesign - part 6 (#1401)
1 parent 4a434e0 commit 98c0333

18 files changed

Lines changed: 474 additions & 391 deletions

julia/jsbsimjl.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
#include <jlcxx/jlcxx.hpp>
22

3+
#if defined(_MSC_VER) || defined(__MINGW32__)
4+
#undef ERROR // Defined by <windows.h>
5+
#endif
6+
37
#include "FGFDMExec.h"
48
#include "initialization/FGInitialCondition.h"
59

src/FGFDMExec.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,12 +1126,12 @@ string FGFDMExec::QueryPropertyCatalog(const string& in, const string& end_of_li
11261126

11271127
void FGFDMExec::PrintPropertyCatalog(void)
11281128
{
1129-
FGLogging log(LogLevel::INFO);
1130-
log << endl
1129+
FGLogging out(LogLevel::STDOUT);
1130+
out << endl
11311131
<< " " << LogFormat::BLUE << highint << LogFormat::UNDERLINE_ON
11321132
<< "Property Catalog for " << modelName << LogFormat::RESET << endl << endl;
11331133
for (auto &catalogElm: PropertyCatalog)
1134-
log << " " << catalogElm << endl;
1134+
out << " " << catalogElm << endl;
11351135
}
11361136

11371137
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

src/initialization/FGTrim.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,29 +98,29 @@ FGTrim::~FGTrim(void) {
9898

9999
void FGTrim::TrimStats() {
100100
int run_sum=0;
101-
FGLogging log(LogLevel::INFO);
102-
log << "\n Trim Statistics:\n";
103-
log << " Total Iterations: " << total_its << "\n";
101+
FGLogging out(LogLevel::STDOUT);
102+
out << "\n Trim Statistics:\n";
103+
out << " Total Iterations: " << total_its << "\n";
104104
if( total_its > 0) {
105-
log << " Sub-iterations:\n";
105+
out << " Sub-iterations:\n";
106106
for (unsigned int current_axis=0; current_axis<TrimAxes.size(); current_axis++) {
107107
run_sum += TrimAxes[current_axis].GetRunCount();
108-
log << " " << setw(5) << TrimAxes[current_axis].GetStateName().c_str()
108+
out << " " << setw(5) << TrimAxes[current_axis].GetStateName().c_str()
109109
<< ": " << setprecision(3) << sub_iterations[current_axis]
110110
<< " average: " << setprecision(5) << sub_iterations[current_axis]/double(total_its)
111111
<< " successful: " << setprecision(3) << successful[current_axis]
112112
<< " stability: " << setprecision(5) << TrimAxes[current_axis].GetAvgStability()
113113
<< "\n";
114114
}
115-
log << " Run Count: " << run_sum << "\n";
115+
out << " Run Count: " << run_sum << "\n";
116116
}
117117
}
118118

119119
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
120120

121121
void FGTrim::Report(void) {
122-
FGLogging log(LogLevel::INFO);
123-
log << " Trim Results:\n";
122+
FGLogging out(LogLevel::STDOUT);
123+
out << " Trim Results:\n";
124124
for(unsigned int current_axis=0; current_axis<TrimAxes.size(); current_axis++)
125125
TrimAxes[current_axis].AxisReport();
126126
}

src/initialization/FGTrimAxis.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -293,17 +293,17 @@ void FGTrimAxis::setThrottlesPct(void) {
293293
/*****************************************************************************/
294294

295295
void FGTrimAxis::AxisReport(void) {
296-
FGLogging log(LogLevel::INFO);
297-
log << " " << left << setw(20) << GetControlName() << ": ";
298-
log << setw(6) << setprecision(2) << GetControl()*control_convert << ' ';
299-
log << setw(5) << GetStateName() << ": ";
300-
log << setw(9) << setprecision(2) << scientific << GetState()+state_target;
301-
log << " Tolerance: " << setw(3) << setprecision(0) << scientific << GetTolerance();
296+
FGLogging out(LogLevel::STDOUT);
297+
out << " " << left << setw(20) << GetControlName() << ": ";
298+
out << setw(6) << setprecision(2) << GetControl()*control_convert << ' ';
299+
out << setw(5) << GetStateName() << ": ";
300+
out << setw(9) << setprecision(2) << scientific << GetState()+state_target;
301+
out << " Tolerance: " << setw(3) << setprecision(0) << scientific << GetTolerance();
302302

303303
if( fabs(GetState()+state_target) < fabs(GetTolerance()) )
304-
log << " Passed\n";
304+
out << " Passed\n";
305305
else
306-
log << " Failed\n";
306+
out << " Failed\n";
307307
}
308308

309309
/*****************************************************************************/

src/input_output/FGLog.cpp

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -179,18 +179,21 @@ FGXMLLogging::FGXMLLogging(Element* el, LogLevel level)
179179
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
180180

181181
void FGLogConsole::Flush(void) {
182-
switch (log_level)
183-
{
184-
case LogLevel::BULK:
185-
case LogLevel::DEBUG:
186-
case LogLevel::INFO:
187-
std::cout << buffer;
188-
std::cout.flush(); // Force the message to be immediately displayed in the console
189-
break;
190-
default:
191-
std::cerr << buffer;
192-
std::cerr.flush(); // Force the message to be immediately displayed in the console
193-
break;
182+
if (log_level >= min_level || log_level == LogLevel::STDOUT) {
183+
switch (log_level)
184+
{
185+
case LogLevel::BULK:
186+
case LogLevel::DEBUG:
187+
case LogLevel::INFO:
188+
case LogLevel::STDOUT:
189+
std::cout << buffer;
190+
std::cout.flush(); // Force the message to be immediately displayed in the console
191+
break;
192+
default:
193+
std::cerr << buffer;
194+
std::cerr.flush(); // Force the message to be immediately displayed in the console
195+
break;
196+
}
194197
}
195198

196199
buffer.clear();

src/input_output/FGLog.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,8 @@ enum class LogLevel {
6969
INFO, // Informatory messages
7070
WARN, // Possible impending problem
7171
ERROR, // Problem that can be recovered
72-
FATAL // Fatal problem => an exception will be thrown
72+
FATAL, // Fatal problem => an exception will be thrown
73+
STDOUT // Standard output - unconditionally displayed.
7374
};
7475

7576
enum class LogFormat {
@@ -135,6 +136,7 @@ class JSBSIM_API FGLogging
135136
FGLogging& operator<<(const SGPath& path) { buffer << path; return *this; }
136137
FGLogging& operator<<(const FGColumnVector3& vec) { buffer << vec; return *this; }
137138
FGLogging& operator<<(LogFormat format);
139+
std::streambuf* rdbuf() { return buffer.rdbuf(); }
138140
void Flush(void);
139141
protected:
140142
FGLogging(FGLogger_ptr l) : logger(l) {}
@@ -155,15 +157,11 @@ class JSBSIM_API FGLogConsole : public FGLogger
155157
void SetMinLevel(LogLevel level) { min_level = level; }
156158
void FileLocation(const std::string& filename, int line) override
157159
{ buffer.append("\nIn file " + filename + ": line " + std::to_string(line) + "\n"); }
160+
void Message(const std::string& message) override { buffer.append(message); }
158161
void Format(LogFormat format) override;
159162
void Flush(void) override;
160163
~FGLogConsole() override { Flush(); }
161164

162-
void Message(const std::string& message) override {
163-
if (log_level < min_level) return;
164-
buffer.append(message);
165-
}
166-
167165
private:
168166
std::string buffer;
169167
LogLevel min_level = LogLevel::BULK;

src/input_output/FGOutputTextFile.cpp

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -83,20 +83,28 @@ bool FGOutputTextFile::Load(Element* el)
8383

8484
bool FGOutputTextFile::OpenFile(void)
8585
{
86-
datafile.clear();
87-
datafile.open(Filename);
88-
if (!datafile) {
89-
FGLogging log(LogLevel::ERROR);
90-
log << LogFormat::RED << LogFormat::BOLD << "\nERROR: unable to open the file "
91-
<< LogFormat::RESET << Filename.c_str()
92-
<< LogFormat::RED << LogFormat::BOLD << "\n => Output to this file is disabled.\n\n"
93-
<< LogFormat::RESET;
94-
Disable();
95-
return false;
86+
unique_ptr<FGLogging> out;
87+
streambuf* buffer = nullptr;
88+
string scratch = Filename.utf8Str();
89+
90+
if (to_upper(scratch) == "COUT") {
91+
out.reset(new FGLogging(LogLevel::STDOUT));
92+
buffer = out->rdbuf();
93+
} else {
94+
datafile.clear();
95+
datafile.open(Filename);
96+
if (!datafile) {
97+
FGLogging log(LogLevel::ERROR);
98+
log << LogFormat::RED << LogFormat::BOLD << "\nERROR: unable to open the file "
99+
<< LogFormat::RESET << Filename.c_str()
100+
<< LogFormat::RED << LogFormat::BOLD << "\n => Output to this file is disabled.\n\n"
101+
<< LogFormat::RESET;
102+
Disable();
103+
return false;
104+
}
105+
buffer = datafile.rdbuf();
96106
}
97107

98-
string scratch = "";
99-
streambuf* buffer = datafile.rdbuf();
100108
ostream outstream(buffer);
101109

102110
outstream.precision(10);
@@ -249,9 +257,11 @@ void FGOutputTextFile::Print(void)
249257
{
250258
streambuf* buffer;
251259
string scratch = Filename.utf8Str();
260+
unique_ptr<FGLogging> out;
252261

253262
if (to_upper(scratch) == "COUT") {
254-
buffer = cout.rdbuf();
263+
out.reset(new FGLogging(LogLevel::STDOUT));
264+
buffer = out->rdbuf();
255265
} else {
256266
buffer = datafile.rdbuf();
257267
}

src/input_output/FGPropertyManager.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,8 @@ void FGPropertyManager::Untie(const string &name)
146146
{
147147
SGPropertyNode* property = root->getNode(name.c_str());
148148
if (!property) {
149-
cerr << "Attempt to untie a non-existant property." << name << endl;
149+
FGLogging log(LogLevel::ERROR);
150+
log << "Attempt to untie a non-existant property." << name << "\n";
150151
return;
151152
}
152153

@@ -165,12 +166,16 @@ void FGPropertyManager::Untie(SGPropertyNode *property)
165166
if (it->node.ptr() == property) {
166167
it->untie();
167168
tied_properties.erase(it);
168-
if (FGJSBBase::debug_lvl & 0x20) cout << "Untied " << name << endl;
169+
if (FGJSBBase::debug_lvl & 0x20) {
170+
FGLogging log(LogLevel::DEBUG);
171+
log << "Untied " << name << "\n";
172+
}
169173
return;
170174
}
171175
}
172176

173-
cerr << "Failed to untie property " << name << endl
174-
<< "JSBSim is not the owner of this property." << endl;
177+
FGLogging log(LogLevel::ERROR);
178+
log << "Failed to untie property " << name
179+
<< "\nJSBSim is not the owner of this property.\n";
175180
}
176181
} // namespace JSBSim

src/input_output/FGPropertyManager.h

Lines changed: 52 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ INCLUDES
5151
#endif
5252

5353
#include "FGJSBBase.h"
54+
#include "input_output/FGLog.h"
5455

5556
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5657
FORWARD DECLARATIONS
@@ -224,15 +225,21 @@ class JSBSIM_API FGPropertyManager
224225
{
225226
SGPropertyNode* property = root->getNode(name.c_str(), true);
226227
if (!property) {
227-
std::cerr << "Could not get or create property " << name << std::endl;
228+
FGLogging log(LogLevel::ERROR);
229+
log << "Could not get or create property " << name << "\n";
228230
return;
229231
}
230232

231-
if (!property->tie(SGRawValuePointer<T>(pointer), false))
232-
std::cerr << "Failed to tie property " << name << " to a pointer" << std::endl;
233+
if (!property->tie(SGRawValuePointer<T>(pointer), false)) {
234+
FGLogging log(LogLevel::ERROR);
235+
log << "Failed to tie property " << name << " to a pointer\n";
236+
}
233237
else {
234238
tied_properties.push_back(PropertyState(property, nullptr));
235-
if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
239+
if (FGJSBBase::debug_lvl & 0x20) {
240+
FGLogging log(LogLevel::DEBUG);
241+
log << name << "\n";
242+
}
236243
}
237244
}
238245

@@ -259,18 +266,23 @@ class JSBSIM_API FGPropertyManager
259266
{
260267
SGPropertyNode* property = root->getNode(name.c_str(), true);
261268
if (!property) {
262-
std::cerr << "Could not get or create property " << name << std::endl;
269+
FGLogging log(LogLevel::ERROR);
270+
log << "Could not get or create property " << name << "\n";
263271
return;
264272
}
265273

266-
if (!property->tie(SGRawValueMethodsEnum<T,V>(*obj, getter, setter), false))
267-
std::cerr << "Failed to tie property " << name << " to object methods"
268-
<< std::endl;
274+
if (!property->tie(SGRawValueMethodsEnum<T,V>(*obj, getter, setter), false)) {
275+
FGLogging log(LogLevel::ERROR);
276+
log << "Failed to tie property " << name << " to object methods\n";
277+
}
269278
else {
270279
tied_properties.push_back(PropertyState(property, obj));
271280
if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
272281
if (!getter) property->setAttribute(SGPropertyNode::READ, false);
273-
if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
282+
if (FGJSBBase::debug_lvl & 0x20) {
283+
FGLogging log(LogLevel::DEBUG);
284+
log << name << "\n";
285+
}
274286
}
275287
}
276288

@@ -281,18 +293,23 @@ class JSBSIM_API FGPropertyManager
281293
{
282294
SGPropertyNode* property = root->getNode(name.c_str(), true);
283295
if (!property) {
284-
std::cerr << "Could not get or create property " << name << std::endl;
296+
FGLogging log(LogLevel::ERROR);
297+
log << "Could not get or create property " << name << "\n";
285298
return;
286299
}
287300

288-
if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), false))
289-
std::cerr << "Failed to tie property " << name << " to object methods"
290-
<< std::endl;
301+
if (!property->tie(SGRawValueMethods<T,V>(*obj, getter, setter), false)) {
302+
FGLogging log(LogLevel::ERROR);
303+
log << "Failed to tie property " << name << " to object methods\n";
304+
}
291305
else {
292306
tied_properties.push_back(PropertyState(property, obj));
293307
if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
294308
if (!getter) property->setAttribute(SGPropertyNode::READ, false);
295-
if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
309+
if (FGJSBBase::debug_lvl & 0x20) {
310+
FGLogging log(LogLevel::DEBUG);
311+
log << name << "\n";
312+
}
296313
}
297314
}
298315

@@ -318,19 +335,25 @@ class JSBSIM_API FGPropertyManager
318335
{
319336
SGPropertyNode* property = root->getNode(name.c_str(), true);
320337
if (!property) {
321-
std::cerr << "Could not get or create property " << name << std::endl;
338+
FGLogging log(LogLevel::ERROR);
339+
log << "Could not get or create property " << name << "\n";
322340
return;
323341
}
324342

325343
if (!property->tie(SGRawValueMethodsIndexed<T,V>(*obj, index, getter, setter),
326-
false))
327-
std::cerr << "Failed to tie property " << name
328-
<< " to indexed object methods" << std::endl;
344+
false)) {
345+
FGLogging log(LogLevel::ERROR);
346+
log << "Failed to tie property " << name
347+
<< " to indexed object methods\n";
348+
}
329349
else {
330350
tied_properties.push_back(PropertyState(property, obj));
331351
if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
332352
if (!getter) property->setAttribute(SGPropertyNode::READ, false);
333-
if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
353+
if (FGJSBBase::debug_lvl & 0x20) {
354+
FGLogging log(LogLevel::DEBUG);
355+
log << name << "\n";
356+
}
334357
}
335358
}
336359

@@ -357,18 +380,23 @@ class JSBSIM_API FGPropertyManager
357380
{
358381
SGPropertyNode* property = root->getNode(name.c_str(), true);
359382
if (!property) {
360-
std::cerr << "Could not get or create property " << name << std::endl;
383+
FGLogging log(LogLevel::ERROR);
384+
log << "Could not get or create property " << name << "\n";
361385
return;
362386
}
363387
if (!property->tie(SGRawValueMethodsIndexedEnum<T, V, U>(*obj, index, getter, setter),
364-
false))
365-
std::cerr << "Failed to tie property " << name
366-
<< " to indexed object methods" << std::endl;
388+
false)) {
389+
FGLogging log(LogLevel::ERROR);
390+
log << "Failed to tie property " << name << " to indexed object methods\n";
391+
}
367392
else {
368393
tied_properties.push_back(PropertyState(property, obj));
369394
if (!setter) property->setAttribute(SGPropertyNode::WRITE, false);
370395
if (!getter) property->setAttribute(SGPropertyNode::READ, false);
371-
if (FGJSBBase::debug_lvl & 0x20) std::cout << name << std::endl;
396+
if (FGJSBBase::debug_lvl & 0x20) {
397+
FGLogging log(LogLevel::DEBUG);
398+
log << name << "\n";
399+
}
372400
}
373401
}
374402

0 commit comments

Comments
 (0)