Skip to content

Commit

Permalink
Add logging tags
Browse files Browse the repository at this point in the history
  • Loading branch information
IAmMoltony committed Dec 31, 2023
1 parent a922148 commit e41fde9
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 43 deletions.
8 changes: 4 additions & 4 deletions source/controlsmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ u32 ControlsManager::buttons[ControlsManager::NUM_BUTTONS];

void ControlsManager::loadControls(void)
{
mtnlogMessage(LOG_INFO, "Loading controls");
mtnlogMessageTag(LOG_INFO, "controlsmgr", "Loading controls");

// write default controls if controls file not found
if (!fsFileExists(std::string(std::string(mtnconfigGet("configDir")) + "/controls.cfg").c_str()))
Expand All @@ -37,7 +37,7 @@ void ControlsManager::loadControls(void)

void ControlsManager::saveControls(void)
{
mtnlogMessage(LOG_INFO, "Saving controls");
mtnlogMessageTag(LOG_INFO, "controlsmgr", "Saving controls");

std::ofstream ofs(std::string(std::string(mtnconfigGet("configDir")) + "/controls.cfg").c_str());
ofs << "goleft " << buttons[BUTTON_GO_LEFT] << "\ngoright " << buttons[BUTTON_GO_RIGHT] << "\njump " << buttons[BUTTON_JUMP]
Expand All @@ -55,7 +55,7 @@ u32 ControlsManager::getButton(u8 button)

void ControlsManager::writeDefaultControls(void)
{
mtnlogMessage(LOG_INFO, "Writing default controls");
mtnlogMessageTag(LOG_INFO, "controlsmgr", "Writing default controls");

buttons[BUTTON_GO_LEFT] = DEFAULT_GO_LEFT;
buttons[BUTTON_GO_RIGHT] = DEFAULT_GO_RIGHT;
Expand Down Expand Up @@ -95,7 +95,7 @@ u8 ControlsManager::buttonIDIndex(const std::string &buttonID)

void ControlsManager::setButton(u8 button, u32 key)
{
mtnlogMessage(LOG_INFO, "Setting button %u to key %u", button, key);
mtnlogMessageTag(LOG_INFO, "controlsmgr", "Setting button %u to key %u", button, key);

// if button index invalid or key is not valid
if (button >= NUM_BUTTONS || (key != KEY_A && key != KEY_B && key != KEY_X && key != KEY_Y && key != KEY_LEFT &&
Expand Down
20 changes: 10 additions & 10 deletions source/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void Game::init(void)
fsInitStatus fsInitSt = fsInit();
if (fsInitSt != FS_INIT_STATUS_OK)
{
mtnlogMessage(LOG_ERROR, "Error initializing filesystem");
mtnlogMessageTag(LOG_ERROR, "init", "Error initializing filesystem");
AssetManager::loadDirtBlock();
loadFonts();
while (true)
Expand Down Expand Up @@ -384,15 +384,15 @@ void Game::init(void)
gamever::InitStatus gvis = gamever::init(mtnconfigGet("gameVersionFile"));
if (gvis == gamever::InitStatus::FileOpenError)
{
mtnlogMessage(LOG_INFO, "Error opening game version file '%s': %s", mtnconfigGet("gameVersionFile"), strerror(errno));
mtnlogMessageTag(LOG_ERROR, "init", "Error opening game version file '%s': %s", mtnconfigGet("gameVersionFile"), strerror(errno));
hang();
}

// init logging
mtnlogInit((MtnLogLevel)mtnconfigGetInt("logLevel"), mtnconfigGet("logFile"));
mtnlogColor(true);

mtnlogMessage(LOG_INFO, "Initializing sound");
mtnlogMessageTag(LOG_INFO, "init", "Initializing sound");

// init sound
mmInitDefault((char *)mtnconfigGet("soundbankFile"));
Expand All @@ -402,19 +402,19 @@ void Game::init(void)
fsCreateDir(mtnconfigGet("worldsDir"));
fsCreateDir(mtnconfigGet("configDir"));

mtnlogMessage(LOG_INFO, "Initializing crafting");
mtnlogMessageTag(LOG_INFO, "init", "Initializing crafting");

// init crafting
Player::initCrafting();

mtnlogMessage(LOG_INFO, "Loading general assets");
mtnlogMessageTag(LOG_INFO, "init", "Loading general assets");

// load some assets
AssetManager::loadGeneralAssets();
sndPop = soundEffect(SFX_POP);
sndClick = soundEffect(SFX_CLICK);

mtnlogMessage(LOG_INFO, "Loading settings");
mtnlogMessageTag(LOG_INFO, "init", "Loading settings");

// update settings if need to
SettingsManager::updateSettingsFormat();
Expand All @@ -425,12 +425,12 @@ void Game::init(void)
// set main screen
setMainScreen(SettingsManager::mainScreen);

mtnlogMessage(LOG_INFO, "Loading controls");
mtnlogMessageTag(LOG_INFO, "init", "Loading controls");

// load controls
ControlsManager::loadControls();

mtnlogMessage(LOG_INFO, "Initializing RNG");
mtnlogMessageTag(LOG_INFO, "init", "Initializing RNG");

// set up random number generator
u32 randomSeed;
Expand All @@ -451,7 +451,7 @@ void Game::init(void)
for (int i = 0; i < rng::rangeSigned(10, 100); ++i)
(void)rng::range(rng::range(0, 900), rng::range(0, 300));

mtnlogMessage(LOG_INFO, "Loading menu assets");
mtnlogMessageTag(LOG_INFO, "init", "Loading menu assets");

// load assets for menu
AssetManager::loadMenuAssets();
Expand Down Expand Up @@ -1923,7 +1923,7 @@ void Game::update(void)
std::string worldVersion = getWorldVersion(normalizeWorldFileName(worldName));
if (worldVersion == "alpha0.0.0") // alpha0.0.0 means error
{
mtnlogMessage(LOG_ERROR, "Failed getting world version for world %s", worldName.c_str());
mtnlogMessageTag(LOG_ERROR, "worldselect", "Failed getting world version for world %s", worldName.c_str());
return;
}
u64 worldVersionHash = gamever::getVersionHash(worldVersion);
Expand Down
8 changes: 4 additions & 4 deletions source/pcximage.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,12 @@

void pcxImageLoad(const char *filePath, bool color0Transparent, PCXImage *image)
{
mtnlogMessage(LOG_INFO, "Loading PCX image from file %s", filePath);
mtnlogMessageTag(LOG_INFO, "pcx", "Loading PCX image from file %s", filePath);

FILE *file = fopen(filePath, "rb");
if (!file)
{
mtnlogMessage(LOG_ERROR, "Failed to open PCX image file %s because %s", filePath, strerror(errno));
mtnlogMessageTag(LOG_ERROR, "pcx", "Failed to open PCX image file %s because %s", filePath, strerror(errno));
return;
}

Expand All @@ -23,7 +23,7 @@ void pcxImageLoad(const char *filePath, bool color0Transparent, PCXImage *image)
u8 *pcxBytes = (u8 *)malloc(fileSize);
if (!pcxBytes)
{
mtnlogMessage(LOG_ERROR, "Failed to allocate memory for PCX bytes");
mtnlogMessageTag(LOG_ERROR, "pcx", "Failed to allocate memory for PCX bytes");
fclose(file);
return;
}
Expand All @@ -34,7 +34,7 @@ void pcxImageLoad(const char *filePath, bool color0Transparent, PCXImage *image)
free(pcxBytes);

if (image->simg.bpp == 0)
mtnlogMessage(LOG_WARNING, "PCX file '%s' is corrupted or has wrong color depth", filePath);
mtnlogMessageTag(LOG_WARNING, "pcx", "PCX file '%s' is corrupted or has wrong color depth", filePath);

int flags = GL_TEXTURE_WRAP_S | GL_TEXTURE_WRAP_T | TEXGEN_OFF;
if (color0Transparent)
Expand Down
5 changes: 2 additions & 3 deletions source/playercrafting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ void Player::initCrafting(void)
cpuStartTiming(0); // start measuring time
_craftingRecipes.push_back(CraftingRecipe(line)); // parse + add the recipe
float timeTook = (float)cpuEndTiming() / BUS_CLOCK; // get how much time it took
mtnlogMessage(LOG_INFO, "loaded %s in %f s", line.c_str(), timeTook); // print how much it took
mtnlogMessageTag(LOG_INFO, "crafting", "loaded %s in %f s", line.c_str(), timeTook); // print how much it took
loadTimes[line] = timeTook; // put the time into the list

// the time measuring functions also measure how much time it takes to
Expand All @@ -39,7 +39,6 @@ void Player::initCrafting(void)
float highest = 0.0f;
std::string lowestName = "";
std::string highestName = "";
mtnlogMessage(LOG_INFO, "calculating crafting load results...");
for (const auto &pair : loadTimes)
{
if (pair.second < lowest)
Expand All @@ -54,7 +53,7 @@ void Player::initCrafting(void)
}
}

mtnlogMessage(LOG_INFO, "*** Load Results\nFastest time: %f (%s)\nSlowest time: %f (%s)", lowest, lowestName.c_str(),
mtnlogMessageTag(LOG_INFO, "crafting", "*** Load Results: Fastest time: %f (%s); Slowest time: %f (%s)", lowest, lowestName.c_str(),
highest, highestName.c_str());
}

Expand Down
10 changes: 5 additions & 5 deletions source/save.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ void loadWorld(const std::string &name, Block::List &blocks, EntityList &entitie
{
std::string worldFolder = std::string(std::string(mtnconfigGet("worldsDir")) + "/" + name);

mtnlogMessage(LOG_INFO, "Loading world with name `%s' folder `%s'", name.c_str(), worldFolder.c_str());
mtnlogMessageTag(LOG_INFO, "save", "Loading world with name `%s' folder `%s'", name.c_str(), worldFolder.c_str());

// clear the current world state
blocks.clear();
Expand All @@ -439,7 +439,7 @@ void loadWorld(const std::string &name, Block::List &blocks, EntityList &entitie
// we can't load something that doesn't exist
if (!fsDirExists(worldFolder.c_str()))
{
mtnlogMessage(LOG_ERROR, "folder %s does not exist", worldFolder.c_str());
mtnlogMessageTag(LOG_ERROR, "save", "folder %s does not exist", worldFolder.c_str());
return;
}

Expand All @@ -464,7 +464,7 @@ void loadWorld(const std::string &name, Block::List &blocks, EntityList &entitie
if (!setLoc)
currentLocation = 0; // default location is 0

mtnlogMessage(LOG_INFO, "current location is %d", currentLocation);
mtnlogMessageTag(LOG_INFO, "save", "current location is %d", currentLocation);

std::ifstream wld(worldFolder + "/locations/location" + std::to_string(currentLocation) + ".wld");
std::string line;
Expand Down Expand Up @@ -760,11 +760,11 @@ void loadWorld(const std::string &name, Block::List &blocks, EntityList &entitie
std::ifstream chestFile(chestFileName);
if (chestFile.bad())
{
mtnlogMessage(LOG_ERROR, "bad chest file %s", line.c_str());
mtnlogMessageTag(LOG_ERROR, "save", "bad chest file %s", line.c_str());
continue;
}

mtnlogMessage(LOG_INFO, "Loading chest with ID %d", chestID);
mtnlogMessageTag(LOG_INFO, "save", "Loading chest with ID %d", chestID);

std::string line2;
while (std::getline(chestFile, line2))
Expand Down
22 changes: 11 additions & 11 deletions source/settingsmgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ void SettingsManager::loadLanguageLegacy(void)
{
if (fsFileExists(std::string(std::string(mtnconfigGet("configDir")) + "/lang.cfg").c_str()))
{
mtnlogMessage(LOG_INFO, "Loading legacy language setting");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Loading legacy language setting");

char *data = fsReadFile(std::string(std::string(mtnconfigGet("configDir")) + "/lang.cfg").c_str());
switch (data[0])
Expand All @@ -33,7 +33,7 @@ void SettingsManager::loadLanguageLegacy(void)
Game::instance->lang = Language::Russian;
break;
default:
mtnlogMessage(LOG_WARNING, "Invalid language code '%c'; defaulting to English");
mtnlogMessageTag(LOG_WARNING, "settingsmgr", "Invalid language code '%c'; defaulting to English");
Game::instance->lang = Language::English;
break;
}
Expand All @@ -59,7 +59,7 @@ void SettingsManager::loadSettingsLegacy(void)
// my reasoning might not be correct, but transitioning from using a billion files for storing settings to
// having just a single file is still better.

mtnlogMessage(LOG_INFO, "Loading legacy settings");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Loading legacy settings");

// legacy language setting
loadLanguageLegacy();
Expand All @@ -80,7 +80,7 @@ void SettingsManager::loadSettingsLegacy(void)
autoSaveSeconds = std::stoi(std::string(data));
if (autoSaveSeconds == 1)
{
mtnlogMessage(LOG_INFO, "Auto save every 1 second detected, changing to 15");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Auto save every 1 second detected, changing to 15");
autoSaveSeconds = 15;
}
}
Expand Down Expand Up @@ -126,7 +126,7 @@ void SettingsManager::removeLegacySettings(void)

void SettingsManager::updateSettingsFormat(void)
{
mtnlogMessage(LOG_INFO, "Updating settings format");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Updating settings format");

// check if we need to update
if (fsFileExists(std::string(std::string(mtnconfigGet("configDir")) + "/settings.cfg").c_str()))
Expand All @@ -144,15 +144,15 @@ void SettingsManager::updateSettingsFormat(void)

void SettingsManager::saveSettings(void)
{
mtnlogMessage(LOG_INFO, "Saving settings");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Saving settings");

// open na file
FILE *settingsFile = fopen(std::string(std::string(mtnconfigGet("configDir")) + "/settings.cfg").c_str(), "w");

// check if error
if (!settingsFile)
{
mtnlogMessage(LOG_ERROR, "error opening settings file: %s", strerror(errno));
mtnlogMessageTag(LOG_ERROR, "settingsmgr", "error opening settings file: %s", strerror(errno));
return;
}

Expand All @@ -167,15 +167,15 @@ void SettingsManager::saveSettings(void)

void SettingsManager::loadSettings(void)
{
mtnlogMessage(LOG_INFO, "Loading settings");
mtnlogMessageTag(LOG_INFO, "settingsmgr", "Loading settings");

// open file
std::ifstream file(std::string(std::string(mtnconfigGet("configDir")) + "/settings.cfg"));

// check if error
if (file.bad())
{
mtnlogMessage(LOG_ERROR, "error opening settings file: %s", strerror(errno));
mtnlogMessageTag(LOG_ERROR, "settingsmgr", "error opening settings file: %s", strerror(errno));
return;
}

Expand Down Expand Up @@ -217,7 +217,7 @@ void SettingsManager::loadSettings(void)
else if (split[1] == "1")
Game::instance->lang = Language::Russian;
else
mtnlogMessage(LOG_WARNING, "Invalid language code %s", split[1].c_str());
mtnlogMessageTag(LOG_WARNING, "settingsmgr", "Invalid language code %s", split[1].c_str());
}
else if (split[0] == "blockparts")
{
Expand Down Expand Up @@ -257,7 +257,7 @@ void SettingsManager::loadSettings(void)
else
{
// invalid key
mtnlogMessage(LOG_WARNING, "Invalid setting '%s', ignoring", split[0].c_str());
mtnlogMessageTag(LOG_WARNING, "settingsmgr", "Invalid setting '%s', ignoring", split[0].c_str());
}
}
}
12 changes: 6 additions & 6 deletions source/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ void statsSetWorld(const std::string &worldName)
std::string worldFolder = getWorldFile(worldName);
if (!fsDirExists(worldFolder.c_str()))
{
mtnlogMessage(LOG_ERROR, "statsSetWorld: world %s (%s) doesnt exist", worldName.c_str(),
mtnlogMessageTag(LOG_ERROR, "stats", "world %s (%s) does not exist", worldName.c_str(),
normalizeWorldFileName(worldName).c_str());
return;
}

mtnlogMessage(LOG_INFO, "setting stats world to `%s'", worldName.c_str());
mtnlogMessageTag(LOG_INFO, "stats", "setting stats world to `%s'", worldName.c_str());

_currentWorld = worldFolder;
if (!fsFileExists(_getStatsFile().c_str()))
{
mtnlogMessage(LOG_INFO, "Stats file does not exist. Creating.");
mtnlogMessageTag(LOG_INFO, "stats", "Stats file does not exist. Creating.");
fsCreateFile(_getStatsFile().c_str());
}
}
Expand All @@ -44,7 +44,7 @@ void statsSetEntry(const std::string &entryKey, int value)

void statsSave(void)
{
mtnlogMessage(LOG_INFO, "saving stats");
mtnlogMessageTag(LOG_INFO, "stats", "saving stats");

std::ofstream ofs(_getStatsFile());
for (const auto &entry : _stats)
Expand All @@ -57,7 +57,7 @@ void statsSave(void)

void statsLoad(void)
{
mtnlogMessage(LOG_INFO, "Loading stats from file %s", _getStatsFile().c_str());
mtnlogMessageTag(LOG_INFO, "stats", "Loading stats from file %s", _getStatsFile().c_str());

std::ifstream ifs(_getStatsFile());
std::string line;
Expand All @@ -76,7 +76,7 @@ void statsLoad(void)
split[1].end(), [](unsigned char c)
{ return !std::isdigit(c); }) == split[1].end()))
{
mtnlogMessage(LOG_WARNING, "Value of key %s in stats file %s is not a number. Skipping.", key.c_str(), _getStatsFile().c_str());
mtnlogMessageTag(LOG_WARNING, "stats", "Value of key %s in stats file %s is not a number. Skipping.", key.c_str(), _getStatsFile().c_str());
continue;
}
int value = std::stoi(split[1]);
Expand Down

0 comments on commit e41fde9

Please sign in to comment.