refactor(log): migra core/resources i core/system a Log::

- resource_pack.cpp i resource_cache.cpp: tots els std::cerr passen a
  Log::error.
- director.cpp: std::cout/cerr/fprintf → Log::info / error / critical
  segons severitat.
- printWithDots (utils.cpp): ara és un wrapper sobre Log::info que
  manté l'amplada total (~50 chars) reproduint l'alineació de punts.
  Els callers pasen el tag sense " : " (ja l'afegeix el logger).
- pack_resources: afegit log.cpp i SDL3 al target perquè
  resource_pack.cpp ara depèn de Log::.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
JailDesignerandClaude Opus 4.7 committed 2026-05-08 09:10:03 +02:00
1 parent 1243b457be
commit 6e99fd0a8b
8 files changed
+51 -44

No files matched your search

+2
View File
@@ -219,8 +219,10 @@ target_link_libraries(${PROJECT_NAME} PRIVATE SDL3::SDL3)
add_executable(pack_resources EXCLUDE_FROM_ALL add_executable(pack_resources EXCLUDE_FROM_ALL
tools/pack_resources/pack_resources.cpp tools/pack_resources/pack_resources.cpp
source/core/resources/resource_pack.cpp source/core/resources/resource_pack.cpp
source/utils/log.cpp
) )
target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source") target_include_directories(pack_resources PRIVATE "${CMAKE_SOURCE_DIR}/source")
target_link_libraries(pack_resources PRIVATE SDL3::SDL3)
target_compile_options(pack_resources PRIVATE -Wall -Wextra -Wpedantic) target_compile_options(pack_resources PRIVATE -Wall -Wextra -Wpedantic)
# --- REGENERACIÓ AUTOMÀTICA DE build/resources.pack --- # --- REGENERACIÓ AUTOMÀTICA DE build/resources.pack ---
+1 -1
View File
@@ -63,7 +63,7 @@ Surface::Surface(const std::string& file_path) {
palette_[i] = Rendering::OPAQUE_ALPHA | rgb[i]; palette_[i] = Rendering::OPAQUE_ALPHA | rgb[i];
} }
printWithDots("Surface : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Surface", getFileName(file_path), "[ LOADED ]");
} }
void Surface::clear(Uint8 color) { void Surface::clear(Uint8 color) {
+1 -1
View File
@@ -101,7 +101,7 @@ auto Text::loadTextFile(const std::string& file_path) -> std::shared_ptr<File> {
} }
} }
printWithDots("Text File : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Text File", getFileName(file_path), "[ LOADED ]");
return tf; return tf;
} }
+10 -10
View File
@@ -1,12 +1,12 @@
#include "core/resources/resource_cache.hpp" #include "core/resources/resource_cache.hpp"
#include <algorithm> #include <algorithm>
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include "core/audio/jail_audio.hpp" #include "core/audio/jail_audio.hpp"
#include "core/resources/palette_loader.hpp" #include "core/resources/palette_loader.hpp"
#include "core/resources/resource_helper.hpp" #include "core/resources/resource_helper.hpp"
#include "utils/log.hpp"
#include "utils/utils.hpp" #include "utils/utils.hpp"
// Deleters dels recursos d'àudio: necessiten veure el tipus complet de // Deleters dels recursos d'àudio: necessiten veure el tipus complet de
@@ -37,19 +37,19 @@ namespace Resource {
throw std::runtime_error("Cache: cannot load animation " + file_path); throw std::runtime_error("Cache: cannot load animation " + file_path);
} }
animations_.emplace_back(AnimationResource{.name = name, .yaml_data = std::move(bytes)}); animations_.emplace_back(AnimationResource{.name = name, .yaml_data = std::move(bytes)});
printWithDots("Animation : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Animation", getFileName(file_path), "[ LOADED ]");
} }
void Cache::loadPalette(const std::string& name, const std::string& file_path) { void Cache::loadPalette(const std::string& name, const std::string& file_path) {
auto palette = PaletteLoader::loadFromFile(file_path); auto palette = PaletteLoader::loadFromFile(file_path);
palettes_.emplace_back(PaletteResource{.name = name, .palette = palette}); palettes_.emplace_back(PaletteResource{.name = name, .palette = palette});
printWithDots("Palette : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Palette", getFileName(file_path), "[ LOADED ]");
} }
auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> { auto Cache::getSurface(const std::string& name) -> std::shared_ptr<Surface> {
auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; }); auto it = std::ranges::find_if(surfaces_, [&name](const auto& s) { return s.name == name; });
if (it == surfaces_.end()) { if (it == surfaces_.end()) {
std::cerr << "Cache: surface not found: " << name << '\n'; Log::error("Cache", "surface not found: {}", name);
throw std::runtime_error("Surface not found: " + name); throw std::runtime_error("Surface not found: " + name);
} }
return it->surface; return it->surface;
@@ -58,7 +58,7 @@ namespace Resource {
auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& { auto Cache::getAnimationData(const std::string& name) -> const AnimationResource& {
auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; }); auto it = std::ranges::find_if(animations_, [&name](const auto& a) { return a.name == name; });
if (it == animations_.end()) { if (it == animations_.end()) {
std::cerr << "Cache: animation not found: " << name << '\n'; Log::error("Cache", "animation not found: {}", name);
throw std::runtime_error("Animation not found: " + name); throw std::runtime_error("Animation not found: " + name);
} }
return *it; return *it;
@@ -67,7 +67,7 @@ namespace Resource {
auto Cache::getPalette(const std::string& name) -> const Palette& { auto Cache::getPalette(const std::string& name) -> const Palette& {
auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; }); auto it = std::ranges::find_if(palettes_, [&name](const auto& p) { return p.name == name; });
if (it == palettes_.end()) { if (it == palettes_.end()) {
std::cerr << "Cache: palette not found: " << name << '\n'; Log::error("Cache", "palette not found: {}", name);
throw std::runtime_error("Palette not found: " + name); throw std::runtime_error("Palette not found: " + name);
} }
return it->palette; return it->palette;
@@ -83,7 +83,7 @@ namespace Resource {
throw std::runtime_error("Cache: Ja::loadMusic failed for " + file_path); throw std::runtime_error("Cache: Ja::loadMusic failed for " + file_path);
} }
musics_.emplace_back(MusicResource{.name = name, .music = std::unique_ptr<Ja::Music, MusicDeleter>(raw)}); musics_.emplace_back(MusicResource{.name = name, .music = std::unique_ptr<Ja::Music, MusicDeleter>(raw)});
printWithDots("Music : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Music", getFileName(file_path), "[ LOADED ]");
} }
void Cache::loadSound(const std::string& name, const std::string& file_path) { void Cache::loadSound(const std::string& name, const std::string& file_path) {
@@ -96,13 +96,13 @@ namespace Resource {
throw std::runtime_error("Cache: Ja::loadSound failed for " + file_path); throw std::runtime_error("Cache: Ja::loadSound failed for " + file_path);
} }
sounds_.emplace_back(SoundResource{.name = name, .sound = std::unique_ptr<Ja::Sound, SoundDeleter>(raw)}); sounds_.emplace_back(SoundResource{.name = name, .sound = std::unique_ptr<Ja::Sound, SoundDeleter>(raw)});
printWithDots("Sound : ", getFileName(file_path), "[ LOADED ]"); printWithDots("Sound", getFileName(file_path), "[ LOADED ]");
} }
auto Cache::getMusic(const std::string& name) -> Ja::Music* { auto Cache::getMusic(const std::string& name) -> Ja::Music* {
auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; }); auto it = std::ranges::find_if(musics_, [&name](const auto& m) { return m.name == name; });
if (it == musics_.end()) { if (it == musics_.end()) {
std::cerr << "Cache: music not found: " << name << '\n'; Log::error("Cache", "music not found: {}", name);
throw std::runtime_error("Music not found: " + name); throw std::runtime_error("Music not found: " + name);
} }
return it->music.get(); return it->music.get();
@@ -111,7 +111,7 @@ namespace Resource {
auto Cache::getSound(const std::string& name) -> Ja::Sound* { auto Cache::getSound(const std::string& name) -> Ja::Sound* {
auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; }); auto it = std::ranges::find_if(sounds_, [&name](const auto& s) { return s.name == name; });
if (it == sounds_.end()) { if (it == sounds_.end()) {
std::cerr << "Cache: sound not found: " << name << '\n'; Log::error("Cache", "sound not found: {}", name);
throw std::runtime_error("Sound not found: " + name); throw std::runtime_error("Sound not found: " + name);
} }
return it->sound.get(); return it->sound.get();
+10 -9
View File
@@ -4,7 +4,8 @@
#include <array> #include <array>
#include <filesystem> #include <filesystem>
#include <fstream> #include <fstream>
#include <iostream>
#include "utils/log.hpp"
namespace Resource { namespace Resource {
@@ -40,14 +41,14 @@ namespace Resource {
std::array<char, 4> header{}; std::array<char, 4> header{};
file.read(header.data(), 4); file.read(header.data(), 4);
if (std::string(header.data(), 4) != MAGIC) { if (std::string(header.data(), 4) != MAGIC) {
std::cerr << "Resource::Pack: invalid pack file format (bad magic): " << pack_file << '\n'; Log::error("Resource::Pack", "invalid pack file format (bad magic): {}", pack_file);
return false; return false;
} }
std::uint32_t version = 0; std::uint32_t version = 0;
file.read(reinterpret_cast<char*>(&version), sizeof(version)); file.read(reinterpret_cast<char*>(&version), sizeof(version));
if (version != VERSION) { if (version != VERSION) {
std::cerr << "Resource::Pack: unsupported pack version: " << version << '\n'; Log::error("Resource::Pack", "unsupported pack version: {}", version);
return false; return false;
} }
@@ -86,7 +87,7 @@ namespace Resource {
auto Pack::savePack(const std::string& pack_file) const -> bool { auto Pack::savePack(const std::string& pack_file) const -> bool {
std::ofstream file(pack_file, std::ios::binary); std::ofstream file(pack_file, std::ios::binary);
if (!file) { if (!file) {
std::cerr << "Resource::Pack: could not create pack file: " << pack_file << '\n'; Log::error("Resource::Pack", "could not create pack file: {}", pack_file);
return false; return false;
} }
@@ -120,7 +121,7 @@ namespace Resource {
auto Pack::addFile(const std::string& filename, const std::string& filepath) -> bool { auto Pack::addFile(const std::string& filename, const std::string& filepath) -> bool {
std::ifstream file(filepath, std::ios::binary | std::ios::ate); std::ifstream file(filepath, std::ios::binary | std::ios::ate);
if (!file) { if (!file) {
std::cerr << "Resource::Pack: could not open file: " << filepath << '\n'; Log::error("Resource::Pack", "could not open file: {}", filepath);
return false; return false;
} }
@@ -129,7 +130,7 @@ namespace Resource {
std::vector<std::uint8_t> file_data(file_size); std::vector<std::uint8_t> file_data(file_size);
if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) { if (!file.read(reinterpret_cast<char*>(file_data.data()), file_size)) {
std::cerr << "Resource::Pack: could not read file: " << filepath << '\n'; Log::error("Resource::Pack", "could not read file: {}", filepath);
return false; return false;
} }
@@ -146,7 +147,7 @@ namespace Resource {
auto Pack::addDirectory(const std::string& directory) -> bool { auto Pack::addDirectory(const std::string& directory) -> bool {
if (!std::filesystem::exists(directory)) { if (!std::filesystem::exists(directory)) {
std::cerr << "Resource::Pack: directory does not exist: " << directory << '\n'; Log::error("Resource::Pack", "directory does not exist: {}", directory);
return false; return false;
} }
@@ -169,7 +170,7 @@ namespace Resource {
const PackEntry& entry = it->second; const PackEntry& entry = it->second;
if (entry.offset + entry.size > data_.size()) { if (entry.offset + entry.size > data_.size()) {
std::cerr << "Resource::Pack: invalid resource data: " << filename << '\n'; Log::error("Resource::Pack", "invalid resource data: {}", filename);
return {}; return {};
} }
@@ -179,7 +180,7 @@ namespace Resource {
std::uint32_t checksum = calculateChecksum(result); std::uint32_t checksum = calculateChecksum(result);
if (checksum != entry.checksum) { if (checksum != entry.checksum) {
std::cerr << "Resource::Pack: checksum mismatch for: " << filename << '\n'; Log::error("Resource::Pack", "checksum mismatch for: {}", filename);
} }
return result; return result;
} }
+11 -13
View File
@@ -4,10 +4,8 @@
#include <algorithm> #include <algorithm>
#include <any> #include <any>
#include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <filesystem> #include <filesystem>
#include <iostream>
#include <memory> #include <memory>
#include <string> #include <string>
#include <utility> #include <utility>
@@ -42,10 +40,11 @@
#include "game/scenes/title_scene.hpp" #include "game/scenes/title_scene.hpp"
#include "game/ui/service_menu.hpp" #include "game/ui/service_menu.hpp"
#include "utils/defaults.hpp" #include "utils/defaults.hpp"
#include "utils/log.hpp"
Director::Director() { Director::Director() {
if (!SDL_Init(0)) { if (!SDL_Init(0)) {
std::fprintf(stderr, "SDL_Init failed: %s\n", SDL_GetError()); Log::critical("Director", "SDL_Init failed: {}", SDL_GetError());
} }
// Sistema de recursos: el pack viu al costat del binari (build/), el // Sistema de recursos: el pack viu al costat del binari (build/), el
@@ -66,7 +65,7 @@ Director::Director() {
constexpr bool ENABLE_FALLBACK = false; constexpr bool ENABLE_FALLBACK = false;
#endif #endif
if (!Resource::Helper::initializeResourceSystem(pack_file, ENABLE_FALLBACK)) { if (!Resource::Helper::initializeResourceSystem(pack_file, ENABLE_FALLBACK)) {
std::fprintf(stderr, "Director: sistema de recursos no disponible; avortem\n"); Log::critical("Director", "sistema de recursos no disponible; avortem");
std::exit(1); std::exit(1);
} }
@@ -85,7 +84,7 @@ Director::Director() {
Options::setConfigFile(system_folder_ + "config.yaml"); Options::setConfigFile(system_folder_ + "config.yaml");
if (!Options::loadFromFile()) { if (!Options::loadFromFile()) {
std::cerr << "Director: no s'ha pogut carregar ni desar config.yaml a " << system_folder_ << '\n'; Log::error("Director", "no s'ha pogut carregar ni desar config.yaml a {}", system_folder_);
} }
// Vinculacions de mando per dispositiu — fitxer aïllat de `config.yaml` // Vinculacions de mando per dispositiu — fitxer aïllat de `config.yaml`
@@ -97,12 +96,12 @@ Director::Director() {
// es crea amb presets per defecte (off, crt, ntsc). // es crea amb presets per defecte (off, crt, ntsc).
Options::setPostFXFile(system_folder_ + Defaults::Video::POSTFX_FILE); Options::setPostFXFile(system_folder_ + Defaults::Video::POSTFX_FILE);
if (!Options::loadPostFXFromFile()) { if (!Options::loadPostFXFromFile()) {
std::cerr << "Director: no s'ha pogut carregar ni desar postfx.yaml.\n"; Log::error("Director", "no s'ha pogut carregar ni desar postfx.yaml.");
} }
// Presets del shader CRT-Pi (shaders/crtpi.yaml). // Presets del shader CRT-Pi (shaders/crtpi.yaml).
Options::setCrtPiFile(system_folder_ + Defaults::Video::CRTPI_FILE); Options::setCrtPiFile(system_folder_ + Defaults::Video::CRTPI_FILE);
if (!Options::loadCrtPiFromFile()) { if (!Options::loadCrtPiFromFile()) {
std::cerr << "Director: no s'ha pogut carregar ni desar crtpi.yaml.\n"; Log::error("Director", "no s'ha pogut carregar ni desar crtpi.yaml.");
} }
// Screen es configura ara (subsystem + finestra + GPU/renderer); la seua // Screen es configura ara (subsystem + finestra + GPU/renderer); la seua
@@ -295,8 +294,7 @@ Director::Director() {
const auto START = DebugConfig::StartScene::LOGO; const auto START = DebugConfig::StartScene::LOGO;
const auto STARTING_PLAYERS = DebugConfig::StartingPlayers::BOTH; const auto STARTING_PLAYERS = DebugConfig::StartingPlayers::BOTH;
#endif #endif
std::cout << "Director: start_scene=" << DebugConfig::toString(START) Log::info("Director", "start_scene={} starting_players={}", DebugConfig::toString(START), DebugConfig::toString(STARTING_PLAYERS));
<< " starting_players=" << DebugConfig::toString(STARTING_PLAYERS) << '\n';
switch (START) { switch (START) {
case DebugConfig::StartScene::LOGO: case DebugConfig::StartScene::LOGO:
@@ -351,7 +349,7 @@ void Director::createSystemFolder() {
// Subcarpeta per als presets de shaders. std::filesystem és tolerant si ja existeix. // Subcarpeta per als presets de shaders. std::filesystem és tolerant si ja existeix.
std::filesystem::create_directories(system_folder_ + "shaders/", error); std::filesystem::create_directories(system_folder_ + "shaders/", error);
if (error) { if (error) {
std::cerr << "Director: no s'ha pogut crear " << system_folder_ << ": " << error.message() << '\n'; Log::error("Director", "no s'ha pogut crear {}: {}", system_folder_, error.message());
} }
} }
@@ -433,7 +431,7 @@ void Director::switchSceneIfRequested() {
// l'escena en aquest mateix frame — l'usuari ha cancel·lat l'estat // l'escena en aquest mateix frame — l'usuari ha cancel·lat l'estat
// de joc explícitament i no ens interessa mantenir-ne la sortida. // de joc explícitament i no ens interessa mantenir-ne la sortida.
if (auto menu_req = ServiceMenu::get().takePendingSceneRequest(); menu_req.has_value()) { if (auto menu_req = ServiceMenu::get().takePendingSceneRequest(); menu_req.has_value()) {
std::cout << "Director: scene transition (menu) → " << toString(menu_req->next) << '\n'; Log::info("Director", "scene transition (menu) → {}", toString(menu_req->next));
current_scene_ = buildScene(menu_req->next, menu_req->params); current_scene_ = buildScene(menu_req->next, menu_req->params);
return; return;
} }
@@ -445,10 +443,10 @@ void Director::switchSceneIfRequested() {
if (!request.has_value()) { return; } if (!request.has_value()) { return; }
try { try {
const auto& req = std::any_cast<const ExitRequest&>(request); const auto& req = std::any_cast<const ExitRequest&>(request);
std::cout << "Director: scene transition → " << toString(req.next) << '\n'; Log::info("Director", "scene transition → {}", toString(req.next));
current_scene_ = buildScene(req.next, req.params); current_scene_ = buildScene(req.next, req.params);
} catch (const std::bad_any_cast&) { } catch (const std::bad_any_cast&) {
std::cerr << "Director: exit_request_ amb tipus inesperat — ignorant\n"; Log::error("Director", "exit_request_ amb tipus inesperat — ignorant");
} }
} }
+13 -9
View File
@@ -1,18 +1,22 @@
#include "utils/utils.hpp" #include "utils/utils.hpp"
#include <filesystem> #include <filesystem>
#include <ios> #include <string>
#include <iostream>
#include "utils/log.hpp"
auto getFileName(const std::string& path) -> std::string { auto getFileName(const std::string& path) -> std::string {
return std::filesystem::path(path).filename().string(); return std::filesystem::path(path).filename().string();
} }
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3) { void printWithDots(const std::string& tag, const std::string& filename, const std::string& status) {
std::cout.setf(std::ios::left, std::ios::adjustfield); // Reprodueix l'alineació històrica (amplada total ≈ 50). El logger anteposa
std::cout << text1; // automàticament "tag: ", per això aquí només componem el cos.
std::cout.width(50 - text1.length() - text3.length()); constexpr std::size_t TOTAL_WIDTH = 50;
std::cout.fill('.'); const std::size_t PREFIX_LEN = tag.size() + 2; // "{tag}: "
std::cout << text2; const std::size_t USED = PREFIX_LEN + status.size();
std::cout << text3 << '\n'; const std::size_t FIELD = (USED < TOTAL_WIDTH) ? (TOTAL_WIDTH - USED) : 0;
std::string body = filename;
if (body.size() < FIELD) { body.resize(FIELD, '.'); }
Log::info(tag, "{}{}", body, status);
} }
+3 -1
View File
@@ -3,4 +3,6 @@
#include <string> #include <string>
auto getFileName(const std::string& path) -> std::string; auto getFileName(const std::string& path) -> std::string;
void printWithDots(const std::string& text1, const std::string& text2, const std::string& text3); // Línia de càrrega amb alineació de punts: "{tag}: filename..........{status}".
// Embolcalla `Log::info`, amb la qual cosa respecta `AEE_LOG`.
void printWithDots(const std::string& tag, const std::string& filename, const std::string& status);