From 8682183bc3e472df1dccaa01d3120a73a72692cc Mon Sep 17 00:00:00 2001
From: Max Kellermann <max@musicpd.org>
Date: Wed, 14 Nov 2018 12:06:24 +0100
Subject: [PATCH] LogInit: default to journal if MPD was started as systemd
 service

---
 NEWS            |  1 +
 src/LogInit.cxx | 14 ++++++++++++++
 2 files changed, 15 insertions(+)

diff --git a/NEWS b/NEWS
index a8bacdbc2..6628488ae 100644
--- a/NEWS
+++ b/NEWS
@@ -2,6 +2,7 @@ ver 0.21.3 (not yet released)
 * output
   - alsa: fix crash bug
   - alsa: fix stuttering at start of playback
+* log: default to journal if MPD was started as systemd service
 
 ver 0.21.2 (2018/11/12)
 * protocol
diff --git a/src/LogInit.cxx b/src/LogInit.cxx
index 6b2a74c9e..46d2ef62a 100644
--- a/src/LogInit.cxx
+++ b/src/LogInit.cxx
@@ -30,6 +30,10 @@
 #include "util/RuntimeError.hxx"
 #include "system/Error.hxx"
 
+#ifdef ENABLE_SYSTEMD_DAEMON
+#include <systemd/sd-daemon.h>
+#endif
+
 #include <assert.h>
 #include <string.h>
 #include <fcntl.h>
@@ -139,6 +143,16 @@ log_init(const ConfigData &config, bool verbose, bool use_stdout)
 		if (param == nullptr) {
 			/* no configuration: default to syslog (if
 			   available) */
+#ifdef ENABLE_SYSTEMD_DAEMON
+			if (sd_booted() &&
+			    getenv("NOTIFY_SOCKET") != nullptr) {
+				/* if MPD was started as a systemd
+				   service, default to journal (which
+				   is connected to fd=2) */
+				out_fd = STDOUT_FILENO;
+				return;
+			}
+#endif
 #ifndef HAVE_SYSLOG
 			throw std::runtime_error("config parameter 'log_file' not found");
 #endif