From 650414865461c075ec4efd00c4ee947ccb1d1bfb Mon Sep 17 00:00:00 2001 From: Robert Date: Mon, 30 Jan 2023 17:14:20 +0100 Subject: [PATCH] add logger module --- CMakeLists.txt | 3 ++ src/CMakeLists.txt | 15 +++++++- src/debugger/Debugger.cpp | 5 +++ src/debugger/Logger.cpp | 80 +++++++++++++++++++++++++++++++++++++++ src/debugger/Logger.hpp | 29 ++++++++++++++ 5 files changed, 131 insertions(+), 1 deletion(-) create mode 100644 src/debugger/Logger.cpp create mode 100644 src/debugger/Logger.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index f72251b..92952b7 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -5,6 +5,9 @@ cmake_minimum_required (VERSION 3.8) project ("NES Emulator") +set(CMAKE_CXX_STANDARD_REQUIRED 17) +set(CMAKE_CXX_STANDARD 17) + find_package(glfw3) if(NOT glfw3_FOUND) add_subdirectory("vendor/glfw") diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 593c13c..00cd492 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,13 +7,26 @@ add_executable(nesemu "mappers/Mapper000.cpp" "Log.cpp" "PPU.cpp" + "APU.cpp" "gfx/Window.cpp" + "gfx/Input.cpp" + "gfx/Screen.cpp" "debugger/CPUWatcher.cpp" "debugger/Debugger.cpp" "debugger/PPUWatcher.cpp" "debugger/Disassembler.cpp" "debugger/MemoryViewer.cpp" - "debugger/NametableViewer.cpp" "ControllerPort.cpp" "controllers/StandardController.cpp" "gfx/Input.cpp" "debugger/ControllerPortViewer.cpp" "gfx/Screen.cpp" "debugger/Palettes.cpp" "APU.cpp" "debugger/PatternTableViewer.cpp" "mappers/Mapper003.cpp" "mappers/Mapper001.cpp" "debugger/OAMViewer.cpp") + "debugger/NametableViewer.cpp" + "debugger/ControllerPortViewer.cpp" + "debugger/PatternTableViewer.cpp" + "debugger/OAMViewer.cpp" + "debugger/Palettes.cpp" + "debugger/Logger.cpp" + "ControllerPort.cpp" + "controllers/StandardController.cpp" + "mappers/Mapper003.cpp" + "mappers/Mapper001.cpp" + ) target_include_directories(nesemu PRIVATE mappers diff --git a/src/debugger/Debugger.cpp b/src/debugger/Debugger.cpp index f745ff0..b00e058 100644 --- a/src/debugger/Debugger.cpp +++ b/src/debugger/Debugger.cpp @@ -14,6 +14,7 @@ #include "PatternTableViewer.hpp" #include "ControllerPortViewer.hpp" #include "Palettes.hpp" +#include "Logger.hpp" Debugger::Debugger(Bus* bus) : bus(bus) @@ -32,6 +33,9 @@ Debugger::Debugger(Bus* bus) : windows.push_back(new PatternTableViewer(this, bus->cartridge.GetMapper())); windows.push_back(new ControllerPortViewer(this, &bus->controllerPort)); windows.push_back(new Palettes(this, bus)); + + Logger::Init(this); + windows.push_back(Logger::GetInstance()); } Debugger::~Debugger() @@ -42,6 +46,7 @@ Debugger::~Debugger() bool Debugger::Frame() { + Logger::GetInstance()->Log("Debugger", "Frame!\n"); try { while (!bus->ppu.IsFrameDone()) diff --git a/src/debugger/Logger.cpp b/src/debugger/Logger.cpp new file mode 100644 index 0000000..73b7fd8 --- /dev/null +++ b/src/debugger/Logger.cpp @@ -0,0 +1,80 @@ +#include "Logger.hpp" + +#include + +Logger::Logger(Debugger* debugger) : + DebugWindow("Log", debugger), autoScroll(true) +{ + buffer.clear(); + + offsets.clear(); + offsets.push_back(0); +} + +void Logger::Init(Debugger* debugger) +{ + if (Logger::instance != nullptr) { + return; + } + + Logger::instance = new Logger(debugger); +} + +void Logger::Log(const char* module, const char* fmt, ...) +{ + int old_size = buffer.size(); + va_list args; + va_start(args, fmt); + + buffer.appendf("[%s] ", module); + buffer.appendfv(fmt, args); + va_end(args); + + for (int new_size = buffer.size(); old_size < new_size; old_size++) { + if (buffer[old_size] == '\n') { + offsets.push_back(old_size + 1); + } + } +} + +void Logger::OnRender() +{ + ImGui::SetNextWindowSize(ImVec2(400, 400), ImGuiCond_FirstUseEver); + + if (!ImGui::Begin(title.c_str(), &isOpen)) { + ImGui::End(); + return; + } + + if (ImGui::BeginPopup("Options")) { + ImGui::Checkbox("Auto scroll", &autoScroll); + ImGui::EndPopup(); + } + + if (ImGui::BeginChild("scrolling", ImVec2(0, 0), false, ImGuiWindowFlags_HorizontalScrollbar)) + { + ImGui::PushStyleVar(ImGuiStyleVar_ItemSpacing, ImVec2(0, 0)); + const char* buf = buffer.begin(); + const char* buf_end = buffer.end(); + + ImGuiListClipper clipper; + clipper.Begin((int)offsets.size()); + while (clipper.Step()) + { + for (int line_no = clipper.DisplayStart; line_no < clipper.DisplayEnd; line_no++) + { + const char* line_start = buf + offsets[line_no]; + const char* line_end = (line_no + 1 < offsets.size()) ? (buf + offsets[line_no + 1] - 1) : buf_end; + ImGui::TextUnformatted(line_start, line_end); + } + } + clipper.End(); + ImGui::PopStyleVar(); + + if (autoScroll && ImGui::GetScrollY() >= ImGui::GetScrollMaxY()) + ImGui::SetScrollHereY(1.0f); + } + ImGui::EndChild(); + + ImGui::End(); +} \ No newline at end of file diff --git a/src/debugger/Logger.hpp b/src/debugger/Logger.hpp new file mode 100644 index 0000000..d8a861c --- /dev/null +++ b/src/debugger/Logger.hpp @@ -0,0 +1,29 @@ +#pragma once + +#include +#include +#include "DebugWindow.hpp" + +class Logger final: + public DebugWindow +{ +public: + static void Init(Debugger* debugger); + + static Logger* GetInstance() { + return instance; + } + + void Log(const char* module, const char* fmt, ...); + virtual void OnRender() override; + +private: + Logger(Debugger* debugger); + +private: + inline static Logger* instance = nullptr; + + ImGuiTextBuffer buffer; + std::vector offsets; + bool autoScroll; +}; \ No newline at end of file