[feat] save log and cache files into user folder

This commit is contained in:
dijunkun
2025-07-22 18:55:12 +08:00
parent 3bf68a396f
commit e8d7ec8daf
11 changed files with 146 additions and 65 deletions

View File

@@ -30,7 +30,7 @@ int KeyboardCapturer::Hook(OnKeyAction on_key_action, void* user_ptr) {
keyboard_hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0); keyboard_hook_ = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, NULL, 0);
if (!keyboard_hook_) { if (!keyboard_hook_) {
LOG_ERROR("Failed to install keyboard hook!") LOG_ERROR("Failed to install keyboard hook");
return -1; return -1;
} }
return 0; return 0;

View File

@@ -10,9 +10,7 @@
#include "render.h" #include "render.h"
int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) { int main([[maybe_unused]] int argc, [[maybe_unused]] char *argv[]) {
LOG_INFO("Remote desk");
Render render; Render render;
render.Run(); render.Run();
return 0; return 0;

View File

@@ -1,15 +1,40 @@
#include "rd_log.h" #include "rd_log.h"
std::shared_ptr<spdlog::logger> get_rd_logger() { #include <atomic>
if (auto logger = spdlog::get(RD_LOGGER_NAME)) { #include <filesystem>
return logger;
namespace {
std::string g_log_dir = "logs";
std::once_flag g_logger_once_flag;
std::shared_ptr<spdlog::logger> g_logger;
std::atomic<bool> g_logger_created{false};
} // namespace
void InitLogger(const std::string& log_dir) {
if (g_logger_created.load()) {
LOG_WARN(
"InitLogger called after logger initialized. Ignoring log_dir: {}, "
"using previous log_dir: {}",
log_dir, g_log_dir);
return;
} }
g_log_dir = log_dir;
}
std::shared_ptr<spdlog::logger> get_logger() {
std::call_once(g_logger_once_flag, []() {
g_logger_created.store(true);
std::error_code ec;
std::filesystem::create_directories(g_log_dir, ec);
auto now = std::chrono::system_clock::now() + std::chrono::hours(8); auto now = std::chrono::system_clock::now() + std::chrono::hours(8);
auto now_time = std::chrono::system_clock::to_time_t(now); auto now_time = std::chrono::system_clock::to_time_t(now);
std::tm tm_info; std::tm tm_info;
#ifdef _WIN32 #ifdef _WIN32
gmtime_s(&tm_info, &now_time); gmtime_s(&tm_info, &now_time);
#else #else
@@ -17,21 +42,21 @@ std::shared_ptr<spdlog::logger> get_rd_logger() {
#endif #endif
std::stringstream ss; std::stringstream ss;
std::string filename; ss << LOGGER_NAME;
ss << RD_LOGGER_NAME;
ss << std::put_time(&tm_info, "-%Y%m%d-%H%M%S.log"); ss << std::put_time(&tm_info, "-%Y%m%d-%H%M%S.log");
ss >> filename;
std::string path = "logs/" + filename; std::string filename = g_log_dir + "/" + ss.str();
std::vector<spdlog::sink_ptr> sinks; std::vector<spdlog::sink_ptr> sinks;
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>()); sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
path, 1048576 * 5, 3)); filename, 5 * 1024 * 1024, 3));
auto combined_logger = std::make_shared<spdlog::logger>( g_logger = std::make_shared<spdlog::logger>(LOGGER_NAME, sinks.begin(),
RD_LOGGER_NAME, begin(sinks), end(sinks)); sinks.end());
combined_logger->flush_on(spdlog::level::info); g_logger->flush_on(spdlog::level::info);
spdlog::register_logger(combined_logger); spdlog::register_logger(g_logger);
});
return combined_logger; return g_logger;
} }

View File

