Unified logging: Replace LogSystem with EditorConsoleSink

- Created EditorConsoleSink (implements ILogSink interface)
- EditorConsoleSink stores logs in memory buffer (max 1000 entries)
- Added to Debug::Logger in Application::Initialize()
- ConsolePanel now reads from EditorConsoleSink via static GetInstance()
- Removed separate LogSystem singleton
- Removed editor/src/Core/LogEntry.h (no longer needed)

Now Editor and Engine share the same Debug::Logger, with ConsolePanel
displaying logs via EditorConsoleSink.
This commit is contained in:
2026-03-25 16:13:02 +08:00
parent b08f682e5c
commit 16e2065c6c
28 changed files with 355 additions and 121 deletions

View File

@@ -33,8 +33,8 @@ add_executable(${PROJECT_NAME} WIN32
src/Application.cpp
src/Theme.cpp
src/Managers/SceneManager.cpp
src/Managers/LogSystem.cpp
src/Managers/ProjectManager.cpp
src/Core/EditorConsoleSink.cpp
src/panels/Panel.cpp
src/panels/MenuBar.cpp
src/panels/HierarchyPanel.cpp

View File

@@ -1,6 +1,7 @@
#include "Application.h"
#include "Layers/EditorLayer.h"
#include "Core/EditorContextImpl.h"
#include "Core/EditorConsoleSink.h"
#include <XCEngine/Debug/Logger.h>
#include <XCEngine/Debug/FileLogSink.h>
#include <XCEngine/Debug/ConsoleLogSink.h>
@@ -66,6 +67,7 @@ bool Application::Initialize(HWND hwnd) {
// Initialize logging first
Debug::Logger::Get().AddSink(std::make_unique<Debug::ConsoleLogSink>());
Debug::Logger::Get().AddSink(std::make_unique<Debug::EditorConsoleSink>());
// Get exe directory for log file path
wchar_t exePath[MAX_PATH];

View File

@@ -0,0 +1,43 @@
#include "Core/EditorConsoleSink.h"
namespace XCEngine {
namespace Debug {
EditorConsoleSink* EditorConsoleSink::GetInstance() {
static EditorConsoleSink instance;
return &instance;
}
EditorConsoleSink::EditorConsoleSink() = default;
EditorConsoleSink::~EditorConsoleSink() = default;
void EditorConsoleSink::Log(const LogEntry& entry) {
std::lock_guard<std::mutex> lock(m_mutex);
if (m_logs.size() >= MAX_LOGS) {
m_logs.erase(m_logs.begin());
}
m_logs.push_back(entry);
if (m_callback) {
m_callback();
}
}
void EditorConsoleSink::Flush() {
}
const std::vector<LogEntry>& EditorConsoleSink::GetLogs() const {
return m_logs;
}
void EditorConsoleSink::Clear() {
std::lock_guard<std::mutex> lock(m_mutex);
m_logs.clear();
}
void EditorConsoleSink::SetCallback(std::function<void()> callback) {
m_callback = std::move(callback);
}
} // namespace Debug
} // namespace XCEngine

View File

@@ -0,0 +1,34 @@
#pragma once
#include <XCEngine/Debug/ILogSink.h>
#include <XCEngine/Debug/LogEntry.h>
#include <vector>
#include <functional>
#include <mutex>
namespace XCEngine {
namespace Debug {
class EditorConsoleSink : public ILogSink {
public:
static EditorConsoleSink* GetInstance();
EditorConsoleSink();
~EditorConsoleSink() override;
void Log(const LogEntry& entry) override;
void Flush() override;
const std::vector<LogEntry>& GetLogs() const;
void Clear();
void SetCallback(std::function<void()> callback);
private:
mutable std::mutex m_mutex;
std::vector<LogEntry> m_logs;
std::function<void()> m_callback;
static constexpr size_t MAX_LOGS = 1000;
};
} // namespace Debug
} // namespace XCEngine

View File

@@ -1,17 +0,0 @@
#pragma once
#include <string>
#include <XCEngine/Debug/LogLevel.h>
namespace XCEngine {
namespace Editor {
struct LogEntry {
::XCEngine::Debug::LogLevel level;
std::string message;
std::string timestamp;
};
}
}

View File

@@ -1,21 +0,0 @@
#include "LogSystem.h"
namespace XCEngine {
namespace Editor {
LogSystem& LogSystem::Get() {
static LogSystem instance;
return instance;
}
void LogSystem::AddLog(::XCEngine::Debug::LogLevel level, const std::string& message) {
m_logs.push_back({level, message});
if (m_callback) m_callback();
}
void LogSystem::Clear() {
m_logs.clear();
}
}
}

View File

@@ -1,28 +0,0 @@
#pragma once
#include "Core/LogEntry.h"
#include <vector>
#include <functional>
namespace XCEngine {
namespace Editor {
class LogSystem {
public:
static LogSystem& Get();
void AddLog(::XCEngine::Debug::LogLevel level, const std::string& message);
void Clear();
const std::vector<LogEntry>& GetLogs() const { return m_logs; }
void SetCallback(std::function<void()> callback) { m_callback = callback; }
private:
LogSystem() = default;
std::vector<LogEntry> m_logs;
std::function<void()> m_callback;
};
}
}

View File

@@ -1,24 +1,19 @@
#include "ConsolePanel.h"
#include "Managers/LogSystem.h"
#include "Core/LogEntry.h"
#include "Core/EditorConsoleSink.h"
#include <XCEngine/Debug/Logger.h>
#include <imgui.h>
namespace XCEngine {
namespace Editor {
ConsolePanel::ConsolePanel() : Panel("Console") {
LogSystem::Get().AddLog(::XCEngine::Debug::LogLevel::Info, "Engine initialized successfully");
LogSystem::Get().AddLog(::XCEngine::Debug::LogLevel::Info, "Loading default scene...");
LogSystem::Get().AddLog(::XCEngine::Debug::LogLevel::Warning, "Missing material on object 'Cube'");
LogSystem::Get().AddLog(::XCEngine::Debug::LogLevel::Error, "Failed to load texture: 'Assets/Textures/missing.png'");
LogSystem::Get().AddLog(::XCEngine::Debug::LogLevel::Info, "Scene loaded successfully");
}
void ConsolePanel::Render() {
ImGui::Begin(m_name.c_str(), nullptr, ImGuiWindowFlags_None);
if (ImGui::Button("Clear")) {
LogSystem::Get().Clear();
Debug::EditorConsoleSink::GetInstance()->Clear();
}
ImGui::SameLine();
@@ -57,17 +52,21 @@ void ConsolePanel::Render() {
ImGui::BeginChild("LogScroll", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar);
const auto& logs = Debug::EditorConsoleSink::GetInstance()->GetLogs();
size_t logIndex = 0;
for (const auto& log : LogSystem::Get().GetLogs()) {
for (const auto& log : logs) {
bool shouldShow = false;
switch (log.level) {
case ::XCEngine::Debug::LogLevel::Info:
case ::XCEngine::Debug::LogLevel::Verbose:
case ::XCEngine::Debug::LogLevel::Debug:
shouldShow = m_showInfo;
break;
case ::XCEngine::Debug::LogLevel::Warning:
shouldShow = m_showWarning;
break;
case ::XCEngine::Debug::LogLevel::Error:
case ::XCEngine::Debug::LogLevel::Fatal:
shouldShow = m_showError;
break;
}
@@ -80,6 +79,8 @@ void ConsolePanel::Render() {
const char* prefix;
switch (log.level) {
case ::XCEngine::Debug::LogLevel::Verbose:
case ::XCEngine::Debug::LogLevel::Debug:
case ::XCEngine::Debug::LogLevel::Info:
color = ImVec4(0.5f, 0.5f, 0.5f, 1.0f);
prefix = "[INFO] ";
@@ -89,6 +90,7 @@ void ConsolePanel::Render() {
prefix = "[WARN] ";
break;
case ::XCEngine::Debug::LogLevel::Error:
case ::XCEngine::Debug::LogLevel::Fatal:
color = ImVec4(1.0f, 0.3f, 0.3f, 1.0f);
prefix = "[ERROR]";
break;
@@ -96,11 +98,10 @@ void ConsolePanel::Render() {
ImGui::PushID(static_cast<int>(logIndex));
std::string timestampStr = "[" + log.timestamp + "] ";
ImGui::TextColored(ImVec4(0.4f, 0.4f, 0.4f, 1.0f), "%s", timestampStr.c_str());
ImGui::TextColored(ImVec4(0.4f, 0.4f, 0.4f, 1.0f), "%s", log.message.CStr());
ImGui::SameLine();
std::string fullMessage = std::string(prefix) + log.message;
std::string fullMessage = std::string(prefix) + log.message.CStr();
ImGui::TextColored(color, "%s", fullMessage.c_str());
if (ImGui::IsItemClicked()) {
@@ -111,11 +112,6 @@ void ConsolePanel::Render() {
logIndex++;
}
if (m_scrollToBottom && !LogSystem::Get().GetLogs().empty()) {
ImGui::SetScrollHereY(1.0f);
m_scrollToBottom = false;
}
ImGui::EndChild();
ImGui::End();
}