45#include "input_output/FGXMLElement.h"
49thread_local FGLogger_ptr GlobalLogger = std::make_shared<FGLogConsole>();
51void SetLogger(FGLogger_ptr logger) { GlobalLogger = logger; }
52FGLogger_ptr
GetLogger(
void) {
return GlobalLogger; }
63 void FileLocation(
const std::string& filename,
int line)
override {
64 this->filename = filename;
67 void Message(
const std::string& message)
override;
68 void Format(LogFormat format)
override;
69 const char* c_str(
void)
const noexcept {
return logMessageBuffer; }
70 ~BufferLogger()
override;
75 std::string_view messageItem;
76 LogFormat format = LogFormat::DEFAULT;
79 char logMessageBuffer[1024];
80 size_t bufferUsed = 0;
81 std::vector<MessageToken> tokens;
89 if (message.empty())
return;
91 size_t available =
sizeof(logMessageBuffer) - bufferUsed - 1;
92 size_t toCopy = std::min(message.size(), available);
96 char* currentPos = logMessageBuffer + bufferUsed;
97 memcpy(currentPos, message.c_str(), toCopy);
99 logMessageBuffer[bufferUsed] =
'\0';
101 auto& new_token = tokens.emplace_back();
102 new_token.messageItem = std::string_view(currentPos, toCopy);
110 auto& new_token = tokens.emplace_back();
111 new_token.format = format;
116BufferLogger::~BufferLogger()
118 if (tokens.empty())
return;
120 GlobalLogger->SetLevel(log_level);
122 if (line > 0) GlobalLogger->FileLocation(filename, line);
124 for (
const auto& token : tokens) {
125 if (token.messageItem.empty()) {
126 GlobalLogger->Format(token.format);
129 GlobalLogger->Message(std::string(token.messageItem));
131 GlobalLogger->Flush();
136FGLogging::FGLogging(LogLevel level)
137 : logger(GlobalLogger)
139 logger->SetLevel(level);
144void FGLogging::Flush(
void)
146 std::string message = buffer.str();
148 if (!message.empty()) {
149 logger->Message(message);
158FGLogging& FGLogging::operator<<(LogFormat format)
160 std::string message = buffer.str();
162 if (!message.empty()) {
163 logger->Message(message);
167 logger->Format(format);
173FGXMLLogging::FGXMLLogging(Element* el, LogLevel level)
176 logger->FileLocation(el->GetFileName(), el->GetLineNumber());
182 if (log_level >= min_level || log_level == LogLevel::STDOUT) {
186 case LogLevel::DEBUG:
188 case LogLevel::STDOUT:
210 case LogFormat::BLUE:
213 case LogFormat::BOLD:
216 case LogFormat::NORMAL:
219 case LogFormat::UNDERLINE_ON:
222 case LogFormat::UNDERLINE_OFF:
225 case LogFormat::DEFAULT:
228 case LogFormat::RESET:
237LogException::LogException()
240 logger->SetLevel(LogLevel::FATAL);
245LogException::LogException(LogException& other)
246: BaseException(
""), FGLogging(other.logger)
253const char* LogException::what() const noexcept
260 const_cast<LogException*
>(
this)->Flush();
261 return static_cast<BufferLogger*
>(logger.get())->c_str();
266XMLLogException::XMLLogException(Element* el)
269 logger->FileLocation(el->GetFileName(), el->GetLineNumber());
void Message(const std::string &message) override
Appends message text. May be called multiple times per log record.
void FileLocation(const std::string &filename, int line) override
Optionally provides source filename and line for contextual diagnostics.
void Format(LogFormat format) override
Applies a formatting hint to subsequent output.
int GetLineNumber(void) const
Returns the line number at which the element has been defined.
const std::string & GetFileName(void) const
Returns the name of the file in which the element has been read.
static char normint[6]
normal intensity text
static char fgred[6]
red text
static char fgblue[6]
blue text
static char underon[5]
underlines text
static char fgdef[6]
default text
static char reset[5]
resets text properties
static char underoff[6]
underline off
static char highint[5]
highlights text
void Flush(void) override
Ends the current log record and commits any buffered output.
void Format(LogFormat format) override
Applies a formatting hint to subsequent output.
Logging backend interface.
Main namespace for the JSBSim Flight Dynamics Model.
FGLogger_ptr GetLogger(void)
Returns the active logger for the current thread.
void SetLogger(FGLogger_ptr logger)
Sets the active logger for the current thread.