Use logger for warnings end errors.

This commit is contained in:
tastytea 2021-05-31 19:10:54 +02:00
parent ac5b31f2d5
commit 11572d5b29
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
3 changed files with 92 additions and 101 deletions

View File

@ -16,9 +16,11 @@
#include "files.hpp" #include "files.hpp"
#include "fs-compat.hpp" #include "fs-compat.hpp"
#include "log.hpp"
#include "options.hpp" #include "options.hpp"
#include "output.hpp" #include "output.hpp"
#include "search.hpp" #include "search.hpp"
#include "version.hpp"
#include "zip.hpp" #include "zip.hpp"
#include <boost/locale/generator.hpp> #include <boost/locale/generator.hpp>
@ -48,8 +50,6 @@ int main(int argc, char *argv[])
using boost::locale::translate; using boost::locale::translate;
using fmt::format; using fmt::format;
using std::cerr;
using std::cout;
using std::string; using std::string;
using std::vector; using std::vector;
@ -61,8 +61,12 @@ int main(int argc, char *argv[])
locale_generator.add_messages_path("/usr/share/locale"); locale_generator.add_messages_path("/usr/share/locale");
locale_generator.add_messages_domain("epubgrep"); locale_generator.add_messages_domain("epubgrep");
std::locale::global(locale_generator("")); std::locale::global(locale_generator(""));
cout.imbue(std::locale()); std::cout.imbue(std::locale());
cerr.imbue(std::locale()); std::cerr.imbue(std::locale());
log::init();
auto &lg{log::logger::get()};
LOG(lg, log::sev::info) << "epubgrep " << version << " started.";
options::options opts; options::options opts;
try try
@ -71,8 +75,8 @@ int main(int argc, char *argv[])
} }
catch (std::exception &e) catch (std::exception &e)
{ // Exceptions we can't recover from or ones we don't know. { // Exceptions we can't recover from or ones we don't know.
cerr << translate("ERROR: ") << e.what(); LOG(lg, log::sev::error)
cerr << translate(" (while parsing options)") << '\n'; << e.what() << translate(" (while parsing options)");
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@ -109,10 +113,9 @@ int main(int argc, char *argv[])
continue; continue;
} }
cerr << translate("ERROR: ") LOG(lg, log::sev::error)
<< format(translate("Could not open {0:s}: {1:s}").str(), << format(translate("Could not open {0:s}: {1:s}").str(),
e.path1(), e.what()) e.path1(), e.what());
<< '\n';
return_code = EXIT_FAILURE; return_code = EXIT_FAILURE;
} }
} }
@ -130,8 +133,8 @@ int main(int argc, char *argv[])
vector<std::future<int>> futurepool; vector<std::future<int>> futurepool;
auto search_file{ auto search_file{
[&opts, &matches_all, &mutex_matches_all, [&opts, &matches_all, &mutex_matches_all, &search_settings,
&search_settings](const fs::path &filepath) &lg](const fs::path &filepath)
{ {
for (const auto &regex : opts.regexp) for (const auto &regex : opts.regexp)
{ {
@ -149,25 +152,17 @@ int main(int argc, char *argv[])
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
cerr << translate("ERROR: ") << e.what() << '\n'; LOG(lg, log::sev::error) << e.what();
return EXIT_FAILURE; return EXIT_FAILURE;
} }
catch (const std::ifstream::failure &e) catch (const std::ifstream::failure &e)
{ {
cerr << translate("ERROR: "); // std::ifstream seems to always return a generic error?
if (e.code() == std::errc::permission_denied) LOG(lg, log::sev::error)
{ << translate("Probably permission denied.") << " ("
cerr << translate("Permission denied."); << e.what() << ')'
} << format(translate(" (while opening {0:s})").str(),
else filepath);
{ // std::ifstream seems to always return a generic error?
cerr << translate("Probably permission denied.") << " ("
<< e.what() << ')';
}
cerr << format(translate(" (while opening {0:s})").str(),
filepath)
<< '\n';
} }
} }

View File

@ -18,6 +18,7 @@
#include "fs-compat.hpp" #include "fs-compat.hpp"
#include "helpers.hpp" #include "helpers.hpp"
#include "log.hpp"
#include <archive.h> #include <archive.h>
#include <archive_entry.h> #include <archive_entry.h>
@ -29,7 +30,6 @@
#include <cstdlib> #include <cstdlib>
#include <cstring> #include <cstring>
#include <fstream> #include <fstream>
#include <iostream>
#include <stdexcept> #include <stdexcept>
#include <string> #include <string>
#include <string_view> #include <string_view>
@ -40,7 +40,6 @@ namespace epubgrep::zip
using boost::locale::translate; using boost::locale::translate;
using fmt::format; using fmt::format;
using std::cerr;
std::vector<std::string> list(const fs::path &filepath) std::vector<std::string> list(const fs::path &filepath)
{ {
@ -53,12 +52,12 @@ std::vector<std::string> list(const fs::path &filepath)
const auto *in_epub_filepath{archive_entry_pathname_utf8(entry)}; const auto *in_epub_filepath{archive_entry_pathname_utf8(entry)};
if (in_epub_filepath == nullptr) if (in_epub_filepath == nullptr)
{ // If the encoding is broken, we skip the file. { // If the encoding is broken, we skip the file.
std::cerr << translate("WARNING: ") LOG(log::logger::get(), log::sev::warning)
<< format(translate("File in {0:s} is damaged. " << format(translate("File in {0:s} is damaged. "
"Skipping in-EPUB file.\n") "Skipping in-EPUB file.\n")
.str() .str()
.data(), .data(),
filepath); filepath);
continue; continue;
} }
toc.emplace_back(in_epub_filepath); toc.emplace_back(in_epub_filepath);
@ -73,6 +72,7 @@ std::vector<std::string> list(const fs::path &filepath)
std::string read_file(const fs::path &filepath, std::string_view entry_path) std::string read_file(const fs::path &filepath, std::string_view entry_path)
{ {
auto *zipfile{open_file(filepath)}; auto *zipfile{open_file(filepath)};
auto &lg{log::logger::get()};
struct archive_entry *entry{}; struct archive_entry *entry{};
while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK) while (archive_read_next_header(zipfile, &entry) == ARCHIVE_OK)
@ -80,12 +80,12 @@ std::string read_file(const fs::path &filepath, std::string_view entry_path)
const auto *path{archive_entry_pathname_utf8(entry)}; const auto *path{archive_entry_pathname_utf8(entry)};
if (path == nullptr) if (path == nullptr)
{ // If the encoding is broken, we skip the file. { // If the encoding is broken, we skip the file.
std::cerr << translate("WARNING: ") LOG(lg, log::sev::warning)
<< format(translate("File in {0:s} is damaged. " << format(translate("File in {0:s} is damaged. "
"Skipping in-EPUB file.\n") "Skipping in-EPUB file.\n")
.str() .str()
.data(), .data(),
filepath); filepath);
continue; continue;
} }
if (std::strcmp(path, entry_path.data()) == 0) if (std::strcmp(path, entry_path.data()) == 0)
@ -122,10 +122,10 @@ std::string read_file(const fs::path &filepath, std::string_view entry_path)
throw exception{e}; throw exception{e};
} }
cerr << translate("WARNING: ") LOG(lg, log::sev::warning)
<< format(translate("{0:s} not found in {1:s}.").str(), entry_path, << format(translate("{0:s} not found in {1:s}.").str(), entry_path,
filepath.string()) filepath.string())
<< '\n'; << '\n';
return {}; return {};
} }
@ -167,8 +167,10 @@ void close_file(struct archive *zipfile, const fs::path &filepath)
std::vector<std::string> list_spine(const fs::path &filepath) std::vector<std::string> list_spine(const fs::path &filepath)
{ {
auto &lg{log::logger::get()};
const fs::path opf_file_path{ const fs::path opf_file_path{
[&filepath] [&filepath, &lg]
{ {
pugi::xml_document xml; pugi::xml_document xml;
const std::string container{ const std::string container{
@ -182,7 +184,7 @@ std::vector<std::string> list_spine(const fs::path &filepath)
.attribute("full-path") .attribute("full-path")
.value(); .value();
} }
cerr << translate("ERROR: ") << result.description() << '\n'; LOG(lg, log::sev::error) << result.description() << '\n';
return ""; return "";
}()}; }()};
@ -223,19 +225,18 @@ std::vector<std::string> list_spine(const fs::path &filepath)
} }
else else
{ {
cerr << translate("ERROR: ") << "XML: " << result.description() LOG(lg, log::sev::error) << "XML: " << result.description() << '\n';
<< '\n';
} }
} }
if (opf_file_path.empty() || spine_filepaths.empty()) if (opf_file_path.empty() || spine_filepaths.empty())
{ {
std::cerr << translate("ERROR: ") LOG(lg, log::sev::error)
<< format(translate("{0:s} is damaged. Could not read spine. " << format(translate("{0:s} is damaged. Could not read spine. "
"Skipping file.\n") "Skipping file.\n")
.str() .str()
.data(), .data(),
filepath); filepath);
return {}; return {};
} }

