From b245b10963881e510b3899994504d037aee90639 Mon Sep 17 00:00:00 2001 From: scawful Date: Wed, 22 Jan 2025 13:36:37 -0500 Subject: [PATCH] Refactor logging to use util::logf and add log utility header --- src/app/core/common.h | 19 ----------- src/app/editor/message/message_data.cc | 7 ++-- src/app/editor/overworld/overworld_editor.cc | 11 +++--- src/app/rom.cc | 13 +++---- src/app/zelda3/overworld/overworld.cc | 29 ++++++++-------- src/app/zelda3/overworld/overworld_map.cc | 3 +- src/util/hex.cc | 2 +- src/util/hex.h | 5 ++- src/util/log.h | 36 ++++++++++++++++++++ 9 files changed, 75 insertions(+), 50 deletions(-) create mode 100644 src/util/log.h diff --git a/src/app/core/common.h b/src/app/core/common.h index 6ef687ea..083b95d4 100644 --- a/src/app/core/common.h +++ b/src/app/core/common.h @@ -112,25 +112,6 @@ class ExperimentFlags { } }; -static const std::string kLogFileOut = "yaze_log.txt"; - -template -static void logf(const absl::FormatSpec &format, const Args &...args) { - std::string message = absl::StrFormat(format, args...); - auto timestamp = std::chrono::system_clock::now(); - - std::time_t now_tt = std::chrono::system_clock::to_time_t(timestamp); - std::tm tm = *std::localtime(&now_tt); - message = absl::StrCat("[", tm.tm_hour, ":", tm.tm_min, ":", tm.tm_sec, "] ", - message, "\n"); - - if (ExperimentFlags::get().kLogToConsole) { - std::cout << message; - } - static std::ofstream fout(kLogFileOut, std::ios::out | std::ios::app); - fout << message; -} - constexpr uint32_t kFastRomRegion = 0x808000; inline uint32_t SnesToPc(uint32_t addr) noexcept { diff --git a/src/app/editor/message/message_data.cc b/src/app/editor/message/message_data.cc index 82f81415..8f3fbba7 100644 --- a/src/app/editor/message/message_data.cc +++ b/src/app/editor/message/message_data.cc @@ -4,6 +4,7 @@ #include "app/core/common.h" #include "util/hex.h" +#include "util/log.h" namespace yaze { namespace editor { @@ -120,7 +121,7 @@ std::vector ParseMessageToData(std::string str) { TextElement(0x80, DICTIONARYTOKEN, true, "Dictionary"); if (!parsedElement.Active) { - core::logf("Error parsing message: %s", temp_string); + util::logf("Error parsing message: %s", temp_string); break; } else if (parsedElement.Parent == dictionary_element) { bytes.push_back(parsedElement.Value); @@ -138,7 +139,7 @@ std::vector ParseMessageToData(std::string str) { uint8_t bb = FindMatchingCharacter(temp_string[pos++]); if (bb != 0xFF) { - core::logf("Error parsing message: %s", temp_string); + util::logf("Error parsing message: %s", temp_string); bytes.push_back(bb); } } @@ -303,7 +304,7 @@ std::vector ImportMessageData(std::string_view filename) { std::vector messages; std::ifstream file(filename.data()); if (!file.is_open()) { - core::logf("Error opening file: %s", filename); + util::logf("Error opening file: %s", filename); return messages; } diff --git a/src/app/editor/overworld/overworld_editor.cc b/src/app/editor/overworld/overworld_editor.cc index f6db8968..6a0d3690 100644 --- a/src/app/editor/overworld/overworld_editor.cc +++ b/src/app/editor/overworld/overworld_editor.cc @@ -24,6 +24,7 @@ #include "imgui/imgui.h" #include "imgui_memory_editor.h" #include "util/hex.h" +#include "util/log.h" #include "util/macro.h" @@ -1020,18 +1021,18 @@ absl::Status OverworldEditor::Save() { } absl::Status OverworldEditor::LoadGraphics() { - core::logf("Loading overworld."); + util::logf("Loading overworld."); // Load the Link to the Past overworld. RETURN_IF_ERROR(overworld_.Load(rom_)) palette_ = overworld_.current_area_palette(); - core::logf("Loading overworld graphics."); + util::logf("Loading overworld graphics."); // Create the area graphics image RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap( 0x80, kOverworldMapSize, 0x40, overworld_.current_graphics(), current_gfx_bmp_, palette_)); - core::logf("Loading overworld tileset."); + util::logf("Loading overworld tileset."); // Create the tile16 blockset image RETURN_IF_ERROR(Renderer::GetInstance().CreateAndRenderBitmap( 0x80, 0x2000, 0x08, overworld_.tile16_blockset_data(), @@ -1041,7 +1042,7 @@ absl::Status OverworldEditor::LoadGraphics() { // Copy the tile16 data into individual tiles. auto tile16_data = overworld_.tile16_blockset_data(); - core::logf("Loading overworld tile16 graphics."); + util::logf("Loading overworld tile16 graphics."); // Loop through the tiles and copy their pixel data into separate vectors for (unsigned int i = 0; i < zelda3::kNumTile16Individual; i++) { tile16_individual_[i].Create(kTile16Size, kTile16Size, 0x08, @@ -1062,7 +1063,7 @@ absl::Status OverworldEditor::LoadGraphics() { Renderer::GetInstance().RenderBitmap(&tile16_individual_[i]); } - core::logf("Loading overworld maps."); + util::logf("Loading overworld maps."); // Render the overworld maps loaded from the ROM. for (int i = 0; i < zelda3::kNumOverworldMaps; ++i) { overworld_.set_current_map(i); diff --git a/src/app/rom.cc b/src/app/rom.cc index c4e0f26a..141ea848 100644 --- a/src/app/rom.cc +++ b/src/app/rom.cc @@ -22,6 +22,7 @@ #include "app/gfx/snes_palette.h" #include "app/gfx/snes_tile.h" #include "util/hex.h" +#include "util/log.h" #include "util/macro.h" namespace yaze { @@ -304,7 +305,7 @@ absl::Status Rom::WriteByte(int addr, uint8_t value) { value, addr)); } rom_data_[addr] = value; - core::logf("WriteByte: %#06X: %s", addr, util::HexByte(value).data()); + util::logf("WriteByte: %#06X: %s", addr, util::HexByte(value).data()); return absl::OkStatus(); } @@ -316,7 +317,7 @@ absl::Status Rom::WriteWord(int addr, uint16_t value) { } rom_data_[addr] = (uint8_t)(value & 0xFF); rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF); - core::logf("WriteWord: %#06X: %s", addr, util::HexWord(value).data()); + util::logf("WriteWord: %#06X: %s", addr, util::HexWord(value).data()); return absl::OkStatus(); } @@ -328,7 +329,7 @@ absl::Status Rom::WriteShort(int addr, uint16_t value) { } rom_data_[addr] = (uint8_t)(value & 0xFF); rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF); - core::logf("WriteShort: %#06X: %s", addr, util::HexWord(value).data()); + util::logf("WriteShort: %#06X: %s", addr, util::HexWord(value).data()); return absl::OkStatus(); } @@ -341,7 +342,7 @@ absl::Status Rom::WriteLong(uint32_t addr, uint32_t value) { rom_data_[addr] = (uint8_t)(value & 0xFF); rom_data_[addr + 1] = (uint8_t)((value >> 8) & 0xFF); rom_data_[addr + 2] = (uint8_t)((value >> 16) & 0xFF); - core::logf("WriteLong: %#06X: %s", addr, util::HexLong(value).data()); + util::logf("WriteLong: %#06X: %s", addr, util::HexLong(value).data()); return absl::OkStatus(); } @@ -354,7 +355,7 @@ absl::Status Rom::WriteVector(int addr, std::vector data) { for (int i = 0; i < static_cast(data.size()); i++) { rom_data_[addr + i] = data[i]; } - core::logf("WriteVector: %#06X: %s", addr, util::HexByte(data[0]).data()); + util::logf("WriteVector: %#06X: %s", addr, util::HexByte(data[0]).data()); return absl::OkStatus(); } @@ -363,7 +364,7 @@ absl::Status Rom::WriteColor(uint32_t address, const gfx::SnesColor &color) { (color.snes() & 0x7C00); // Write the 16-bit color value to the ROM at the specified address - core::logf("WriteColor: %#06X: %s", address, util::HexWord(bgr).data()); + util::logf("WriteColor: %#06X: %s", address, util::HexWord(bgr).data()); return WriteShort(address, bgr); } diff --git a/src/app/zelda3/overworld/overworld.cc b/src/app/zelda3/overworld/overworld.cc index 41c6647b..f70fb1e9 100644 --- a/src/app/zelda3/overworld/overworld.cc +++ b/src/app/zelda3/overworld/overworld.cc @@ -10,6 +10,7 @@ #include "app/gfx/snes_tile.h" #include "app/rom.h" #include "util/hex.h" +#include "util/log.h" #include "util/macro.h" namespace yaze { @@ -532,7 +533,7 @@ absl::Status Overworld::Save(Rom &rom) { } absl::Status Overworld::SaveOverworldMaps() { - core::logf("Saving Overworld Maps"); + util::logf("Saving Overworld Maps"); // Initialize map pointers std::fill(map_pointers1_id.begin(), map_pointers1_id.end(), -1); @@ -572,7 +573,7 @@ absl::Status Overworld::SaveOverworldMaps() { } if ((pos + size_a) >= 0x6411F && (pos + size_a) <= 0x70000) { - core::logf("Pos set to overflow region for map %s at %s", + util::logf("Pos set to overflow region for map %s at %s", std::to_string(i), util::HexLong(pos)); pos = kOverworldMapDataOverflow; // 0x0F8780; } @@ -609,7 +610,7 @@ absl::Status Overworld::SaveOverworldMaps() { std::copy(a.begin(), a.end(), map_data_p1[i].begin()); int snes_pos = core::PcToSnes(pos); map_pointers1[i] = snes_pos; - core::logf("Saving map pointers1 and compressed data for map %s at %s", + util::logf("Saving map pointers1 and compressed data for map %s at %s", util::HexByte(i), util::HexLong(snes_pos)); RETURN_IF_ERROR(rom()->WriteLong( rom()->version_constants().kCompressedAllMap32PointersLow + (3 * i), @@ -619,7 +620,7 @@ absl::Status Overworld::SaveOverworldMaps() { } else { // Save pointer for map1 int snes_pos = map_pointers1[map_pointers1_id[i]]; - core::logf("Saving map pointers1 for map %s at %s", util::HexByte(i), + util::logf("Saving map pointers1 for map %s at %s", util::HexByte(i), util::HexLong(snes_pos)); RETURN_IF_ERROR(rom()->WriteLong( rom()->version_constants().kCompressedAllMap32PointersLow + (3 * i), @@ -631,7 +632,7 @@ absl::Status Overworld::SaveOverworldMaps() { } if ((pos + b.size()) >= 0x6411F && (pos + b.size()) <= 0x70000) { - core::logf("Pos set to overflow region for map %s at %s", + util::logf("Pos set to overflow region for map %s at %s", util::HexByte(i), util::HexLong(pos)); pos = kOverworldMapDataOverflow; } @@ -641,7 +642,7 @@ absl::Status Overworld::SaveOverworldMaps() { std::copy(b.begin(), b.end(), map_data_p2[i].begin()); int snes_pos = core::PcToSnes(pos); map_pointers2[i] = snes_pos; - core::logf("Saving map pointers2 and compressed data for map %s at %s", + util::logf("Saving map pointers2 and compressed data for map %s at %s", util::HexByte(i), util::HexLong(snes_pos)); RETURN_IF_ERROR(rom()->WriteLong( rom()->version_constants().kCompressedAllMap32PointersHigh + (3 * i), @@ -651,7 +652,7 @@ absl::Status Overworld::SaveOverworldMaps() { } else { // Save pointer for map2 int snes_pos = map_pointers2[map_pointers2_id[i]]; - core::logf("Saving map pointers2 for map %s at %s", util::HexByte(i), + util::logf("Saving map pointers2 for map %s at %s", util::HexByte(i), util::HexLong(snes_pos)); RETURN_IF_ERROR(rom()->WriteLong( rom()->version_constants().kCompressedAllMap32PointersHigh + (3 * i), @@ -661,7 +662,7 @@ absl::Status Overworld::SaveOverworldMaps() { // Check if too many maps data if (pos > kOverworldCompressedOverflowPos) { - core::logf("Too many maps data %s", util::HexLong(pos)); + util::logf("Too many maps data %s", util::HexLong(pos)); return absl::AbortedError("Too many maps data " + std::to_string(pos)); } @@ -670,7 +671,7 @@ absl::Status Overworld::SaveOverworldMaps() { } absl::Status Overworld::SaveLargeMaps() { - core::logf("Saving Large Maps"); + util::logf("Saving Large Maps"); std::vector checked_map; for (int i = 0; i < 0x40; i++) { @@ -1148,7 +1149,7 @@ absl::Status Overworld::SaveMap32Expanded() { } absl::Status Overworld::SaveMap32Tiles() { - core::logf("Saving Map32 Tiles"); + util::logf("Saving Map32 Tiles"); constexpr int kMaxUniqueTiles = 0x4540; constexpr int kTilesPer32x32Tile = 6; @@ -1355,7 +1356,7 @@ absl::Status Overworld::SaveMap16Expanded() { } absl::Status Overworld::SaveMap16Tiles() { - core::logf("Saving Map16 Tiles"); + util::logf("Saving Map16 Tiles"); int tpos = kMap16Tiles; // 3760 for (int i = 0; i < NumberOfMap16; i += 1) { @@ -1376,7 +1377,7 @@ absl::Status Overworld::SaveMap16Tiles() { } absl::Status Overworld::SaveEntrances() { - core::logf("Saving Entrances"); + util::logf("Saving Entrances"); int ow_entrance_map_ptr = kOverworldEntranceMap; int ow_entrance_pos_ptr = kOverworldEntrancePos; int ow_entrance_id_ptr = kOverworldEntranceEntranceId; @@ -1410,7 +1411,7 @@ absl::Status Overworld::SaveEntrances() { } absl::Status Overworld::SaveExits() { - core::logf("Saving Exits"); + util::logf("Saving Exits"); for (int i = 0; i < kNumOverworldExits; i++) { RETURN_IF_ERROR( rom()->WriteShort(OWExitRoomId + (i * 2), all_exits_[i].room_id_)); @@ -1552,7 +1553,7 @@ absl::Status Overworld::SaveItems() { } absl::Status Overworld::SaveMapProperties() { - core::logf("Saving Map Properties"); + util::logf("Saving Map Properties"); for (int i = 0; i < kDarkWorldMapIdStart; i++) { RETURN_IF_ERROR(rom()->WriteByte(kAreaGfxIdPtr + i, overworld_maps_[i].area_graphics())); diff --git a/src/app/zelda3/overworld/overworld_map.cc b/src/app/zelda3/overworld/overworld_map.cc index 0e3453fb..cf4e7e72 100644 --- a/src/app/zelda3/overworld/overworld_map.cc +++ b/src/app/zelda3/overworld/overworld_map.cc @@ -9,6 +9,7 @@ #include "app/rom.h" #include "app/zelda3/overworld/overworld.h" #include "util/hex.h" +#include "util/log.h" namespace yaze { namespace zelda3 { @@ -77,7 +78,7 @@ void OverworldMap::LoadAreaInfo() { message_id_ = message_id.value(); } else { message_id_ = 0; - core::logf("Error reading message id for map %d", parent_); + util::logf("Error reading message id for map %d", parent_); } if (index_ < kDarkWorldMapIdStart) { diff --git a/src/util/hex.cc b/src/util/hex.cc index e86361e2..9f87cd3b 100644 --- a/src/util/hex.cc +++ b/src/util/hex.cc @@ -2,7 +2,7 @@ #include -#include "absl/strings/str_cat" +#include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/str_join.h" diff --git a/src/util/hex.h b/src/util/hex.h index ff86ca68..6ed9e0ff 100644 --- a/src/util/hex.h +++ b/src/util/hex.h @@ -2,6 +2,7 @@ #define YAZE_UTIL_HEX_H #include +#include namespace yaze { namespace util { @@ -17,4 +18,6 @@ std::string HexLong(uint32_t dword, HexStringParams params = {}); std::string HexLongLong(uint64_t qword, HexStringParams params = {}); } // namespace util -} // namespace yaze \ No newline at end of file +} // namespace yaze + +#endif \ No newline at end of file diff --git a/src/util/log.h b/src/util/log.h new file mode 100644 index 00000000..eb8990e5 --- /dev/null +++ b/src/util/log.h @@ -0,0 +1,36 @@ +#ifndef YAZE_UTIL_LOG_H +#define YAZE_UTIL_LOG_H + +#include +#include +#include +#include +#include + +#include "absl/strings/str_cat.h" +#include "absl/strings/str_format.h" + +namespace yaze { +namespace util { + +static const std::string kLogFileOut = "yaze_log.txt"; + +template +static void logf(const absl::FormatSpec &format, const Args &...args) { + std::string message = absl::StrFormat(format, args...); + auto timestamp = std::chrono::system_clock::now(); + + std::time_t now_tt = std::chrono::system_clock::to_time_t(timestamp); + std::tm tm = *std::localtime(&now_tt); + message = absl::StrCat("[", tm.tm_hour, ":", tm.tm_min, ":", tm.tm_sec, "] ", + message, "\n"); + + if (ExperimentFlags::get().kLogToConsole) { + std::cout << message; + } + static std::ofstream fout(kLogFileOut, std::ios::out | std::ios::app); + fout << message; +} + +} // namespace util +} // namespace yaze \ No newline at end of file