protocols: refactor protocol logging to a macro (#7324)

this avoids the usage of the unique_ptr PROTO::protocol before it has
been constructed incase one wants to log something inside the
constructor itself, move the logging to macros and print file:linenumber
on ERR,CRIT,WARN and classname on the rest of the levels.
This commit is contained in:
Tom Englund 2024-08-15 18:16:18 +02:00 committed by GitHub
parent 15f942000e
commit 12d9901472
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
45 changed files with 42 additions and 104 deletions

View file

@ -11,6 +11,35 @@
#define PROTO NProtocols
#define EXTRACT_CLASS_NAME() \
[]() constexpr -> std::string_view { \
constexpr std::string_view prettyFunction = __PRETTY_FUNCTION__; \
constexpr size_t colons = prettyFunction.find("::"); \
if (colons != std::string_view::npos) { \
constexpr size_t begin = prettyFunction.substr(0, colons).rfind(' ') + 1; \
constexpr size_t end = colons - begin; \
return prettyFunction.substr(begin, end); \
} else { \
return "Global"; \
} \
}()
#define LOGM(level, ...) \
do { \
std::ostringstream oss; \
if (level == WARN || level == ERR || level == CRIT) { \
oss << "[" << __FILE__ << ":" << __LINE__ << "] "; \
} else if (level == LOG || level == INFO || level == TRACE) { \
oss << "[" << EXTRACT_CLASS_NAME() << "] "; \
} \
if constexpr (std::is_same_v<decltype(__VA_ARGS__), std::string>) { \
oss << __VA_ARGS__; \
Debug::log(level, oss.str()); \
} else { \
Debug::log(level, std::format("{}{}", oss.str(), std::format(__VA_ARGS__))); \
} \
} while (0)
class IWaylandProtocol;
struct IWaylandProtocolDestroyWrapper {
wl_listener listener;
@ -22,15 +51,10 @@ class IWaylandProtocol {
IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name);
virtual ~IWaylandProtocol();
virtual void onDisplayDestroy();
virtual void removeGlobal();
virtual void onDisplayDestroy();
virtual void removeGlobal();
virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) = 0;
template <typename... Args>
void protoLog(LogLevel level, std::format_string<Args...> fmt, Args&&... args) {
Debug::log(level, std::format("[{}] ", m_szName) + std::vformat(fmt.get(), std::make_format_args(args...)));
};
virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) = 0;
IWaylandProtocolDestroyWrapper m_liDisplayDestroy;