diff --git a/src/debug/Log.cpp b/src/debug/Log.cpp index 66c430d5..32d235e1 100644 --- a/src/debug/Log.cpp +++ b/src/debug/Log.cpp @@ -32,3 +32,38 @@ void Debug::wlrLog(wlr_log_importance level, const char* fmt, va_list args) { if (!disableStdout) std::cout << output << "\n"; } + +void Debug::log(LogLevel level, std::string str) { + if (level == TRACE && !trace) + return; + + if (shuttingDown) + return; + + switch (level) { + case LOG: str = "[LOG] " + str; break; + case WARN: str = "[WARN] " + str; break; + case ERR: str = "[ERR] " + str; break; + case CRIT: str = "[CRITICAL] " + str; break; + case INFO: str = "[INFO] " + str; break; + case TRACE: str = "[TRACE] " + str; break; + default: break; + } + + rollingLog += str + "\n"; + if (rollingLog.size() > ROLLING_LOG_SIZE) + rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE); + + if (!disableLogs || !**disableLogs) { + // log to a file + std::ofstream ofs; + ofs.open(logFile, std::ios::out | std::ios::app); + ofs << str << "\n"; + + ofs.close(); + } + + // log it to the stdout too. + if (!disableStdout) + std::cout << str << "\n"; +} \ No newline at end of file diff --git a/src/debug/Log.hpp b/src/debug/Log.hpp index a43d6fee..f1dd3eab 100644 --- a/src/debug/Log.hpp +++ b/src/debug/Log.hpp @@ -31,6 +31,10 @@ namespace Debug { inline std::string rollingLog = ""; // rolling log contains the ROLLING_LOG_SIZE tail of the log void init(const std::string& IS); + + // + void log(LogLevel level, std::string str); + template void log(LogLevel level, std::format_string fmt, Args&&... args) { if (level == TRACE && !trace) @@ -41,16 +45,6 @@ namespace Debug { std::string logMsg = ""; - switch (level) { - case LOG: logMsg += "[LOG] "; break; - case WARN: logMsg += "[WARN] "; break; - case ERR: logMsg += "[ERR] "; break; - case CRIT: logMsg += "[CRITICAL] "; break; - case INFO: logMsg += "[INFO] "; break; - case TRACE: logMsg += "[TRACE] "; break; - default: break; - } - // print date and time to the ofs if (disableTime && !**disableTime) { #ifndef _LIBCPP_VERSION @@ -58,7 +52,6 @@ namespace Debug { #else auto c = std::chrono::hh_mm_ss{std::chrono::system_clock::now() - std::chrono::floor(std::chrono::system_clock::now())}; logMsg += std::format("{:%H}:{:%M}:{:%S}", c.hours(), c.minutes(), c.subseconds()); - #endif } @@ -69,22 +62,7 @@ namespace Debug { // 3. this is actually what std::format in stdlib does logMsg += std::vformat(fmt.get(), std::make_format_args(args...)); - rollingLog += logMsg + "\n"; - if (rollingLog.size() > ROLLING_LOG_SIZE) - rollingLog = rollingLog.substr(rollingLog.size() - ROLLING_LOG_SIZE); - - if (!disableLogs || !**disableLogs) { - // log to a file - std::ofstream ofs; - ofs.open(logFile, std::ios::out | std::ios::app); - ofs << logMsg << "\n"; - - ofs.close(); - } - - // log it to the stdout too. - if (!disableStdout) - std::cout << logMsg << "\n"; + log(level, logMsg); } void wlrLog(wlr_log_importance level, const char* fmt, va_list args); diff --git a/src/protocols/AlphaModifier.cpp b/src/protocols/AlphaModifier.cpp index 12bc3512..55349572 100644 --- a/src/protocols/AlphaModifier.cpp +++ b/src/protocols/AlphaModifier.cpp @@ -3,6 +3,8 @@ #include "../desktop/WLSurface.hpp" #include "../render/Renderer.hpp" +#define LOGM PROTO::alphaModifier->protoLog + CAlphaModifier::CAlphaModifier(SP resource_, wlr_surface* surface_) : resource(resource_), pSurface(surface_) { if (!resource->resource()) return; @@ -21,6 +23,7 @@ CAlphaModifier::CAlphaModifier(SP resource_, wlr_surf resource->setSetMultiplier([this](CWpAlphaModifierSurfaceV1* mod, uint32_t alpha) { if (!pSurface) { + LOGM(ERR, "Resource {:x} tried to setMultiplier but surface is gone", (uintptr_t)mod->resource()); wl_resource_post_error(mod->resource(), WP_ALPHA_MODIFIER_SURFACE_V1_ERROR_NO_SURFACE, "Surface is gone"); return; } @@ -47,7 +50,7 @@ void CAlphaModifier::setSurfaceAlpha(float a) { CWLSurface* surf = CWLSurface::surfaceFromWlr(pSurface); if (!surf) { - Debug::log(ERR, "Error in CAlphaModifier::setSurfaceAlpha: No CWLSurface for given surface??"); + LOGM(ERR, "CAlphaModifier::setSurfaceAlpha: No CWLSurface for given surface??"); return; } @@ -93,7 +96,7 @@ void CAlphaModifierProtocol::destroyModifier(CAlphaModifier* modifier) { } if (!deadptr) { - Debug::log(ERR, "CAlphaModifierProtocol::destroyModifier: dead resource but no deadptr???"); + LOGM(ERR, "CAlphaModifierProtocol::destroyModifier: dead resource but no deadptr???"); return; } @@ -103,6 +106,7 @@ void CAlphaModifierProtocol::destroyModifier(CAlphaModifier* modifier) { void CAlphaModifierProtocol::onGetSurface(CWpAlphaModifierV1* pMgr, uint32_t id, wlr_surface* surface) { if (m_mAlphaModifiers.contains(surface)) { + LOGM(ERR, "AlphaModifier already present for surface {:x}", (uintptr_t)surface); wl_resource_post_error(pMgr->resource(), WP_ALPHA_MODIFIER_V1_ERROR_ALREADY_CONSTRUCTED, "AlphaModifier already present"); return; } diff --git a/src/protocols/CursorShape.cpp b/src/protocols/CursorShape.cpp index e04cccb4..fb26d7a2 100644 --- a/src/protocols/CursorShape.cpp +++ b/src/protocols/CursorShape.cpp @@ -1,6 +1,8 @@ #include "CursorShape.hpp" #include +#define LOGM PROTO::cursorShape->protoLog + // clang-format off constexpr const char* SHAPE_NAMES[] = { "invalid", @@ -72,6 +74,7 @@ void CCursorShapeProtocol::onGetTabletToolV2(CWpCursorShapeManagerV1* pMgr, uint void CCursorShapeProtocol::createCursorShapeDevice(CWpCursorShapeManagerV1* pMgr, uint32_t id, wl_resource* resource) { if (m_mDevices.contains(resource)) { + LOGM(ERR, "CursorShape device already exists for {:x}", (uintptr_t)resource); wl_resource_post_error(resource, 0, "Device already exists"); return; } diff --git a/src/protocols/FractionalScale.cpp b/src/protocols/FractionalScale.cpp index 993d963b..86662d90 100644 --- a/src/protocols/FractionalScale.cpp +++ b/src/protocols/FractionalScale.cpp @@ -1,5 +1,7 @@ #include "FractionalScale.hpp" +#define LOGM PROTO::fractional->protoLog + static void onWlrSurfaceDestroy(void* owner, void* data) { const auto SURF = (wlr_surface*)owner; @@ -28,6 +30,12 @@ void CFractionalScaleProtocol::onManagerResourceDestroy(wl_resource* res) { } void CFractionalScaleProtocol::onGetFractionalScale(CWpFractionalScaleManagerV1* pMgr, uint32_t id, wlr_surface* surface) { + if (m_mAddons.contains(surface)) { + LOGM(ERR, "Surface {:x} already has a fractionalScale addon", (uintptr_t)surface); + wl_resource_post_error(pMgr->resource(), WP_FRACTIONAL_SCALE_MANAGER_V1_ERROR_FRACTIONAL_SCALE_EXISTS, "Fractional scale already exists"); + return; + } + const auto PADDON = m_mAddons .emplace(surface, std::make_unique( diff --git a/src/protocols/GammaControl.cpp b/src/protocols/GammaControl.cpp index fa7fddd7..b0f9af7a 100644 --- a/src/protocols/GammaControl.cpp +++ b/src/protocols/GammaControl.cpp @@ -4,6 +4,8 @@ #include "../helpers/Monitor.hpp" #include "../Compositor.hpp" +#define LOGM PROTO::gamma->protoLog + CGammaControl::CGammaControl(SP resource_, wl_resource* output) : resource(resource_) { if (!resource_->resource()) return; @@ -11,7 +13,7 @@ CGammaControl::CGammaControl(SP resource_, wl_resource* out wlr_output* wlrOutput = wlr_output_from_resource(output); if (!wlrOutput) { - Debug::log(ERR, "[gamma] No wlr_output"); + LOGM(ERR, "No wlr_output in CGammaControl"); resource->sendFailed(); return; } @@ -19,7 +21,7 @@ CGammaControl::CGammaControl(SP resource_, wl_resource* out pMonitor = g_pCompositor->getRealMonitorFromOutput(wlrOutput); if (!pMonitor) { - Debug::log(ERR, "[gamma] No CMonitor"); + LOGM(ERR, "No CMonitor"); resource->sendFailed(); return; } @@ -34,7 +36,7 @@ CGammaControl::CGammaControl(SP resource_, wl_resource* out gammaSize = wlr_output_get_gamma_size(wlrOutput); if (gammaSize <= 0) { - Debug::log(ERR, "[gamma] Output {} doesn't support gamma", pMonitor->szName); + LOGM(ERR, "Output {} doesn't support gamma", pMonitor->szName); resource->sendFailed(); return; } @@ -45,18 +47,18 @@ CGammaControl::CGammaControl(SP resource_, wl_resource* out resource->setOnDestroy([this](CZwlrGammaControlV1* gamma) { PROTO::gamma->destroyGammaControl(this); }); resource->setSetGamma([this](CZwlrGammaControlV1* gamma, int32_t fd) { - Debug::log(LOG, "[gamma] setGamma for {}", pMonitor->szName); + LOGM(LOG, "setGamma for {}", pMonitor->szName); int fdFlags = fcntl(fd, F_GETFL, 0); if (fdFlags < 0) { - Debug::log(ERR, "[gamma] Failed to get fd flags"); + LOGM(ERR, "Failed to get fd flags"); resource->sendFailed(); close(fd); return; } if (fcntl(fd, F_SETFL, fdFlags | O_NONBLOCK) < 0) { - Debug::log(ERR, "[gamma] Failed to set fd flags"); + LOGM(ERR, "Failed to set fd flags"); resource->sendFailed(); close(fd); return; @@ -64,7 +66,7 @@ CGammaControl::CGammaControl(SP resource_, wl_resource* out ssize_t readBytes = pread(fd, gammaTable.data(), gammaTable.size() * sizeof(uint16_t), 0); if (readBytes < 0 || (size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) { - Debug::log(ERR, "[gamma] Failed to read bytes"); + LOGM(ERR, "Failed to read bytes"); close(fd); if ((size_t)readBytes != gammaTable.size() * sizeof(uint16_t)) { @@ -103,7 +105,7 @@ void CGammaControl::applyToMonitor() { if (!pMonitor) return; // ?? - Debug::log(LOG, "[gamma] setting to monitor {}", pMonitor->szName); + LOGM(LOG, "setting to monitor {}", pMonitor->szName); if (!gammaTableSet) { wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), 0, nullptr, nullptr, nullptr); @@ -117,7 +119,7 @@ void CGammaControl::applyToMonitor() { wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), gammaSize, red, green, blue); if (!pMonitor->state.test()) { - Debug::log(LOG, "[gamma] setting to monitor {} failed", pMonitor->szName); + LOGM(LOG, "setting to monitor {} failed", pMonitor->szName); wlr_output_state_set_gamma_lut(pMonitor->state.wlr(), 0, nullptr, nullptr, nullptr); } diff --git a/src/protocols/WaylandProtocol.cpp b/src/protocols/WaylandProtocol.cpp index 988836db..30120235 100644 --- a/src/protocols/WaylandProtocol.cpp +++ b/src/protocols/WaylandProtocol.cpp @@ -13,18 +13,18 @@ void IWaylandProtocol::onDisplayDestroy() { wl_global_destroy(m_pGlobal); } -IWaylandProtocol::IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name) { +IWaylandProtocol::IWaylandProtocol(const wl_interface* iface, const int& ver, const std::string& name) : m_szName(name) { m_pGlobal = wl_global_create(g_pCompositor->m_sWLDisplay, iface, ver, this, &bindManagerInternal); if (!m_pGlobal) { - Debug::log(ERR, "[proto {}] could not create a global", name); + protoLog(ERR, "could not create a global"); return; } m_liDisplayDestroy.notify = displayDestroyInternal; wl_display_add_destroy_listener(g_pCompositor->m_sWLDisplay, &m_liDisplayDestroy); - Debug::log(LOG, "[proto {}] started", name); + protoLog(LOG, "Registered global"); } IWaylandProtocol::~IWaylandProtocol() { diff --git a/src/protocols/WaylandProtocol.hpp b/src/protocols/WaylandProtocol.hpp index d23a0e48..b0a33900 100644 --- a/src/protocols/WaylandProtocol.hpp +++ b/src/protocols/WaylandProtocol.hpp @@ -24,7 +24,13 @@ class IWaylandProtocol { virtual void bindManager(wl_client* client, void* data, uint32_t ver, uint32_t id) = 0; + template + void protoLog(LogLevel level, std::format_string fmt, Args&&... args) { + Debug::log(level, std::format("[{}] ", m_szName) + std::vformat(fmt.get(), std::make_format_args(args...))); + }; + private: + std::string m_szName; wl_global* m_pGlobal = nullptr; wl_listener m_liDisplayDestroy; }; \ No newline at end of file diff --git a/src/protocols/XDGDecoration.cpp b/src/protocols/XDGDecoration.cpp index 7a5303a3..b62f89bc 100644 --- a/src/protocols/XDGDecoration.cpp +++ b/src/protocols/XDGDecoration.cpp @@ -1,6 +1,8 @@ #include "XDGDecoration.hpp" #include +#define LOGM PROTO::xdgDecoration->protoLog + CXDGDecoration::CXDGDecoration(SP resource_, wl_resource* toplevel) : resource(resource_), pToplevelResource(toplevel) { if (!resource->resource()) return; @@ -16,13 +18,12 @@ CXDGDecoration::CXDGDecoration(SP resource_, wl_resou default: modeString = "INVALID"; break; } - Debug::log(LOG, "[xdgDecoration] setMode: {}. {} MODE_SERVER_SIDE as reply.", modeString, - (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE ? "Sending" : "Ignoring and sending")); + LOGM(LOG, "setMode: {}. {} MODE_SERVER_SIDE as reply.", modeString, (mode == ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE ? "Sending" : "Ignoring and sending")); resource->sendConfigure(ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); }); resource->setUnsetMode([this](CZxdgToplevelDecorationV1*) { - Debug::log(LOG, "[xdgDecoration] unsetMode. Sending MODE_SERVER_SIDE."); + LOGM(LOG, "unsetMode. Sending MODE_SERVER_SIDE."); resource->sendConfigure(ZXDG_TOPLEVEL_DECORATION_V1_MODE_SERVER_SIDE); }); } diff --git a/src/protocols/XDGOutput.cpp b/src/protocols/XDGOutput.cpp index 551d9d90..d472df92 100644 --- a/src/protocols/XDGOutput.cpp +++ b/src/protocols/XDGOutput.cpp @@ -10,6 +10,8 @@ // +#define LOGM PROTO::xdgOutput->protoLog + void CXDGOutputProtocol::onManagerResourceDestroy(wl_resource* res) { std::erase_if(m_vManagerResources, [&](const auto& other) { return other->resource() == res; }); } @@ -22,7 +24,7 @@ void CXDGOutputProtocol::bindManager(wl_client* client, void* data, uint32_t ver const auto RESOURCE = m_vManagerResources.emplace_back(std::make_unique(client, ver, id)).get(); if (!RESOURCE->resource()) { - Debug::log(LOG, "Couldn't bind XDGOutputMgr"); + LOGM(LOG, "Couldn't bind XDGOutputMgr"); wl_client_post_no_memory(client); return; }