From 16e1f27353aabdd2ca9f95bcd2af5c5384216620 Mon Sep 17 00:00:00 2001 From: Petr Ohlidal Date: Sun, 26 Dec 2021 02:51:06 +0100 Subject: [PATCH] :bulb: ConfigFile: parsing errors now contain filename + subsystem. --- .../resources/otc_fileformat/OTCFileFormat.cpp | 2 ++ .../terrn2_fileformat/Terrn2FileFormat.cpp | 1 + source/main/utils/ConfigFile.cpp | 11 +++++++++-- source/main/utils/ConfigFile.h | 18 +++++++++++++++--- 4 files changed, 27 insertions(+), 5 deletions(-) diff --git a/source/main/resources/otc_fileformat/OTCFileFormat.cpp b/source/main/resources/otc_fileformat/OTCFileFormat.cpp index a125201bc7..f82c526ea8 100644 --- a/source/main/resources/otc_fileformat/OTCFileFormat.cpp +++ b/source/main/resources/otc_fileformat/OTCFileFormat.cpp @@ -23,6 +23,7 @@ #include "OTCFileFormat.h" #include "Application.h" +#include "Console.h" #include "SimConstants.h" #include "ConfigFile.h" #include "Utils.h" @@ -43,6 +44,7 @@ bool RoR::OTCParser::LoadMasterConfig(Ogre::DataStreamPtr &ds, const char* filen try { cfg.load(ds, "\t:=", false); + cfg.setLoggingInfo(ds->getName(), Console::CONSOLE_MSGTYPE_TERRN); m_def->disable_cache = cfg.getBool ("disableCaching", false); m_def->world_size_x = cfg.getInt ("WorldSizeX", 1024); diff --git a/source/main/resources/terrn2_fileformat/Terrn2FileFormat.cpp b/source/main/resources/terrn2_fileformat/Terrn2FileFormat.cpp index d379407b18..a8019f1928 100644 --- a/source/main/resources/terrn2_fileformat/Terrn2FileFormat.cpp +++ b/source/main/resources/terrn2_fileformat/Terrn2FileFormat.cpp @@ -39,6 +39,7 @@ bool Terrn2Parser::LoadTerrn2(Terrn2Def& def, Ogre::DataStreamPtr &ds) { RoR::ConfigFile file; file.load(ds, "\t:=", true); + file.setLoggingInfo(ds->getName(), Console::CONSOLE_MSGTYPE_TERRN); // read in the settings def.name = file.getString("Name", "General"); diff --git a/source/main/utils/ConfigFile.cpp b/source/main/utils/ConfigFile.cpp index 74a36e51fe..8a2a1313a6 100644 --- a/source/main/utils/ConfigFile.cpp +++ b/source/main/utils/ConfigFile.cpp @@ -52,7 +52,7 @@ Ogre::ColourValue ConfigFile::getColourValue(Ogre::String const& key, Ogre::Stri } else { - App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_WARNING, + this->logMessage( fmt::format("Could not parse '{}/{}' ({}) as color, format must be 'R G B A' or 'R G B', falling back to '{}'", section, key, value, Ogre::StringConverter::toString(defaultValue))); return defaultValue; @@ -69,7 +69,7 @@ Ogre::Vector3 ConfigFile::getVector3(Ogre::String const& key, Ogre::String const } else { - App::GetConsole()->putMessage(Console::CONSOLE_MSGTYPE_INFO, Console::CONSOLE_SYSTEM_WARNING, + this->logMessage( fmt::format("Could not parse '{}/{}' ({}) as vector3, format must be 'X Y Z', falling back to '{}'", section, key, value, Ogre::StringConverter::toString(defaultValue))); return defaultValue; @@ -120,3 +120,10 @@ bool ConfigFile::HasSection(std::string const & name) // without either an OGRE exception being logged or using deprecated API. return this->getSettingsBySection().find(name) != this->getSettingsBySection().end(); } + +void ConfigFile::logMessage(std::string const & msg) +{ + // If filename is specified, log "filename: message", otherwise just "message". + App::GetConsole()->putMessage(m_log_area, Console::CONSOLE_SYSTEM_WARNING, + fmt::format("{}{}{}", m_log_filename, (m_log_filename == "" ? "" : ": "), msg)); +} diff --git a/source/main/utils/ConfigFile.h b/source/main/utils/ConfigFile.h index e8a9ab9f4d..2271cf9011 100644 --- a/source/main/utils/ConfigFile.h +++ b/source/main/utils/ConfigFile.h @@ -27,9 +27,9 @@ #pragma once -#include -#include -#include +#include "Console.h" + +#include namespace RoR { @@ -74,10 +74,22 @@ class ConfigFile: public Ogre::ConfigFile bool HasSection(std::string const & name); + void setLoggingInfo(std::string const & filename, Console::MessageArea area) + { + m_log_filename = filename; + m_log_area = area; + } + private: //Block access to Ogre::ConfigFile::getSetting() - not UTF8 safe! Ogre::String getSetting(Ogre::String, Ogre::String); Ogre::String getSetting(Ogre::String, Ogre::String, Ogre::String); + + void logMessage(std::string const & msg); + + // Logging info. + std::string m_log_filename; + Console::MessageArea m_log_area = Console::CONSOLE_MSGTYPE_INFO; }; } // namespace RoR