diff --git a/src/files.cpp b/src/files.cpp index a937dd2..5f4c36e 100644 --- a/src/files.cpp +++ b/src/files.cpp @@ -17,6 +17,7 @@ #include "files.hpp" #include "fs-compat.hpp" +#include "log.hpp" #include #include @@ -42,6 +43,7 @@ std::vector list_recursive(const fs::path &directory, if (!path.is_directory()) { paths.emplace_back(path); + DEBUGLOG << "Added file: " << path; } } diff --git a/src/main.cpp b/src/main.cpp index a339f7f..b9f6cf1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -84,6 +84,7 @@ int main(int argc, char *argv[]) { log::enable_debug(); } + DEBUGLOG << "Options: " << opts; if (opts.help || opts.version) { @@ -97,8 +98,8 @@ int main(int argc, char *argv[]) { if (!opts.recursive && !opts.dereference_recursive) { - input_files.emplace_back(filepath); + DEBUGLOG << "Added to input_files: " << filepath; } else { @@ -109,12 +110,14 @@ int main(int argc, char *argv[]) opts.dereference_recursive)}; input_files.insert(input_files.end(), files_in_dir.begin(), files_in_dir.end()); + DEBUGLOG << "Added directory to input_files."; } catch (const fs::filesystem_error &e) { if (e.code().value() == 20) { // Is not a directory. input_files.emplace_back(filepath); + DEBUGLOG << "Added to input_files: " << filepath; continue; } @@ -203,15 +206,18 @@ int main(int argc, char *argv[]) auto n{static_cast(std::thread::hardware_concurrency())}; return static_cast(std::ceil(n / 2 + n / 4)); }()}; + DEBUGLOG << "max_threads = " << max_threads; for (const auto &filepath : input_files) { while (futurepool.size() >= max_threads) { + DEBUGLOG << "Attempting to clean up threads"; futures_cleanup(); } futurepool.emplace_back( std::async(std::launch::async, search_file, filepath)); + DEBUGLOG << "Launched new thread"; if (!matches_all.empty()) { @@ -221,6 +227,7 @@ int main(int argc, char *argv[]) matches_all.erase(matches_all.begin()); } } + DEBUGLOG << "Waiting for remaining threads to finish"; futures_cleanup(true); for (const auto &matches : matches_all) diff --git a/src/search.cpp b/src/search.cpp index 1be5c92..91bf4ae 100644 --- a/src/search.cpp +++ b/src/search.cpp @@ -18,9 +18,12 @@ #include "fs-compat.hpp" #include "helpers.hpp" +#include "log.hpp" #include "zip.hpp" #include +#include +#include // For compatibility with fmt 4. #include #include @@ -31,11 +34,14 @@ namespace epubgrep::search { +using fmt::format; using std::string; std::vector search(const fs::path &filepath, const std::string_view regex, const settings &opts) { + DEBUGLOG << format(R"(Starting search in {0:s} using regex "{1:s}")", + filepath, regex); boost::regex::flag_type flags{}; switch (opts.regex) @@ -75,6 +81,7 @@ std::vector search(const fs::path &filepath, for (const auto &entry : epub_filepaths) { + DEBUGLOG << "Processing " << entry; auto document{zip::read_file(filepath, entry)}; if (!opts.raw) { diff --git a/src/zip.cpp b/src/zip.cpp index 4e9153a..a8cd40a 100644 --- a/src/zip.cpp +++ b/src/zip.cpp @@ -61,6 +61,7 @@ std::vector list(const fs::path &filepath) continue; } toc.emplace_back(in_epub_filepath); + DEBUGLOG << "Found in file: " << in_epub_filepath; archive_read_data_skip(zipfile); } @@ -189,6 +190,7 @@ std::vector list_spine(const fs::path &filepath) std::vector spine_filepaths; if (!opf_file_path.empty()) { + DEBUGLOG << "Parsing " << opf_file_path; pugi::xml_document xml; const std::string opf_file{read_file(filepath, opf_file_path.string())}; const auto result{xml.load_buffer(&opf_file[0], opf_file.size())}; @@ -209,15 +211,20 @@ std::vector list_spine(const fs::path &filepath) { const auto &idref{itemref.attribute("idref").value()}; const auto &item{manifest.find_child_by_attribute("id", idref)}; - const std::string href{ + const auto href{ helpers::urldecode(item.attribute("href").value())}; - if (href[0] != '/') - { - spine_filepaths.emplace_back( - opf_file_path.parent_path() /= href); - continue; - } - spine_filepaths.emplace_back(href); + const auto real_href{ + [&href, &spine_filepaths, &opf_file_path] + { + if (href[0] != '/') + { + return spine_filepaths.emplace_back( + opf_file_path.parent_path() /= href); + } + return href; + }()}; + DEBUGLOG << "Found in spine: " << real_href; + spine_filepaths.emplace_back(real_href); } } else