Scanframe Modular Application 0.1.0
Loading...
Searching...
No Matches
logger.h
Go to the documentation of this file.
1#pragma once
2
3#include "global.h"
4#include <array>
5#include <iomanip>
6#include <ios>
7#include <ostream>
8#include <sstream>
9#include <string>
10#if IS_QT
11 #include <QString>
12#else
13 #include <cstdint>
14#endif
15
16namespace sf
17{
18
31_LOG_FUNC void setLogMessagePattern(const std::string& pattern);
32
36enum class LogLevel : uint8_t
37{
38 Debug = 0,
42};
43
51{
52
53 public:
58 explicit LoggingCategory(const char* name);
59
64
65 // Prevent copying and moving to preserve category identity and bridge safety.
70
74 const char* categoryName() const noexcept;
75
80 bool isEnabled(LogLevel level) const noexcept;
81
87 void setEnabled(LogLevel level, bool enable) noexcept;
88
89 private:
97 const char* _name{nullptr};
105 std::array<bool, 4> _enabledLevels{true, true, true, true};
106
107 class Private;
114 Private* _d{nullptr};
115};
116
121{
122 const char* file{nullptr};
123 int line{0};
124 const char* function{nullptr};
125 const char* categoryName{nullptr};
126};
127
131using LogHandler = void (*)(LogLevel level, const LogLocation& location, const std::string& message);
132
139
144
149{
150 public:
155 {
156 _buffer << std::boolalpha;
157 }
158
162 LogStream(const LogStream&) = delete;
163
167 LogStream(const LogStream&&) = delete;
168
172 LogStream& operator=(const LogStream&) = delete;
173
177 LogStream& operator=(const LogStream&&) = delete;
178
182 ~LogStream() = default;
183
187 void escapeString(const std::string& str);
188
192 template<typename T>
193 LogStream& operator<<(const T& value)
194 {
195 if constexpr (std::is_base_of_v<std::string, std::decay_t<T>>)
196 {
197 escapeString(value);
198 }
199#if IS_QT_TARGET
200 else if constexpr (std::is_base_of_v<QString, std::decay_t<T>>)
201 {
202 escapeString(value.toStdString());
203 }
204#endif
205 else
206 {
207 _buffer << value;
208 }
209 if (_space)
210 {
211 _buffer << ' ';
212 }
213 return *this;
214 }
215
219 LogStream& operator<<(std::ostream& (*manip)(std::ostream&) )
220 {
221 manip(_buffer);
222 return *this;
223 }
224
228 LogStream& operator<<(std::ios_base& (*manip)(std::ios_base&) )
229 {
230 manip(_buffer);
231 return *this;
232 }
233
237 std::string str() const
238 {
239 return _buffer.str();
240 }
241
243 {
244 _space = false;
245 return *this;
246 }
247
248 private:
249 std::ostringstream _buffer;
250 bool _space{true};
251};
252
257{
258 public:
262 LogMessage(LogLevel level, const LoggingCategory& category, const char* file, int line, const char* function);
263
268
269 LogMessage(const LogMessage&) = delete;
270 LogMessage& operator=(const LogMessage&) = delete;
273
277 LogStream& stream() noexcept
278 {
279 return _stream;
280 }
281
282 private:
283 LogLevel _level;
284 const LoggingCategory& _category;
285 const char* _file{nullptr};
286 int _line{0};
287 const char* _function{nullptr};
288 LogStream _stream;
289};
290
295{
296 public:
297 template<typename T>
298 void operator&(const T& value) const noexcept
299 {}
300};
301
302}// namespace sf
303
308#define SF_DECLARE_LOGGING_CATEGORY(name) static const sf::LoggingCategory& name();
309
314#define SF_LOGGING_CATEGORY(name, stringName) \
315 static const sf::LoggingCategory& name() \
316 { \
317 static const sf::LoggingCategory category(stringName); \
318 return category; \
319 }
320
321#define SF_MESSAGE_LOGGER_COMMON(category, level) \
322 if (category().isEnabled(level)) \
323 sf::LogMessage(level, category(), __FILE__, __LINE__, __func__).stream()
324
325#ifndef SF_NO_DEBUG_OUTPUT
326 #define sfCDebug(category) SF_MESSAGE_LOGGER_COMMON(category, sf::LogLevel::Debug)
327#else
328 #define sfCDebug(category) \
329 while (false) \
330 sf::LogMessageVoidify() & sf::LogMessage(sf::LogLevel::Debug, category(), __FILE__, __LINE__, __func__).stream()
331#endif
332
333#ifndef SF_NO_INFO_OUTPUT
334 #define sfCInfo(category) SF_MESSAGE_LOGGER_COMMON(category, sf::LogLevel::Info)
335#else
336 #define sfCInfo(category) \
337 while (false) \
338 sf::LogMessageVoidify() & sf::LogMessage(sf::LogLevel::Info, category(), __FILE__, __LINE__, __func__).stream()
339#endif
340
341#ifndef SF_NO_WARNING_OUTPUT
342 #define sfCWarning(category) SF_MESSAGE_LOGGER_COMMON(category, sf::LogLevel::Warning)
343#else
344 #define sfCWarning(category) \
345 while (false) \
346 sf::LogMessageVoidify() & sf::LogMessage(sf::LogLevel::Warning, category(), __FILE__, __LINE__, __func__).stream()
347#endif
348
349#define sfCCritical(category) SF_MESSAGE_LOGGER_COMMON(category, sf::LogLevel::Critical)
Helper structure used to discard stripped log expressions at compile time.
Definition logger.h:295
void operator&(const T &value) const noexcept
Definition logger.h:298
LogStream & stream() noexcept
Returns the underlying stream buffer for chained logging.
Definition logger.h:277
LogMessage & operator=(const LogMessage &)=delete
~LogMessage()
Destructor. Flushes buffered message to backend / handlers.
LogMessage(const LogMessage &)=delete
LogMessage(LogMessage &&)=delete
LogMessage(LogLevel level, const LoggingCategory &category, const char *file, int line, const char *function)
Constructs a log message context.
LogMessage & operator=(LogMessage &&)=delete
Stream buffer helper for formatted logging output.
Definition logger.h:149
LogStream & operator<<(std::ios_base &(*manip)(std::ios_base &))
Stream insertion operator for ios_base manipulators (e.g. std::hex).
Definition logger.h:228
void escapeString(const std::string &str)
Escape a string by adding escape characters.
LogStream & operator=(const LogStream &)=delete
Copy assignment operator deleted to prevent accidental copying.
LogStream & operator<<(const T &value)
Stream insertion operator for general types.
Definition logger.h:193
std::string str() const
Returns the accumulated formatted string.
Definition logger.h:237
~LogStream()=default
Destructor.
LogStream & operator<<(std::ostream &(*manip)(std::ostream &))
Stream insertion operator for ostream manipulators (e.g. std::endl).
Definition logger.h:219
LogStream(const LogStream &&)=delete
Move constructor deleted to prevent accidental copying.
LogStream & operator=(const LogStream &&)=delete
Move assignment operator deleted to prevent accidental copying.
LogStream()
Constructs a log stream with default formatting options.
Definition logger.h:154
LogStream & nospace()
Definition logger.h:242
LogStream(const LogStream &)=delete
Copy constructor deleted to prevent accidental copying.
Represents a category used for logging purposes. Provides a way to group and control log messages bas...
Definition logger.h:51
LoggingCategory & operator=(LoggingCategory &&)=delete
LoggingCategory(const LoggingCategory &)=delete
~LoggingCategory()
Destructor. Releases internal bridge resources if allocated.
LoggingCategory(const char *name)
Constructs a logging category with the given unique name.
LoggingCategory(LoggingCategory &&)=delete
void setEnabled(LogLevel level, bool enable) noexcept
Dynamically enables or disables a specific log level for this category.
bool isEnabled(LogLevel level) const noexcept
Checks if a specific log level is enabled for this category.
LoggingCategory & operator=(const LoggingCategory &)=delete
const char * categoryName() const noexcept
Gets the category name string.
#define _LOG_CLASS
Definition log/global.h:35
#define _LOG_FUNC
Definition log/global.h:34
Definition Application.h:10
LogLevel
Severity levels for logging output.
Definition logger.h:37
@ Warning
Definition logger.h:40
@ Critical
Definition logger.h:41
@ Info
Definition logger.h:39
@ Debug
Definition logger.h:38
TARGET_IMPORT LogHandler installLogHandler(LogHandler handler)
Installs a custom message handler callback.
TARGET_IMPORT void setLogMessagePattern(const std::string &pattern)
Sets the pattern for formatting log messages. This method configures the format used to display log m...
TARGET_IMPORT LogHandler defaultLogHandler()
Gets the default log message handler.
void(*)(LogLevel level, const LogLocation &location, const std::string &message) LogHandler
Custom log message handler callback type.
Definition logger.h:131
Source code location metadata captured during log message generation.
Definition logger.h:121
int line
Definition logger.h:123
const char * categoryName
Definition logger.h:125
const char * function
Definition logger.h:124
const char * file
Definition logger.h:122