diff options
Diffstat (limited to 'src/common')
-rw-r--r-- | src/common/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/common/common_paths.h | 6 | ||||
-rw-r--r-- | src/common/file_util.cpp | 68 | ||||
-rw-r--r-- | src/common/file_util.h | 27 | ||||
-rw-r--r-- | src/common/logging/backend.cpp | 25 | ||||
-rw-r--r-- | src/common/logging/backend.h | 7 | ||||
-rw-r--r-- | src/common/logging/filter.cpp | 75 | ||||
-rw-r--r-- | src/common/logging/filter.h | 6 | ||||
-rw-r--r-- | src/common/param_package.cpp | 19 | ||||
-rw-r--r-- | src/common/param_package.h | 2 | ||||
-rw-r--r-- | src/common/synchronized_wrapper.h | 85 |
11 files changed, 119 insertions, 202 deletions
diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index f49a31612..d5d4f6f82 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -63,7 +63,6 @@ add_library(common STATIC string_util.cpp string_util.h swap.h - synchronized_wrapper.h telemetry.cpp telemetry.h thread.cpp diff --git a/src/common/common_paths.h b/src/common/common_paths.h index 9bf3efaf2..6799a357a 100644 --- a/src/common/common_paths.h +++ b/src/common/common_paths.h @@ -26,7 +26,7 @@ #define USA_DIR "USA" #define JAP_DIR "JAP" -// Subdirs in the User dir returned by GetUserPath(D_USER_IDX) +// Subdirs in the User dir returned by GetUserPath(UserPath::UserDir) #define CONFIG_DIR "config" #define CACHE_DIR "cache" #define SDMC_DIR "sdmc" @@ -35,11 +35,11 @@ #define LOG_DIR "log" // Filenames -// Files in the directory returned by GetUserPath(D_CONFIG_IDX) +// Files in the directory returned by GetUserPath(UserPath::ConfigDir) #define EMU_CONFIG "emu.ini" #define DEBUGGER_CONFIG "debugger.ini" #define LOGGER_CONFIG "logger.ini" -// Files in the directory returned by GetUserPath(D_LOGS_IDX) +// Files in the directory returned by GetUserPath(UserPath::LogDir) #define LOG_FILE "yuzu_log.txt" // Sys files diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index c882ab39f..1e28f7cbb 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -3,6 +3,7 @@ // Refer to the license.txt file included. #include <sstream> +#include <unordered_map> #include "common/assert.h" #include "common/common_funcs.h" #include "common/common_paths.h" @@ -681,67 +682,68 @@ std::string GetSysDirectory() { // Returns a string with a yuzu data dir or file in the user's home // directory. To be used in "multi-user" mode (that is, installed). -const std::string& GetUserPath(const unsigned int DirIDX, const std::string& newPath) { - static std::string paths[NUM_PATH_INDICES]; +const std::string& GetUserPath(UserPath path, const std::string& new_path) { + static std::unordered_map<UserPath, std::string> paths; + auto& user_path = paths[UserPath::UserDir]; // Set up all paths and files on the first run - if (paths[D_USER_IDX].empty()) { + if (user_path.empty()) { #ifdef _WIN32 - paths[D_USER_IDX] = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; - if (!FileUtil::IsDirectory(paths[D_USER_IDX])) { - paths[D_USER_IDX] = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; + user_path = GetExeDirectory() + DIR_SEP USERDATA_DIR DIR_SEP; + if (!FileUtil::IsDirectory(user_path)) { + user_path = AppDataRoamingDirectory() + DIR_SEP EMU_DATA_DIR DIR_SEP; } else { LOG_INFO(Common_Filesystem, "Using the local user directory"); } - paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; - paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; + paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); + paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); #else if (FileUtil::Exists(ROOT_DIR DIR_SEP USERDATA_DIR)) { - paths[D_USER_IDX] = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; - paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; - paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; + user_path = ROOT_DIR DIR_SEP USERDATA_DIR DIR_SEP; + paths.emplace(UserPath::ConfigDir, user_path + CONFIG_DIR DIR_SEP); + paths.emplace(UserPath::CacheDir, user_path + CACHE_DIR DIR_SEP); } else { std::string data_dir = GetUserDirectory("XDG_DATA_HOME"); std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME"); std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME"); - paths[D_USER_IDX] = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; - paths[D_CONFIG_IDX] = config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; - paths[D_CACHE_IDX] = cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; + user_path = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; + paths.emplace(UserPath::ConfigDir, config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP); + paths.emplace(UserPath::CacheDir, cache_dir + DIR_SEP EMU_DATA_DIR DIR_SEP); } #endif - paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; - paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP; - paths[D_SYSDATA_IDX] = paths[D_USER_IDX] + SYSDATA_DIR DIR_SEP; + paths.emplace(UserPath::SDMCDir, user_path + SDMC_DIR DIR_SEP); + paths.emplace(UserPath::NANDDir, user_path + NAND_DIR DIR_SEP); + paths.emplace(UserPath::SysDataDir, user_path + SYSDATA_DIR DIR_SEP); // TODO: Put the logs in a better location for each OS - paths[D_LOGS_IDX] = paths[D_USER_IDX] + LOG_DIR DIR_SEP; + paths.emplace(UserPath::LogDir, user_path + LOG_DIR DIR_SEP); } - if (!newPath.empty()) { - if (!FileUtil::IsDirectory(newPath)) { - LOG_ERROR(Common_Filesystem, "Invalid path specified {}", newPath); - return paths[DirIDX]; + if (!new_path.empty()) { + if (!FileUtil::IsDirectory(new_path)) { + LOG_ERROR(Common_Filesystem, "Invalid path specified {}", new_path); + return paths[path]; } else { - paths[DirIDX] = newPath; + paths[path] = new_path; } - switch (DirIDX) { - case D_ROOT_IDX: - paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP; + switch (path) { + case UserPath::RootDir: + user_path = paths[UserPath::RootDir] + DIR_SEP; break; - case D_USER_IDX: - paths[D_USER_IDX] = paths[D_ROOT_IDX] + DIR_SEP; - paths[D_CONFIG_IDX] = paths[D_USER_IDX] + CONFIG_DIR DIR_SEP; - paths[D_CACHE_IDX] = paths[D_USER_IDX] + CACHE_DIR DIR_SEP; - paths[D_SDMC_IDX] = paths[D_USER_IDX] + SDMC_DIR DIR_SEP; - paths[D_NAND_IDX] = paths[D_USER_IDX] + NAND_DIR DIR_SEP; + case UserPath::UserDir: + user_path = paths[UserPath::RootDir] + DIR_SEP; + paths[UserPath::ConfigDir] = user_path + CONFIG_DIR DIR_SEP; + paths[UserPath::CacheDir] = user_path + CACHE_DIR DIR_SEP; + paths[UserPath::SDMCDir] = user_path + SDMC_DIR DIR_SEP; + paths[UserPath::NANDDir] = user_path + NAND_DIR DIR_SEP; break; } } - return paths[DirIDX]; + return paths[path]; } size_t WriteStringToFile(bool text_file, const std::string& str, const char* filename) { diff --git a/src/common/file_util.h b/src/common/file_util.h index 1f38b1560..ff01bf0ff 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -16,21 +16,20 @@ #include "common/string_util.h" #endif -// User directory indices for GetUserPath -enum { - D_USER_IDX, - D_ROOT_IDX, - D_CONFIG_IDX, - D_CACHE_IDX, - D_SDMC_IDX, - D_NAND_IDX, - D_SYSDATA_IDX, - D_LOGS_IDX, - NUM_PATH_INDICES -}; - namespace FileUtil { +// User paths for GetUserPath +enum class UserPath { + CacheDir, + ConfigDir, + LogDir, + NANDDir, + RootDir, + SDMCDir, + SysDataDir, + UserDir, +}; + // FileSystem tree node/ struct FSTEntry { bool isDirectory; @@ -123,7 +122,7 @@ bool SetCurrentDir(const std::string& directory); // Returns a pointer to a string with a yuzu data dir in the user's home // directory. To be used in "multi-user" mode (that is, installed). -const std::string& GetUserPath(const unsigned int DirIDX, const std::string& newPath = ""); +const std::string& GetUserPath(UserPath path, const std::string& new_path = ""); // Returns the path to where the sys file are std::string GetSysDirectory(); diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index ed1e93cc2..59b999935 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -3,19 +3,20 @@ // Refer to the license.txt file included. #include <algorithm> -#include <array> +#include <atomic> #include <chrono> #include <climits> #include <condition_variable> #include <memory> +#include <mutex> #include <thread> +#include <vector> #ifdef _WIN32 #include <share.h> // For _SH_DENYWR #else #define _SH_DENYWR 0 #endif #include "common/assert.h" -#include "common/common_funcs.h" // snprintf compatibility define #include "common/logging/backend.h" #include "common/logging/log.h" #include "common/logging/text_formatter.h" @@ -48,11 +49,11 @@ public: backends.push_back(std::move(backend)); } - void RemoveBackend(const std::string& backend_name) { + void RemoveBackend(std::string_view backend_name) { std::lock_guard<std::mutex> lock(writing_mutex); - auto it = std::remove_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { - return !strcmp(i->GetName(), backend_name.c_str()); - }); + const auto it = + std::remove_if(backends.begin(), backends.end(), + [&backend_name](const auto& i) { return backend_name == i->GetName(); }); backends.erase(it, backends.end()); } @@ -64,10 +65,10 @@ public: filter = f; } - Backend* GetBackend(const std::string& backend_name) { - auto it = std::find_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { - return !strcmp(i->GetName(), backend_name.c_str()); - }); + Backend* GetBackend(std::string_view backend_name) { + const auto it = + std::find_if(backends.begin(), backends.end(), + [&backend_name](const auto& i) { return backend_name == i->GetName(); }); if (it == backends.end()) return nullptr; return it->get(); @@ -265,11 +266,11 @@ void AddBackend(std::unique_ptr<Backend> backend) { Impl::Instance().AddBackend(std::move(backend)); } -void RemoveBackend(const std::string& backend_name) { +void RemoveBackend(std::string_view backend_name) { Impl::Instance().RemoveBackend(backend_name); } -Backend* GetBackend(const std::string& backend_name) { +Backend* GetBackend(std::string_view backend_name) { return Impl::Instance().GetBackend(backend_name); } diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index 57cdf6b2d..b3f4b9cef 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -4,10 +4,9 @@ #pragma once #include <chrono> -#include <cstdarg> #include <memory> #include <string> -#include <utility> +#include <string_view> #include "common/file_util.h" #include "common/logging/filter.h" #include "common/logging/log.h" @@ -106,9 +105,9 @@ private: void AddBackend(std::unique_ptr<Backend> backend); -void RemoveBackend(const std::string& backend_name); +void RemoveBackend(std::string_view backend_name); -Backend* GetBackend(const std::string& backend_name); +Backend* GetBackend(std::string_view backend_name); /** * Returns the name of the passed log class as a C-string. Subclasses are separated by periods diff --git a/src/common/logging/filter.cpp b/src/common/logging/filter.cpp index 6ed087beb..2dd331152 100644 --- a/src/common/logging/filter.cpp +++ b/src/common/logging/filter.cpp @@ -8,39 +8,9 @@ #include "common/string_util.h" namespace Log { - -Filter::Filter(Level default_level) { - ResetAll(default_level); -} - -void Filter::ResetAll(Level level) { - class_levels.fill(level); -} - -void Filter::SetClassLevel(Class log_class, Level level) { - class_levels[static_cast<size_t>(log_class)] = level; -} - -void Filter::ParseFilterString(const std::string& filter_str) { - auto clause_begin = filter_str.cbegin(); - while (clause_begin != filter_str.cend()) { - auto clause_end = std::find(clause_begin, filter_str.cend(), ' '); - - // If clause isn't empty - if (clause_end != clause_begin) { - ParseFilterRule(clause_begin, clause_end); - } - - if (clause_end != filter_str.cend()) { - // Skip over the whitespace - ++clause_end; - } - clause_begin = clause_end; - } -} - +namespace { template <typename It> -static Level GetLevelByName(const It begin, const It end) { +Level GetLevelByName(const It begin, const It end) { for (u8 i = 0; i < static_cast<u8>(Level::Count); ++i) { const char* level_name = GetLevelName(static_cast<Level>(i)); if (Common::ComparePartialString(begin, end, level_name)) { @@ -51,7 +21,7 @@ static Level GetLevelByName(const It begin, const It end) { } template <typename It> -static Class GetClassByName(const It begin, const It end) { +Class GetClassByName(const It begin, const It end) { for (ClassType i = 0; i < static_cast<ClassType>(Class::Count); ++i) { const char* level_name = GetLogClassName(static_cast<Class>(i)); if (Common::ComparePartialString(begin, end, level_name)) { @@ -61,8 +31,8 @@ static Class GetClassByName(const It begin, const It end) { return Class::Count; } -bool Filter::ParseFilterRule(const std::string::const_iterator begin, - const std::string::const_iterator end) { +template <typename Iterator> +bool ParseFilterRule(Filter& instance, Iterator begin, Iterator end) { auto level_separator = std::find(begin, end, ':'); if (level_separator == end) { LOG_ERROR(Log, "Invalid log filter. Must specify a log level after `:`: {}", @@ -77,7 +47,7 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, } if (Common::ComparePartialString(begin, level_separator, "*")) { - ResetAll(level); + instance.ResetAll(level); return true; } @@ -87,9 +57,40 @@ bool Filter::ParseFilterRule(const std::string::const_iterator begin, return false; } - SetClassLevel(log_class, level); + instance.SetClassLevel(log_class, level); return true; } +} // Anonymous namespace + +Filter::Filter(Level default_level) { + ResetAll(default_level); +} + +void Filter::ResetAll(Level level) { + class_levels.fill(level); +} + +void Filter::SetClassLevel(Class log_class, Level level) { + class_levels[static_cast<size_t>(log_class)] = level; +} + +void Filter::ParseFilterString(std::string_view filter_view) { + auto clause_begin = filter_view.cbegin(); + while (clause_begin != filter_view.cend()) { + auto clause_end = std::find(clause_begin, filter_view.cend(), ' '); + + // If clause isn't empty + if (clause_end != clause_begin) { + ParseFilterRule(*this, clause_begin, clause_end); + } + + if (clause_end != filter_view.cend()) { + // Skip over the whitespace + ++clause_end; + } + clause_begin = clause_end; + } +} bool Filter::CheckMessage(Class log_class, Level level) const { return static_cast<u8>(level) >= static_cast<u8>(class_levels[static_cast<size_t>(log_class)]); diff --git a/src/common/logging/filter.h b/src/common/logging/filter.h index 2a4f7c845..d5ffc5a58 100644 --- a/src/common/logging/filter.h +++ b/src/common/logging/filter.h @@ -6,7 +6,7 @@ #include <array> #include <cstddef> -#include <string> +#include <string_view> #include "common/logging/log.h" namespace Log { @@ -40,9 +40,7 @@ public: * - `Service:Info` -- Sets the level of Service to Info. * - `Service.FS:Trace` -- Sets the level of the Service.FS class to Trace. */ - void ParseFilterString(const std::string& filter_str); - bool ParseFilterRule(const std::string::const_iterator start, - const std::string::const_iterator end); + void ParseFilterString(std::string_view filter_view); /// Matches class/level combination against the filter, returning true if it passed. bool CheckMessage(Class log_class, Level level) const; diff --git a/src/common/param_package.cpp b/src/common/param_package.cpp index e0df430ab..9526ca0c6 100644 --- a/src/common/param_package.cpp +++ b/src/common/param_package.cpp @@ -3,7 +3,9 @@ // Refer to the license.txt file included. #include <array> +#include <utility> #include <vector> + #include "common/logging/log.h" #include "common/param_package.h" #include "common/string_util.h" @@ -12,10 +14,11 @@ namespace Common { constexpr char KEY_VALUE_SEPARATOR = ':'; constexpr char PARAM_SEPARATOR = ','; + constexpr char ESCAPE_CHARACTER = '$'; -const std::string KEY_VALUE_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '0'}; -const std::string PARAM_SEPARATOR_ESCAPE{ESCAPE_CHARACTER, '1'}; -const std::string ESCAPE_CHARACTER_ESCAPE{ESCAPE_CHARACTER, '2'}; +constexpr char KEY_VALUE_SEPARATOR_ESCAPE[] = "$0"; +constexpr char PARAM_SEPARATOR_ESCAPE[] = "$1"; +constexpr char ESCAPE_CHARACTER_ESCAPE[] = "$2"; ParamPackage::ParamPackage(const std::string& serialized) { std::vector<std::string> pairs; @@ -35,7 +38,7 @@ ParamPackage::ParamPackage(const std::string& serialized) { part = Common::ReplaceAll(part, ESCAPE_CHARACTER_ESCAPE, {ESCAPE_CHARACTER}); } - Set(key_value[0], key_value[1]); + Set(key_value[0], std::move(key_value[1])); } } @@ -101,16 +104,16 @@ float ParamPackage::Get(const std::string& key, float default_value) const { } } -void ParamPackage::Set(const std::string& key, const std::string& value) { - data[key] = value; +void ParamPackage::Set(const std::string& key, std::string value) { + data.insert_or_assign(key, std::move(value)); } void ParamPackage::Set(const std::string& key, int value) { - data[key] = std::to_string(value); + data.insert_or_assign(key, std::to_string(value)); } void ParamPackage::Set(const std::string& key, float value) { - data[key] = std::to_string(value); + data.insert_or_assign(key, std::to_string(value)); } bool ParamPackage::Has(const std::string& key) const { diff --git a/src/common/param_package.h b/src/common/param_package.h index c4c11b221..7842cd4ef 100644 --- a/src/common/param_package.h +++ b/src/common/param_package.h @@ -28,7 +28,7 @@ public: std::string Get(const std::string& key, const std::string& default_value) const; int Get(const std::string& key, int default_value) const; float Get(const std::string& key, float default_value) const; - void Set(const std::string& key, const std::string& value); + void Set(const std::string& key, std::string value); void Set(const std::string& key, int value); void Set(const std::string& key, float value); bool Has(const std::string& key) const; diff --git a/src/common/synchronized_wrapper.h b/src/common/synchronized_wrapper.h deleted file mode 100644 index 4a1984c46..000000000 --- a/src/common/synchronized_wrapper.h +++ /dev/null @@ -1,85 +0,0 @@ -// Copyright 2015 Citra Emulator Project -// Licensed under GPLv2 or any later version -// Refer to the license.txt file included. - -#pragma once - -#include <algorithm> -#include <mutex> - -namespace Common { - -template <typename T> -class SynchronizedWrapper; - -/** - * Synchronized reference, that keeps a SynchronizedWrapper's mutex locked during its lifetime. This - * greatly reduces the chance that someone will access the wrapped resource without locking the - * mutex. - */ -template <typename T> -class SynchronizedRef { -public: - SynchronizedRef(SynchronizedWrapper<T>& wrapper) : wrapper(&wrapper) { - wrapper.mutex.lock(); - } - - SynchronizedRef(SynchronizedRef&) = delete; - SynchronizedRef(SynchronizedRef&& o) : wrapper(o.wrapper) { - o.wrapper = nullptr; - } - - ~SynchronizedRef() { - if (wrapper) - wrapper->mutex.unlock(); - } - - SynchronizedRef& operator=(SynchronizedRef&) = delete; - SynchronizedRef& operator=(SynchronizedRef&& o) { - std::swap(wrapper, o.wrapper); - return *this; - } - - T& operator*() { - return wrapper->data; - } - const T& operator*() const { - return wrapper->data; - } - - T* operator->() { - return &wrapper->data; - } - const T* operator->() const { - return &wrapper->data; - } - -private: - SynchronizedWrapper<T>* wrapper; -}; - -/** - * Wraps an object, only allowing access to it via a locking reference wrapper. Good to ensure no - * one forgets to lock a mutex before acessing an object. To access the wrapped object construct a - * SyncronizedRef on this wrapper. Inspired by Rust's Mutex type - * (http://doc.rust-lang.org/std/sync/struct.Mutex.html). - */ -template <typename T> -class SynchronizedWrapper { -public: - template <typename... Args> - SynchronizedWrapper(Args&&... args) : data(std::forward<Args>(args)...) {} - - SynchronizedRef<T> Lock() { - return {*this}; - } - -private: - template <typename U> - friend class SynchronizedRef; - - std::mutex mutex; - T data; -}; - -} // namespace Common |