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

View File

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

View File

@ -2,8 +2,8 @@ msgid ""
msgstr ""
"Project-Id-Version: epubgrep 0.4.0\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2021-05-30 19:04+0200\n"
"PO-Revision-Date: 2021-05-30 19:05+0200\n"
"POT-Creation-Date: 2021-05-31 19:11+0200\n"
"PO-Revision-Date: 2021-05-31 19:11+0200\n"
"Last-Translator: tastytea <tastytea@tastytea.de>\n"
"Language-Team: tastytea <https://schlomp.space/tastytea/epubgrep>\n"
"Language: de\n"
@ -17,93 +17,92 @@ msgstr ""
"X-Poedit-KeywordsList: translate\n"
"X-Poedit-SearchPath-0: .\n"
#: src/main.cpp:74 src/main.cpp:118 src/main.cpp:158 src/main.cpp:163
#: src/zip.cpp:184 src/zip.cpp:225 src/zip.cpp:232
msgid "ERROR: "
msgstr "FEHLER: "
#: src/log.cpp:66
msgid "WARNING"
msgstr "WARNUNG"
#: src/main.cpp:75
#: src/log.cpp:70
msgid "ERROR"
msgstr "FEHLER"
#: src/main.cpp:79
msgid " (while parsing options)"
msgstr " (während Optionen interpretiert wurden)"
#: src/main.cpp:119
#: src/main.cpp:117
msgid "Could not open {0:s}: {1:s}"
msgstr "Konnte {0:s} nicht öffnen: {1:s}"
#: src/main.cpp:166
msgid "Permission denied."
msgstr "Erlaubnis verweigert."
#: src/main.cpp:170
#: src/main.cpp:162
msgid "Probably permission denied."
msgstr "Vermutlich Erlaubnis verweigert."
#: src/main.cpp:174
#: src/main.cpp:164
msgid " (while opening {0:s})"
msgstr " (während {0:s} durchsucht wurde)"
#: src/options.cpp:48
#: src/options.cpp:49
msgid "Available options"
msgstr "Verfügbare Optionen"
#: src/options.cpp:52
#: src/options.cpp:53
msgid "Display this help and exit."
msgstr "Diese Hilfe ausgeben und beenden."
#: src/options.cpp:54
#: src/options.cpp:55
msgid "Display version information and exit."
msgstr "Versionsinformationen ausgeben und beenden."
#: src/options.cpp:56
#: src/options.cpp:57
msgid "PATTERN is a basic regular expression (default)."
msgstr "MUSTER ist eine „basic regular expression“ (standard)."
#: src/options.cpp:59
#: src/options.cpp:60
msgid "PATTERN is an 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."
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."
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."
msgstr "Unterschied zwischen Groß- und Kleinschreibung ignorieren."
#: src/options.cpp:69
#: src/options.cpp:70
msgid "PATTERN"
msgstr "MUSTER"
#: src/options.cpp:70
#: src/options.cpp:71
msgid "Use additional PATTERN for matching."
msgstr "Benutze zusätzliches MUSTER zum Abgleich."
#: src/options.cpp:72
#: src/options.cpp:73
msgid "Do not clean up text before searching."
msgstr "Nicht den Text vor dem suchen säubern."
#: src/options.cpp:74
#: src/options.cpp:75
msgid "NUMBER"
msgstr "ANZAHL"
#: src/options.cpp:75
#: src/options.cpp:76
msgid "Print NUMBER words of context around matches."
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."
msgstr "Schalte Farben und andere Dekorationen aus."
# Bezieht sich auf --no-filename.
#: src/options.cpp:79
#: src/options.cpp:80
msgid "WHICH"
msgstr "WELCHE"
#: src/options.cpp:80
#: src/options.cpp:81
msgid ""
"Suppress the mentioning of file names on output. WHICH is filesystem, in-"
"epub or all."
@ -111,24 +110,24 @@ msgstr ""
"Unterdrücke die Erwähnung der Dateinamens in der Ausgabe. WELCHE kann "
"filesystem, in-epub or all sein."
#: src/options.cpp:83
#: src/options.cpp:84
msgid "Read all files under each directory, recursively."
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."
msgstr ""
"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."
msgstr "Ignoriere Fehlermeldungen wegen des falschen Dateiformats."
#: src/options.cpp:130
#: src/options.cpp:131
msgid "Usage: epubgrep [OPTION]… PATTERN [FILE]…\n"
msgstr "Aufruf: epubgrep [OPTION]… MUSTER [DATEI]…\n"
#: src/options.cpp:132
#: src/options.cpp:133
msgid ""
"\n"
"You can access the full manual with `man epubgrep`.\n"
@ -136,7 +135,7 @@ msgstr ""
"\n"
"Du kannst mit `man epubgrep` auf das vollständige Handbuch zugreifen.\n"
#: src/options.cpp:139
#: src/options.cpp:140
msgid ""
"Copyright © 2021 tastytea <tastytea@tastytea.de>\n"
"License AGPL-3.0-only <https://gnu.org/licenses/agpl.html>.\n"
@ -152,32 +151,28 @@ msgstr ""
msgid " In {0:s}: \n"
msgstr " In {0:s}:\n"
#: src/zip.cpp:55 src/zip.cpp:82 src/zip.cpp:124
msgid "WARNING: "
msgstr "WARNUNG: "
#: src/zip.cpp:56 src/zip.cpp:83
#: src/zip.cpp:56 src/zip.cpp:84
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"
#: src/zip.cpp:103
#: src/zip.cpp:104
msgid "Could not read {0:s} in {1:s}."
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}."
msgstr "{0:s} nicht gefunden in {1:s}."
#: src/zip.cpp:148
#: src/zip.cpp:149
msgid "Could not open {0:s}."
msgstr "Konnte {0:s} nicht öffnen."
#: src/zip.cpp:162
#: src/zip.cpp:163
msgid "Could not close {0:s}."
msgstr "Konnte {0:s} nicht schließen."
# „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"
msgstr ""
"{0:s} ist beschädigt. Konnte „Spine“ nicht lesen. Überspringe Datei.\n"