Refactor logging to use util::logf and add log utility header

This commit is contained in:
scawful
2025-01-22 13:36:37 -05:00
parent a5b94ab173
commit b245b10963
9 changed files with 75 additions and 50 deletions

View File

@@ -112,25 +112,6 @@ class ExperimentFlags {
}
};
static const std::string kLogFileOut = "yaze_log.txt";
template <typename... Args>
static void logf(const absl::FormatSpec<Args...> &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 {

View File

@@ -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<uint8_t> 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<uint8_t> 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<std::string> ImportMessageData(std::string_view filename) {
std::vector<std::string> 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;
}

View File

@@ -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);

View File

@@ -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<uint8_t> data) {
for (int i = 0; i < static_cast<int>(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);
}

View File

@@ -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<uint8_t> 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()));

View File

@@ -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) {

View File

@@ -2,7 +2,7 @@
#include <string>
#include "absl/strings/str_cat"
#include "absl/strings/str_cat.h"
#include "absl/strings/str_format.h"
#include "absl/strings/str_join.h"

View File

@@ -2,6 +2,7 @@
#define YAZE_UTIL_HEX_H
#include <cstdint>
#include <string>
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
} // namespace yaze
#endif

36
src/util/log.h Normal file
View File

@@ -0,0 +1,36 @@
#ifndef YAZE_UTIL_LOG_H
#define YAZE_UTIL_LOG_H
#include <chrono>
#include <cstdint>
#include <fstream>
#include <iostream>
#include <string>
#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 <typename... Args>
static void logf(const absl::FormatSpec<Args...> &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