log-manager: enable non-standard path for local tests

Enable the log-manager to be launched with a path other than
`/var/phosphor-logging` to allow it to be ran on a development system
for test purposes.  This required some refactoring throughout as to
how paths were handled.

Tested:

After running tests, `/tmp/phosphor-logging` is populated with
entries.  Launching `./builddir/phosphor-log-manager
/tmp/phosphor-logging`, the log-manager will have the entries on
dbus:

```
$ busctl --user tree xyz.openbmc_project.Logging
└─ /xyz
  └─ /xyz/openbmc_project
    └─ /xyz/openbmc_project/logging
      ├─ /xyz/openbmc_project/logging/entry
      │ ├─ /xyz/openbmc_project/logging/entry/100
      │ ├─ /xyz/openbmc_project/logging/entry/101
      │ ├─ /xyz/openbmc_project/logging/entry/102
      │ ├─ /xyz/openbmc_project/logging/entry/103
```

Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I84968edeed0dcf73aaf8bb653060b6d3400b9b7b
diff --git a/config/config.h.meson b/config/config.h.meson
index cd91f80..698b09d 100644
--- a/config/config.h.meson
+++ b/config/config.h.meson
@@ -25,8 +25,6 @@
 
 #define RSYSLOG_SERVER_CONFIG_FILE "@rsyslog_server_conf@"
 
-extern const char *ERRLOG_PERSIST_PATH;
-extern const char *EXTENSION_PERSIST_DIR;
 extern const bool IS_UNIT_TEST;
 
 static constexpr size_t ERROR_CAP = @error_cap@;
diff --git a/config_main.h b/config_main.h
index 78c223e..258bcf7 100644
--- a/config_main.h
+++ b/config_main.h
@@ -1,5 +1,4 @@
 #pragma once
 
-const char* ERRLOG_PERSIST_PATH = "/var/lib/phosphor-logging/errors";
-const char* EXTENSION_PERSIST_DIR = "/var/lib/phosphor-logging/extensions";
+const char* PERSIST_PATH_ROOT = "/var/lib/phosphor-logging";
 const bool IS_UNIT_TEST = false;
diff --git a/elog_entry.cpp b/elog_entry.cpp
index 183ecbb..8f86cca 100644
--- a/elog_entry.cpp
+++ b/elog_entry.cpp
@@ -3,6 +3,7 @@
 #include "elog_serialize.hpp"
 #include "extensions.hpp"
 #include "log_manager.hpp"
+#include "paths.hpp"
 
 #include <fcntl.h>
 #include <unistd.h>
diff --git a/elog_serialize.hpp b/elog_serialize.hpp
index 38ff424..29791b2 100644
--- a/elog_serialize.hpp
+++ b/elog_serialize.hpp
@@ -3,6 +3,7 @@
 #include "config.h"
 
 #include "elog_entry.hpp"
+#include "paths.hpp"
 
 #include <filesystem>
 #include <string>
@@ -22,7 +23,7 @@
  *  @return fs::path - pathname of persisted error file
  */
 fs::path serialize(const Entry& e,
-                   const fs::path& dir = fs::path(ERRLOG_PERSIST_PATH));
+                   const fs::path& dir = fs::path(paths::error()));
 
 /** @brief Deserialze a persisted error into a d-bus object
  *  @param[in] path - pathname of persisted error file
@@ -38,8 +39,8 @@
  *                   be placed.
  *  @return fs::path - pathname of persisted error file
  */
-fs::path getEntrySerializePath(
-    uint32_t id, const fs::path& dir = fs::path(ERRLOG_PERSIST_PATH));
+fs::path getEntrySerializePath(uint32_t id,
+                               const fs::path& dir = fs::path(paths::error()));
 
 } // namespace logging
 } // namespace phosphor
diff --git a/extensions/openpower-pels/meson.build b/extensions/openpower-pels/meson.build
index fb6282b..db57568 100644
--- a/extensions/openpower-pels/meson.build
+++ b/extensions/openpower-pels/meson.build
@@ -68,6 +68,7 @@
     'severity.cpp',
     'user_header.cpp',
     'temporary_file.cpp',
+    '../../paths.cpp',
     '../../util.cpp',
     extra_sources,
 )
