[fix] initialize logging before startup operations and update MiniRTC

This commit is contained in:
dijunkun
2026-09-16 12:54:07 +08:00
parent d8ccdbaa38
commit 57cab3979e
4 changed files with 37 additions and 16 deletions
+9 -1
View File
@@ -22,6 +22,7 @@
#include "config_center.h" #include "config_center.h"
#include "daemon.h" #include "daemon.h"
#include "path_manager.h" #include "path_manager.h"
#include "rd_log.h"
#include "render.h" #include "render.h"
#ifdef __linux__ #ifdef __linux__
@@ -192,6 +193,14 @@ int main(int argc, char* argv[]) {
std::strcmp(argv[1], crossdesk::kSlintRendererProbeArgument) == 0) { std::strcmp(argv[1], crossdesk::kSlintRendererProbeArgument) == 0) {
return crossdesk::RunSlintRendererProbe(); return crossdesk::RunSlintRendererProbe();
} }
#endif
// Service operations and config migration can log before the GUI starts.
// Configure the directory first, including CLI and daemon child paths.
auto path_manager = std::make_unique<crossdesk::PathManager>("CrossDesk");
crossdesk::InitLogger(path_manager->GetLogPath().string());
#ifdef _WIN32
if (argc > 1 && IsServiceCliCommand(argv[1])) { if (argc > 1 && IsServiceCliCommand(argv[1])) {
return HandleServiceCliCommand(argv[1]); return HandleServiceCliCommand(argv[1]);
} }
@@ -218,7 +227,6 @@ int main(int argc, char* argv[]) {
#endif #endif
bool enable_daemon = false; bool enable_daemon = false;
auto path_manager = std::make_unique<crossdesk::PathManager>("CrossDesk");
if (path_manager) { if (path_manager) {
std::string cache_path = path_manager->GetCachePath().string(); std::string cache_path = path_manager->GetCachePath().string();
crossdesk::ConfigCenter config_center(cache_path + "/config.ini"); crossdesk::ConfigCenter config_center(cache_path + "/config.ini");
@@ -950,10 +950,10 @@ int GuiApplication::Run() {
} }
exec_log_path_ = path_manager_->GetLogPath().string(); exec_log_path_ = path_manager_->GetLogPath().string();
dll_log_path_ = exec_log_path_; dll_log_path_ = exec_log_path_;
InitializeLogger();
cache_path_ = path_manager_->GetCachePath().string(); cache_path_ = path_manager_->GetCachePath().string();
config_center_ = std::make_unique<ConfigCenter>(cache_path_ + "/config.ini"); config_center_ = std::make_unique<ConfigCenter>(cache_path_ + "/config.ini");
InitializeLogger();
LOG_INFO("CrossDesk version: {} (Slint UI)", CROSSDESK_VERSION); LOG_INFO("CrossDesk version: {} (Slint UI)", CROSSDESK_VERSION);
strncpy(signal_server_ip_self_, config_center_->GetSignalServerHost().c_str(), strncpy(signal_server_ip_self_, config_center_->GetSignalServerHost().c_str(),
+26 -13
View File
@@ -1,6 +1,5 @@
#include "rd_log.h" #include "rd_log.h"
#include <atomic>
#include <filesystem> #include <filesystem>
namespace crossdesk { namespace crossdesk {
@@ -9,26 +8,38 @@ namespace {
std::string g_log_dir = "logs"; std::string g_log_dir = "logs";
std::once_flag g_logger_once_flag; std::once_flag g_logger_once_flag;
std::mutex g_logger_mutex;
std::shared_ptr<spdlog::logger> g_logger; std::shared_ptr<spdlog::logger> g_logger;
std::atomic<bool> g_logger_created{false};
std::string NormalizeLogDirectory(const std::string& log_dir) {
const std::filesystem::path path = log_dir.empty() ? "logs" : log_dir;
std::error_code ec;
const auto absolute = std::filesystem::absolute(path, ec);
return (ec ? path : absolute).lexically_normal().string();
}
} // namespace } // namespace
void InitLogger(const std::string& log_dir) { void InitLogger(const std::string& log_dir) {
if (g_logger_created.load()) { const auto directory = NormalizeLogDirectory(log_dir);
LOG_WARN( std::lock_guard<std::mutex> lock(g_logger_mutex);
"InitLogger called after logger initialized. Ignoring log_dir: {}, " if (g_logger) {
"using previous log_dir: {}", if (directory != g_log_dir) {
log_dir, g_log_dir); g_logger->warn(
"InitLogger called after logger initialized. Ignoring log_dir: {}, "
"using previous log_dir: {}",
directory, g_log_dir);
}
return; return;
} }
g_log_dir = log_dir; g_log_dir = directory;
} }
std::shared_ptr<spdlog::logger> get_logger() { std::shared_ptr<spdlog::logger> get_logger() {
std::call_once(g_logger_once_flag, []() { std::call_once(g_logger_once_flag, []() {
g_logger_created.store(true); std::lock_guard<std::mutex> lock(g_logger_mutex);
g_log_dir = NormalizeLogDirectory(g_log_dir);
std::error_code ec; std::error_code ec;
std::filesystem::create_directories(g_log_dir, ec); std::filesystem::create_directories(g_log_dir, ec);
@@ -54,10 +65,12 @@ std::shared_ptr<spdlog::logger> get_logger() {
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>( sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
filename, 5 * 1024 * 1024, 3)); filename, 5 * 1024 * 1024, 3));
g_logger = std::make_shared<spdlog::logger>(LOGGER_NAME, sinks.begin(), auto logger = std::make_shared<spdlog::logger>(LOGGER_NAME, sinks.begin(),
sinks.end()); sinks.end());
g_logger->flush_on(spdlog::level::info); logger->flush_on(spdlog::level::info);
spdlog::register_logger(g_logger); spdlog::register_logger(logger);
logger->info("Logger initialized: path={}", filename);
g_logger = std::move(logger);
}); });
return g_logger; return g_logger;