Skip to content

Commit

Permalink
Improve the performance of logging. (#1142)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoconni authored Aug 18, 2024
1 parent 42562f7 commit 4d93022
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 25 deletions.
32 changes: 18 additions & 14 deletions src/input_output/FGLog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ HISTORY
INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <iostream>

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

Expand All @@ -53,10 +55,9 @@ void FGLogging::Flush(void)
if (!message.empty()) {
logger->Message(message);
buffer.str("");
logger->Flush();
}

buffer.clear();
logger->Flush();
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -83,19 +84,22 @@ FGXMLLogging::FGXMLLogging(std::shared_ptr<FGLogger> logger, Element* el, LogLev

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

void FGLogConsole::SetLevel(LogLevel level) {
FGLogger::SetLevel(level);
void FGLogConsole::Flush(void) {
switch (level)
{
case LogLevel::BULK:
case LogLevel::DEBUG:
case LogLevel::INFO:
out.tie(&std::cout);
std::cout << buffer.str();
std::cout.flush(); // Force the message to be immediately displayed in the console
break;
default:
out.tie(&std::cerr);
std::cerr << buffer.str();
std::cerr.flush(); // Force the message to be immediately displayed in the console
break;
}

buffer.str("");
}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
Expand All @@ -104,29 +108,29 @@ void FGLogConsole::Format(LogFormat format) {
switch (format)
{
case LogFormat::RED:
out << FGJSBBase::fgred;
buffer << FGJSBBase::fgred;
break;
case LogFormat::BLUE:
out << FGJSBBase::fgblue;
buffer << FGJSBBase::fgblue;
break;
case LogFormat::BOLD:
out << FGJSBBase::highint;
buffer << FGJSBBase::highint;
break;
case LogFormat::NORMAL:
out << FGJSBBase::normint;
buffer << FGJSBBase::normint;
break;
case LogFormat::UNDERLINE_ON:
out << FGJSBBase::underon;
buffer << FGJSBBase::underon;
break;
case LogFormat::UNDERLINE_OFF:
out << FGJSBBase::underoff;
buffer << FGJSBBase::underoff;
break;
case LogFormat::DEFAULT:
out << FGJSBBase::fgdef;
buffer << FGJSBBase::fgdef;
break;
case LogFormat::RESET:
default:
out << FGJSBBase::reset;
buffer << FGJSBBase::reset;
break;
}
}
Expand Down
15 changes: 4 additions & 11 deletions src/input_output/FGLog.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ INCLUDES
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/

#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -148,24 +147,18 @@ class JSBSIM_API FGXMLLogging : public FGLogging
class JSBSIM_API FGLogConsole : public FGLogger
{
public:
FGLogConsole() : out(std::cout.rdbuf()) {}

void SetLevel(LogLevel level) override;
void FileLocation(const std::string& filename, int line) override
{ out << std::endl << "In file " << filename << ": line" << line << std::endl; }
{ buffer << std::endl << "In file " << filename << ": line" << line << std::endl; }
void Format(LogFormat format) override;
void Flush(void) override {
out.flush();
out.clear();
}
void Flush(void) override;

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

private:
std::ostream out;
std::ostringstream buffer;
};
} // namespace JSBSim
#endif

0 comments on commit 4d93022

Please sign in to comment.