diff options
| author | Yuqian Yang <crupest@crupest.life> | 2025-12-05 20:49:10 +0800 |
|---|---|---|
| committer | Yuqian Yang <crupest@crupest.life> | 2025-12-05 20:49:10 +0800 |
| commit | f5c801de04f423269d9cefa3d725b52b87c7a0b9 (patch) | |
| tree | 51bbda6cf58cdd0a6d9128d073ad00c9337680aa | |
| parent | 5edaa008efabb1b97446bed2f6b542cf01eb3c70 (diff) | |
| download | cru-f5c801de04f423269d9cefa3d725b52b87c7a0b9.tar.gz cru-f5c801de04f423269d9cefa3d725b52b87c7a0b9.tar.bz2 cru-f5c801de04f423269d9cefa3d725b52b87c7a0b9.zip | |
Refactor CRU_LOG_TAG_*.
25 files changed, 145 insertions, 188 deletions
diff --git a/include/cru/base/log/Logger.h b/include/cru/base/log/Logger.h index 671d004e..d98ee3e3 100644 --- a/include/cru/base/log/Logger.h +++ b/include/cru/base/log/Logger.h @@ -2,11 +2,10 @@ #include "../Base.h" #include <condition_variable> -#include <format> // IWYU pragma: keep +#include <format> #include <list> #include <memory> #include <mutex> -#include <string_view> #include <thread> #include <unordered_set> #include <utility> @@ -44,14 +43,6 @@ struct CRU_BASE_API ILogger : virtual Interface { Log(LogInfo(level, std::move(tag), std::move(message))); } - template <typename... Args> - void FormatLog(LogLevel level, std::string tag, - std::string_view message_format, Args&&... args) { - // Clang is buggy in consteval, so we can't use std::format for now. - Log(level, std::move(tag), - std::vformat(message_format, std::make_format_args(args...))); - } - protected: std::unordered_set<std::string> debug_tags_; }; @@ -95,49 +86,20 @@ class CRU_BASE_API Logger : public Object, public virtual ILogger { std::mutex target_list_mutex_; std::vector<std::unique_ptr<ILogTarget>> target_list_; }; - -class CRU_BASE_API LoggerCppStream : public Object { - public: - explicit LoggerCppStream(ILogger* logger, LogLevel level, std::string tag); - - LoggerCppStream WithLevel(LogLevel level) const; - LoggerCppStream WithTag(std::string tag) const; - - private: - void Consume(std::string_view str); - - public: - LoggerCppStream& operator<<(std::string_view str) { - this->Consume(str); - return *this; - } - - template <typename T> - LoggerCppStream& operator<<(T&& arg) { - this->Consume(ToString(std::forward<T>(arg))); - return *this; - } - - private: - ILogger* logger_; - LogLevel level_; - std::string tag_; -}; - } // namespace cru::log -#define CRU_LOG_TAG_DEBUG(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Debug, \ - kLogTag, __VA_ARGS__) - -#define CRU_LOG_TAG_INFO(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Info, \ - kLogTag, __VA_ARGS__) +#define CRU_DEFINE_LOG_FUNC(level) \ + template <typename... Args> \ + void CruLog##level(std::string tag, std::format_string<Args...> message_fmt, \ + Args&&... args) { \ + cru::log::ILogger::GetInstance()->Log( \ + cru::log::LogLevel::level, std::move(tag), \ + std::format(message_fmt, std::forward<Args>(args)...)); \ + } -#define CRU_LOG_TAG_WARN(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Warn, \ - kLogTag, __VA_ARGS__) +CRU_DEFINE_LOG_FUNC(Debug) +CRU_DEFINE_LOG_FUNC(Info) +CRU_DEFINE_LOG_FUNC(Warn) +CRU_DEFINE_LOG_FUNC(Error) -#define CRU_LOG_TAG_ERROR(...) \ - cru::log::ILogger::GetInstance()->FormatLog(cru::log::LogLevel::Error, \ - kLogTag, __VA_ARGS__) +#undef CRU_DEFINE_LOG_FUNC
\ No newline at end of file diff --git a/include/cru/ui/controls/ControlHost.h b/include/cru/ui/controls/ControlHost.h index 5e8d678b..ddd9d893 100644 --- a/include/cru/ui/controls/ControlHost.h +++ b/include/cru/ui/controls/ControlHost.h @@ -100,7 +100,7 @@ class CRU_UI_API ControlHost : public Object { Guard logging_guard([&] { log += "\nEnd dispatching routed event " + (original_sender->*event_ptr)()->GetName() + "."; - CRU_LOG_TAG_DEBUG("{}", log); + CruLogDebug(kLogTag, "{}", log); }); std::vector<ObjectResolver<Control>> receive_list; diff --git a/src/base/log/Logger.cpp b/src/base/log/Logger.cpp index a3582305..d75e006b 100644 --- a/src/base/log/Logger.cpp +++ b/src/base/log/Logger.cpp @@ -167,21 +167,4 @@ void Logger::LogThreadRun() { if (stop) return; } } - -LoggerCppStream::LoggerCppStream(ILogger* logger, LogLevel level, - std::string tag) - : logger_(logger), level_(level), tag_(std::move(tag)) {} - -LoggerCppStream LoggerCppStream::WithLevel(LogLevel level) const { - return LoggerCppStream(this->logger_, level, this->tag_); -} - -LoggerCppStream LoggerCppStream::WithTag(std::string tag) const { - return LoggerCppStream(this->logger_, this->level_, std::move(tag)); -} - -void LoggerCppStream::Consume(std::string_view str) { - this->logger_->Log(this->level_, this->tag_, std::string(str)); -} - } // namespace cru::log diff --git a/src/base/platform/unix/PosixSpawnSubProcess.cpp b/src/base/platform/unix/PosixSpawnSubProcess.cpp index c287d253..dd4f4cd2 100644 --- a/src/base/platform/unix/PosixSpawnSubProcess.cpp +++ b/src/base/platform/unix/PosixSpawnSubProcess.cpp @@ -146,7 +146,7 @@ SubProcessExitResult PosixSpawnSubProcessImpl::PlatformWaitForProcess() { while (waitpid(pid_, &wstatus, 0) == -1) { if (errno == EINTR) { - CRU_LOG_TAG_INFO("Waitpid is interrupted by a signal. Call it again."); + CruLogInfo(kLogTag, "Waitpid is interrupted by a signal. Call it again."); continue; } diff --git a/src/base/platform/unix/UnixFile.cpp b/src/base/platform/unix/UnixFile.cpp index 001b4fb9..3d42516b 100644 --- a/src/base/platform/unix/UnixFile.cpp +++ b/src/base/platform/unix/UnixFile.cpp @@ -21,8 +21,8 @@ UnixFileDescriptor::~UnixFileDescriptor() { constexpr auto kLogTag = "cru::platform::unix::UnixFileDescriptor"; if (this->IsValid() && this->IsAutoClose()) { if (!this->DoClose()) { - CRU_LOG_TAG_ERROR("Failed to close file descriptor {}, errno {}.", - descriptor_, errno); + CruLogError(kLogTag, "Failed to close file descriptor {}, errno {}.", + descriptor_, errno); } } } diff --git a/src/platform/graphics/cairo/CairoPainter.cpp b/src/platform/graphics/cairo/CairoPainter.cpp index 97ef7fe0..30162506 100644 --- a/src/platform/graphics/cairo/CairoPainter.cpp +++ b/src/platform/graphics/cairo/CairoPainter.cpp @@ -236,7 +236,7 @@ void CairoPainter::PopState() { void CairoPainter::EndDraw() { if (cairo_surface_ != nullptr) { - CRU_LOG_TAG_DEBUG("Flush cairo painter."); + CruLogDebug(kLogTag, "Flush cairo painter."); cairo_surface_flush(cairo_surface_); cairo_device_t* device = cairo_surface_get_device(cairo_surface_); if (device) { diff --git a/src/platform/graphics/direct2d/Painter.cpp b/src/platform/graphics/direct2d/Painter.cpp index 79401f6e..76eed621 100644 --- a/src/platform/graphics/direct2d/Painter.cpp +++ b/src/platform/graphics/direct2d/Painter.cpp @@ -19,8 +19,8 @@ D2DDeviceContextPainter::D2DDeviceContextPainter( D2DDeviceContextPainter::~D2DDeviceContextPainter() { if (is_drawing_) { - CRU_LOG_TAG_INFO( - "You may forget to call EndDraw before destroying painter."); + CruLogInfo(kLogTag, + "You may forget to call EndDraw before destroying painter."); } if (release_) { diff --git a/src/platform/gui/osx/Clipboard.mm b/src/platform/gui/osx/Clipboard.mm index 61652589..b14c78ab 100644 --- a/src/platform/gui/osx/Clipboard.mm +++ b/src/platform/gui/osx/Clipboard.mm @@ -25,7 +25,7 @@ OsxClipboardPrivate::~OsxClipboardPrivate() {} std::string OsxClipboardPrivate::GetText() { auto result = [pasteboard_ readObjectsForClasses:@[ NSString.class ] options:nil]; if (result == nil) { - CRU_LOG_TAG_WARN("Failed to get text from clipboard"); + CruLogWarn(kLogTag, "Failed to get text from clipboard"); return ""; } else { if (result.count == 0) { diff --git a/src/platform/gui/sdl/OpenGLRenderer.cpp b/src/platform/gui/sdl/OpenGLRenderer.cpp index 7e8fdf33..07b58147 100644 --- a/src/platform/gui/sdl/OpenGLRenderer.cpp +++ b/src/platform/gui/sdl/OpenGLRenderer.cpp @@ -60,8 +60,9 @@ SdlOpenGLRenderer::SdlOpenGLRenderer(SdlWindow* window, int width, int height) { auto _ = MakeContextCurrent(); auto version = gladLoadGLContext(&glad_gl_context_, SDL_GL_GetProcAddress); - CRU_LOG_TAG_DEBUG("SDL window id {}, openGL version: {}.{}.", sdl_window_id_, - GLAD_VERSION_MAJOR(version), GLAD_VERSION_MINOR(version)); + CruLogDebug(kLogTag, "SDL window id {}, openGL version: {}.{}.", + sdl_window_id_, GLAD_VERSION_MAJOR(version), + GLAD_VERSION_MINOR(version)); glad_gl_context_.Enable(GL_BLEND); glad_gl_context_.BlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA); @@ -114,7 +115,7 @@ SdlOpenGLRenderer::~SdlOpenGLRenderer() { } void SdlOpenGLRenderer::Resize(int width, int height) { - CRU_LOG_TAG_DEBUG("Resize to {} x {}", width, height); + CruLogDebug(kLogTag, "Resize to {} x {}", width, height); width_ = width; height_ = height; @@ -198,8 +199,8 @@ GLuint SdlOpenGLRenderer::CreateGLProgram() { glad_gl_context_.GetShaderiv(shader, GL_COMPILE_STATUS, &success); if (!success) { glad_gl_context_.GetShaderInfoLog(shader, 512, nullptr, infoLog); - CRU_LOG_TAG_ERROR("Failed to compile OpenGL {} shader: {}", name, - infoLog); + CruLogError(kLogTag, "Failed to compile OpenGL {} shader: {}", name, + infoLog); } }; @@ -209,7 +210,8 @@ GLuint SdlOpenGLRenderer::CreateGLProgram() { glad_gl_context_.GetProgramiv(program, GL_COMPILE_STATUS, &success); if (!success) { glad_gl_context_.GetProgramInfoLog(program, 512, nullptr, infoLog); - CRU_LOG_TAG_ERROR("Failed to link OpenGL {} program: {}", name, infoLog); + CruLogError(kLogTag, "Failed to link OpenGL {} program: {}", name, + infoLog); } }; diff --git a/src/platform/gui/win/Clipboard.cpp b/src/platform/gui/win/Clipboard.cpp index 2a41a1eb..a269822d 100644 --- a/src/platform/gui/win/Clipboard.cpp +++ b/src/platform/gui/win/Clipboard.cpp @@ -11,25 +11,25 @@ WinClipboard::~WinClipboard() {} std::string WinClipboard::GetText() { if (!::OpenClipboard(nullptr)) { - CRU_LOG_TAG_WARN("Failed to open clipboard."); + CruLogWarn(kLogTag, "Failed to open clipboard."); return {}; } if (!::IsClipboardFormatAvailable(CF_UNICODETEXT)) { - CRU_LOG_TAG_WARN("Clipboard format for text is not available."); + CruLogWarn(kLogTag, "Clipboard format for text is not available."); return {}; } auto handle = ::GetClipboardData(CF_UNICODETEXT); if (handle == nullptr) { - CRU_LOG_TAG_WARN("Failed to get clipboard data."); + CruLogWarn(kLogTag, "Failed to get clipboard data."); return {}; } auto ptr = ::GlobalLock(handle); if (ptr == nullptr) { - CRU_LOG_TAG_WARN("Failed to lock clipboard data."); + CruLogWarn(kLogTag, "Failed to lock clipboard data."); ::CloseClipboard(); return {}; } @@ -46,21 +46,21 @@ void WinClipboard::SetText(std::string utf8_text) { auto text = string::ToUtf16WString(utf8_text); if (!::OpenClipboard(nullptr)) { - CRU_LOG_TAG_WARN("Failed to open clipboard."); + CruLogWarn(kLogTag, "Failed to open clipboard."); return; } auto handle = GlobalAlloc(GMEM_MOVEABLE, (text.size() + 1) * sizeof(wchar_t)); if (handle == nullptr) { - CRU_LOG_TAG_WARN("Failed to allocate clipboard data."); + CruLogWarn(kLogTag, "Failed to allocate clipboard data."); ::CloseClipboard(); return; } auto ptr = ::GlobalLock(handle); if (ptr == nullptr) { - CRU_LOG_TAG_WARN("Failed to lock clipboard data."); + CruLogWarn(kLogTag, "Failed to lock clipboard data."); ::GlobalFree(handle); ::CloseClipboard(); return; @@ -71,7 +71,7 @@ void WinClipboard::SetText(std::string utf8_text) { ::GlobalUnlock(handle); if (::SetClipboardData(CF_UNICODETEXT, handle) == nullptr) { - CRU_LOG_TAG_WARN("Failed to set clipboard data."); + CruLogWarn(kLogTag, "Failed to set clipboard data."); } ::CloseClipboard(); diff --git a/src/platform/gui/win/Cursor.cpp b/src/platform/gui/win/Cursor.cpp index 0ed40416..b789c47b 100644 --- a/src/platform/gui/win/Cursor.cpp +++ b/src/platform/gui/win/Cursor.cpp @@ -14,8 +14,8 @@ WinCursor::~WinCursor() { if (!::DestroyCursor(handle_)) { // This is not a fetal error but might still need notice because it may // cause leak. - CRU_LOG_TAG_WARN("Failed to destroy a cursor. Last error code: {}", - ::GetLastError()); + CruLogWarn(kLogTag, "Failed to destroy a cursor. Last error code: {}", + ::GetLastError()); } } } diff --git a/src/platform/gui/win/InputMethod.cpp b/src/platform/gui/win/InputMethod.cpp index 3439df32..12f30297 100644 --- a/src/platform/gui/win/InputMethod.cpp +++ b/src/platform/gui/win/InputMethod.cpp @@ -30,7 +30,7 @@ AutoHIMC& AutoHIMC::operator=(AutoHIMC&& other) { AutoHIMC::~AutoHIMC() { if (handle_) { if (!::ImmReleaseContext(hwnd_, handle_)) - CRU_LOG_TAG_WARN("Failed to release HIMC."); + CruLogWarn(kLogTag, "Failed to release HIMC."); } } @@ -161,7 +161,7 @@ WinInputMethodContext::~WinInputMethodContext() {} void WinInputMethodContext::EnableIME() { const auto hwnd = native_window_->GetWindowHandle(); if (::ImmAssociateContextEx(hwnd, nullptr, IACE_DEFAULT) == FALSE) { - CRU_LOG_TAG_WARN("Failed to enable ime."); + CruLogWarn(kLogTag, "Failed to enable ime."); } } @@ -172,21 +172,21 @@ void WinInputMethodContext::DisableIME() { ::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0); if (::ImmAssociateContextEx(hwnd, nullptr, 0) == FALSE) { - CRU_LOG_TAG_WARN("Failed to disable ime."); + CruLogWarn(kLogTag, "Failed to disable ime."); } } void WinInputMethodContext::CompleteComposition() { auto himc = GetHIMC(); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_COMPLETE, 0)) { - CRU_LOG_TAG_WARN("Failed to complete composition."); + CruLogWarn(kLogTag, "Failed to complete composition."); } } void WinInputMethodContext::CancelComposition() { auto himc = GetHIMC(); if (!::ImmNotifyIME(himc.Get(), NI_COMPOSITIONSTR, CPS_CANCEL, 0)) { - CRU_LOG_TAG_WARN("Failed to complete composition."); + CruLogWarn(kLogTag, "Failed to complete composition."); } } @@ -205,7 +205,8 @@ void WinInputMethodContext::SetCandidateWindowPosition(const Point& point) { form.ptCurrentPos = native_window_->DipToPixel(point); if (!::ImmSetCandidateWindow(himc.Get(), &form)) - CRU_LOG_TAG_DEBUG("Failed to set input method candidate window position."); + CruLogDebug(kLogTag, + "Failed to set input method candidate window position."); } IEvent<std::nullptr_t>* WinInputMethodContext::CompositionStartEvent() { @@ -234,9 +235,9 @@ void WinInputMethodContext::OnWindowNativeMessage( // I don't think this will happen because normal key strike without ime // should only trigger ascci character. If it is a charater from // supplementary planes, it should be handled with ime messages. - CRU_LOG_TAG_WARN( - "A WM_CHAR message for character from supplementary " - "planes is ignored."); + CruLogWarn(kLogTag, + "A WM_CHAR message for character from supplementary " + "planes is ignored."); } else { if (c != '\b') { // ignore backspace if (c == '\r') c = '\n'; // Change \r to \n @@ -249,8 +250,8 @@ void WinInputMethodContext::OnWindowNativeMessage( case WM_IME_COMPOSITION: { composition_event_.Raise(nullptr); auto composition_text = GetCompositionText(); - CRU_LOG_TAG_DEBUG("WM_IME_COMPOSITION composition text: {}", - composition_text.text); + CruLogDebug(kLogTag, "WM_IME_COMPOSITION composition text: {}", + composition_text.text); if (message.l_param & GCS_RESULTSTR) { auto result_string = GetResultString(); text_event_.Raise(result_string); @@ -258,12 +259,12 @@ void WinInputMethodContext::OnWindowNativeMessage( break; } case WM_IME_STARTCOMPOSITION: { - CRU_LOG_TAG_DEBUG("WM_IME_STARTCOMPOSITION received."); + CruLogDebug(kLogTag, "WM_IME_STARTCOMPOSITION received."); composition_start_event_.Raise(nullptr); break; } case WM_IME_ENDCOMPOSITION: { - CRU_LOG_TAG_DEBUG("WM_IME_ENDCOMPOSITION received."); + CruLogDebug(kLogTag, "WM_IME_ENDCOMPOSITION received."); composition_end_event_.Raise(nullptr); break; } diff --git a/src/platform/gui/win/Window.cpp b/src/platform/gui/win/Window.cpp index fb2ce024..f1138a78 100644 --- a/src/platform/gui/win/Window.cpp +++ b/src/platform/gui/win/Window.cpp @@ -136,8 +136,8 @@ void WinNativeWindow::SetVisibility(WindowVisibilityType visibility) { Size WinNativeWindow::GetClientSize() { return GetClientRect().GetSize(); } void WinNativeWindow::SetClientSize(const Size& size) { - CRU_LOG_TAG_DEBUG("{} set client size to {}.", - static_cast<void*>(GetWindowHandle()), size); + CruLogDebug(kLogTag, "{} set client size to {}.", + static_cast<void*>(GetWindowHandle()), size); client_rect_.SetSize(size); @@ -154,8 +154,8 @@ void WinNativeWindow::SetClientSize(const Size& size) { Rect WinNativeWindow::GetClientRect() { return client_rect_; } void WinNativeWindow::SetClientRect(const Rect& rect) { - CRU_LOG_TAG_DEBUG("{} set client rect to {}.", - static_cast<void*>(GetWindowHandle()), rect); + CruLogDebug(kLogTag, "{} set client rect to {}.", + static_cast<void*>(GetWindowHandle()), rect); client_rect_ = rect; @@ -183,8 +183,8 @@ Rect WinNativeWindow::GetWindowRect() { } void WinNativeWindow::SetWindowRect(const Rect& rect) { - CRU_LOG_TAG_DEBUG("{} set window rect to {}.", - static_cast<void*>(GetWindowHandle()), rect); + CruLogDebug(kLogTag, "{} set window rect to {}.", + static_cast<void*>(GetWindowHandle()), rect); client_rect_ = CalcClientRectFromWindow(rect, style_flag_, dpi_); @@ -223,7 +223,7 @@ bool WinNativeWindow::ReleaseMouse() { } void WinNativeWindow::RequestRepaint() { - CRU_LOG_TAG_DEBUG("A repaint is requested."); + CruLogDebug(kLogTag, "A repaint is requested."); if (!hwnd_) return; if (!::InvalidateRect(hwnd_, nullptr, FALSE)) throw Win32Error(::GetLastError(), "Failed to invalidate window."); @@ -250,17 +250,18 @@ void WinNativeWindow::SetCursor(std::shared_ptr<ICursor> cursor) { if (!::SetClassLongPtrW(hwnd_, GCLP_HCURSOR, reinterpret_cast<LONG_PTR>(cursor_->GetHandle()))) { - CRU_LOG_TAG_WARN( - "Failed to set cursor because failed to set class long. Last " - "error code: {}.", - ::GetLastError()); + CruLogWarn(kLogTag, + "Failed to set cursor because failed to set class long. Last " + "error code: {}.", + ::GetLastError()); return; } if (GetVisibility() != WindowVisibilityType::Show) return; auto lg = [](std::string_view reason) { - CRU_LOG_TAG_WARN( + CruLogWarn( + kLogTag, "Failed to set cursor because {} when window is visible. (We need to " "update cursor if it is inside the window.) Last error code: {}.", reason, ::GetLastError()); @@ -488,8 +489,8 @@ void WinNativeWindow::RecreateWindow() { if (dpi == 0) throw Win32Error(::GetLastError(), "Failed to get dpi of window."); dpi_ = static_cast<float>(dpi); - CRU_LOG_TAG_DEBUG("Dpi of window {} is {}.", - static_cast<void*>(GetWindowHandle()), dpi_); + CruLogDebug(kLogTag, "Dpi of window {} is {}.", + static_cast<void*>(GetWindowHandle()), dpi_); application_->RegisterWindow(this); @@ -508,14 +509,14 @@ void WinNativeWindow::RecreateWindow() { } void WinNativeWindow::OnCreateInternal() { - CRU_LOG_TAG_DEBUG("A native window is created, hwnd {}.", - static_cast<void*>(GetWindowHandle())); + CruLogDebug(kLogTag, "A native window is created, hwnd {}.", + static_cast<void*>(GetWindowHandle())); CreateEvent_.Raise(nullptr); } void WinNativeWindow::OnDestroyInternal() { - CRU_LOG_TAG_DEBUG("A native window is destroying, hwnd {}.", - static_cast<void*>(GetWindowHandle())); + CruLogDebug(kLogTag, "A native window is destroying, hwnd {}.", + static_cast<void*>(GetWindowHandle())); DestroyEvent_.Raise(nullptr); hwnd_ = nullptr; @@ -537,7 +538,7 @@ void WinNativeWindow::OnPaintInternal() { } Paint1Event_.Raise(args); ::ValidateRect(hwnd_, nullptr); - CRU_LOG_TAG_DEBUG("A repaint is finished."); + CruLogDebug(kLogTag, "A repaint is finished."); } void WinNativeWindow::OnMoveInternal(const int new_left, const int new_top) { diff --git a/src/platform/gui/xcb/InputMethod.cpp b/src/platform/gui/xcb/InputMethod.cpp index 96d04348..2a370eca 100644 --- a/src/platform/gui/xcb/InputMethod.cpp +++ b/src/platform/gui/xcb/InputMethod.cpp @@ -20,9 +20,9 @@ XcbXimInputMethodManager::XcbXimInputMethodManager( xcb_xim_im_callback kXimCallbacks = { .forward_event = - [](xcb_xim_t *im, xcb_xic_t ic, xcb_key_press_event_t *event, - void *user_data) { - CRU_LOG_TAG_DEBUG("Key event forwarded back from XIM."); + [](xcb_xim_t* im, xcb_xic_t ic, xcb_key_press_event_t* event, + void* user_data) { + CruLogDebug(kLogTag, "Key event forwarded back from XIM."); auto manager = static_cast<XcbXimInputMethodManager *>(user_data); if ((event->response_type & ~0x80) == XCB_KEY_PRESS) { auto text = @@ -45,9 +45,9 @@ XcbXimInputMethodManager::XcbXimInputMethodManager( } }, .commit_string = - [](xcb_xim_t *im, xcb_xic_t ic, uint32_t flag, char *str, - uint32_t length, uint32_t *keysym, size_t nKeySym, - void *user_data) { + [](xcb_xim_t* im, xcb_xic_t ic, uint32_t flag, char* str, + uint32_t length, uint32_t* keysym, size_t nKeySym, + void* user_data) { auto manager = static_cast<XcbXimInputMethodManager *>(user_data); if (flag & XCB_XIM_LOOKUP_KEYSYM) { @@ -91,7 +91,7 @@ xcb_xim_t *XcbXimInputMethodManager::GetXcbXim() { return im_; } void XcbXimInputMethodManager::DispatchCommit(xcb_xim_t *im, xcb_xic_t ic, std::string text) { - CRU_LOG_TAG_DEBUG("IC {} dispatch commit string: {}", ic, text); + CruLogDebug(kLogTag, "IC {} dispatch commit string: {}", ic, text); if (focus_context_) { focus_context_->composition_event_.Raise(nullptr); focus_context_->composition_end_event_.Raise(nullptr); @@ -104,7 +104,7 @@ bool XcbXimInputMethodManager::HandleXEvent(xcb_generic_event_t *event) { if (focus_context_ && focus_context_->ic_ && (((event->response_type & ~0x80) == XCB_KEY_PRESS) || ((event->response_type & ~0x80) == XCB_KEY_RELEASE))) { - CRU_LOG_TAG_DEBUG("Forward key event to XIM."); + CruLogDebug(kLogTag, "Forward key event to XIM."); xcb_xim_forward_event(im_, *focus_context_->ic_, (xcb_key_press_event_t *)event); application_->XcbFlush(); @@ -175,7 +175,7 @@ static void EmptyXimDestroyIcCallback(xcb_xim_t *im, xcb_xic_t ic, void XcbXimInputMethodContext::SetCandidateWindowPosition(const Point &point) { if (!ic_) return; - CRU_LOG_TAG_DEBUG("IC {} set candidate window position: {}", *ic_, point); + CruLogDebug(kLogTag, "IC {} set candidate window position: {}", *ic_, point); xcb_point_t spot; spot.x = point.x; @@ -212,7 +212,7 @@ void XcbXimInputMethodContext::CreateIc(xcb_window_t window) { auto XimCreateIcCallback = [](xcb_xim_t *im, xcb_xic_t ic, void *user_data) { auto context = static_cast<XcbXimInputMethodContext *>(user_data); context->ic_ = ic; - CRU_LOG_TAG_DEBUG("XIM IC {} is created.", ic); + CruLogDebug(kLogTag, "XIM IC {} is created.", ic); if (context->window_->HasFocus()) { context->SetFocus(); } @@ -223,27 +223,27 @@ void XcbXimInputMethodContext::CreateIc(xcb_window_t window) { xcb_xim_create_ic(manager_->GetXcbXim(), XimCreateIcCallback, this, XCB_XIM_XNInputStyle, &input_style, XCB_XIM_XNClientWindow, &window, XCB_XIM_XNFocusWindow, &window, NULL); - CRU_LOG_TAG_DEBUG("Create XIM IC."); + CruLogDebug(kLogTag, "Create XIM IC."); manager_->application_->XcbFlush(); } void XcbXimInputMethodContext::SetFocus() { manager_->focus_context_ = this; if (ic_) { - CRU_LOG_TAG_DEBUG("Focus XIM IC {}.", *ic_); + CruLogDebug(kLogTag, "Focus XIM IC {}.", *ic_); xcb_xim_set_ic_focus(manager_->GetXcbXim(), *ic_); manager_->application_->XcbFlush(); } } void XcbXimInputMethodContext::DestroyIc() { - auto destroy_callback = [](xcb_xim_t *im, xcb_xic_t ic, void *user_data) { - CRU_LOG_TAG_DEBUG("XIM IC {} is destroyed.", ic); + auto destroy_callback = [](xcb_xim_t* im, xcb_xic_t ic, void* user_data) { + CruLogDebug(kLogTag, "XIM IC {} is destroyed.", ic); }; if (!ic_) return; xcb_xim_destroy_ic(manager_->GetXcbXim(), *ic_, destroy_callback, this); - CRU_LOG_TAG_DEBUG("Destroy XIM IC {}.", *ic_); + CruLogDebug(kLogTag, "Destroy XIM IC {}.", *ic_); ic_ = std::nullopt; manager_->application_->XcbFlush(); } diff --git a/src/platform/gui/xcb/Window.cpp b/src/platform/gui/xcb/Window.cpp index 50f2c9aa..06d82de9 100644 --- a/src/platform/gui/xcb/Window.cpp +++ b/src/platform/gui/xcb/Window.cpp @@ -49,7 +49,7 @@ XcbWindow::XcbWindow(XcbUiApplication* application) PaintEvent_.AddSpyOnlyHandler([this] { if (xcb_window_) - CRU_LOG_TAG_DEBUG("{:#x} Paint event triggered.", *xcb_window_); + CruLogDebug(kLogTag, "{:#x} Paint event triggered.", *xcb_window_); }); } @@ -287,7 +287,7 @@ void XcbWindow::SetToForeground() { void XcbWindow::RequestRepaint() { if (!xcb_window_.has_value()) return; - CRU_LOG_TAG_DEBUG("{:#x} Repaint requested.", *xcb_window_); + CruLogDebug(kLogTag, "{:#x} Repaint requested.", *xcb_window_); // TODO: true throttle repaint_canceler_.Reset(application_->SetImmediate([this] { PaintEvent_.Raise(nullptr); @@ -412,8 +412,8 @@ void XcbWindow::HandleEvent(xcb_generic_event_t* event) { (xcb_configure_notify_event_t*)event; auto width = configure->width, height = configure->height; if (width != current_size_.width || height != current_size_.height) { - CRU_LOG_TAG_DEBUG("{:#x} Size changed {} x {}.", *xcb_window_, width, - height); + CruLogDebug(kLogTag, "{:#x} Size changed {} x {}.", *xcb_window_, width, + height); current_size_ = Size(width, height); assert(cairo_surface_); cairo_xcb_surface_set_size(cairo_surface_, width, height); diff --git a/src/ui/ThemeResourceDictionary.cpp b/src/ui/ThemeResourceDictionary.cpp index 9f734054..82cad6c0 100644 --- a/src/ui/ThemeResourceDictionary.cpp +++ b/src/ui/ThemeResourceDictionary.cpp @@ -48,10 +48,11 @@ void ThemeResourceDictionary::UpdateResourceMap(xml::XmlElementNode* xml_root) { resource_map_[entry.name] = std::move(entry); } else { - CRU_LOG_TAG_DEBUG("Ignore unknown element {} of theme.", c->GetTag()); + CruLogDebug(kLogTag, "Ignore unknown element {} of theme.", + c->GetTag()); } } else { - CRU_LOG_TAG_DEBUG("Ignore text or comment node of theme."); + CruLogDebug(kLogTag, "Ignore text or comment node of theme."); } } } diff --git a/src/ui/controls/Control.cpp b/src/ui/controls/Control.cpp index 2a94252f..548c9726 100644 --- a/src/ui/controls/Control.cpp +++ b/src/ui/controls/Control.cpp @@ -23,7 +23,8 @@ Control::Control(std::string name) Control::~Control() { if (host_ && host_->IsInEventHandling()) { - CRU_LOG_TAG_WARN( + CruLogWarn( + kLogTag, "Better use delete later to delete control during event handling."); } diff --git a/src/ui/controls/ControlHost.cpp b/src/ui/controls/ControlHost.cpp index 7c934b7b..1dabb26f 100644 --- a/src/ui/controls/ControlHost.cpp +++ b/src/ui/controls/ControlHost.cpp @@ -168,7 +168,7 @@ void ControlHost::RelayoutWithSize(const Size& available_size, } render_object->Layout(Point{}); - CRU_LOG_TAG_DEBUG("A relayout is finished."); + CruLogDebug(kLogTag, "A relayout is finished."); AfterLayoutEvent_.Raise(nullptr); diff --git a/src/ui/controls/TextHostControlService.cpp b/src/ui/controls/TextHostControlService.cpp index 39c28393..9c57140c 100644 --- a/src/ui/controls/TextHostControlService.cpp +++ b/src/ui/controls/TextHostControlService.cpp @@ -222,7 +222,7 @@ void TextHostControlService::SetText(std::string text, bool stop_composition) { void TextHostControlService::InsertText(Index position, std::string_view text, bool stop_composition) { if (!Utf8IsValidInsertPosition(this->text_, position)) { - CRU_LOG_TAG_ERROR("Invalid text insert position."); + CruLogError(kLogTag, "Invalid text insert position."); return; } this->text_.insert(this->text_.cbegin() + position, text.cbegin(), @@ -236,7 +236,7 @@ void TextHostControlService::InsertText(Index position, std::string_view text, void TextHostControlService::DeleteChar(Index position, bool stop_composition) { if (!Utf8IsValidInsertPosition(this->text_, position)) { - CRU_LOG_TAG_ERROR("Invalid text delete position {}.", position); + CruLogError(kLogTag, "Invalid text delete position {}.", position); return; } if (position == static_cast<Index>(this->text_.size())) return; @@ -249,7 +249,7 @@ void TextHostControlService::DeleteChar(Index position, bool stop_composition) { Index TextHostControlService::DeleteCharPrevious(Index position, bool stop_composition) { if (!Utf8IsValidInsertPosition(this->text_, position)) { - CRU_LOG_TAG_ERROR("Invalid text delete position {}.", position); + CruLogError(kLogTag, "Invalid text delete position {}.", position); return 0; } if (position == 0) return 0; @@ -265,12 +265,13 @@ void TextHostControlService::DeleteText(TextRange range, if (range.count == 0) return; range = range.Normalize(); if (!Utf8IsValidInsertPosition(this->text_, range.GetStart())) { - CRU_LOG_TAG_ERROR("Invalid text delete start position {}.", - range.GetStart()); + CruLogError(kLogTag, "Invalid text delete start position {}.", + range.GetStart()); return; } if (!Utf8IsValidInsertPosition(this->text_, range.GetEnd())) { - CRU_LOG_TAG_ERROR("Invalid text delete end position {}.", range.GetEnd()); + CruLogError(kLogTag, "Invalid text delete end position {}.", + range.GetEnd()); return; } this->text_.erase(this->text_.cbegin() + range.GetStart(), @@ -464,8 +465,9 @@ void TextHostControlService::UpdateInputMethodPosition() { right_bottom.y += 5; if constexpr (debug_flags::text_service) { - CRU_LOG_TAG_DEBUG("Calculate input method candidate window position: {}.", - right_bottom); + CruLogDebug(kLogTag, + "Calculate input method candidate window position: {}.", + right_bottom); } input_method_context->SetCandidateWindowPosition(right_bottom); @@ -691,7 +693,7 @@ void TextHostControlService::SetUpShortcuts() { void TextHostControlService::OpenContextMenu(const Point& position, ContextMenuItem items) { - CRU_LOG_TAG_DEBUG("Open context menu."); + CruLogDebug(kLogTag, "Open context menu."); if (!context_menu_) { context_menu_.reset(new components::PopupMenu()); } diff --git a/src/ui/helper/ClickDetector.cpp b/src/ui/helper/ClickDetector.cpp index 0ac0515f..caa7061f 100644 --- a/src/ui/helper/ClickDetector.cpp +++ b/src/ui/helper/ClickDetector.cpp @@ -55,8 +55,8 @@ ClickDetector::ClickDetector(controls::Control* control) { this->state_ == ClickState::Hover) { if (!this->control_->CaptureMouse()) { if constexpr (debug_flags::click_detector) { - CRU_LOG_TAG_DEBUG( - "Failed to capture mouse when begin click."); + CruLogDebug(kLogTag, + "Failed to capture mouse when begin click."); } return; } @@ -133,7 +133,8 @@ void ClickDetector::SetState(ClickState state) { UnreachableCode(); } }; - CRU_LOG_TAG_DEBUG("Click state changed, new state: {}.", to_string(state)); + CruLogDebug(kLogTag, "Click state changed, new state: {}.", + to_string(state)); } state_ = state; diff --git a/src/ui/helper/ShortcutHub.cpp b/src/ui/helper/ShortcutHub.cpp index fd23802e..13cce216 100644 --- a/src/ui/helper/ShortcutHub.cpp +++ b/src/ui/helper/ShortcutHub.cpp @@ -64,7 +64,8 @@ const std::vector<ShortcutInfo>& ShortcutHub::GetShortcutByKeyBind( void ShortcutHub::Install(controls::Control* control) { if (!event_guard_.IsEmpty()) { - CRU_LOG_TAG_ERROR("Shortcut hub is already installed. Failed to install."); + CruLogError(kLogTag, + "Shortcut hub is already installed. Failed to install."); return; } @@ -74,7 +75,7 @@ void ShortcutHub::Install(controls::Control* control) { void ShortcutHub::Uninstall() { if (event_guard_.IsEmpty()) { - CRU_LOG_TAG_WARN("Shortcut hub is not installed. Failed to uninstall."); + CruLogWarn(kLogTag, "Shortcut hub is not installed. Failed to uninstall."); return; } @@ -89,17 +90,17 @@ void ShortcutHub::OnKeyDown(events::KeyEventArgs& event) { if constexpr (debug_flags::shortcut) { if (shortcut_list.empty()) { - CRU_LOG_TAG_DEBUG("No shortcut for key bind {}.", key_bind.ToString()); + CruLogDebug(kLogTag, "No shortcut for key bind {}.", key_bind.ToString()); } - CRU_LOG_TAG_DEBUG("Begin to handle shortcut for key bind {}.", - key_bind.ToString()); + CruLogDebug(kLogTag, "Begin to handle shortcut for key bind {}.", + key_bind.ToString()); } for (const auto& shortcut : shortcut_list) { auto is_handled = shortcut.handler(); if (is_handled) { if constexpr (debug_flags::shortcut) { - CRU_LOG_TAG_DEBUG("Handle {} handled it.", shortcut.name); + CruLogDebug(kLogTag, "Handle {} handled it.", shortcut.name); } handled = true; @@ -108,23 +109,23 @@ void ShortcutHub::OnKeyDown(events::KeyEventArgs& event) { break; } else { if constexpr (debug_flags::shortcut) { - CRU_LOG_TAG_DEBUG("Handle {} didn't handle it.", shortcut.name); + CruLogDebug(kLogTag, "Handle {} didn't handle it.", shortcut.name); } } } if constexpr (debug_flags::shortcut) { if (!shortcut_list.empty()) { - CRU_LOG_TAG_DEBUG("End handling shortcut for key bind {}.", - key_bind.ToString()); + CruLogDebug(kLogTag, "End handling shortcut for key bind {}.", + key_bind.ToString()); } } if (!handled) { if constexpr (debug_flags::shortcut) { - CRU_LOG_TAG_DEBUG( - "Raise fallback event for unhandled shortcut of key bind {}.", - key_bind.ToString()); + CruLogDebug(kLogTag, + "Raise fallback event for unhandled shortcut of key bind {}.", + key_bind.ToString()); } fallback_event_.Raise(event); } diff --git a/src/ui/render/BorderRenderObject.cpp b/src/ui/render/BorderRenderObject.cpp index 18fc6475..080bcc4f 100644 --- a/src/ui/render/BorderRenderObject.cpp +++ b/src/ui/render/BorderRenderObject.cpp @@ -82,8 +82,8 @@ RenderObject* BorderRenderObject::HitTest(const Point& point) { void BorderRenderObject::Draw(platform::graphics::IPainter* painter) { if constexpr (debug_flags::draw) { - CRU_LOG_TAG_DEBUG( - "BorderRenderObject draw, background: {}, foreground: {}.", + CruLogDebug( + kLogTag, "BorderRenderObject draw, background: {}, foreground: {}.", background_brush_ == nullptr ? "NONE" : background_brush_->GetDebugString(), foreground_brush_ == nullptr ? "NONE" @@ -96,7 +96,7 @@ void BorderRenderObject::Draw(platform::graphics::IPainter* painter) { if (is_border_enabled_) { if (border_brush_ == nullptr) { - CRU_LOG_TAG_WARN("Border is enabled but border brush is null."); + CruLogWarn(kLogTag, "Border is enabled but border brush is null."); } else { painter->FillGeometry(geometry_.get(), border_brush_.get()); } diff --git a/src/ui/render/FlexLayoutRenderObject.cpp b/src/ui/render/FlexLayoutRenderObject.cpp index 124f7e70..4c7762e3 100644 --- a/src/ui/render/FlexLayoutRenderObject.cpp +++ b/src/ui/render/FlexLayoutRenderObject.cpp @@ -299,7 +299,8 @@ Size FlexLayoutMeasureContentImpl( if (max_main_length.IsSpecified() && total_length > max_main_length.GetLengthOrUndefined()) { - CRU_LOG_TAG_WARN( + CruLogWarn( + kLogTag, "(Measure) Children's main axis length exceeds required max length."); total_length = max_main_length.GetLengthOrUndefined(); } else if (min_main_length.IsSpecified() && diff --git a/src/ui/render/RenderObject.cpp b/src/ui/render/RenderObject.cpp index 424fd2ee..f6f081ec 100644 --- a/src/ui/render/RenderObject.cpp +++ b/src/ui/render/RenderObject.cpp @@ -82,8 +82,8 @@ void RenderObject::SetMaxSize(const MeasureSize& max_size) { } void RenderObject::Measure(const MeasureRequirement& requirement) { - CRU_LOG_TAG_DEBUG("{} Measure begins, requirement {}.", - this->GetDebugPathInTree(), requirement); + CruLogDebug(kLogTag, "{} Measure begins, requirement {}.", + this->GetDebugPathInTree(), requirement); if (layout_valid_ && requirement == last_measure_requirement_) { return; @@ -96,8 +96,8 @@ void RenderObject::Measure(const MeasureRequirement& requirement) { last_measure_requirement_ = requirement; - CRU_LOG_TAG_DEBUG("{} Measure ends, result size: {}.", - this->GetDebugPathInTree(), measure_result_size_); + CruLogDebug(kLogTag, "{} Measure ends, result size: {}.", + this->GetDebugPathInTree(), measure_result_size_); } void RenderObject::Layout(const Point& offset) { @@ -105,8 +105,8 @@ void RenderObject::Layout(const Point& offset) { } void RenderObject::Layout(const Rect& rect) { - CRU_LOG_TAG_DEBUG("{} Layout begins, rect: {}.", this->GetDebugPathInTree(), - rect); + CruLogDebug(kLogTag, "{} Layout begins, rect: {}.", + this->GetDebugPathInTree(), rect); offset_ = rect.GetLeftTop(); auto new_size = rect.GetSize(); @@ -118,7 +118,7 @@ void RenderObject::Layout(const Rect& rect) { OnLayoutCore(rect); layout_valid_ = true; - CRU_LOG_TAG_DEBUG("{} Layout ends.", this->GetDebugPathInTree()); + CruLogDebug(kLogTag, "{} Layout ends.", this->GetDebugPathInTree()); } Thickness RenderObject::GetTotalSpaceThickness() { return margin_ + padding_; } diff --git a/src/ui/render/TextRenderObject.cpp b/src/ui/render/TextRenderObject.cpp index 1fe8e652..12d77d0d 100644 --- a/src/ui/render/TextRenderObject.cpp +++ b/src/ui/render/TextRenderObject.cpp @@ -178,11 +178,12 @@ RenderObject* TextRenderObject::HitTest(const Point& point) { void TextRenderObject::Draw(platform::graphics::IPainter* painter) { if constexpr (debug_flags::draw) { - CRU_LOG_TAG_DEBUG( - "Begin to paint, total_offset: {}, size: {}, text_layout: " - "{}, brush: {}.", - this->GetTotalOffset(), this->GetMeasureResultSize(), - this->text_layout_->GetDebugString(), this->brush_->GetDebugString()); + CruLogDebug(kLogTag, + "Begin to paint, total_offset: {}, size: {}, text_layout: " + "{}, brush: {}.", + this->GetTotalOffset(), this->GetMeasureResultSize(), + this->text_layout_->GetDebugString(), + this->brush_->GetDebugString()); } if (this->selection_range_.has_value()) { |