@@ -86,7 +87,7 @@
 libpel_lib = static_library(
     'pel',
     libpel_sources,
-    'paths.cpp', # paths is separate because it is overridden during test.
+    'pel_paths.cpp', # paths is separate because it is overridden during test.
     include_directories: include_directories('../..', '../../gen'),
     cpp_args: extra_args,
     dependencies: [
diff --git a/extensions/openpower-pels/paths.hpp b/extensions/openpower-pels/paths.hpp
index 600073a..b837d7c 100644
--- a/extensions/openpower-pels/paths.hpp
+++ b/extensions/openpower-pels/paths.hpp
@@ -1,4 +1,6 @@
 #pragma once
+#include "../../paths.hpp"
+
 #include <filesystem>
 
 namespace openpower
diff --git a/extensions/openpower-pels/paths.cpp b/extensions/openpower-pels/pel_paths.cpp
similarity index 91%
rename from extensions/openpower-pels/paths.cpp
rename to extensions/openpower-pels/pel_paths.cpp
index 14e5d71..6ea4219 100644
--- a/extensions/openpower-pels/paths.cpp
+++ b/extensions/openpower-pels/pel_paths.cpp
@@ -30,14 +30,14 @@
 
 fs::path getPELIDFile()
 {
-    fs::path logIDPath{EXTENSION_PERSIST_DIR};
+    fs::path logIDPath{phosphor::logging::paths::extension()};
     logIDPath /= fs::path{"pels"} / fs::path{"pelID"};
     return logIDPath;
 }
 
 fs::path getPELRepoPath()
 {
-    std::filesystem::path repoPath{EXTENSION_PERSIST_DIR};
+    std::filesystem::path repoPath{phosphor::logging::paths::extension()};
     repoPath /= "pels";
     return repoPath;
 }
diff --git a/extensions/openpower-pels/tools/peltool.cpp b/extensions/openpower-pels/tools/peltool.cpp
index 85fe9d1..810223a 100644
--- a/extensions/openpower-pels/tools/peltool.cpp
+++ b/extensions/openpower-pels/tools/peltool.cpp
@@ -65,7 +65,7 @@
 
 std::string pelLogDir()
 {
-    return std::string(EXTENSION_PERSIST_DIR) + "/pels/logs";
+    return std::string(phosphor::logging::paths::extension()) + "/pels/logs";
 }
 
 /**
diff --git a/log_manager.cpp b/log_manager.cpp
index 4a46cd8..0992160 100644
--- a/log_manager.cpp
+++ b/log_manager.cpp
@@ -6,6 +6,7 @@
 #include "elog_meta.hpp"
 #include "elog_serialize.hpp"
 #include "extensions.hpp"
+#include "paths.hpp"
 #include "util.hpp"
 
 #include <systemd/sd-bus.h>
@@ -584,7 +585,7 @@
         }
 
         // Delete the persistent representation of this error.
-        fs::path errorPath(ERRLOG_PERSIST_PATH);
+        fs::path errorPath(paths::error());
         errorPath /= std::to_string(entryId);
         fs::remove(errorPath);
 
@@ -633,7 +634,7 @@
         return id == restoredId;
     };
 
-    fs::path dir(ERRLOG_PERSIST_PATH);
+    fs::path dir(paths::error());
     if (!fs::exists(dir) || fs::is_empty(dir))
     {
         return;
diff --git a/log_manager_main.cpp b/log_manager_main.cpp
index 207c93c..fda9119 100644
--- a/log_manager_main.cpp
+++ b/log_manager_main.cpp
@@ -4,6 +4,7 @@
 
 #include "extensions.hpp"
 #include "log_manager.hpp"
+#include "paths.hpp"
 
 #include <phosphor-logging/lg2.hpp>
 #include <sdbusplus/bus.hpp>
@@ -12,10 +13,16 @@
 
 #include <filesystem>
 
-int main(int /*argc*/, char* /*argv*/[])
+int main(int argc, char* argv[])
 {
     PHOSPHOR_LOG2_USING_WITH_FLAGS;
 
+    if (argc >= 1)
+    {
+        PERSIST_PATH_ROOT = strdup(argv[1]);
+        info("Using temporary {PATH} for logs", "PATH", PERSIST_PATH_ROOT);
+    }
+
     auto bus = sdbusplus::bus::new_default();
     auto event = sdeventplus::Event::get_default();
     bus.attach_event(event.get(), SD_EVENT_PRIORITY_NORMAL);
@@ -28,7 +35,7 @@
     phosphor::logging::Manager mgr(bus, OBJ_LOGGING, iMgr);
 
     // Create a directory to persist errors.
-    std::filesystem::create_directories(ERRLOG_PERSIST_PATH);
+    std::filesystem::create_directories(phosphor::logging::paths::error());
 
     // Recreate error d-bus objects from persisted errors.
     iMgr.restore();
diff --git a/meson.build b/meson.build
index 2832504..ed2ea44 100644
--- a/meson.build
+++ b/meson.build
@@ -128,6 +128,7 @@
         'elog_serialize.cpp',
         'extensions.cpp',
         'log_manager.cpp',
+        'paths.cpp',
         'util.cpp',
     )
 ]
diff --git a/paths.cpp b/paths.cpp
new file mode 100644
index 0000000..cf9c7b7
--- /dev/null
+++ b/paths.cpp
@@ -0,0 +1,15 @@
+#include "config.h"
+
+#include "paths.hpp"
+
+namespace phosphor::logging::paths
+{
+auto error() -> std::filesystem::path
+{
+    return std::filesystem::path(PERSIST_PATH_ROOT) / "errors";
+}
+auto extension() -> std::filesystem::path
+{
+    return std::filesystem::path(PERSIST_PATH_ROOT) / "extensions";
+}
+} // namespace phosphor::logging::paths
diff --git a/paths.hpp b/paths.hpp
new file mode 100644
index 0000000..3d81852
--- /dev/null
+++ b/paths.hpp
@@ -0,0 +1,14 @@
+#pragma once
+
+#include <filesystem>
+#include <string>
+
+extern const char* PERSIST_PATH_ROOT;
+
+namespace phosphor::logging::paths
+{
+
+auto error() -> std::filesystem::path;
+auto extension() -> std::filesystem::path;
+
+} // namespace phosphor::logging::paths
diff --git a/test/common.cpp b/test/common.cpp
index 8905793..89cad22 100644
--- a/test/common.cpp
+++ b/test/common.cpp
@@ -1,5 +1,4 @@
 #include "config.h"
 
