Skip to content

Commit

Permalink
Logging redesign - part 3 (JSBSim-Team#1147)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni authored Sep 3, 2024
1 parent e3ac75a commit e607c27
Show file tree
Hide file tree
Showing 10 changed files with 156 additions and 122 deletions.
2 changes: 1 addition & 1 deletion src/input_output/FGLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ FGXMLLogging::FGXMLLogging(std::shared_ptr<FGLogger> logger, Element* el, LogLev
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

void FGLogConsole::Flush(void) {
switch (level)
switch (log_level)
{
case LogLevel::BULK:
case LogLevel::DEBUG:
Expand Down
12 changes: 6 additions & 6 deletions src/input_output/FGLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,13 @@ class JSBSIM_API FGLogger
{
public:
virtual ~FGLogger() {}
virtual void SetLevel(LogLevel l) { level = l; }
virtual void SetLevel(LogLevel level) { log_level = level;}
virtual void FileLocation(const std::string& filename, int line) {}
void SetMinLevel(LogLevel level) { min_level = level; }
virtual void Message(const std::string& message) = 0;
virtual void Format(LogFormat format) {}
virtual void Flush(void) {}
protected:
LogLevel level = LogLevel::BULK;
LogLevel min_level = LogLevel::INFO;
LogLevel log_level = LogLevel::BULK;
};

class JSBSIM_API FGLogging
Expand Down Expand Up @@ -147,18 +145,20 @@ class JSBSIM_API FGXMLLogging : public FGLogging
class JSBSIM_API FGLogConsole : public FGLogger
{
public:
void SetMinLevel(LogLevel level) { min_level = level; }
void FileLocation(const std::string& filename, int line) override
{ buffer << std::endl << "In file " << filename << ": line " << line << std::endl; }
{ buffer << "\nIn file " << filename << ": line " << line << "\n"; }
void Format(LogFormat format) override;
void Flush(void) override;

void Message(const std::string& message) override {
// if (level < min_level) return;
if (log_level < min_level) return;
buffer << message;
}

private:
std::ostringstream buffer;
LogLevel min_level = LogLevel::BULK;
};
} // namespace JSBSim
#endif
6 changes: 4 additions & 2 deletions src/models/FGAccelerations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ INCLUDES

#include "FGAccelerations.h"
#include "FGFDMExec.h"
#include "input_output/FGLog.h"

using namespace std;

Expand Down Expand Up @@ -411,8 +412,9 @@ void FGAccelerations::Debug(int from)
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) cout << "Instantiated: FGAccelerations" << endl;
if (from == 1) cout << "Destroyed: FGAccelerations" << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGAccelerations\n";
if (from == 1) log << "Destroyed: FGAccelerations\n";
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
Expand Down
120 changes: 62 additions & 58 deletions src/models/FGAerodynamics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include "FGAerodynamics.h"
#include "FGFDMExec.h"
#include "input_output/FGXMLElement.h"
#include "input_output/FGLog.h"

using namespace std;

Expand Down Expand Up @@ -230,11 +232,10 @@ bool FGAerodynamics::Run(bool Holding)
break;
default:
{
stringstream s;
s << " A proper axis type has NOT been selected. Check "
<< "your aerodynamics definition.";
cerr << endl << s.str() << endl;
throw BaseException(s.str());
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
log << "\n A proper axis type has NOT been selected. Check "
<< "your aerodynamics definition.\n";
throw BaseException(log.str());
}
}
// Calculate aerodynamic reference point shift, if any. The shift takes place
Expand Down Expand Up @@ -277,11 +278,10 @@ bool FGAerodynamics::Run(bool Holding)
break;
default:
{
stringstream s;
s << " A proper axis type has NOT been selected. Check "
<< "your aerodynamics definition.";
cerr << endl << s.str() << endl;
throw BaseException(s.str());
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
log << "\n A proper axis type has NOT been selected. Check "
<< "your aerodynamics definition.\n";
throw BaseException(log.str());
}
}

