main: Allowing logging and tuning to also be enabled by files

The logging and tuning flags, which come from the command line,
are now also able to be enabled by the presence of certain files.

This allows the logging and tuning features to be enabled,
at runtime, without having to change the command line.

The content of /etc/thermal.d/tuning does not matter,
only the existence of this file is checked for.
The content of /etc/thermal.d/logging can optionally
be a directory path, which will be used if it exists,
otherwise use the systemwide default temporary directory.

This makes it easy for people doing thermal calibration
to enable these options as they need, without having to
destabilize the system by hand-editing the systemd configuration,
which is non-trivial to change at runtime, due to systemd caching.

The directory choice of /etc/thermal.d has precedence,
as it was already hardcoded in the setpoint file during tuning.

Tested: Tuning mode was enabled and logging appeared in /tmp
mkdir -p /etc/thermal.d
touch /etc/thermal.d/logging
touch /etc/thermal.d/tuning
killall swampd

Signed-off-by: Josh Lehan <krellan@google.com>
Change-Id: Ie74673664de806cbb8a0c70c61310e646ec38014
diff --git a/main.cpp b/main.cpp
index ec9b576..b55a111 100644
--- a/main.cpp
+++ b/main.cpp
@@ -184,7 +184,31 @@
 
     CLI11_PARSE(app, argc, argv);
 
-    loggingEnabled = (!loggingPath.empty());
+    static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
+    static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
+
+    // If this file exists, enable logging at runtime
+    std::ifstream fsLogging(loggingEnablePath);
+    if (fsLogging)
+    {
+        // Unless file contents are a valid directory path, use system default
+        std::getline(fsLogging, loggingPath);
+        if (!(std::filesystem::exists(loggingPath)))
+        {
+            loggingPath = std::filesystem::temp_directory_path();
+        }
+        fsLogging.close();
+
+        loggingEnabled = true;
+        std::cerr << "Logging enabled: " << loggingPath << "\n";
+    }
+
+    // If this file exists, enable tuning at runtime
+    if (std::filesystem::exists(tuningEnablePath))
+    {
+        tuningEnabled = true;
+        std::cerr << "Tuning enabled\n";
+    }
 
     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
     // Create a manager for the ModeBus because we own it.