-const char* ERRLOG_PERSIST_PATH = "/tmp/errors";
-const char* EXTENSION_PERSIST_DIR = "/tmp/extensions";
+const char* PERSIST_PATH_ROOT = "/tmp/phosphor-logging";
 const bool IS_UNIT_TEST = true;
diff --git a/test/elog_errorwrap_test.hpp b/test/elog_errorwrap_test.hpp
index d8b1295..82269c4 100644
--- a/test/elog_errorwrap_test.hpp
+++ b/test/elog_errorwrap_test.hpp
@@ -2,6 +2,7 @@
 
 #include "elog_serialize.hpp"
 #include "log_manager.hpp"
+#include "paths.hpp"
 #include "xyz/openbmc_project/Common/error.hpp"
 
 #include <phosphor-logging/elog-errors.hpp>
@@ -87,7 +88,7 @@
         bus(sdbusplus::bus::new_default()),
         manager(bus, "/xyz/openbmc_test/abc")
     {
-        fs::create_directories(ERRLOG_PERSIST_PATH);
+        fs::create_directories(paths::error());
     }
 
     ~TestLogManager()
diff --git a/test/elog_quiesce_test.cpp b/test/elog_quiesce_test.cpp
index a865168..7894f95 100644
--- a/test/elog_quiesce_test.cpp
+++ b/test/elog_quiesce_test.cpp
@@ -2,6 +2,7 @@
 
 #include "elog_entry.hpp"
 #include "log_manager.hpp"
+#include "paths.hpp"
 
 #include <sdbusplus/bus.hpp>
 #include <sdbusplus/test/sdbus_mock.hpp>
@@ -28,7 +29,7 @@
     TestQuiesceOnError() : manager(mockedBus, OBJ_INTERNAL)
     {
         // Ensure any errors serializing to filesystem have directory created
-        std::filesystem::create_directory(ERRLOG_PERSIST_PATH);
+        std::filesystem::create_directories(paths::error());
     }
 };
 
diff --git a/test/elog_update_ts_test.cpp b/test/elog_update_ts_test.cpp
index 07d9613..e8ddc09 100644
--- a/test/elog_update_ts_test.cpp
+++ b/test/elog_update_ts_test.cpp
@@ -4,6 +4,7 @@
 #include "elog_serialize.hpp"
 #include "extensions.hpp"
 #include "log_manager.hpp"
+#include "paths.hpp"
 
 #include <filesystem>
 #include <thread>
@@ -34,11 +35,11 @@
 TEST(TestUpdateTS, testChangeResolved)
 {
     // Setting resolved will serialize, so need this directory.
-    fs::create_directory(ERRLOG_PERSIST_PATH);
+    fs::create_directories(paths::error());
 
-    if (!fs::exists(ERRLOG_PERSIST_PATH))
+    if (!fs::exists(paths::error()))
     {
-        ADD_FAILURE() << "Could not create " << ERRLOG_PERSIST_PATH << "\n";
+        ADD_FAILURE() << "Could not create " << paths::error() << "\n";
         exit(1);
     }
 
@@ -50,7 +51,7 @@
     std::srand(std::time(nullptr));
     uint32_t id = std::rand();
 
-    if (fs::exists(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id)))
+    if (fs::exists(fs::path{paths::error()} / std::to_string(id)))
     {
         std::cerr << "Another testcase is using ID " << id << "\n";
         id = std::rand();
@@ -96,7 +97,7 @@
     EXPECT_EQ(updateTS, elog.updateTimestamp());
 
     // Leave the directory in case other CI instances are running
-    fs::remove(fs::path{ERRLOG_PERSIST_PATH} / std::to_string(id));
+    fs::remove(fs::path{paths::error()} / std::to_string(id));
 }
 
 TEST(TestResolveProhibited, testResolveFlagChange)
diff --git a/test/openpower-pels/meson.build b/test/openpower-pels/meson.build
index d2a44c9..14021a9 100644
--- a/test/openpower-pels/meson.build
+++ b/test/openpower-pels/meson.build
@@ -67,7 +67,7 @@
 openpower_test_lib = static_library(
     'openpower_test_lib',
     'pel_utils.cpp',
-    'paths.cpp',
+    'pel_paths.cpp',
     libpel_sources,
     peltool_sources,
     '../common.cpp',
diff --git a/test/openpower-pels/paths.cpp b/test/openpower-pels/pel_paths.cpp
similarity index 100%
rename from test/openpower-pels/paths.cpp
rename to test/openpower-pels/pel_paths.cpp