Expand Down Expand Up @@ -381,18 +381,18 @@ bool FGAerodynamics::Load(Element *document)
try {
ca.push_back( new FGFunction(FDMExec, function_element) );
} catch (const string& str) {
cerr << endl << axis_element->ReadFrom()
<< endl << fgred << "Error loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting." << reset << endl;
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::ERROR);
log << LogFormat::RED << "\nError loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting.\n" << LogFormat::RESET;
return false;
}
} else {
try {
ca_atCG.push_back( new FGFunction(FDMExec, function_element) );
} catch (const string& str) {
cerr << endl << axis_element->ReadFrom()
<< endl << fgred << "Error loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting." << reset << endl;
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::ERROR);
log << LogFormat::RED << "\nError loading aerodynamic function in "
<< current_func_name << ":" << str << " Aborting.\n" << LogFormat::RESET;
return false;
}
}
Expand Down Expand Up @@ -464,43 +464,43 @@ void FGAerodynamics::DetermineAxisSystem(Element* document)
} else if (axis == "LIFT" || axis == "DRAG") {
if (forceAxisType == atNone) forceAxisType = atWind;
else if (forceAxisType != atWind) {
cerr << endl << axis_element->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (LIFT DRAG)" << endl;
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (LIFT DRAG)\n";
}
} else if (axis == "SIDE") {
if (forceAxisType != atNone && forceAxisType != atWind && forceAxisType != atBodyAxialNormal) {
cerr << endl << axis_element->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (SIDE)" << endl;
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (SIDE)\n";
}
} else if (axis == "AXIAL" || axis == "NORMAL") {
if (forceAxisType == atNone) forceAxisType = atBodyAxialNormal;
else if (forceAxisType != atBodyAxialNormal) {
cerr << endl << axis_element->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (NORMAL AXIAL)" << endl;
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the"
<< " aircraft config file. (NORMAL AXIAL)\n";
}
} else { // error
stringstream s;
s << axis_element->ReadFrom()
<< endl << " An unknown axis type, " << axis << " has been specified"
<< " in the aircraft configuration file.";
cerr << endl << s.str() << endl;
throw BaseException(s.str());
FGXMLLogging log(FDMExec->GetLogger(), axis_element, LogLevel::FATAL);
log << "\n An unknown axis type, " << axis << " has been specified"
<< " in the aircraft configuration file.\n";
throw BaseException(log.str());
}
axis_element = document->FindNextElement("axis");
}

if (forceAxisType == atNone) {
forceAxisType = atWind;
cerr << endl << " The aerodynamic axis system has been set by default"
<< " to the Lift/Side/Drag system." << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::INFO);
log << "\n The aerodynamic axis system has been set by default"
<< " to the Lift/Side/Drag system.\n";
}
if (momentAxisType == atNone) {
momentAxisType = atBodyXYZ;
cerr << endl << " The aerodynamic moment axis system has been set by default"
<< " to the bodyXYZ system." << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::INFO);
log << "\n The aerodynamic moment axis system has been set by default"
<< " to the bodyXYZ system.\n";
}
}

