Skip to content

Commit 6cd0291

Browse files
authored
Logging redesign (cont'd) (#1136)
1 parent b41fcdd commit 6cd0291

File tree

15 files changed

+409
-319
lines changed

15 files changed

+409
-319
lines changed

src/input_output/FGLog.cpp

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,28 @@ CLASS IMPLEMENTATION
4848

4949
void FGLogging::Flush(void)
5050
{
51-
logger->Message(buffer.str());
52-
buffer.str("");
53-
logger->Format(LogFormat::RESET);
54-
logger->Flush();
51+
std::string message = buffer.str();
52+
53+
if (!message.empty()) {
54+
logger->Message(message);
55+
buffer.str("");
56+
logger->Format(LogFormat::RESET);
57+
logger->Flush();
58+
}
59+
5560
buffer.clear();
5661
}
5762

5863
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5964

6065
FGLogging& FGLogging::operator<<(LogFormat format) {
61-
logger->Message(buffer.str());
62-
buffer.str("");
66+
std::string message = buffer.str();
67+
68+
if (!message.empty()) {
69+
logger->Message(message);
70+
buffer.str("");
71+
}
72+
6373
logger->Format(format);
6474
return *this;
6575
}

src/input_output/FGLog.h

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ INCLUDES
4646

4747
#include "simgear/misc/sg_path.hxx"
4848
#include "FGJSBBase.h"
49+
#include "math/FGColumnVector3.h"
4950

5051
/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
5152
FORWARD DECLARATIONS
@@ -55,9 +56,13 @@ namespace JSBSim {
5556

5657
class Element;
5758

58-
// The return type of std::setprecision is unspecified by the C++ standard so we
59-
// need some C++ magic to be able to overload the operator<< for std::setprecision
59+
// The return type of these functions is unspecified by the C++ standard so we
60+
// need some C++ magic to be able to overload the operator<< for these functions.
6061
using setprecision_t = decltype(std::setprecision(0));
62+
// For MSVC set_precision_t and setw_t are the same type
63+
#ifndef _MSC_VER
64+
using setw_t = decltype(std::setw(0));
65+
#endif
6166

6267
enum class LogLevel {
6368
BULK, // For frequent messages
@@ -114,10 +119,18 @@ class JSBSIM_API FGLogging
114119
virtual ~FGLogging() { Flush(); }
115120
FGLogging& operator<<(const char* message) { buffer << message ; return *this; }
116121
FGLogging& operator<<(const std::string& message) { buffer << message ; return *this; }
117-
FGLogging& operator<<(unsigned int value) { buffer << value; return *this; }
122+
template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
123+
FGLogging& operator<<(T value) { buffer << value; return *this; }
118124
FGLogging& operator<<(std::ostream& (*manipulator)(std::ostream&)) { buffer << manipulator; return *this; }
125+
FGLogging& operator<<(std::ios_base& (*manipulator)(std::ios_base&)) { buffer << manipulator; return *this; }
119126
FGLogging& operator<<(setprecision_t value) { buffer << value; return *this; }
127+
// Avoid duplicate definition for MSVC for which set_precision_t and setw_t
128+
// are the same type
129+
#ifndef _MSC_VER
130+
FGLogging& operator<<(setw_t value) { buffer << value; return *this; }
131+
#endif
120132
FGLogging& operator<<(const SGPath& path) { buffer << path; return *this; }
133+
FGLogging& operator<<(const FGColumnVector3& vec) { buffer << vec; return *this; }
121134
FGLogging& operator<<(LogFormat format);
122135
std::string str(void) const { return buffer.str(); }
123136
void Flush(void);

src/input_output/FGModelLoader.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ HISTORY
3737
INCLUDES
3838
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
3939

40-
#include "FGJSBBase.h"
40+
#include "FGFDMExec.h"
4141
#include "FGModelLoader.h"
4242
#include "FGXMLFileRead.h"
4343
#include "models/FGModel.h"
44+
#include "input_output/FGLog.h"
4445

4546
using namespace std;
4647

@@ -67,8 +68,8 @@ Element_ptr FGModelLoader::Open(Element *el)
6768
else {
6869
document = XMLFileRead.LoadXMLDocument(path);
6970
if (document == 0L) {
70-
cerr << endl << el->ReadFrom()
71-
<< "Could not open file: " << fname << endl;
71+
FGXMLLogging log(model->GetExec()->GetLogger(), el, LogLevel::ERROR);
72+
log << "Could not open file: " << fname << endl;
7273
return NULL;
7374
}
7475
CachedFiles[path.utf8Str()] = document;
@@ -83,6 +84,8 @@ Element_ptr FGModelLoader::Open(Element *el)
8384
return document;
8485
}
8586

87+
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
88+
8689
SGPath CheckPathName(const SGPath& path, const SGPath& filename) {
8790
SGPath fullName = path/filename.utf8Str();
8891

src/input_output/FGfdmSocket.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ INCLUDES
4444
#if defined(_MSC_VER) || defined(__MINGW32__)
4545
#include <winsock.h>
4646
#include <io.h>
47+
#undef ERROR
4748
#else
4849
#include <netdb.h>
4950
#endif

src/models/FGAtmosphere.cpp

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ INCLUDES
4444

4545
#include "FGFDMExec.h"
4646
#include "FGAtmosphere.h"
47+
#include "input_output/FGLog.h"
4748

4849
namespace JSBSim {
4950

@@ -104,8 +105,9 @@ double FGAtmosphere::ValidatePressure(double p, const string& msg, bool quiet) c
104105
const double MinPressure = ConvertToPSF(1E-15, ePascals);
105106
if (p < MinPressure) {
106107
if (!quiet) {
107-
cerr << msg << " " << p << " is too low." << endl
108-
<< msg << " is capped to " << MinPressure << endl;
108+
FGLogging log(FDMExec->GetLogger(), LogLevel::WARN);
109+
log << msg << " " << p << " is too low." << endl
110+
<< msg << " will be capped to " << MinPressure << endl;
109111
}
110112
return MinPressure;
111113
}
@@ -124,8 +126,9 @@ double FGAtmosphere::ValidateTemperature(double t, const string& msg, bool quiet
124126

125127
if (t < minUniverseTemperature) {
126128
if (!quiet) {
127-
cerr << msg << " " << t << " is too low." << endl
128-
<< msg << " is capped to " << minUniverseTemperature << endl;
129+
FGLogging log(FDMExec->GetLogger(), LogLevel::WARN);
130+
log << msg << " " << t << " is too low." << endl
131+
<< msg << " will be capped to " << minUniverseTemperature << endl;
129132
}
130133
return minUniverseTemperature;
131134
}
@@ -354,8 +357,9 @@ void FGAtmosphere::Debug(int from)
354357
}
355358
}
356359
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
357-
if (from == 0) std::cout << "Instantiated: FGAtmosphere" << std::endl;
358-
if (from == 1) std::cout << "Destroyed: FGAtmosphere" << std::endl;
360+
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
361+
if (from == 0) log << "Instantiated: FGAtmosphere" << std::endl;
362+
if (from == 1) log << "Destroyed: FGAtmosphere" << std::endl;
359363
}
360364
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
361365
}

src/models/FGAuxiliary.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ INCLUDES
4848
#include "input_output/FGPropertyManager.h"
4949
#include "FGInertial.h"
5050
#include "FGAtmosphere.h"
51+
#include "input_output/FGLog.h"
5152

5253
using namespace std;
5354

@@ -444,7 +445,9 @@ void FGAuxiliary::bind(void)
444445

445446
double FGAuxiliary::BadUnits(void) const
446447
{
447-
cerr << "Bad units" << endl; return 0.0;
448+
FGLogging log(FDMExec->GetLogger(), LogLevel::ERROR);
449+
log << "Bad units" << endl;
450+
return 0.0;
448451
}
449452

450453
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
@@ -476,18 +479,20 @@ void FGAuxiliary::Debug(int from)
476479
}
477480
}
478481
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
479-
if (from == 0) cout << "Instantiated: FGAuxiliary" << endl;
480-
if (from == 1) cout << "Destroyed: FGAuxiliary" << endl;
482+
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
483+
if (from == 0) log << "Instantiated: FGAuxiliary" << endl;
484+
if (from == 1) log << "Destroyed: FGAuxiliary" << endl;
481485
}
482486
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
483487
}
484488
if (debug_lvl & 8 ) { // Runtime state variables
485489
}
486490
if (debug_lvl & 16) { // Sanity checking
491+
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
487492
if (Mach > 100 || Mach < 0.00)
488-
cout << "FGPropagate::Mach is out of bounds: " << Mach << endl;
493+
log << "FGPropagate::Mach is out of bounds: " << Mach << endl;
489494
if (qbar > 1e6 || qbar < 0.00)
490-
cout << "FGPropagate::qbar is out of bounds: " << qbar << endl;
495+
log << "FGPropagate::qbar is out of bounds: " << qbar << endl;
491496
}
492497
if (debug_lvl & 64) {
493498
if (from == 0) { // Constructor

0 commit comments

Comments
 (0)