JSBSim Flight Dynamics Model 1.3.0 (09 Apr 2026)
An Open Source Flight Dynamics and Control Software Library in C++
Loading...
Searching...
No Matches
FGLog.h
1/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
2
3 Header: FGLog.h
4 Author: Bertrand Coconnier
5 Date started: 05/03/24
6
7 ------------- Copyright (C) 2024 Bertrand Coconnier -------------
8
9 This program is free software; you can redistribute it and/or modify it under
10 the terms of the GNU Lesser General Public License as published by the Free
11 Software Foundation; either version 2 of the License, or (at your option) any
12 later version.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
17 details.
18
19 You should have received a copy of the GNU Lesser General Public License along
20 with this program; if not, write to the Free Software Foundation, Inc.,
21 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22
23 Further information about the GNU Lesser General Public License can also be
24 found on the world wide web at http://www.gnu.org.
25
26HISTORY
27--------------------------------------------------------------------------------
2805/03/24 BC Created
29
30%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
31SENTRY
32%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
33
34#ifndef FGLOG_H
35#define FGLOG_H
36
37/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
38INCLUDES
39%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
40
41#include <iomanip>
42#include <memory>
43#include <sstream>
44#include <string>
45
46#include "simgear/misc/sg_path.hxx"
47#include "FGJSBBase.h"
48#include "math/FGColumnVector3.h"
49
50/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
51FORWARD DECLARATIONS
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
53
54namespace JSBSim {
55
56class Element;
57
58// The return type of these functions is unspecified by the C++ standard so we
59// need some C++ magic to be able to overload the operator<< for these functions.
60using setprecision_t = decltype(std::setprecision(0));
61// For MSVC set_precision_t and setw_t are the same type
62#ifndef _MSC_VER
63using setw_t = decltype(std::setw(0));
64#endif
65
66enum class LogLevel {
67 BULK, // For frequent messages
68 DEBUG, // Less frequent debug type messages
69 INFO, // Informatory messages
70 WARN, // Possible impending problem
71 ERROR, // Problem that can be recovered
72 FATAL, // Fatal problem => an exception will be thrown
73 STDOUT // Standard output - unconditionally displayed.
74};
75
76enum class LogFormat {
77 RESET,
78 RED,
79 BLUE,
80 CYAN,
81 GREEN,
82 DEFAULT,
83 BOLD,
84 NORMAL,
85 UNDERLINE_ON,
86 UNDERLINE_OFF
87};
88
89/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
90CLASS DOCUMENTATION
91%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
92
93/*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
94CLASS DECLARATION
95%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
96
124class JSBSIM_API FGLogger
125{
126public:
128 virtual ~FGLogger() {}
130 virtual void SetLevel(LogLevel level) { log_level = level;}
132 virtual void FileLocation(const std::string& filename, int line) {}
134 virtual void Message(const std::string& message) = 0;
136 virtual void Format(LogFormat format) {}
138 virtual void Flush(void) {}
139protected:
140 LogLevel log_level = LogLevel::BULK;
141};
142
143using FGLogger_ptr = std::shared_ptr<FGLogger>;
144
154JSBSIM_API void SetLogger(FGLogger_ptr logger);
155
161JSBSIM_API FGLogger_ptr GetLogger(void);
162
163class JSBSIM_API FGLogging
164{
165public:
166 FGLogging(LogLevel level);
167
168 virtual ~FGLogging() { Flush(); }
169 FGLogging& operator<<(const char* message) { buffer << message ; return *this; }
170 FGLogging& operator<<(const std::string& message) { buffer << message ; return *this; }
171 // Operator for ints and anonymous enums
172 FGLogging& operator<<(int value) { buffer << value; return *this; }
173 // Operator for other numerical types
174 template<typename T, typename = std::enable_if_t<std::is_arithmetic<T>::value>>
175 FGLogging& operator<<(T value) { buffer << value; return *this; }
176 FGLogging& operator<<(std::ostream& (*manipulator)(std::ostream&)) { buffer << manipulator; return *this; }
177 FGLogging& operator<<(std::ios_base& (*manipulator)(std::ios_base&)) { buffer << manipulator; return *this; }
178 FGLogging& operator<<(setprecision_t value) { buffer << value; return *this; }
179 // Avoid duplicate definition for MSVC for which set_precision_t and setw_t
180 // are the same type
181#ifndef _MSC_VER
182 FGLogging& operator<<(setw_t value) { buffer << value; return *this; }
183#endif
184 FGLogging& operator<<(const SGPath& path) { buffer << path; return *this; }
185 FGLogging& operator<<(const FGColumnVector3& vec) { buffer << vec; return *this; }
186 FGLogging& operator<<(LogFormat format);
187 std::streambuf* rdbuf() { return buffer.rdbuf(); }
188 void Flush(void);
189protected:
190 FGLogging(FGLogger_ptr l) : logger(l) {}
191
192 FGLogger_ptr logger;
193 std::ostringstream buffer;
194};
195
196class JSBSIM_API FGXMLLogging : public FGLogging
197{
198public:
199 FGXMLLogging(Element* el, LogLevel level);
200};
201
202class JSBSIM_API FGLogConsole : public FGLogger
203{
204public:
205 void SetMinLevel(LogLevel level) { min_level = level; }
206 void FileLocation(const std::string& filename, int line) override
207 { buffer.append("\nIn file " + filename + ": line " + std::to_string(line) + "\n"); }
208 void Message(const std::string& message) override { buffer.append(message); }
209 void Format(LogFormat format) override;
210 void Flush(void) override;
211 ~FGLogConsole() override { Flush(); }
212
213protected:
214 std::string buffer;
215 LogLevel min_level = LogLevel::BULK;
216};
217
218class JSBSIM_API LogException : public BaseException, public FGLogging
219{
220public:
221 LogException();
223 const char* what() const noexcept override;
224};
225
226class JSBSIM_API XMLLogException : public LogException
227{
228public:
229 // Construct an XMLLogException using the current thread-local logger
230 // and the supplied Element to add file/line location information.
238 XMLLogException(LogException& exception, Element* el);
239};
240} // namespace JSBSim
241#endif
This class implements a 3 element column vector.
void Message(const std::string &message) override
Appends message text. May be called multiple times per log record.
Definition FGLog.h:208
void FileLocation(const std::string &filename, int line) override
Optionally provides source filename and line for contextual diagnostics.
Definition FGLog.h:206
Logging backend interface.
Definition FGLog.h:125
virtual void FileLocation(const std::string &filename, int line)
Optionally provides source filename and line for contextual diagnostics.
Definition FGLog.h:132
virtual void SetLevel(LogLevel level)
Starts a new log record and provides its severity level.
Definition FGLog.h:130
virtual void Message(const std::string &message)=0
Appends message text. May be called multiple times per log record.
virtual void Flush(void)
Ends the current log record and commits any buffered output.
Definition FGLog.h:138
virtual void Format(LogFormat format)
Applies a formatting hint to subsequent output.
Definition FGLog.h:136
virtual ~FGLogger()
Virtual destructor for polymorphic use.
Definition FGLog.h:128
Main namespace for the JSBSim Flight Dynamics Model.
Definition FGFDMExec.cpp:71
FGLogger_ptr GetLogger(void)
Returns the active logger for the current thread.
Definition FGLog.cpp:52
ostream & operator<<(ostream &os, const FGColumnVector3 &col)
Write vector to a stream.
void SetLogger(FGLogger_ptr logger)
Sets the active logger for the current thread.
Definition FGLog.cpp:51