Skip to content

Commit

Permalink
Path: add logs
Browse files Browse the repository at this point in the history
  • Loading branch information
fufexan committed Aug 29, 2024
1 parent 0db21e9 commit 4fb4760
Showing 1 changed file with 15 additions and 3 deletions.
18 changes: 15 additions & 3 deletions src/path/Path.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <hyprutils/path/Path.hpp>
#include <hyprutils/string/VarList.hpp>
#include <hyprutils/debug/Log.hpp>
#include <filesystem>

using namespace Hyprutils;
Expand All @@ -16,17 +17,21 @@ namespace Hyprutils::Path {
std::optional<std::string> getHome() {
static const auto homeDir = getenv("HOME");

if (!homeDir || !std::filesystem::path(homeDir).is_absolute())
if (!homeDir || !std::filesystem::path(homeDir).is_absolute()) {
Debug::log(INFO, "'$HOME' is either not found or not an absolute path");
return std::nullopt;
}

return std::string(homeDir).append("/.config");
}

std::optional<String::CVarList> getXdgConfigDirs() {
static const auto xdgConfigDirs = getenv("XDG_CONFIG_DIRS");

if (!xdgConfigDirs)
if (!xdgConfigDirs) {
Debug::log(INFO, "'$XDG_CONFIG_DIRS' is not set");
return std::nullopt;
}

static const auto xdgConfigDirsList = String::CVarList(xdgConfigDirs, 0, ':');

Expand All @@ -36,8 +41,10 @@ namespace Hyprutils::Path {
std::optional<std::string> getXdgConfigHome() {
static const auto xdgConfigHome = getenv("XDG_CONFIG_HOME");

if (!xdgConfigHome || !std::filesystem::path(xdgConfigHome).is_absolute())
if (!xdgConfigHome || !std::filesystem::path(xdgConfigHome).is_absolute()) {
Debug::log(INFO, "'$XDG_CONFIG_HOME' is either not found or not an absolute path");
return std::nullopt;
}

return xdgConfigHome;
}
Expand All @@ -50,6 +57,7 @@ namespace Hyprutils::Path {
xdgConfigHomeExists = true;
if (checkConfigExists(xdgConfigHome.value(), programName))
return std::make_pair(fullConfigPath(xdgConfigHome.value(), programName), xdgConfigHome);
Debug::log(INFO, "No config file could be found in {}/{}", xdgConfigHome.value(), programName);
}

bool homeExists = false;
Expand All @@ -58,24 +66,28 @@ namespace Hyprutils::Path {
homeExists = true;
if (checkConfigExists(home.value(), programName))
return std::make_pair(fullConfigPath(home.value(), programName), home);
Debug::log(INFO, "No config file could be found in {}/{}", home.value(), programName);
}

static const auto xdgConfigDirs = getXdgConfigDirs();
if (xdgConfigDirs.has_value()) {
for (auto dir : xdgConfigDirs.value()) {
if (checkConfigExists(dir, programName))
return std::make_pair(fullConfigPath(dir, programName), std::nullopt);
Debug::log(INFO, "No config file could be found in {}/{}", dir, programName);
}
}

if (checkConfigExists("/etc/xdg", programName))
return std::make_pair(fullConfigPath("/etc/xdg", programName), std::nullopt);
Debug::log(INFO, "No config file could be found in /etc/xdg/{}", programName);

if (xdgConfigHomeExists)
return std::make_pair(std::nullopt, xdgConfigHome);
else if (homeExists)
return std::make_pair(std::nullopt, home);

Debug::log(ERR, "No config file could be found. Check previous logs for clues");
return std::make_pair(std::nullopt, std::nullopt);
}
}

0 comments on commit 4fb4760

Please sign in to comment.