Skip to content

Commit

Permalink
feat: make log file optional
Browse files Browse the repository at this point in the history
fix: log file should be in XDG_STATE_HOME

Fixes Vencord/Vesktop#234
  • Loading branch information
Curve committed Nov 11, 2023
1 parent b4cf6ae commit d24a5de
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.21)
project(venmic LANGUAGES CXX VERSION 2.0.1)
project(venmic LANGUAGES CXX VERSION 2.1.0)

# --------------------------------------------------------------------------------------------------------
# Library options
Expand Down
47 changes: 42 additions & 5 deletions src/logger.cpp
Original file line number Diff line number Diff line change
@@ -1,28 +1,65 @@
#include "logger.hpp"

#include <filesystem>
#include <spdlog/sinks/ansicolor_sink.h>
#include <spdlog/sinks/basic_file_sink.h>

namespace vencord
{
namespace fs = std::filesystem;

struct logger::impl
{
std::unique_ptr<spdlog::logger> logger;
};

fs::path config_dir()
{
fs::path rtn = fs::temp_directory_path();

if (auto home = std::getenv("HOME"))
{
rtn = home / ".local" / "state";
}

if (auto state_home = std::getenv("XDG_STATE_HOME"))
{
rtn = state_home;
}

return rtn / "venmic";
}

logger::logger() : m_impl(std::make_unique<impl>())
{
namespace sinks = spdlog::sinks;

auto file_sink = std::make_shared<sinks::basic_file_sink_mt>("venmic.log");
file_sink->set_level(spdlog::level::trace);
m_impl->logger = std::make_unique<spdlog::logger>("venmic");
m_impl->logger->set_level(spdlog::level::trace);
m_impl->logger->flush_on(spdlog::level::trace);

auto stdout_sink = std::make_shared<sinks::ansicolor_stdout_sink_mt>();

stdout_sink->set_level(spdlog::level::info);
m_impl->logger->sinks().emplace_back(stdout_sink);

m_impl->logger = std::make_unique<spdlog::logger>("venmic", spdlog::sinks_init_list{stdout_sink, file_sink});
m_impl->logger->set_level(spdlog::level::trace);
m_impl->logger->flush_on(spdlog::level::trace);
if (!std::getenv("VENMIC_ENABLE_LOG"))
{
return;
}

auto config_path = config_dir() / "venmic.log";

if (!fs::exists(config_path))
{
[[maybe_unused]] std::error_code ec;
fs::create_directories(config_path.parent_path(), ec);
}

auto file_sink = std::make_shared<sinks::basic_file_sink_mt>(config_path.string());

file_sink->set_level(spdlog::level::trace);
m_impl->logger->sinks().emplace_back(file_sink);
}

spdlog::logger *logger::operator->() const
Expand Down

0 comments on commit d24a5de

Please sign in to comment.