@@ -1,7 +1,7 @@
/* /*
* @Author: DI JUNKUN * @Author: DI JUNKUN
* @Date: 2024-11-26 * @Date: 2025-07-21
* Copyright (c) 2024 by DI JUNKUN, All Rights Reserved. * Copyright (c) 2025 by DI JUNKUN, All Rights Reserved.
*/ */
#ifndef _RD_LOG_H_ #ifndef _RD_LOG_H_
@@ -10,8 +10,11 @@
#include <chrono> #include <chrono>
#include <iomanip> #include <iomanip>
#include <iostream> #include <iostream>
#include <memory>
#include <mutex>
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <vector>
#include "spdlog/common.h" #include "spdlog/common.h"
#include "spdlog/logger.h" #include "spdlog/logger.h"
@@ -20,20 +23,17 @@
#include "spdlog/sinks/stdout_color_sinks.h" #include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h" #include "spdlog/spdlog.h"
using namespace std::chrono;
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO #define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
constexpr auto RD_LOGGER_NAME = "rd"; constexpr auto LOGGER_NAME = "crossdesk";
std::shared_ptr<spdlog::logger> get_rd_logger(); void InitLogger(const std::string& log_dir);
#define LOG_INFO(...) SPDLOG_LOGGER_INFO(get_rd_logger(), __VA_ARGS__); std::shared_ptr<spdlog::logger> get_logger();
#define LOG_WARN(...) SPDLOG_LOGGER_WARN(get_rd_logger(), __VA_ARGS__); #define LOG_INFO(...) SPDLOG_LOGGER_INFO(get_logger(), __VA_ARGS__)
#define LOG_WARN(...) SPDLOG_LOGGER_WARN(get_logger(), __VA_ARGS__)
#define LOG_ERROR(...) SPDLOG_LOGGER_ERROR(get_rd_logger(), __VA_ARGS__); #define LOG_ERROR(...) SPDLOG_LOGGER_ERROR(get_logger(), __VA_ARGS__)
#define LOG_FATAL(...) SPDLOG_LOGGER_CRITICAL(get_logger(), __VA_ARGS__)
#define LOG_FATAL(...) SPDLOG_LOGGER_CRITICAL(get_rd_logger(), __VA_ARGS__);
#endif #endif

View File