View File

@ -2,8 +2,8 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: epubgrep 0.4.0\n" "Project-Id-Version: epubgrep 0.4.0\n"
"Report-Msgid-Bugs-To: \n" "Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-05-30 19:04+0200\n" "POT-Creation-Date: 2021-05-31 19:11+0200\n"
"PO-Revision-Date: 2021-05-30 19:05+0200\n" "PO-Revision-Date: 2021-05-31 19:11+0200\n"
"Last-Translator: tastytea <tastytea@tastytea.de>\n" "Last-Translator: tastytea <tastytea@tastytea.de>\n"
"Language-Team: tastytea <https://schlomp.space/tastytea/epubgrep>\n" "Language-Team: tastytea <https://schlomp.space/tastytea/epubgrep>\n"
"Language: de\n" "Language: de\n"
@ -17,93 +17,92 @@ msgstr ""
"X-Poedit-KeywordsList: translate\n" "X-Poedit-KeywordsList: translate\n"
"X-Poedit-SearchPath-0: .\n" "X-Poedit-SearchPath-0: .\n"
#: src/main.cpp:74 src/main.cpp:118 src/main.cpp:158 src/main.cpp:163 #: src/log.cpp:66
#: src/zip.cpp:184 src/zip.cpp:225 src/zip.cpp:232 msgid "WARNING"
msgid "ERROR: " msgstr "WARNUNG"
msgstr "FEHLER: "
#: src/main.cpp:75 #: src/log.cpp:70
msgid "ERROR"
msgstr "FEHLER"
#: src/main.cpp:79
msgid " (while parsing options)" msgid " (while parsing options)"
msgstr " (während Optionen interpretiert wurden)" msgstr " (während Optionen interpretiert wurden)"
#: src/main.cpp:119 #: src/main.cpp:117
msgid "Could not open {0:s}: {1:s}" msgid "Could not open {0:s}: {1:s}"
msgstr "Konnte {0:s} nicht öffnen: {1:s}" msgstr "Konnte {0:s} nicht öffnen: {1:s}"
#: src/main.cpp:166 #: src/main.cpp:162
msgid "Permission denied."
msgstr "Erlaubnis verweigert."
#: src/main.cpp:170
msgid "Probably permission denied." msgid "Probably permission denied."
msgstr "Vermutlich Erlaubnis verweigert." msgstr "Vermutlich Erlaubnis verweigert."
#: src/main.cpp:174 #: src/main.cpp:164
msgid " (while opening {0:s})" msgid " (while opening {0:s})"
msgstr " (während {0:s} durchsucht wurde)" msgstr " (während {0:s} durchsucht wurde)"
#: src/options.cpp:48 #: src/options.cpp:49
msgid "Available options" msgid "Available options"
msgstr "Verfügbare Optionen" msgstr "Verfügbare Optionen"
#: src/options.cpp:52 #: src/options.cpp:53
msgid "Display this help and exit." msgid "Display this help and exit."
msgstr "Diese Hilfe ausgeben und beenden." msgstr "Diese Hilfe ausgeben und beenden."
#: src/options.cpp:54 #: src/options.cpp:55
msgid "Display version information and exit." msgid "Display version information and exit."
msgstr "Versionsinformationen ausgeben und beenden." msgstr "Versionsinformationen ausgeben und beenden."
#: src/options.cpp:56 #: src/options.cpp:57
msgid "PATTERN is a basic regular expression (default)." msgid "PATTERN is a basic regular expression (default)."
msgstr "MUSTER ist eine „basic regular expression“ (standard)." msgstr "MUSTER ist eine „basic regular expression“ (standard)."
#: src/options.cpp:59 #: src/options.cpp:60
msgid "PATTERN is an extended regular expression." msgid "PATTERN is an extended regular expression."
msgstr "MUSTER ist eine „extended regular expression“." msgstr "MUSTER ist eine „extended regular expression“."
#: src/options.cpp:61 #: src/options.cpp:62
msgid "Use grep-variation of regular expressions with -G and -E." msgid "Use grep-variation of regular expressions with -G and -E."
msgstr "Benutze grep-Variante von regulären ausdrücken mit -G und -E." msgstr "Benutze grep-Variante von regulären ausdrücken mit -G und -E."
#: src/options.cpp:64 #: src/options.cpp:65
msgid "PATTERN is a Perl regular expression." msgid "PATTERN is a Perl regular expression."
msgstr "MUSTER ist ein regulärer Ausdruck, wie Perl ihn akzeptiert." msgstr "MUSTER ist ein regulärer Ausdruck, wie Perl ihn akzeptiert."
#: src/options.cpp:66 #: src/options.cpp:67
msgid "Ignore case distinctions in pattern and data." msgid "Ignore case distinctions in pattern and data."
msgstr "Unterschied zwischen Groß- und Kleinschreibung ignorieren." msgstr "Unterschied zwischen Groß- und Kleinschreibung ignorieren."
#: src/options.cpp:69 #: src/options.cpp:70
msgid "PATTERN" msgid "PATTERN"
msgstr "MUSTER" msgstr "MUSTER"
#: src/options.cpp:70 #: src/options.cpp:71
msgid "Use additional PATTERN for matching." msgid "Use additional PATTERN for matching."
msgstr "Benutze zusätzliches MUSTER zum Abgleich." msgstr "Benutze zusätzliches MUSTER zum Abgleich."
#: src/options.cpp:72 #: src/options.cpp:73
msgid "Do not clean up text before searching." msgid "Do not clean up text before searching."
msgstr "Nicht den Text vor dem suchen säubern." msgstr "Nicht den Text vor dem suchen säubern."
#: src/options.cpp:74 #: src/options.cpp:75
msgid "NUMBER" msgid "NUMBER"
msgstr "ANZAHL" msgstr "ANZAHL"
#: src/options.cpp:75 #: src/options.cpp:76
msgid "Print NUMBER words of context around matches." msgid "Print NUMBER words of context around matches."
msgstr "ANZAHL Wörter an Kontext um die Treffer herum ausgeben." msgstr "ANZAHL Wörter an Kontext um die Treffer herum ausgeben."
#: src/options.cpp:77 #: src/options.cpp:78
msgid "Turn off colors and other decorations." msgid "Turn off colors and other decorations."
msgstr "Schalte Farben und andere Dekorationen aus." msgstr "Schalte Farben und andere Dekorationen aus."
# Bezieht sich auf --no-filename. # Bezieht sich auf --no-filename.
#: src/options.cpp:79 #: src/options.cpp:80
msgid "WHICH" msgid "WHICH"
msgstr "WELCHE" msgstr "WELCHE"
#: src/options.cpp:80 #: src/options.cpp:81
msgid "" msgid ""
"Suppress the mentioning of file names on output. WHICH is filesystem, in-" "Suppress the mentioning of file names on output. WHICH is filesystem, in-"
"epub or all." "epub or all."
@ -111,24 +110,24 @@ msgstr ""
"Unterdrücke die Erwähnung der Dateinamens in der Ausgabe. WELCHE kann " "Unterdrücke die Erwähnung der Dateinamens in der Ausgabe. WELCHE kann "
"filesystem, in-epub or all sein." "filesystem, in-epub or all sein."
#: src/options.cpp:83 #: src/options.cpp:84
msgid "Read all files under each directory, recursively." msgid "Read all files under each directory, recursively."
msgstr "Lies rekursiv alle Dateien unter jedem Verzeichnis." msgstr "Lies rekursiv alle Dateien unter jedem Verzeichnis."
#: src/options.cpp:86 #: src/options.cpp:87
msgid "Read all files under each directory, recursively, following symlinks." msgid "Read all files under each directory, recursively, following symlinks."
msgstr "" msgstr ""
"Lies rekursiv alle Dateien unter jedem Verzeichnis und folge dabei symlinks." "Lies rekursiv alle Dateien unter jedem Verzeichnis und folge dabei symlinks."
#: src/options.cpp:89 #: src/options.cpp:90
msgid "Ignore errors about wrong file formats." msgid "Ignore errors about wrong file formats."
msgstr "Ignoriere Fehlermeldungen wegen des falschen Dateiformats." msgstr "Ignoriere Fehlermeldungen wegen des falschen Dateiformats."
#: src/options.cpp:130 #: src/options.cpp:131
msgid "Usage: epubgrep [OPTION]… PATTERN [FILE]…\n" msgid "Usage: epubgrep [OPTION]… PATTERN [FILE]…\n"
msgstr "Aufruf: epubgrep [OPTION]… MUSTER [DATEI]…\n" msgstr "Aufruf: epubgrep [OPTION]… MUSTER [DATEI]…\n"
#: src/options.cpp:132 #: src/options.cpp:133
msgid "" msgid ""
"\n" "\n"
"You can access the full manual with `man epubgrep`.\n" "You can access the full manual with `man epubgrep`.\n"
@ -136,7 +135,7 @@ msgstr ""
"\n" "\n"
"Du kannst mit `man epubgrep` auf das vollständige Handbuch zugreifen.\n" "Du kannst mit `man epubgrep` auf das vollständige Handbuch zugreifen.\n"
#: src/options.cpp:139 #: src/options.cpp:140
msgid "" msgid ""
"Copyright © 2021 tastytea <tastytea@tastytea.de>\n" "Copyright © 2021 tastytea <tastytea@tastytea.de>\n"
"License AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\n" "License AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\n"
@ -152,32 +151,28 @@ msgstr ""
msgid " In {0:s}: \n" msgid " In {0:s}: \n"
msgstr " In {0:s}:\n" msgstr " In {0:s}:\n"
#: src/zip.cpp:55 src/zip.cpp:82 src/zip.cpp:124 #: src/zip.cpp:56 src/zip.cpp:84
msgid "WARNING: "
msgstr "WARNUNG: "
#: src/zip.cpp:56 src/zip.cpp:83
msgid "File in {0:s} is damaged. Skipping in-EPUB file.\n" msgid "File in {0:s} is damaged. Skipping in-EPUB file.\n"
msgstr "Datei in {0:s} ist beschädigt. Überspringe Datei in der EPUB.\n" msgstr "Datei in {0:s} ist beschädigt. Überspringe Datei in der EPUB.\n"
#: src/zip.cpp:103 #: src/zip.cpp:104
msgid "Could not read {0:s} in {1:s}." msgid "Could not read {0:s} in {1:s}."
msgstr "Konnte {0:s} in {1:s} nicht lesen." msgstr "Konnte {0:s} in {1:s} nicht lesen."
#: src/zip.cpp:118 src/zip.cpp:125 #: src/zip.cpp:119 src/zip.cpp:126
msgid "{0:s} not found in {1:s}." msgid "{0:s} not found in {1:s}."
msgstr "{0:s} nicht gefunden in {1:s}." msgstr "{0:s} nicht gefunden in {1:s}."
#: src/zip.cpp:148 #: src/zip.cpp:149
msgid "Could not open {0:s}." msgid "Could not open {0:s}."
msgstr "Konnte {0:s} nicht öffnen." msgstr "Konnte {0:s} nicht öffnen."
#: src/zip.cpp:162 #: src/zip.cpp:163
msgid "Could not close {0:s}." msgid "Could not close {0:s}."
msgstr "Konnte {0:s} nicht schließen." msgstr "Konnte {0:s} nicht schließen."
# „Spine“ ist ein Fachbegriff, daher habe ich ihn nicht übersetzt. # „Spine“ ist ein Fachbegriff, daher habe ich ihn nicht übersetzt.
#: src/zip.cpp:233 #: src/zip.cpp:235
msgid "{0:s} is damaged. Could not read spine. Skipping file.\n" msgid "{0:s} is damaged. Could not read spine. Skipping file.\n"
msgstr "" msgstr ""
"{0:s} ist beschädigt. Konnte „Spine“ nicht lesen. Überspringe Datei.\n" "{0:s} ist beschädigt. Konnte „Spine“ nicht lesen. Überspringe Datei.\n"