Expand All @@ -512,30 +512,32 @@ void FGAerodynamics::ProcessAxesNameAndFrame(eAxisType& axisType, const string&
{
if (frame == "BODY" || frame.empty()) {
if (axisType == atNone) axisType = atBodyXYZ;
else if (axisType != atBodyXYZ)
cerr << endl << el->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - BODY" << endl;
else if (axisType != atBodyXYZ) {
FGXMLLogging log(FDMExec->GetLogger(), el, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - BODY\n";
}
}
else if (frame == "STABILITY") {
if (axisType == atNone) axisType = atStability;
else if (axisType != atStability)
cerr << endl << el->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - STABILITY" << endl;
else if (axisType != atStability) {
FGXMLLogging log(FDMExec->GetLogger(), el, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - STABILITY\n";
}
}
else if (frame == "WIND") {
if (axisType == atNone) axisType = atWind;
else if (axisType != atWind)
cerr << endl << el->ReadFrom()
<< endl << " Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - WIND" << endl;
else if (axisType != atWind){
FGXMLLogging log(FDMExec->GetLogger(), el, LogLevel::WARN);
log << "\n Mixed aerodynamic axis systems have been used in the "
<< " aircraft config file." << validNames << " - WIND\n";
}
}
else {
stringstream s;
s << " Unknown axis frame type of - " << frame;
cerr << endl << s.str() << endl;
throw BaseException(s.str());
FGXMLLogging log(FDMExec->GetLogger(), el, LogLevel::FATAL);
log << "\n Unknown axis frame type of - " << frame << "\n";
throw BaseException(log.str());
}
}

Expand Down Expand Up @@ -696,28 +698,30 @@ void FGAerodynamics::Debug(int from)

if (debug_lvl & 1) { // Standard console startup message output
if (from == 2) { // Loader
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
switch (forceAxisType) {
case (atWind):
cout << endl << " Aerodynamics (Lift|Side|Drag axes):" << endl << endl;
log << "\n Aerodynamics (Lift|Side|Drag axes):\n\n";
break;
case (atBodyAxialNormal):
cout << endl << " Aerodynamics (Axial|Side|Normal axes):" << endl << endl;
log << "\n Aerodynamics (Axial|Side|Normal axes):\n\n";
break;
case (atBodyXYZ):
cout << endl << " Aerodynamics (Body X|Y|Z axes):" << endl << endl;
log << "\n Aerodynamics (Body X|Y|Z axes):\n\n";
break;
case (atStability):
cout << endl << " Aerodynamics (Stability X|Y|Z axes):" << endl << endl;
log << "\n Aerodynamics (Stability X|Y|Z axes):\n\n";
break;
case (atNone):
cout << endl << " Aerodynamics (undefined axes):" << endl << endl;
log << "\n Aerodynamics (undefined axes):\n\n";
break;
}
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) cout << "Instantiated: FGAerodynamics" << endl;
if (from == 1) cout << "Destroyed: FGAerodynamics" << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGAerodynamics\n";
if (from == 1) log << "Destroyed: FGAerodynamics\n";
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
Expand Down
32 changes: 18 additions & 14 deletions src/models/FGAircraft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,9 @@ INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include "FGAircraft.h"
#include "FGFDMExec.h"
#include "input_output/FGXMLElement.h"
#include "input_output/FGLog.h"

using namespace std;

Expand Down Expand Up @@ -222,23 +224,25 @@ void FGAircraft::Debug(int from)

if (debug_lvl & 1) { // Standard console startup message output
if (from == 2) { // Loading
cout << endl << " Aircraft Metrics:" << endl;
cout << " WingArea: " << WingArea << endl;
cout << " WingSpan: " << WingSpan << endl;
cout << " Incidence: " << WingIncidence << endl;
cout << " Chord: " << cbar << endl;
cout << " H. Tail Area: " << HTailArea << endl;
cout << " H. Tail Arm: " << HTailArm << endl;
cout << " V. Tail Area: " << VTailArea << endl;
cout << " V. Tail Arm: " << VTailArm << endl;
cout << " Eyepoint (x, y, z): " << vXYZep << endl;
cout << " Ref Pt (x, y, z): " << vXYZrp << endl;
cout << " Visual Ref Pt (x, y, z): " << vXYZvrp << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
log << "\n Aircraft Metrics:\n" << fixed;
log << " WingArea: " << WingArea << "\n";
log << " WingSpan: " << WingSpan << "\n";
log << " Incidence: " << WingIncidence << "\n";
log << " Chord: " << cbar << "\n";
log << " H. Tail Area: " << HTailArea << "\n";
log << " H. Tail Arm: " << HTailArm << "\n";
log << " V. Tail Area: " << VTailArea << "\n";
log << " V. Tail Arm: " << VTailArm << "\n";
log << " Eyepoint (x, y, z): " << vXYZep << "\n";
log << " Ref Pt (x, y, z): " << vXYZrp << "\n";
log << " Visual Ref Pt (x, y, z): " << vXYZvrp << "\n";
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) cout << "Instantiated: FGAircraft" << endl;
if (from == 1) cout << "Destroyed: FGAircraft" << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGAircraft\n";
if (from == 1) log << "Destroyed: FGAircraft\n";
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
Expand Down
15 changes: 9 additions & 6 deletions src/models/FGBuoyantForces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ INCLUDES
#include "FGFDMExec.h"
#include "FGBuoyantForces.h"
#include "input_output/FGXMLElement.h"
#include "input_output/FGLog.h"

using namespace std;

Expand Down Expand Up @@ -126,7 +127,7 @@ bool FGBuoyantForces::Load(Element *document)
Cells.push_back(new FGGasCell(FDMExec, gas_cell_element, Cells.size(), in));
gas_cell_element = document->FindNextElement("gas_cell");
}

PostLoad(document, FDMExec);

if (!NoneDefined) {
Expand Down Expand Up @@ -165,15 +166,15 @@ const FGColumnVector3& FGBuoyantForces::GetGasMassMoment(void)
const FGMatrix33& FGBuoyantForces::GetGasMassInertia(void)
{
size_t size = Cells.size();

if (size == 0) return gasCellJ;

gasCellJ.InitMatrix();

for (unsigned int i=0; i < size; i++) {
gasCellJ += Cells[i]->GetInertia();
}

return gasCellJ;
}

Expand Down Expand Up @@ -281,12 +282,14 @@ void FGBuoyantForces::Debug(int from)

if (debug_lvl & 1) { // Standard console startup message output
if (from == 2) { // Loader
cout << endl << " Buoyant Forces: " << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
log << "\n Buoyant Forces: \n";
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) cout << "Instantiated: FGBuoyantForces" << endl;
if (from == 1) cout << "Destroyed: FGBuoyantForces" << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGBuoyantForces\n";
if (from == 1) log << "Destroyed: FGBuoyantForces\n";
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
Expand Down
Loading

0 comments on commit e607c27

Please sign in to comment.