From 455e9a13ce450ad04e9bfab0a68d26850f146472 Mon Sep 17 00:00:00 2001 From: Pengfei Date: Sat, 11 Sep 2021 08:56:34 +0800 Subject: [PATCH] Fix log destination again on macOS --- src/common/file_util.cpp | 10 +++++----- src/common/file_util.h | 2 ++ src/common/logging/log.cpp | 18 +++++++++++------- src/common/logging/log.h | 1 + src/core/db/seed_db.cpp | 4 ++-- src/frontend/main.cpp | 2 ++ 6 files changed, 23 insertions(+), 14 deletions(-) diff --git a/src/common/file_util.cpp b/src/common/file_util.cpp index fc3d7a3..8a6c3be 100644 --- a/src/common/file_util.cpp +++ b/src/common/file_util.cpp @@ -651,7 +651,7 @@ static const std::string& GetHomeDirectory() { * @return The directory path * @sa http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html */ -static const std::string GetUserDirectory(const std::string& envvar) { +std::string GetXDGDirectory(const std::string& envvar) { const char* directory = getenv(envvar.c_str()); std::string user_dir; @@ -715,7 +715,7 @@ UserPathType GetUserPathType() { } const bool normal_exists = - FileUtil::Exists(GetUserDirectory("XDG_DATA_HOME") + DIR_SEP EMU_DATA_DIR DIR_SEP); + FileUtil::Exists(GetXDGDirectory("XDG_DATA_HOME") + DIR_SEP EMU_DATA_DIR DIR_SEP); const bool flatpak_exists = FileUtil::Exists( GetHomeDirectory() + "/.var/app/org.citra_emu.citra/data" + DIR_SEP EMU_DATA_DIR DIR_SEP); if (!normal_exists || flatpak_exists) { // Flatpak takes precedence @@ -785,9 +785,9 @@ void SetUserPath(const std::string& path) { break; } case UserPathType::Normal: { - std::string data_dir = GetUserDirectory("XDG_DATA_HOME"); - std::string config_dir = GetUserDirectory("XDG_CONFIG_HOME"); - std::string cache_dir = GetUserDirectory("XDG_CACHE_HOME"); + std::string data_dir = GetXDGDirectory("XDG_DATA_HOME"); + std::string config_dir = GetXDGDirectory("XDG_CONFIG_HOME"); + std::string cache_dir = GetXDGDirectory("XDG_CACHE_HOME"); user_path = data_dir + DIR_SEP EMU_DATA_DIR DIR_SEP; g_paths.emplace(UserPath::ConfigDir, config_dir + DIR_SEP EMU_DATA_DIR DIR_SEP); diff --git a/src/common/file_util.h b/src/common/file_util.h index 3e1696e..447d8f7 100644 --- a/src/common/file_util.h +++ b/src/common/file_util.h @@ -157,6 +157,8 @@ std::string GetBundleDirectory(); #ifdef _WIN32 const std::string& GetExeDirectory(); std::string AppDataRoamingDirectory(); +#else +std::string GetXDGDirectory(const std::string& envvar); #endif std::size_t WriteStringToFile(bool text_file, const std::string& filename, std::string_view str); diff --git a/src/common/logging/log.cpp b/src/common/logging/log.cpp index 1517253..0eae394 100644 --- a/src/common/logging/log.cpp +++ b/src/common/logging/log.cpp @@ -22,14 +22,16 @@ std::uint64_t GetLoggingTime() { .count(); } +static FileUtil::IOFile g_log_file; +void InitializeLogging() { #ifdef __WIN32 -// _SH_DENYWR allows read-only access for other programs. -static FileUtil::IOFile g_log_file{FileUtil::GetExeDirectory() + DIR_SEP LOG_FILE, "w", _SH_DENYWR}; + g_log_file.Open(FileUtil::GetExeDirectory() + DIR_SEP LOG_FILE, "w", _SH_DENYWR); #elif __APPLE__ -static FileUtil::IOFile g_log_file{FileUtil::GetBundleDirectory() + "/../" LOG_FILE, "w"}; + g_log_file.Open(FileUtil::GetXDGDirectory("XDG_DATA_HOME") + DIR_SEP LOG_FILE, "w"); #else -static FileUtil::IOFile g_log_file{ROOT_DIR DIR_SEP LOG_FILE, "w"}; + g_log_file.Open(ROOT_DIR DIR_SEP LOG_FILE, "w"); #endif +} static std::array g_error_buffer{}; static int g_error_buffer_pos = 0; @@ -39,9 +41,11 @@ void WriteLog(Entry entry) { fmt::print(stderr, entry.style, entry.message); // log file - g_log_file.WriteString(entry.message); - if (entry.level >= Level::Error) { - g_log_file.Flush(); // Do not flush the file too often + if (g_log_file.IsOpen()) { + g_log_file.WriteString(entry.message); + if (entry.level >= Level::Error) { + g_log_file.Flush(); // Do not flush the file too often + } } // log buffer diff --git a/src/common/logging/log.h b/src/common/logging/log.h index 448b3fa..eb3f13a 100644 --- a/src/common/logging/log.h +++ b/src/common/logging/log.h @@ -23,6 +23,7 @@ struct Entry { std::string message; }; +void InitializeLogging(); void WriteLog(Entry entry); // Returns up to 3 latest error messages diff --git a/src/core/db/seed_db.cpp b/src/core/db/seed_db.cpp index 72e2472..fe22c13 100644 --- a/src/core/db/seed_db.cpp +++ b/src/core/db/seed_db.cpp @@ -20,7 +20,7 @@ bool SeedDB::AddFromFile(const std::string& path) { return false; } u32_le count; - if (!file.ReadBytes(&count, sizeof(count))) { + if (file.ReadBytes(&count, sizeof(count)) != sizeof(count)) { LOG_ERROR(Service_FS, "Failed to read seed database count fully"); return false; } @@ -58,7 +58,7 @@ bool SeedDB::Save(const std::string& path) const { LOG_ERROR(Service_FS, "Failed to open seed database"); return false; } - u32 count{static_cast(seeds.size())}; + u32_le count{static_cast(seeds.size())}; if (file.WriteBytes(&count, sizeof(count)) != sizeof(count)) { LOG_ERROR(Service_FS, "Failed to write seed database count fully"); return false; diff --git a/src/frontend/main.cpp b/src/frontend/main.cpp index 8811365..eb3ef4e 100644 --- a/src/frontend/main.cpp +++ b/src/frontend/main.cpp @@ -251,6 +251,8 @@ int main(int argc, char* argv[]) { QCoreApplication::setOrganizationName(QStringLiteral("zhaowenlan1779")); QCoreApplication::setApplicationName(QStringLiteral("threeSD")); + Common::Logging::InitializeLogging(); + #ifdef __APPLE__ std::string bin_path = FileUtil::GetBundleDirectory() + DIR_SEP + ".."; chdir(bin_path.c_str());