Skip to content

Linux + iPhone Continuity / iMessage / SMS

fix: logger inconsistent

Zack Bartel02f4102a99cc
diff --git a/src/core/include/tether/net.hpp b/src/core/include/tether/net.hpp
index 844ee8f..46b77c7 100644
--- a/src/core/include/tether/net.hpp
+++ b/src/core/include/tether/net.hpp
@@ -13,6 +13,8 @@ namespace tether {
// Check if tetherd is already running. Exits if another instance holds the lock.
void ensure_single_instance();
std::string get_runtime_dir();
+ // $XDG_STATE_HOME/tether (or ~/.local/state/tether), created if absent.
+ std::string get_state_dir();
class TcpServer;
class UnixServer;
diff --git a/src/core/src/net.cpp b/src/core/src/net.cpp
index 1a2a766..29fe24e 100644
--- a/src/core/src/net.cpp
+++ b/src/core/src/net.cpp
@@ -568,6 +568,22 @@ namespace tether {
return tether_dir.string();
}
+ std::string get_state_dir() {
+ const char* xdg_state = std::getenv("XDG_STATE_HOME");
+ const char* home = std::getenv("HOME");
+ std::filesystem::path base;
+ if (xdg_state && *xdg_state)
+ base = xdg_state;
+ else if (home && *home)
+ base = std::filesystem::path(home) / ".local" / "state";
+ else
+ base = "/tmp";
+
+ std::filesystem::path tether_dir = base / "tether";
+ std::filesystem::create_directories(tether_dir);
+ return tether_dir.string();
+ }
+
void ensure_single_instance() {
std::string lock_file = get_runtime_dir() + "/tetherd.lock";
int fd = open(lock_file.c_str(), O_RDWR | O_CREAT, 0600);
diff --git a/src/daemon/main.cpp b/src/daemon/main.cpp
index fb03f23..ffad2c7 100644
--- a/src/daemon/main.cpp
+++ b/src/daemon/main.cpp
@@ -1,5 +1,6 @@
#include "notification.hpp"
#include <csignal>
+#include <cstdio>
#include <ctime>
#include <mutex>
#include <nlohmann/json.hpp>
@@ -29,7 +30,23 @@ void signal_handler(int) {
}
}
+// Append stderr/stdout to the state-dir log unless attached to a terminal.
+static void redirect_output_to_log() {
+ if (isatty(STDERR_FILENO))
+ return;
+ try {
+ const std::string log_path = tether::get_state_dir() + "/tetherd.log";
+ if (freopen(log_path.c_str(), "a", stderr) == nullptr)
+ return;
+ if (freopen(log_path.c_str(), "a", stdout) == nullptr) {
+ }
+ } catch (const std::exception&) {
+ // Losing the log is survivable; failing to start is not.
+ }
+}
+
int main(int argc, char** argv) {
+ redirect_output_to_log();
tether::init_locale();
debug::log(INFO, "tetherd version {}", tether::get_version());
diff --git a/src/gtk/daemon_client.cpp b/src/gtk/daemon_client.cpp
index 44ed081..e969bae 100644
--- a/src/gtk/daemon_client.cpp
+++ b/src/gtk/daemon_client.cpp
@@ -130,22 +130,10 @@ namespace tether::ui {
std::filesystem::path self_path = std::filesystem::read_symlink("/proc/self/exe");
std::string daemon_path = (self_path.parent_path() / "tetherd").string();
if (fork() == 0) {
- const char* state = getenv("XDG_STATE_HOME");
- std::filesystem::path log_dir =
- state && *state ? std::filesystem::path(state)
- : std::filesystem::path(getenv("HOME") ? getenv("HOME") : "/tmp") /
- ".local" / "state";
- log_dir /= "tether";
- std::error_code ec;
- std::filesystem::create_directories(log_dir, ec);
- const std::string log_path = (log_dir / "tetherd.log").string();
- if (freopen(log_path.c_str(), "a", stdout) == nullptr) {
- if (freopen("/dev/null", "w", stdout) == nullptr) {
- }
+ // tetherd reopens these onto its own log file.
+ if (freopen("/dev/null", "w", stdout) == nullptr) {
}
- if (freopen(log_path.c_str(), "a", stderr) == nullptr) {
- if (freopen("/dev/null", "w", stderr) == nullptr) {
- }
+ if (freopen("/dev/null", "w", stderr) == nullptr) {
}
if (freopen("/dev/null", "r", stdin) == nullptr) {
}