|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <cstdint> |
| 4 | +#include <string> |
| 5 | + |
| 6 | +namespace libprojectM { |
| 7 | + |
| 8 | +/** |
| 9 | + * @class Logging |
| 10 | + * @brief A simple logger implementation to forward messages to the outside app. |
| 11 | + * |
| 12 | + * This class wraps logging functionality which can be used at either a global or thread-local |
| 13 | + * level, for both callbacks and log levels. |
| 14 | + */ |
| 15 | +class Logging |
| 16 | +{ |
| 17 | +public: |
| 18 | + /** |
| 19 | + * The configurable log levels. If not set, "Information" is used by default. |
| 20 | + */ |
| 21 | + enum class LogLevel : uint8_t |
| 22 | + { |
| 23 | + NotSet, //!< Log level not set |
| 24 | + Trace, //!< Most verbose logging, used to trace individual function calls or values. |
| 25 | + Debug, //!< Debug-related messages for relevant data and developer information. |
| 26 | + Information, //!< Not-too-frequent messages possibly relevant for users and developers. |
| 27 | + Warning, //!< Something is wrong, but doesn't affect execution in a major way. |
| 28 | + Error, //!< A recoverable error occurred which negatively affects projectM, e.g. shader compilation issues. |
| 29 | + Fatal //!< An unrecoverable error occurred and the projectM instance cannot continue execution. |
| 30 | + }; |
| 31 | + |
| 32 | + /** |
| 33 | + * The application callback function. |
| 34 | + */ |
| 35 | + using CallbackFunction = void (*)(const char* message, int severity, void* userData); |
| 36 | + |
| 37 | + /** |
| 38 | + * A struct holding the user callback function and data pointer. |
| 39 | + */ |
| 40 | + struct UserCallback { |
| 41 | + CallbackFunction callbackFunction{}; //!< Function pointer of the user callback. |
| 42 | + void* userData{}; //!< User data pointer for the callback. |
| 43 | + }; |
| 44 | + |
| 45 | + Logging() = delete; |
| 46 | + |
| 47 | + /** |
| 48 | + * Sets the global callback function pointer used across all threads. |
| 49 | + * @param callback A UserCallback struct with the new function and user data pointers. |
| 50 | + */ |
| 51 | + static void SetGlobalCallback(UserCallback callback); |
| 52 | + |
| 53 | + /** |
| 54 | + * Sets the thread-specific callback function pointer only used in the thread which registered it. |
| 55 | + * @param callback A UserCallback struct with the new function and user data pointers. |
| 56 | + */ |
| 57 | + static void SetThreadCallback(UserCallback callback); |
| 58 | + |
| 59 | + /** |
| 60 | + * Sets the global log level used across all threads. |
| 61 | + * @param logLevel The log level to use. If set to LogLevel::NotSet, the value of m_defaultLogLevel is used. |
| 62 | + */ |
| 63 | + static void SetGlobalLogLevel(LogLevel logLevel); |
| 64 | + |
| 65 | + /** |
| 66 | + * Sets the thread-specific log level only used in the thread which set it. |
| 67 | + * @param logLevel The log level to use. If set to LogLevel::NotSet, the value of m_defaultLogLevel is used. |
| 68 | + */ |
| 69 | + static void SetThreadLogLevel(LogLevel logLevel); |
| 70 | + |
| 71 | + /** |
| 72 | + * Returns the effective log level for the current thread. |
| 73 | + * @return The log level set for this thread, or, if LogLevel::NotSet, the global log level. |
| 74 | + * If no global log level is set, it returns the value of m_defaultLogLevel. |
| 75 | + */ |
| 76 | + static auto GetLogLevel() -> LogLevel; |
| 77 | + |
| 78 | + /** |
| 79 | + * Returns whether a callback is registered or not. |
| 80 | + * @return true if a callback is registered for the current thread or globally, false if none is registered. |
| 81 | + */ |
| 82 | + static auto HasCallback() -> bool; |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Passes a log message with the given severity to the active thread or global callback. |
| 86 | + * If no callbacks are registered, this function does nothing. |
| 87 | + * @param message |
| 88 | + * @param severity |
| 89 | + */ |
| 90 | + static void Log(const std::string& message, LogLevel severity); |
| 91 | + |
| 92 | + /** |
| 93 | + * The default log level used if no log level is set (LogLevel::Information) |
| 94 | + */ |
| 95 | + static const LogLevel m_defaultLogLevel; |
| 96 | + |
| 97 | +private: |
| 98 | + /** |
| 99 | + * @brief Returns the active callback for this thread. |
| 100 | + * If the thread has a local callback, this is returned, otherwise the global callback. |
| 101 | + * @return A pointer to the active callback function, or nullptr if none is registered. |
| 102 | + */ |
| 103 | + static auto GetLoggingCallback() -> UserCallback; |
| 104 | + |
| 105 | + static UserCallback m_globalCallback; //!< The global callback function. |
| 106 | + thread_local static UserCallback m_threadCallback; //!< The thread-specific callback function. |
| 107 | + |
| 108 | + static LogLevel m_globalLogLevel; //!< The global log level. |
| 109 | + thread_local static LogLevel m_threadLogLevel; //!< The thread-specific log level. |
| 110 | +}; |
| 111 | + |
| 112 | +#define LOG_TRACE(message) \ |
| 113 | + if (Logging::HasCallback() && Logging::GetLogLevel() == Logging::LogLevel::Trace) \ |
| 114 | + { \ |
| 115 | + Logging::Log(message, Logging::LogLevel::Trace); \ |
| 116 | + } |
| 117 | + |
| 118 | +#define LOG_DEBUG(message) \ |
| 119 | + if (Logging::HasCallback() && Logging::GetLogLevel() <= Logging::LogLevel::Debug) \ |
| 120 | + { \ |
| 121 | + Logging::Log(message, Logging::LogLevel::Debug); \ |
| 122 | + } |
| 123 | + |
| 124 | +#define LOG_INFO(message) \ |
| 125 | + if (Logging::HasCallback() && Logging::GetLogLevel() <= Logging::LogLevel::Information) \ |
| 126 | + { \ |
| 127 | + Logging::Log(message, Logging::LogLevel::Information); \ |
| 128 | + } |
| 129 | + |
| 130 | +#define LOG_WARN(message) \ |
| 131 | + if (Logging::HasCallback() && Logging::GetLogLevel() <= Logging::LogLevel::Warning) \ |
| 132 | + { \ |
| 133 | + Logging::Log(message, Logging::LogLevel::Warning); \ |
| 134 | + } |
| 135 | + |
| 136 | +#define LOG_ERROR(message) \ |
| 137 | + if (Logging::HasCallback() && Logging::GetLogLevel() <= Logging::LogLevel::Error) \ |
| 138 | + { \ |
| 139 | + Logging::Log(message, Logging::LogLevel::Error); \ |
| 140 | + } |
| 141 | + |
| 142 | +#define LOG_FATAL(message) \ |
| 143 | + if (Logging::HasCallback() && Logging::GetLogLevel() <= Logging::LogLevel::Fatal) \ |
| 144 | + { \ |
| 145 | + Logging::Log(message, Logging::LogLevel::Fatal); \ |
| 146 | + } |
| 147 | + |
| 148 | +} // namespace libprojectM |
0 commit comments