Skip to content

Commit

Permalink
Logging redesign (cont'd) (JSBSim-Team#1136)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni authored Aug 11, 2024
1 parent b41fcdd commit 6cd0291
Show file tree
Hide file tree
Showing 15 changed files with 409 additions and 319 deletions.
22 changes: 16 additions & 6 deletions src/input_output/FGLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,28 @@ CLASS IMPLEMENTATION

void FGLogging::Flush(void)
{
logger->Message(buffer.str());
buffer.str("");
logger->Format(LogFormat::RESET);
logger->Flush();
std::string message = buffer.str();

if (!message.empty()) {
logger->Message(message);
buffer.str("");
logger->Format(LogFormat::RESET);
logger->Flush();
}

buffer.clear();
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

FGLogging& FGLogging::operator<<(LogFormat format) {
logger->Message(buffer.str());
buffer.str("");
std::string message = buffer.str();

if (!message.empty()) {
logger->Message(message);
buffer.str("");
}

logger->Format(format);
return *this;
}
Expand Down
19 changes: 16 additions & 3 deletions src/input_output/FGLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ INCLUDES

#include "simgear/misc/sg_path.hxx"
#include "FGJSBBase.h"
#include "math/FGColumnVector3.h"

/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
FORWARD DECLARATIONS
Expand All @@ -55,9 +56,13 @@ namespace JSBSim {

class Element;

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

enum class LogLevel {
BULK, // For frequent messages
Expand Down Expand Up @@ -114,10 +119,18 @@ class JSBSIM_API FGLogging
virtual ~FGLogging() { Flush(); }
FGLogging& operator<<(const char* message) { buffer << message ; return *this; }
FGLogging& operator<<(const std::string& message) { buffer << message ; return *this; }
FGLogging& operator<<(unsigned int value) { buffer << value; return *this; }
template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
FGLogging& operator<<(T value) { buffer << value; return *this; }
FGLogging& operator<<(std::ostream& (*manipulator)(std::ostream&)) { buffer << manipulator; return *this; }
FGLogging& operator<<(std::ios_base& (*manipulator)(std::ios_base&)) { buffer << manipulator; return *this; }
FGLogging& operator<<(setprecision_t value) { buffer << value; return *this; }
// Avoid duplicate definition for MSVC for which set_precision_t and setw_t
// are the same type
#ifndef _MSC_VER
FGLogging& operator<<(setw_t value) { buffer << value; return *this; }
#endif
FGLogging& operator<<(const SGPath& path) { buffer << path; return *this; }
FGLogging& operator<<(const FGColumnVector3& vec) { buffer << vec; return *this; }
FGLogging& operator<<(LogFormat format);
std::string str(void) const { return buffer.str(); }
void Flush(void);
Expand Down
9 changes: 6 additions & 3 deletions src/input_output/FGModelLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ HISTORY
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include "FGJSBBase.h"
#include "FGFDMExec.h"
#include "FGModelLoader.h"
#include "FGXMLFileRead.h"
#include "models/FGModel.h"
#include "input_output/FGLog.h"

using namespace std;

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

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

SGPath CheckPathName(const SGPath& path, const SGPath& filename) {
SGPath fullName = path/filename.utf8Str();

Expand Down
1 change: 1 addition & 0 deletions src/input_output/FGfdmSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ INCLUDES
#if defined(_MSC_VER) || defined(__MINGW32__)
#include <winsock.h>
#include <io.h>
#undef ERROR
#else
#include <netdb.h>
#endif
Expand Down
16 changes: 10 additions & 6 deletions src/models/FGAtmosphere.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ INCLUDES

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

namespace JSBSim {

Expand Down Expand Up @@ -104,8 +105,9 @@ double FGAtmosphere::ValidatePressure(double p, const string& msg, bool quiet) c
const double MinPressure = ConvertToPSF(1E-15, ePascals);
if (p < MinPressure) {
if (!quiet) {
cerr << msg << " " << p << " is too low." << endl
<< msg << " is capped to " << MinPressure << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::WARN);
log << msg << " " << p << " is too low." << endl
<< msg << " will be capped to " << MinPressure << endl;
}
return MinPressure;
}
Expand All @@ -124,8 +126,9 @@ double FGAtmosphere::ValidateTemperature(double t, const string& msg, bool quiet

if (t < minUniverseTemperature) {
if (!quiet) {
cerr << msg << " " << t << " is too low." << endl
<< msg << " is capped to " << minUniverseTemperature << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::WARN);
log << msg << " " << t << " is too low." << endl
<< msg << " will be capped to " << minUniverseTemperature << endl;
}
return minUniverseTemperature;
}
Expand Down Expand Up @@ -354,8 +357,9 @@ void FGAtmosphere::Debug(int from)
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) std::cout << "Instantiated: FGAtmosphere" << std::endl;
if (from == 1) std::cout << "Destroyed: FGAtmosphere" << std::endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGAtmosphere" << std::endl;
if (from == 1) log << "Destroyed: FGAtmosphere" << std::endl;
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
Expand Down
15 changes: 10 additions & 5 deletions src/models/FGAuxiliary.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ INCLUDES
#include "input_output/FGPropertyManager.h"
#include "FGInertial.h"
#include "FGAtmosphere.h"
#include "input_output/FGLog.h"

using namespace std;

Expand Down Expand Up @@ -444,7 +445,9 @@ void FGAuxiliary::bind(void)

double FGAuxiliary::BadUnits(void) const
{
cerr << "Bad units" << endl; return 0.0;
FGLogging log(FDMExec->GetLogger(), LogLevel::ERROR);
log << "Bad units" << endl;
return 0.0;
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand Down Expand Up @@ -476,18 +479,20 @@ void FGAuxiliary::Debug(int from)
}
}
if (debug_lvl & 2 ) { // Instantiation/Destruction notification
if (from == 0) cout << "Instantiated: FGAuxiliary" << endl;
if (from == 1) cout << "Destroyed: FGAuxiliary" << endl;
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (from == 0) log << "Instantiated: FGAuxiliary" << endl;
if (from == 1) log << "Destroyed: FGAuxiliary" << endl;
}
if (debug_lvl & 4 ) { // Run() method entry print for FGModel-derived objects
}
if (debug_lvl & 8 ) { // Runtime state variables
}
if (debug_lvl & 16) { // Sanity checking
FGLogging log(FDMExec->GetLogger(), LogLevel::DEBUG);
if (Mach > 100 || Mach < 0.00)
cout << "FGPropagate::Mach is out of bounds: " << Mach << endl;
log << "FGPropagate::Mach is out of bounds: " << Mach << endl;
if (qbar > 1e6 || qbar < 0.00)
cout << "FGPropagate::qbar is out of bounds: " << qbar << endl;
log << "FGPropagate::qbar is out of bounds: " << qbar << endl;
}
if (debug_lvl & 64) {
if (from == 0) { // Constructor
Expand Down
Loading

0 comments on commit 6cd0291

Please sign in to comment.