From 78ab009d5cc4830d3563bd1e769fded29114a531 Mon Sep 17 00:00:00 2001 From: Project579 Date: Wed, 29 Jun 2022 23:08:33 +0200 Subject: [PATCH] Add required overloads to logger for handling unicode paths, also add conversion functions from std::filesystem::path to std::string without losing unicode data. --- components/debug/debuglog.cpp | 45 ++++++++++++++++++++++- components/debug/debuglog.hpp | 11 ++++++ components/files/configurationmanager.cpp | 25 +++++++++++++ components/files/configurationmanager.hpp | 10 +++++ 4 files changed, 90 insertions(+), 1 deletion(-) diff --git a/components/debug/debuglog.cpp b/components/debug/debuglog.cpp index f4f0fdffa6..ef453283e8 100644 --- a/components/debug/debuglog.cpp +++ b/components/debug/debuglog.cpp @@ -1,6 +1,9 @@ #include "debuglog.hpp" + #include +#include + namespace Debug { Level CurrentDebugLevel = Level::NoLevel; @@ -8,7 +11,7 @@ namespace Debug static std::mutex sLock; -Log::Log(Debug::Level level) +Log::Log(Debug::Level level) : mShouldLog(level <= Debug::CurrentDebugLevel) { // No need to hold the lock if there will be no logging anyway @@ -34,3 +37,43 @@ Log::~Log() std::cout << std::endl; sLock.unlock(); } + +Log& Log::operator<<(std::filesystem::path&& rhs) +{ + if (mShouldLog) + std::cout << Files::pathToUnicodeString(std::move(rhs)); + + return *this; +} + +Log& Log::operator<<(const std::filesystem::path& rhs) +{ + if (mShouldLog) + std::cout << Files::pathToUnicodeString(rhs); + + return *this; +} + +Log& Log::operator<<(std::u8string&& rhs) +{ + if (mShouldLog) + std::cout << Misc::StringUtils::u8StringToString(std::move(rhs)); + + return *this; +} + +Log& Log::operator<<(const std::u8string& rhs) +{ + if (mShouldLog) + std::cout << Misc::StringUtils::u8StringToString(rhs); + + return *this; +} + +Log& Log::operator<<(const char8_t* rhs) +{ + if (mShouldLog) + std::cout << Misc::StringUtils::u8StringToString(rhs); + + return *this; +} diff --git a/components/debug/debuglog.hpp b/components/debug/debuglog.hpp index aa8156e119..526763d9f6 100644 --- a/components/debug/debuglog.hpp +++ b/components/debug/debuglog.hpp @@ -2,6 +2,7 @@ #define DEBUG_LOG_H #include +#include namespace Debug { @@ -36,6 +37,16 @@ public: return *this; } + Log& operator<<(std::filesystem::path&& rhs); + + Log& operator<<(const std::filesystem::path& rhs); + + Log& operator<<(std::u8string&& rhs); + + Log& operator<<(const std::u8string& rhs); + + Log& operator<<(const char8_t* rhs); + private: const bool mShouldLog; }; diff --git a/components/files/configurationmanager.cpp b/components/files/configurationmanager.cpp index 6ceab167e5..90ac748361 100644 --- a/components/files/configurationmanager.cpp +++ b/components/files/configurationmanager.cpp @@ -460,4 +460,29 @@ PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathCon return res; } +std::string pathToUnicodeString(const std::filesystem::path& path) +{ + return Misc::StringUtils::u8StringToString(path.u8string()); +} + +std::string pathToUnicodeString(std::filesystem::path&& path) +{ + return Misc::StringUtils::u8StringToString(path.u8string()); +} + +std::filesystem::path unicodeStringToPath(const std::string_view path) +{ + return Misc::StringUtils::stringToU8String(path); +} + +std::filesystem::path unicodeStringToPath(std::string&& path) +{ + return Misc::StringUtils::stringToU8String(std::move(path)); +} + +std::filesystem::path unicodeStringToPath(const char* path) +{ + return Misc::StringUtils::stringToU8String(path); +} + } /* namespace Files */ diff --git a/components/files/configurationmanager.hpp b/components/files/configurationmanager.hpp index 74f035ffb6..028feb2c0d 100644 --- a/components/files/configurationmanager.hpp +++ b/components/files/configurationmanager.hpp @@ -105,6 +105,16 @@ typedef std::vector MaybeQuotedPathContainer; PathContainer asPathContainer(const MaybeQuotedPathContainer& MaybeQuotedPathContainer); +std::string pathToUnicodeString(const std::filesystem::path& path); + +std::string pathToUnicodeString(std::filesystem::path&& path); + +std::filesystem::path unicodeStringToPath(const std::string_view path); + +std::filesystem::path unicodeStringToPath(std::string&& path); + +std::filesystem::path unicodeStringToPath(const char* path); + } /* namespace Files */ #endif /* COMPONENTS_FILES_CONFIGURATIONMANAGER_HPP */