Skip to content

Commit

Permalink
LooseFileLoader: Add options to log to disk
Browse files Browse the repository at this point in the history
  • Loading branch information
praydog committed Nov 1, 2024
1 parent 14ba65f commit bb22652
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
39 changes: 39 additions & 0 deletions src/mods/LooseFileLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,27 @@
#include <utility/Scan.hpp>
#include <utility/Module.hpp>

#include <spdlog/sinks/basic_file_sink.h>

#include "LooseFileLoader.hpp"

LooseFileLoader* g_loose_file_loader{nullptr};

LooseFileLoader::LooseFileLoader()
: m_logger{spdlog::basic_logger_mt("LooseFileLoader", "reframework_accessed_files.txt")},
m_loose_file_logger{spdlog::basic_logger_mt("LooseFileLoader2", "reframework_loose_files.txt")}
{
m_logger->set_level(spdlog::level::info);
m_logger->flush_on(spdlog::level::info);

m_logger->info("LooseFileLoader constructed");

m_loose_file_logger->set_level(spdlog::level::info);
m_loose_file_logger->flush_on(spdlog::level::info);

m_loose_file_logger->info("LooseFileLoader constructed");
}

std::shared_ptr<LooseFileLoader>& LooseFileLoader::get() {
static auto instance = std::shared_ptr<LooseFileLoader>(new LooseFileLoader());
return instance;
Expand Down Expand Up @@ -87,6 +104,20 @@ void LooseFileLoader::on_draw_ui() {
ImGui::TreePop();
}

m_log_accessed_files->draw("Log accessed files");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("Logs all accessed files to <game_dir>/reframework_accessed_files.txt");
ImGui::EndTooltip();
}

m_log_loose_files->draw("Log loose files");
if (ImGui::IsItemHovered()) {
ImGui::BeginTooltip();
ImGui::Text("Logs loaded loose files to <game_dir>/reframework_loose_files.txt");
ImGui::EndTooltip();
}

ImGui::Checkbox("Show recent files", &m_show_recent_files);

if (m_show_recent_files) {
Expand Down Expand Up @@ -297,6 +328,14 @@ bool LooseFileLoader::handle_path(const wchar_t* path, size_t hash) {
exists_on_disk = true;
}

if (m_log_accessed_files->value()) {
m_logger->info("{}", utility::narrow(path));
}

if (exists_on_disk && m_log_loose_files->value()) {
m_loose_file_logger->info("{}", utility::narrow(path));
}

m_seen_files.insert(hash); // Global
seen_files_local.insert(hash); // Thread local
++m_uncached_hits;
Expand Down
11 changes: 10 additions & 1 deletion src/mods/LooseFileLoader.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <deque>
#include <unordered_set>
#include <spdlog/spdlog.h>

#include <utility/FunctionHook.hpp>

Expand All @@ -12,6 +13,7 @@ class LooseFileLoader : public Mod {
static std::shared_ptr<LooseFileLoader>& get();

public:
LooseFileLoader();
std::string_view get_name() const override { return "LooseFileLoader"; }

std::optional<std::string> on_initialize() override;
Expand Down Expand Up @@ -51,10 +53,17 @@ class LooseFileLoader : public Mod {
std::unique_ptr<FunctionHook> m_path_to_hash_hook{nullptr};

ModToggle::Ptr m_enabled{ ModToggle::create(generate_name("Enabled")) };
ModToggle::Ptr m_log_accessed_files{ ModToggle::create(generate_name("LogAccessedFiles")) };
ModToggle::Ptr m_log_loose_files{ ModToggle::create(generate_name("LogLooseFiles")) };
bool m_show_recent_files{false}; // Not persistent because its for dev purposes
bool m_enable_file_cache{true};

std::shared_ptr<spdlog::logger> m_logger{nullptr};
std::shared_ptr<spdlog::logger> m_loose_file_logger{nullptr};

ValueList m_options{
*m_enabled
*m_enabled,
*m_log_accessed_files,
*m_log_loose_files,
};
};

0 comments on commit bb22652

Please sign in to comment.