Adding new feature of core PID loop logging

This differs from the normal logging,
as this focuses on the core of the PID loop computations.

All variables within the core pid/ec/pid.cpp pid()
function are now logged, so math can be debugged.

Output is throttled to only log a new line
when it changes, or when 60 seconds have elapsed.

Creates 2 files for each PID loop,
one showing coefficients that were configured for it,
and one showing the variables changing over time.

Enable by --corelogging command line option,
or by creating /etc/thermal.d/corelogging file.

Tested:
Log files appear as expected, when enabled.
No changes noticed, when this feature is disabled.

Signed-off-by: Josh Lehan <krellan@google.com>
Change-Id: I3f37fe918e7cbc6fb885ffa2f268600d5a317d32
diff --git a/main.cpp b/main.cpp
index 8ae6be5..09d9b70 100644
--- a/main.cpp
+++ b/main.cpp
@@ -245,6 +245,7 @@
     loggingEnabled = false;
     tuningEnabled = false;
     debugEnabled = false;
+    coreLoggingEnabled = false;
 
     CLI::App app{"OpenBMC Fan Control Daemon"};
 
@@ -256,12 +257,15 @@
         ->check(CLI::ExistingDirectory);
     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
     app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
+    app.add_flag("-g,--corelogging", coreLoggingEnabled,
+                 "Enable or disable logging of core PID loop computations");
 
     CLI11_PARSE(app, argc, argv);
 
     static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
     static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
     static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
+    static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
 
     // Set up default logging path, preferring command line if it was given
     std::string defLoggingPath(loggingPath);
@@ -279,19 +283,18 @@
     std::ifstream fsLogging(loggingEnablePath);
     if (fsLogging)
     {
-        // The first line of file might be a valid directory path
-        std::getline(fsLogging, loggingPath);
+        // Allow logging path to be changed by file content
+        std::string altPath;
+        std::getline(fsLogging, altPath);
         fsLogging.close();
 
-        // If so, use it, otherwise use default logging path instead
-        if (!(std::filesystem::exists(loggingPath)))
+        if (std::filesystem::exists(altPath))
         {
-            loggingPath = defLoggingPath;
+            loggingPath = altPath;
         }
 
         loggingEnabled = true;
     }
-
     if (loggingEnabled)
     {
         std::cerr << "Logging enabled: " << loggingPath << "\n";
@@ -302,8 +305,6 @@
     {
         tuningEnabled = true;
     }
-
-    // This can also be enabled from the command line
     if (tuningEnabled)
     {
         std::cerr << "Tuning enabled\n";
@@ -320,6 +321,16 @@
         std::cerr << "Debug mode enabled\n";
     }
 
+    // If this file exists, enable core logging at runtime
+    if (std::filesystem::exists(coreLoggingEnablePath))
+    {
+        coreLoggingEnabled = true;
+    }
+    if (coreLoggingEnabled)
+    {
+        std::cerr << "Core logging enabled\n";
+    }
+
     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
     // Create a manager for the ModeBus because we own it.
     sdbusplus::server::manager_t(static_cast<sdbusplus::bus_t&>(modeControlBus),