From 4d93022dcc1f44cbe3447ce90044a44dffac447d Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 18 Aug 2024 14:57:21 +0200 Subject: [PATCH] Improve the performance of logging. (#1142) --- src/input_output/FGLog.cpp | 32 ++++++++++++++++++-------------- src/input_output/FGLog.h | 15 ++++----------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/input_output/FGLog.cpp b/src/input_output/FGLog.cpp index e7fce0a50..35b7ff9d6 100644 --- a/src/input_output/FGLog.cpp +++ b/src/input_output/FGLog.cpp @@ -37,6 +37,8 @@ HISTORY INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#include + #include "FGLog.h" #include "input_output/FGXMLElement.h" @@ -53,10 +55,9 @@ void FGLogging::Flush(void) if (!message.empty()) { logger->Message(message); buffer.str(""); - logger->Flush(); } - buffer.clear(); + logger->Flush(); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -83,19 +84,22 @@ FGXMLLogging::FGXMLLogging(std::shared_ptr 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(""); } //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% @@ -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; } } diff --git a/src/input_output/FGLog.h b/src/input_output/FGLog.h index 7b12fbaf8..05e068a7b 100644 --- a/src/input_output/FGLog.h +++ b/src/input_output/FGLog.h @@ -39,7 +39,6 @@ INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ #include -#include #include #include #include @@ -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