@@ -16,7 +16,7 @@ std::filesystem::path PathManager::GetConfigPath() {
std::filesystem::path PathManager::GetCachePath() { std::filesystem::path PathManager::GetCachePath() {
#ifdef _WIN32 #ifdef _WIN32
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "Cache"; return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "cache";
#elif __APPLE__ #elif __APPLE__
return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_; return GetEnvOrDefault("XDG_CACHE_HOME", GetHome() + "/.cache") / app_name_;
#else #else
@@ -26,7 +26,7 @@ std::filesystem::path PathManager::GetCachePath() {
std::filesystem::path PathManager::GetLogPath() { std::filesystem::path PathManager::GetLogPath() {
#ifdef _WIN32 #ifdef _WIN32
return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "Logs"; return GetKnownFolder(FOLDERID_LocalAppData) / app_name_ / "logs";
#elif __APPLE__ #elif __APPLE__
return GetHome() + "/Library/Logs/" + app_name_; return GetHome() + "/Library/Logs/" + app_name_;
#else #else
@@ -34,6 +34,20 @@ std::filesystem::path PathManager::GetLogPath() {
#endif #endif
} }
std::filesystem::path PathManager::GetCertPath() {
#ifdef _WIN32
// %APPDATA%\AppName\Certs
return GetKnownFolder(FOLDERID_RoamingAppData) / app_name_ / "certs";
#elif __APPLE__
// $HOME/Library/Application Support/AppName/certs
return GetHome() + "/Library/Application Support/" + app_name_ + "/certs";
#else
// $XDG_CONFIG_HOME/AppName/certs
return GetEnvOrDefault("XDG_CONFIG_HOME", GetHome() + "/.config") /
app_name_ / "certs";
#endif
}
bool PathManager::CreateDirectories(const std::filesystem::path& p) { bool PathManager::CreateDirectories(const std::filesystem::path& p) {
std::error_code ec; std::error_code ec;
bool created = std::filesystem::create_directories(p, ec); bool created = std::filesystem::create_directories(p, ec);

View File

@@ -24,6 +24,8 @@ class PathManager {
std::filesystem::path GetLogPath(); std::filesystem::path GetLogPath();
std::filesystem::path GetCertPath();
bool CreateDirectories(const std::filesystem::path& p); bool CreateDirectories(const std::filesystem::path& p);
private: private:

View File

@@ -171,7 +171,7 @@ Render::~Render() {}
int Render::SaveSettingsIntoCacheFile() { int Render::SaveSettingsIntoCacheFile() {
cd_cache_mutex_.lock(); cd_cache_mutex_.lock();
std::ofstream cd_cache_file("cache.cd", std::ios::binary); std::ofstream cd_cache_file(cache_path_ + "/cache.cd", std::ios::binary);
if (!cd_cache_file) { if (!cd_cache_file) {
cd_cache_mutex_.unlock(); cd_cache_mutex_.unlock();
return -1; return -1;
@@ -212,7 +212,7 @@ int Render::SaveSettingsIntoCacheFile() {
int Render::LoadSettingsFromCacheFile() { int Render::LoadSettingsFromCacheFile() {
cd_cache_mutex_.lock(); cd_cache_mutex_.lock();
std::ifstream cd_cache_file("cache.cd", std::ios::binary); std::ifstream cd_cache_file(cache_path_ + "/cache.cd", std::ios::binary);
if (!cd_cache_file) { if (!cd_cache_file) {
cd_cache_mutex_.unlock(); cd_cache_mutex_.unlock();
@@ -235,7 +235,7 @@ int Render::LoadSettingsFromCacheFile() {
config_center_.SetTurn(enable_turn_); config_center_.SetTurn(enable_turn_);
thumbnail_.reset(); thumbnail_.reset();
thumbnail_ = std::make_unique<Thumbnail>(); thumbnail_ = std::make_unique<Thumbnail>(cache_path_ + "/thumbnails/");
thumbnail_->GetKeyAndIv(aes128_key_, aes128_iv_); thumbnail_->GetKeyAndIv(aes128_key_, aes128_iv_);
thumbnail_->DeleteAllFilesInDirectory(); thumbnail_->DeleteAllFilesInDirectory();
@@ -276,7 +276,8 @@ int Render::LoadSettingsFromCacheFile() {
memcpy(aes128_iv_, cd_cache_.iv, sizeof(cd_cache_.iv)); memcpy(aes128_iv_, cd_cache_.iv, sizeof(cd_cache_.iv));
thumbnail_.reset(); thumbnail_.reset();
thumbnail_ = std::make_unique<Thumbnail>(aes128_key_, aes128_iv_); thumbnail_ = std::make_unique<Thumbnail>(cache_path_ + "/thumbnails/",
aes128_key_, aes128_iv_);
language_button_value_ = cd_cache_.language; language_button_value_ = cd_cache_.language;
video_quality_button_value_ = cd_cache_.video_quality; video_quality_button_value_ = cd_cache_.video_quality;
@@ -351,7 +352,7 @@ int Render::ScreenCapturerInit() {
int Render::StartScreenCapturer() { int Render::StartScreenCapturer() {
if (screen_capturer_) { if (screen_capturer_) {
LOG_INFO("Start screen capturer") LOG_INFO("Start screen capturer");
screen_capturer_->Start(); screen_capturer_->Start();
} }
@@ -360,7 +361,7 @@ int Render::StartScreenCapturer() {
int Render::StopScreenCapturer() { int Render::StopScreenCapturer() {
if (screen_capturer_) { if (screen_capturer_) {
LOG_INFO("Stop screen capturer") LOG_INFO("Stop screen capturer");
screen_capturer_->Stop(); screen_capturer_->Stop();
} }
@@ -407,7 +408,7 @@ int Render::StartMouseController() {
int mouse_controller_init_ret = mouse_controller_->Init(display_info_list_); int mouse_controller_init_ret = mouse_controller_->Init(display_info_list_);
if (0 != mouse_controller_init_ret) { if (0 != mouse_controller_init_ret) {
LOG_INFO("Destroy mouse controller") LOG_INFO("Destroy mouse controller");
mouse_controller_->Destroy(); mouse_controller_->Destroy();
mouse_controller_ = nullptr; mouse_controller_ = nullptr;
} }
@@ -465,7 +466,9 @@ int Render::CreateConnectionPeer() {
params_.turn_server_port = 3478; params_.turn_server_port = 3478;
params_.turn_server_username = "dijunkun"; params_.turn_server_username = "dijunkun";
params_.turn_server_password = "dijunkunpw"; params_.turn_server_password = "dijunkunpw";
params_.tls_cert_path = "certs/crossdesk.cn_root.crt"; params_.tls_cert_path =
cert_path_.empty() ? "certs/crossdesk.cn_root.crt" : cert_path_.c_str();
params_.log_path = dll_log_path_.empty() ? "logs" : dll_log_path_.c_str();
params_.hardware_acceleration = config_center_.IsHardwareVideoCodec(); params_.hardware_acceleration = config_center_.IsHardwareVideoCodec();
params_.av1_encoding = config_center_.GetVideoEncodeFormat() == params_.av1_encoding = config_center_.GetVideoEncodeFormat() ==
ConfigCenter::VIDEO_ENCODE_FORMAT::AV1 ConfigCenter::VIDEO_ENCODE_FORMAT::AV1
@@ -704,6 +707,10 @@ int Render::DestroyStreamWindow() {
int Render::SetupFontAndStyle() { int Render::SetupFontAndStyle() {
// Setup Dear ImGui style // Setup Dear ImGui style
ImGuiIO& io = ImGui::GetIO(); ImGuiIO& io = ImGui::GetIO();
imgui_cache_path_ = cache_path_ + "/crossdesk.ini";
io.IniFilename = imgui_cache_path_.c_str();
io.ConfigFlags |= io.ConfigFlags |=
ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls
io.ConfigFlags |= io.ConfigFlags |=
@@ -892,6 +899,16 @@ int Render::DrawStreamWindow() {
} }
int Render::Run() { int Render::Run() {
path_manager_ = std::make_unique<PathManager>("CrossDesk");
if (path_manager_) {
cert_path_ =
(path_manager_->GetCertPath() / "crossdesk.cn_root.crt").string();
exec_log_path_ = path_manager_->GetLogPath().string();
dll_log_path_ = path_manager_->GetLogPath().string();
cache_path_ = path_manager_->GetCachePath().string();
}
InitializeLogger();
InitializeSettings(); InitializeSettings();
InitializeSDL(); InitializeSDL();
InitializeModules(); InitializeModules();
@@ -907,6 +924,14 @@ int Render::Run() {
return 0; return 0;
} }
void Render::InitializeLogger() {
if (!exec_log_path_.empty()) {
InitLogger(exec_log_path_);
} else {
InitLogger("logs");
}
}
void Render::InitializeSettings() { void Render::InitializeSettings() {
LoadSettingsFromCacheFile(); LoadSettingsFromCacheFile();

View File

@@ -25,6 +25,7 @@
#include "imgui_impl_sdlrenderer2.h" #include "imgui_impl_sdlrenderer2.h"
#include "imgui_internal.h" #include "imgui_internal.h"
#include "minirtc.h" #include "minirtc.h"
#include "path_manager.h"
#include "screen_capturer_factory.h" #include "screen_capturer_factory.h"
#include "speaker_capturer_factory.h" #include "speaker_capturer_factory.h"
#include "thumbnail.h" #include "thumbnail.h"
@@ -118,6 +119,7 @@ class Render {
int Run(); int Run();
private: private:
void InitializeLogger();
void InitializeSettings(); void InitializeSettings();
void InitializeSDL(); void InitializeSDL();
void InitializeModules(); void InitializeModules();
@@ -254,10 +256,15 @@ class Render {
ConfigCenter config_center_; ConfigCenter config_center_;
ConfigCenter::LANGUAGE localization_language_ = ConfigCenter::LANGUAGE localization_language_ =
ConfigCenter::LANGUAGE::CHINESE; ConfigCenter::LANGUAGE::CHINESE;
std::unique_ptr<PathManager> path_manager_;
std::string cert_path_;
std::string exec_log_path_;
std::string dll_log_path_;
std::string cache_path_;
std::string imgui_cache_path_;
int localization_language_index_ = -1; int localization_language_index_ = -1;
int localization_language_index_last_ = -1; int localization_language_index_last_ = -1;
bool modules_inited_ = false; bool modules_inited_ = false;
/* ------ all windows property start ------ */ /* ------ all windows property start ------ */
float title_bar_width_ = 640; float title_bar_width_ = 640;
float title_bar_height_ = 30; float title_bar_height_ = 30;

View File

@@ -127,16 +127,25 @@ void ScaleNv12ToABGR(char* src, int src_w, int src_h, int dst_w, int dst_h,
} }
} }
Thumbnail::Thumbnail() { Thumbnail::Thumbnail(std::string save_path) {
if (!save_path.empty()) {
save_path_ = save_path;
}
RAND_bytes(aes128_key_, sizeof(aes128_key_)); RAND_bytes(aes128_key_, sizeof(aes128_key_));
RAND_bytes(aes128_iv_, sizeof(aes128_iv_)); RAND_bytes(aes128_iv_, sizeof(aes128_iv_));
std::filesystem::create_directory(image_path_); std::filesystem::create_directory(save_path_);
} }
Thumbnail::Thumbnail(unsigned char* aes128_key, unsigned char* aes128_iv) { Thumbnail::Thumbnail(std::string save_path, unsigned char* aes128_key,
unsigned char* aes128_iv) {
if (!save_path.empty()) {
save_path_ = save_path;
}
memcpy(aes128_key_, aes128_key, sizeof(aes128_key_)); memcpy(aes128_key_, aes128_key, sizeof(aes128_key_));
memcpy(aes128_iv_, aes128_iv, sizeof(aes128_iv_)); memcpy(aes128_iv_, aes128_iv, sizeof(aes128_iv_));
std::filesystem::create_directory(image_path_); std::filesystem::create_directory(save_path_);
} }
Thumbnail::~Thumbnail() { Thumbnail::~Thumbnail() {
@@ -177,7 +186,7 @@ int Thumbnail::SaveToThumbnail(const char* yuv420p, int width, int height,
std::string cipher_password = AES_encrypt(password, aes128_key_, aes128_iv_); std::string cipher_password = AES_encrypt(password, aes128_key_, aes128_iv_);
image_file_name = remote_id + 'Y' + host_name + '@' + cipher_password; image_file_name = remote_id + 'Y' + host_name + '@' + cipher_password;
std::string file_path = image_path_ + image_file_name; std::string file_path = save_path_ + image_file_name;
stbi_write_png(file_path.data(), thumbnail_width_, thumbnail_height_, 4, stbi_write_png(file_path.data(), thumbnail_width_, thumbnail_height_, 4,
rgba_buffer_, thumbnail_width_ * 4); rgba_buffer_, thumbnail_width_ * 4);
@@ -197,14 +206,14 @@ int Thumbnail::LoadThumbnail(
recent_connections.clear(); recent_connections.clear();
std::vector<std::filesystem::path> image_paths = std::vector<std::filesystem::path> image_paths =
FindThumbnailPath(image_path_); FindThumbnailPath(save_path_);
if (image_paths.size() == 0) { if (image_paths.size() == 0) {
return -1; return -1;
} else { } else {
for (int i = 0; i < image_paths.size(); i++) { for (int i = 0; i < image_paths.size(); i++) {
size_t pos1 = image_paths[i].string().find('/') + 1; size_t pos1 = image_paths[i].string().find('/') + 1;
std::string cipher_image_name = image_paths[i].string().substr(pos1); std::string cipher_image_name = image_paths[i].filename().string();
std::string remote_id; std::string remote_id;
std::string cipher_password; std::string cipher_password;
std::string remote_host_name; std::string remote_host_name;
@@ -245,7 +254,7 @@ int Thumbnail::LoadThumbnail(
AES_decrypt(cipher_password, aes128_key_, aes128_iv_); AES_decrypt(cipher_password, aes128_key_, aes128_iv_);
} }
std::string image_path = image_path_ + cipher_image_name; std::string image_path = save_path_ + cipher_image_name;
recent_connections[original_image_name].texture = nullptr; recent_connections[original_image_name].texture = nullptr;
LoadTextureFromFile(image_path.c_str(), renderer, LoadTextureFromFile(image_path.c_str(), renderer,
&(recent_connections[original_image_name].texture), &(recent_connections[original_image_name].texture),
@@ -257,7 +266,7 @@ int Thumbnail::LoadThumbnail(
} }
int Thumbnail::DeleteThumbnail(const std::string& filename_keyword) { int Thumbnail::DeleteThumbnail(const std::string& filename_keyword) {
for (const auto& entry : std::filesystem::directory_iterator(image_path_)) { for (const auto& entry : std::filesystem::directory_iterator(save_path_)) {
if (entry.is_regular_file()) { if (entry.is_regular_file()) {
const std::string filename = entry.path().filename().string(); const std::string filename = entry.path().filename().string();
std::string id_hostname = filename_keyword.substr(0, filename.find('@')); std::string id_hostname = filename_keyword.substr(0, filename.find('@'));
@@ -295,9 +304,9 @@ std::vector<std::filesystem::path> Thumbnail::FindThumbnailPath(
} }
int Thumbnail::DeleteAllFilesInDirectory() { int Thumbnail::DeleteAllFilesInDirectory() {
if (std::filesystem::exists(image_path_) && if (std::filesystem::exists(save_path_) &&
std::filesystem::is_directory(image_path_)) { std::filesystem::is_directory(save_path_)) {
for (const auto& entry : std::filesystem::directory_iterator(image_path_)) { for (const auto& entry : std::filesystem::directory_iterator(save_path_)) {
if (std::filesystem::is_regular_file(entry.status())) { if (std::filesystem::is_regular_file(entry.status())) {
std::filesystem::remove(entry.path()); std::filesystem::remove(entry.path());
} }

View File

@@ -25,8 +25,9 @@ class Thumbnail {
}; };
public: public:
Thumbnail(); Thumbnail(std::string save_path);
explicit Thumbnail(unsigned char* aes128_key, unsigned char* aes128_iv); explicit Thumbnail(std::string save_path, unsigned char* aes128_key,
unsigned char* aes128_iv);
~Thumbnail(); ~Thumbnail();
public: public:
@@ -74,7 +75,7 @@ class Thumbnail {
int thumbnail_width_ = 160; int thumbnail_width_ = 160;
int thumbnail_height_ = 90; int thumbnail_height_ = 90;
char* rgba_buffer_ = nullptr; char* rgba_buffer_ = nullptr;
std::string image_path_ = "thumbnails/"; std::string save_path_ = "thumbnails/";
unsigned char aes128_key_[16]; unsigned char aes128_key_[16];
unsigned char aes128_iv_[16]; unsigned char aes128_iv_[16];