From 22c09fff70dc8550831338ffba4774d1269cbd1e Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 12 Jan 2022 06:56:55 +0100 Subject: [PATCH 01/23] kdb: implement prototype of `kdb validate ` and do some improvements and bugfixes esp. regarding handling of warnings --- doc/news/_preparation_next_release.md | 2 +- src/plugins/range/range.c | 4 + src/tools/kdb/cmdline.hpp | 2 +- src/tools/kdb/coloredkdbio.hpp | 35 +++++-- src/tools/kdb/factory.hpp | 2 + src/tools/kdb/plugincheck.cpp | 17 +-- src/tools/kdb/plugincheck.hpp | 9 +- src/tools/kdb/validate.cpp | 142 ++++++++++++++++++++++++++ src/tools/kdb/validate.hpp | 61 +++++++++++ 9 files changed, 243 insertions(+), 31 deletions(-) create mode 100644 src/tools/kdb/validate.cpp create mode 100644 src/tools/kdb/validate.hpp diff --git a/doc/news/_preparation_next_release.md b/doc/news/_preparation_next_release.md index 0b99f49407d..d9c6acd964f 100644 --- a/doc/news/_preparation_next_release.md +++ b/doc/news/_preparation_next_release.md @@ -150,7 +150,7 @@ _(Michael Tucek)_ ## Tools -- <> +- Implement `kdb validate `, collect warnings and errors while kdb.get() and kdb.set(), see #3674 _(@flo91)_, _(@JakobWonisch)_ - <> - <> diff --git a/src/plugins/range/range.c b/src/plugins/range/range.c index 2f8d015609e..e4940667517 100644 --- a/src/plugins/range/range.c +++ b/src/plugins/range/range.c @@ -520,6 +520,10 @@ int elektraRangeGet (Plugin * handle ELEKTRA_UNUSED, KeySet * returned ELEKTRA_U Key * cur; while ((cur = ksNext (returned)) != NULL) { + /* skip parent namespaces that differ from the key namespace, + * otherwise every warning would get issued multiple times */ + if (keyGetNamespace (parentKey) != keyGetNamespace (cur)) continue; + const Key * meta = keyGetMeta (cur, "check/range"); if (meta) { diff --git a/src/tools/kdb/cmdline.hpp b/src/tools/kdb/cmdline.hpp index 521e342a900..8916880da96 100644 --- a/src/tools/kdb/cmdline.hpp +++ b/src/tools/kdb/cmdline.hpp @@ -13,7 +13,7 @@ * * To add an option there are 5 steps. * Beware not to introduce options which already have - * an meaning in one of the utilities. + * a meaning in one of the utilities. * Please always append the options in alphabetical order * with capitals later. */ diff --git a/src/tools/kdb/coloredkdbio.hpp b/src/tools/kdb/coloredkdbio.hpp index f262ccdef0a..915a2522f94 100644 --- a/src/tools/kdb/coloredkdbio.hpp +++ b/src/tools/kdb/coloredkdbio.hpp @@ -76,26 +76,41 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, // TODO: use C++ binding version of keyMeta KeySet meta (ckdb::ksDup (ckdb::keyMeta (error.getKey ()))); Key parent ("meta:/warnings", KEY_END); - auto warnings = meta.cut (parent); - - int nr = warnings.size (); - if (nr == 0) + KeySet warnings = meta.cut (parent); + if (warnings.size () == 0) { return os; } - os << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::MAGENTA) << " Sorry, " << nr + 1 << " warning" - << (!nr ? " was" : "s were") << " issued ;(" << getErrorColor (ANSI_COLOR::RESET) << std::endl; + // get number of warning + Key keyMetaWarnings = warnings.lookup ("meta:/warnings"); + int cntWarnings = 0; + if (!keyMetaWarnings.isNull () && keyMetaWarnings.isValid ()) + { + std::string strWarningCount = keyMetaWarnings.getString (); + + // skip leading '#' and '_' characters + size_t i; + for (i = 0; i < strWarningCount.length () && (strWarningCount[i] == '#' || strWarningCount[i] == '_'); i++) + ; + strWarningCount = strWarningCount.substr (i); + cntWarnings = std::stoi (strWarningCount) + 1; + } + + os << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::MAGENTA) << " Sorry, " << cntWarnings << " warning" + << ((cntWarnings == 1) ? " was" : "s were") << " issued ;(" << getErrorColor (ANSI_COLOR::RESET) << std::endl; + cntWarnings = 0; for (auto it = warnings.begin () + 1; it != warnings.end (); ++it) { auto name = it->getName (); if (it->isDirectBelow (parent)) { - os << "\tSorry, module " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::BLUE) - << warnings.get (name + "/module") << getErrorColor (ANSI_COLOR::RESET) - << " issued the warning " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::RED) - << warnings.get (name + "/number") << getErrorColor (ANSI_COLOR::RESET) << ":" << std::endl; + os << ' ' << ++cntWarnings << ": Module " << getErrorColor (ANSI_COLOR::BOLD) + << getErrorColor (ANSI_COLOR::BLUE) << warnings.get (name + "/module") + << getErrorColor (ANSI_COLOR::RESET) << " issued the warning " << getErrorColor (ANSI_COLOR::BOLD) + << getErrorColor (ANSI_COLOR::RED) << warnings.get (name + "/number") + << getErrorColor (ANSI_COLOR::RESET) << ":" << std::endl; os << "\t" << warnings.get (name + "/description") << ": " << warnings.get (name + "/reason") << std::endl; if (printVerbose) diff --git a/src/tools/kdb/factory.hpp b/src/tools/kdb/factory.hpp index dae8cb2d0df..ab989706a4f 100644 --- a/src/tools/kdb/factory.hpp +++ b/src/tools/kdb/factory.hpp @@ -62,6 +62,7 @@ #include #include #include +#include class Instancer { @@ -130,6 +131,7 @@ class Factory m_factory.insert (std::make_pair ("namespace", std::make_shared> ())); m_factory.insert (std::make_pair ("basename", std::make_shared> ())); m_factory.insert (std::make_pair ("dirname", std::make_shared> ())); + m_factory.insert (std::make_pair ("validate", std::make_shared> ())); } std::vector getPrettyCommands () const diff --git a/src/tools/kdb/plugincheck.cpp b/src/tools/kdb/plugincheck.cpp index d251849b6fc..a4356e0d6db 100644 --- a/src/tools/kdb/plugincheck.cpp +++ b/src/tools/kdb/plugincheck.cpp @@ -34,7 +34,7 @@ int printProblems (Key const & k, std::string const & action, int off) return (wo + eo * 2) << off; } -int doKDBcheck (bool force) +int doKDBcheck () { Key x; try @@ -54,18 +54,7 @@ int doKDBcheck (bool force) } ret += printProblems (a, "getting", 2); - if (force) - { - Key b ("/", KEY_END); - try - { - kdb.set (ks, b); - } - catch (...) - { - } - ret += printProblems (b, "setting", 4); - } + /* write checks now handled by 'kdb validate */ Key y; kdb.close (y); @@ -83,7 +72,7 @@ int PluginCheckCommand::execute (Cmdline const & cl) { if (cl.arguments.size () == 0) { - return doKDBcheck (cl.force); + return doKDBcheck (); } std::string name = cl.arguments[0]; diff --git a/src/tools/kdb/plugincheck.hpp b/src/tools/kdb/plugincheck.hpp index 637a92bdc8a..0efca51b428 100644 --- a/src/tools/kdb/plugincheck.hpp +++ b/src/tools/kdb/plugincheck.hpp @@ -22,7 +22,7 @@ class PluginCheckCommand : public Command virtual std::string getShortOptions () override { - return "fc"; + return "c"; } virtual std::string getSynopsis () override @@ -37,11 +37,10 @@ class PluginCheckCommand : public Command virtual std::string getLongHelpText () override { - return "If no arguments are given checks on key database\n" - "are done instead. Use -f to also do a write test\n" - "(might change configuration files!)\n" + return "If no arguments are given, checks on the key database\n" + "are done instead.\n" "\n" - "If a plugin name is given, checks will only be done with given plugin.\n" + "If a plugin name is given, checks will only be done with the given plugin.\n" "Use -c to pass options to the plugin.\n" "\n" "Please report any output caused by official plugins to https://www.libelektra.org\n"; diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp new file mode 100644 index 00000000000..1efc4b99916 --- /dev/null +++ b/src/tools/kdb/validate.cpp @@ -0,0 +1,142 @@ +/** + * @file + * + * @brief + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#include + +#include +#include +#include + +#include + +using namespace std; +using namespace kdb; + + +int ValidateCommand::execute (Cmdline const & cl) +{ + int argc = cl.arguments.size (); + if (argc != 1) + { + throw invalid_argument ("1 argument needed"); + } + + cout << "The given path was: " << cl.arguments[0] << endl; + + KeySet ksUnfiltered; + /* use the given cmd line argument as the start key */ + Key root = cl.createKey (0); + + /* if -f (force) was given, the namespace is kept + * and check-constraints in the spec:/ namespace + * are not considered --> the key can be set to a + * value that does not pass the validation criteria, + * otherwise a cascading key is created for the + * following kdb.get() */ + // Key parentKey = cl.getParentKey (root); + + // do not resume on any get errors + // otherwise the user might break + // the config + kdb.get (ksUnfiltered, root); + + stringstream streamWarnings; + printWarnings (streamWarnings, root, cl.verbose, cl.debug); + + string strWarnings = streamWarnings.str (); + if (strWarnings.empty ()) + { + cout << getFormattedSuccessString ("No warnings were issued! :)") << endl; + } + else + { + cerr << strWarnings; + root.clear (); + + if (cl.force) + { + cout << getFormattedInfoString ( + "Because -f was given, we now try to set the values " + "despite warnings during getting them...") + << endl; + + + root = cl.createKey (0); // use a fresh root key for kdb.set, so that no warnings from kdb.get remain + // cout << "Now no warnings should be printed: " << endl; + // printWarnings (cerr, root, cl.verbose, cl.debug); + // return 0; + } + else + { + cerr << getFormattedErrorString ( + "The validation was stopped because of warnings " + "while getting the values!") + << endl; + kdb.close (); + return 1; + } + } + + + // cout << "size of all keys in mount point: " << ksUnfiltered.size () << endl; + KeySet ksPart (ksUnfiltered.cut (root)); + // cout << "size of requested keys: " << ksPart.size () << endl; + + /* iterate over the result keys */ + // cout << "Iterating over keys:" << endl; + for (Key curKey : ksPart) + { + /* do lookup (needed for resolving cascading keys) */ + Key lookupKey = ksPart.lookup (curKey); + + if (lookupKey.isBinary ()) continue; // only validate string keys + + // cout << "1. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() + // << ", sync: " << lookupKey.needSync() << endl; + + /* change value (to enable sync flag */ + lookupKey.setString (lookupKey.getString () + "^"); + + + // cout << "2. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() + // << ", sync: " << lookupKey.needSync() << endl; + + /* change value back to original */ + std::string tmpStr = lookupKey.getString (); + + /* remove last char that was added in the previous step */ + tmpStr.pop_back (); + lookupKey.setString (tmpStr); + + // cout << "3. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() + // << ", sync: " << lookupKey.needSync() << endl; + } + /* write back values */ + kdb.set (ksPart, root); + + printWarnings (streamWarnings, root, cl.verbose, cl.debug); + printError (cerr, root, cl.verbose, cl.debug); + kdb.close (); + return 0; +} + + +std::string ValidateCommand::getFormattedErrorString (const std::string & str) +{ + return getErrorColor (ANSI_COLOR::BOLD) + getErrorColor (ANSI_COLOR::MAGENTA) + str + getErrorColor (ANSI_COLOR::RESET); +} + +std::string ValidateCommand::getFormattedSuccessString (const std::string & str) +{ + return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::GREEN) + str + getStdColor (ANSI_COLOR::RESET); +} + +std::string ValidateCommand::getFormattedInfoString (const std::string & str) +{ + return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::YELLOW) + str + getStdColor (ANSI_COLOR::RESET); +} diff --git a/src/tools/kdb/validate.hpp b/src/tools/kdb/validate.hpp new file mode 100644 index 00000000000..d5943f1f1ec --- /dev/null +++ b/src/tools/kdb/validate.hpp @@ -0,0 +1,61 @@ +/** + * @file + * + * @brief + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + */ + +#ifndef VALIDATE_HPP +#define VALIDATE_HPP + +#include "coloredkdbio.hpp" +#include +#include + +class ValidateCommand : public Command +{ + kdb::KDB kdb; + +public: + ValidateCommand () = default; + ~ValidateCommand () override = default; + + virtual std::string getShortOptions () override + { + return "f"; + } + + virtual std::string getSynopsis () override + { + return ""; + } + + virtual std::string getShortHelpText () override + { + return "Validate the values of keys below a given name."; + } + + virtual std::string getLongHelpText () override + { + return "This command is useful for validating configuration files against\n" + "their specifications.\n" + "For keys to be validated, they must contain the 'check'-metakeys\n" + "and the respective plugins for validation must be loaded\n" + "for the backend that was used while mounting.\n" + "If a validation is done while using 'kdb set'\n" + "the same validation is also done by 'kdb validate'\n" + "\n" + "Use -f to do a write test even if the previous read\n" + "from the key database has issued warnings."; + } + + virtual int execute (Cmdline const & cmdline) override; + +private: + std::string getFormattedErrorString (const std::string &); + std::string getFormattedInfoString (const std::string &); + std::string getFormattedSuccessString (const std::string &); +}; + +#endif From fb1bc285c9f41bcde4a0539208f230cd3d5ce4e0 Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 12 Jan 2022 07:22:36 +0100 Subject: [PATCH 02/23] libtools: WIP! - add draft for Error/Warning API (binding) for C++ currently mostly a wrapper to the implementation in /src/libs/highlevel/elektra_error.c not finished or in a working state yet! --- src/libs/tools/include/CMakeLists.txt | 7 +++ src/libs/tools/include/errors/errbase.hpp | 44 +++++++++++++ src/libs/tools/include/errors/error.hpp | 42 +++++++++++++ src/libs/tools/include/errors/warning.hpp | 30 +++++++++ src/libs/tools/src/errors/errbase.cpp | 77 +++++++++++++++++++++++ src/libs/tools/src/errors/error.cpp | 67 ++++++++++++++++++++ src/libs/tools/src/errors/warning.cpp | 23 +++++++ 7 files changed, 290 insertions(+) create mode 100644 src/libs/tools/include/errors/errbase.hpp create mode 100644 src/libs/tools/include/errors/error.hpp create mode 100644 src/libs/tools/include/errors/warning.hpp create mode 100644 src/libs/tools/src/errors/errbase.cpp create mode 100644 src/libs/tools/src/errors/error.cpp create mode 100644 src/libs/tools/src/errors/warning.cpp diff --git a/src/libs/tools/include/CMakeLists.txt b/src/libs/tools/include/CMakeLists.txt index fcb168b4330..292fb3bc91a 100644 --- a/src/libs/tools/include/CMakeLists.txt +++ b/src/libs/tools/include/CMakeLists.txt @@ -5,6 +5,13 @@ install ( DESTINATION include/${TARGET_INCLUDE_FOLDER} COMPONENT libelektra-dev) +file (GLOB ERROR_HDR_FILES errors/*.hpp) + +install ( + FILES ${ERROR_HDR_FILES} + DESTINATION include/${TARGET_INCLUDE_FOLDER}/errors + COMPONENT libelektra-dev) + file (GLOB HELPER_HDR_FILES helper/*.hpp) install ( diff --git a/src/libs/tools/include/errors/errbase.hpp b/src/libs/tools/include/errors/errbase.hpp new file mode 100644 index 00000000000..92a20a99454 --- /dev/null +++ b/src/libs/tools/include/errors/errbase.hpp @@ -0,0 +1,44 @@ +#ifndef ELEKTRA_ERRBASE_HPP +#define ELEKTRA_ERRBASE_HPP + +#include +#include +//#include "elektra/error.h" + +namespace kdb +{ + +namespace tools +{ + +/* common abstract class for warnings and errors */ +class ErrBase +{ +public: + ErrBase () = default; + ErrBase (const std::string & code, const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t line*/ long line); + explicit ErrBase (kdb::Key & errKey); + virtual ~ErrBase () = default; // pure virtual destructor + + /* setters */ + void setData (kdb::Key & errKey); + void setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, /*kdb::long_t*/ long line); + + /* getters */ + std::string errorCode (); + std::string description (); + std::string module (); + std::string file (); + long line (); //kdb::long_t line (); + /*ElektraError*/char* internalError (); + +protected: + /*ElektraError*/char* err = nullptr; +}; + + +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_ERRBASE_HPP diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp new file mode 100644 index 00000000000..8cd3d1fd583 --- /dev/null +++ b/src/libs/tools/include/errors/error.hpp @@ -0,0 +1,42 @@ +#ifndef ELEKTRA_ERROR_HPP +#define ELEKTRA_ERROR_HPP + +#include + +namespace kdb +{ + + +namespace tools +{ + + +class Error : public ErrBase +{ +public: + Error () : ErrBase {} {} + Error (const std::string & code, const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) + : ErrBase {code, description, module, file, line} {} + explicit Error (kdb::Key & errorKey) : ErrBase {errorKey} {} + ~Error () override; + + void addWarning (std::string code, std::string description, std::string module, std::string file, /*kdb::long_t*/long line); + void addWarning (Warning & warning); + void addWarning (/*ElektraError*/char* warning); + + /* getters */ + /*kdb::long_t*/long warningCount (); + + /* special types of errors and warnings */ + void setSemanticValidationError (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line); + void setSyntacticValidationError (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line); + +}; + +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_ERROR_HPP diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp new file mode 100644 index 00000000000..7b8355e5d85 --- /dev/null +++ b/src/libs/tools/include/errors/warning.hpp @@ -0,0 +1,30 @@ + +#ifndef ELEKTRA_WARNING_HPP +#define ELEKTRA_WARNING_HPP + +#include "errors/errbase.hpp" + +namespace kdb +{ + + +namespace tools +{ + +class Warning : public ErrBase +{ +public: + Warning (const std::string & code, const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) + : ErrBase {code, description, module, file, line} {} + ~Warning() override = default; + void setSemanticValidationWarning (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line); + void setSyntacticValidationWarning (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line); +}; + + +} // namespace tools +} // namespace kdb +#endif // ELEKTRA_WARNING_HPP diff --git a/src/libs/tools/src/errors/errbase.cpp b/src/libs/tools/src/errors/errbase.cpp new file mode 100644 index 00000000000..84c48e16b36 --- /dev/null +++ b/src/libs/tools/src/errors/errbase.cpp @@ -0,0 +1,77 @@ + +#include "errors/errbase.hpp" +//#include "kdbprivate.h" + +namespace kdb +{ + +namespace tools +{ + +ErrBase::ErrBase (const std::string & code, const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/ long line) +{ + setData(code, description, module, file, line); +} + +ErrBase::ErrBase (kdb::Key & errKey) +{ + setData (errKey); +} + +void ErrBase::setData (kdb::Key & errKey) +{ + if (err) + { + err = nullptr; + //elektraErrorReset (&err); + } + err = nullptr; //elektraErrorFromKey (errKey.getKey()); +} + + +void ErrBase::setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, /*kdb::long_t*/ long line) +{ + if (err) + { + err = nullptr; + //elektraErrorReset (&err); + } + // strings get duplicated by elektraErrorCreate + err = nullptr; //elektraErrorCreate (code.c_str(), description.c_str(), module.c_str(), file.c_str(), line); +} + +/* getters */ +std::string ErrBase::errorCode () +{ + return ""; //elektraErrorCode(err); +} + +std::string ErrBase::description () +{ + return ""; //elektraErrorDescription(err); +} + +/* TODO: maybe implement functions C (currently in highlevel-API (elektra_error.c) + * and call them like for code and description */ +std::string ErrBase::module () +{ + return ""; //err->module; +} + +std::string ErrBase::file () +{ + return ""; //err->file; +} + +/*kdb::long_t*/ long ErrBase::line () +{ + return 0; //err->line; +} + +/*ElektraError*/char* ErrBase::internalError () { + return nullptr; //err; +} + +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp new file mode 100644 index 00000000000..c911bf927dd --- /dev/null +++ b/src/libs/tools/src/errors/error.cpp @@ -0,0 +1,67 @@ + + +//#include "kdbprivate.h" +//#include "kdberrors.h" +#include + +namespace kdb +{ +namespace tools +{ + + + +Error::~Error () +{ + if (err) + { + err = nullptr; + //elektraErrorReset (&err); + } +} + + +void Error::addWarning (std::string code, std::string description, std::string module, std::string file, /*kdb::long_t*/long line) +{ + Warning w {std::move(code), std::move(description), std::move(module), std::move(file), line}; + addWarning (w); +} + +void Error::addWarning (/*ElektraError*/char* warning) +{ + if(!err) { + // create a dummy error key for only storing warnings + //err = ;//elektraErrorPureWarning(); + } + //elektraErrorAddWarning (err, warning); +} + +void Error::addWarning (Warning & warning) +{ + addWarning (warning.internalError()); +} + +/* getters */ +/*kdb::long_t*/long Error::warningCount () +{ + return 0; //err->warningCount; +} + + + +void Error::setSemanticValidationError (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) +{ + //setData(ELEKTRA_ERROR_VALIDATION_SEMANTIC, description, module, file, line); +} + +void Error::setSyntacticValidationError (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) +{ + //setData(ELEKTRA_ERROR_VALIDATION_SYNTACTIC, description, module, file, line); +} + + + +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp new file mode 100644 index 00000000000..2edef15d729 --- /dev/null +++ b/src/libs/tools/src/errors/warning.cpp @@ -0,0 +1,23 @@ +#include "errors/warning.hpp" +//#include "kdberrors.h" + +namespace kdb +{ + +namespace tools +{ + +void Warning::setSemanticValidationWarning (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) +{ + //setData(ELEKTRA_WARNING_VALIDATION_SEMANTIC, description, module, file, line); +} + +void Warning::setSyntacticValidationWarning (const std::string & description, const std::string & module, + const std::string & file, /*kdb::long_t*/long line) +{ + //setData(ELEKTRA_WARNING_VALIDATION_SYNTACTIC, description, module, file, line); +} + +} // namespace tools +} // namespace kdb \ No newline at end of file From 936daa0f45b65247e769786d20610900b128670f Mon Sep 17 00:00:00 2001 From: flo91 Date: Sat, 15 Jan 2022 08:43:07 +0100 Subject: [PATCH 03/23] libtools: Work on C++ Error/Warnings API, wrapper for elektra_error.c of the highlevel API --- src/libs/highlevel/symbols.map | 10 ++++ src/libs/tools/include/errors/errbase.hpp | 26 +++++---- src/libs/tools/include/errors/error.hpp | 44 ++++++++++----- src/libs/tools/include/errors/warning.hpp | 27 ++++++--- src/libs/tools/src/CMakeLists.txt | 2 +- src/libs/tools/src/errors/errbase.cpp | 57 +++++++++++-------- src/libs/tools/src/errors/error.cpp | 68 ++++++++++++++++------- src/libs/tools/src/errors/warning.cpp | 20 ++++--- src/tools/kdb/coloredkdbio.hpp | 2 +- 9 files changed, 170 insertions(+), 86 deletions(-) diff --git a/src/libs/highlevel/symbols.map b/src/libs/highlevel/symbols.map index 30678b18b16..8f47667971c 100644 --- a/src/libs/highlevel/symbols.map +++ b/src/libs/highlevel/symbols.map @@ -106,6 +106,16 @@ libelektra_0.9 { elektraFindReference; elektraFindReferenceArrayElement; elektraHelpKey; + + # elektra/error.h; + # added for C++ error api; + elektraErrorCode; + elektraErrorFromKey; + elektraErrorAddWarning; + elektraErrorCreate; + elektraErrorPureWarning; + elektraErrorAddWarning; + }; libelektraprivate_1.0 { diff --git a/src/libs/tools/include/errors/errbase.hpp b/src/libs/tools/include/errors/errbase.hpp index 92a20a99454..53789a34524 100644 --- a/src/libs/tools/include/errors/errbase.hpp +++ b/src/libs/tools/include/errors/errbase.hpp @@ -3,7 +3,7 @@ #include #include -//#include "elektra/error.h" +#include namespace kdb { @@ -11,34 +11,40 @@ namespace kdb namespace tools { -/* common abstract class for warnings and errors */ +namespace errors +{ + /* common abstract class for warnings and errors */ + class ErrBase { public: ErrBase () = default; - ErrBase (const std::string & code, const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t line*/ long line); + ErrBase (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); explicit ErrBase (kdb::Key & errKey); + explicit ErrBase (ElektraError *err); virtual ~ErrBase () = default; // pure virtual destructor /* setters */ void setData (kdb::Key & errKey); - void setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, /*kdb::long_t*/ long line); + void setData (ElektraError * err); + void setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); /* getters */ std::string errorCode (); std::string description (); std::string module (); std::string file (); - long line (); //kdb::long_t line (); - /*ElektraError*/char* internalError (); + kdb::long_t line (); + kdb::boolean_t isNull(); protected: - /*ElektraError*/char* err = nullptr; + ElektraError * err = nullptr; }; - +} // namespace errors } // namespace tools } // namespace kdb -#endif // ELEKTRA_ERRBASE_HPP +#endif // ELEKTRA_ERRBASE_HPP \ No newline at end of file diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 8cd3d1fd583..e9557390a09 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -1,41 +1,55 @@ #ifndef ELEKTRA_ERROR_HPP #define ELEKTRA_ERROR_HPP +#include +#include #include namespace kdb { - - namespace tools { +namespace errors +{ class Error : public ErrBase { public: - Error () : ErrBase {} {} - Error (const std::string & code, const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) - : ErrBase {code, description, module, file, line} {} - explicit Error (kdb::Key & errorKey) : ErrBase {errorKey} {} + Error () : ErrBase{} + { + } + Error (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) + : ErrBase{ code, description, module, file, line } + { + } + explicit Error (kdb::Key & errorKey) : ErrBase{ errorKey } + { + } + explicit Error (ElektraError *err) : ErrBase { err } {} ~Error () override; - void addWarning (std::string code, std::string description, std::string module, std::string file, /*kdb::long_t*/long line); + void addWarning (const std::string& code, const std::string& description, const std::string& module, const std::string& file, + kdb::long_t line); void addWarning (Warning & warning); - void addWarning (/*ElektraError*/char* warning); + void addWarning (ElektraError * warning); /* getters */ - /*kdb::long_t*/long warningCount (); + kdb::long_t warningCount (); + Warning getWarning(kdb::long_t index); + Warning copyWarning(kdb::long_t index); + /* special types of errors and warnings */ - void setSemanticValidationError (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line); - void setSyntacticValidationError (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line); + void setSemanticValidationError (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); + void setSyntacticValidationError (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); -}; +}; +} // namespace errors } // namespace tools } // namespace kdb diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp index 7b8355e5d85..e711fffd8c2 100644 --- a/src/libs/tools/include/errors/warning.hpp +++ b/src/libs/tools/include/errors/warning.hpp @@ -11,20 +11,29 @@ namespace kdb namespace tools { +namespace errors +{ + class Warning : public ErrBase { public: - Warning (const std::string & code, const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) - : ErrBase {code, description, module, file, line} {} - ~Warning() override = default; - void setSemanticValidationWarning (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line); - void setSyntacticValidationWarning (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line); + Warning (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) + : ErrBase { code, description, module, file, line } + { + } + Warning () = default; + explicit Warning (ElektraError *err) : ErrBase { err } {} + ~Warning () override = default; + void setSemanticValidationWarning (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); + void setSyntacticValidationWarning (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line); +private: + friend class Error; // Error has access to ElektraError *err (internal errors) of warnings }; - +} // namespace errors } // namespace tools } // namespace kdb #endif // ELEKTRA_WARNING_HPP diff --git a/src/libs/tools/src/CMakeLists.txt b/src/libs/tools/src/CMakeLists.txt index 5a17b10ca83..ac393ede041 100644 --- a/src/libs/tools/src/CMakeLists.txt +++ b/src/libs/tools/src/CMakeLists.txt @@ -23,7 +23,7 @@ set (__symbols_file "${CMAKE_CURRENT_SOURCE_DIR}/libelektratools-symbols.map") if (BUILD_SHARED) add_library (elektratools SHARED ${SOURCES}) - target_link_libraries (elektratools elektra-core elektra-kdb elektra-plugin elektra-ease elektra-meta) + target_link_libraries (elektratools elektra-core elektra-kdb elektra-plugin elektra-ease elektra-meta elektra-highlevel) set_target_properties ( elektratools diff --git a/src/libs/tools/src/errors/errbase.cpp b/src/libs/tools/src/errors/errbase.cpp index 84c48e16b36..5766b918390 100644 --- a/src/libs/tools/src/errors/errbase.cpp +++ b/src/libs/tools/src/errors/errbase.cpp @@ -1,17 +1,17 @@ +#include #include "errors/errbase.hpp" -//#include "kdbprivate.h" namespace kdb { - namespace tools { - -ErrBase::ErrBase (const std::string & code, const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/ long line) +namespace errors +{ +ErrBase::ErrBase (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) { - setData(code, description, module, file, line); + setData (code, description, module, file, line); } ErrBase::ErrBase (kdb::Key & errKey) @@ -19,59 +19,72 @@ ErrBase::ErrBase (kdb::Key & errKey) setData (errKey); } +ErrBase::ErrBase (ElektraError * err) +{ + this->err = err; +} + + +void ErrBase::setData (ElektraError * _err) +{ + this->err = _err; +} + void ErrBase::setData (kdb::Key & errKey) { if (err) { - err = nullptr; - //elektraErrorReset (&err); + elektraErrorReset (&err); } - err = nullptr; //elektraErrorFromKey (errKey.getKey()); + err = elektraErrorFromKey (errKey.getKey ()); } -void ErrBase::setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, /*kdb::long_t*/ long line) +void ErrBase::setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) { if (err) { - err = nullptr; - //elektraErrorReset (&err); + elektraErrorReset (&err); } // strings get duplicated by elektraErrorCreate - err = nullptr; //elektraErrorCreate (code.c_str(), description.c_str(), module.c_str(), file.c_str(), line); + err = elektraErrorCreate (code.c_str (), description.c_str (), module.c_str (), file.c_str (), line); } /* getters */ std::string ErrBase::errorCode () { - return ""; //elektraErrorCode(err); + elektraErrorReset (&err); + return elektraErrorCode (err); } std::string ErrBase::description () { - return ""; //elektraErrorDescription(err); + return elektraErrorDescription (err); } -/* TODO: maybe implement functions C (currently in highlevel-API (elektra_error.c) +/* TODO: maybe implement getter functions in C (currently in highlevel-API (elektra_error.c)) * and call them like for code and description */ std::string ErrBase::module () { - return ""; //err->module; + return err->module; } std::string ErrBase::file () { - return ""; //err->file; + return err->file; } -/*kdb::long_t*/ long ErrBase::line () +kdb::long_t ErrBase::line () { - return 0; //err->line; + return err->line; } -/*ElektraError*/char* ErrBase::internalError () { - return nullptr; //err; +kdb::boolean_t ErrBase::isNull () +{ + return (err == nullptr); } +} // namespace errors } // namespace tools } // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index c911bf927dd..57f46ae6ad3 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -1,67 +1,93 @@ -//#include "kdbprivate.h" -//#include "kdberrors.h" -#include +#include +#include +#include "errors/error.hpp" namespace kdb { namespace tools { - +namespace errors +{ Error::~Error () { if (err) { - err = nullptr; - //elektraErrorReset (&err); + elektraErrorReset (&err); } } -void Error::addWarning (std::string code, std::string description, std::string module, std::string file, /*kdb::long_t*/long line) +void Error::addWarning (const std::string & code, const std::string & description, const std::string & module, + const std::string & file, kdb::long_t line) { - Warning w {std::move(code), std::move(description), std::move(module), std::move(file), line}; + Warning w { code, description, module, file, line }; addWarning (w); } -void Error::addWarning (/*ElektraError*/char* warning) +void Error::addWarning (ElektraError * warning) { - if(!err) { + if (!err) + { // create a dummy error key for only storing warnings - //err = ;//elektraErrorPureWarning(); + setData (elektraErrorPureWarning ()); } - //elektraErrorAddWarning (err, warning); + elektraErrorAddWarning (err, warning); } void Error::addWarning (Warning & warning) { - addWarning (warning.internalError()); + addWarning (warning.err); } /* getters */ -/*kdb::long_t*/long Error::warningCount () +kdb::long_t Error::warningCount () { - return 0; //err->warningCount; + return err->warningCount; } +void Error::setSemanticValidationError (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) +{ + setData (ELEKTRA_ERROR_VALIDATION_SEMANTIC, description, module, file, line); +} -void Error::setSemanticValidationError (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) +void Error::setSyntacticValidationError (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) { - //setData(ELEKTRA_ERROR_VALIDATION_SEMANTIC, description, module, file, line); + setData (ELEKTRA_ERROR_VALIDATION_SYNTACTIC, description, module, file, line); } -void Error::setSyntacticValidationError (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) +/* Extract Warning from Error object + * Caution: No deep copy of the underlying ElektraError* err is created! + * The memory for it must be freed by the destructor of this Error object! + * The returned Warning must not be used after the Error object it originated from was destructed! + * This method can be used for conveniently iterating over the Warnings of an Error */ +Warning Error::getWarning(kdb::long_t index) { - //setData(ELEKTRA_ERROR_VALIDATION_SYNTACTIC, description, module, file, line); + return (err->warningCount>index) ? (Warning (err->warnings[index])) : Warning (); } +/* Copy a Warning from an Error Object, a deep copy of the underlying ElektraError *err is made. + * Can be used for adding it to another Error object, the memory for the Warning + * than gets freed by the Destructor of the other Error. */ +Warning Error::copyWarning(kdb::long_t index) +{ + if (err->warningCount > index) + { + return { err->code, err->description, err->module, err->file, err->line }; + } + else + { + return {}; + } +} +} // namespace errors } // namespace tools } // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp index 2edef15d729..92e949c8c97 100644 --- a/src/libs/tools/src/errors/warning.cpp +++ b/src/libs/tools/src/errors/warning.cpp @@ -1,23 +1,29 @@ + +#include #include "errors/warning.hpp" -//#include "kdberrors.h" + namespace kdb { namespace tools { +namespace errors +{ -void Warning::setSemanticValidationWarning (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) +void Warning::setSemanticValidationWarning (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) { - //setData(ELEKTRA_WARNING_VALIDATION_SEMANTIC, description, module, file, line); + setData(ELEKTRA_WARNING_VALIDATION_SEMANTIC, description, module, file, line); } -void Warning::setSyntacticValidationWarning (const std::string & description, const std::string & module, - const std::string & file, /*kdb::long_t*/long line) +void Warning::setSyntacticValidationWarning (const std::string & description, const std::string & module, const std::string & file, + kdb::long_t line) { - //setData(ELEKTRA_WARNING_VALIDATION_SYNTACTIC, description, module, file, line); + setData(ELEKTRA_WARNING_VALIDATION_SYNTACTIC, description, module, file, line); } + +} // namespace errors } // namespace tools } // namespace kdb \ No newline at end of file diff --git a/src/tools/kdb/coloredkdbio.hpp b/src/tools/kdb/coloredkdbio.hpp index 915a2522f94..55a4e932db9 100644 --- a/src/tools/kdb/coloredkdbio.hpp +++ b/src/tools/kdb/coloredkdbio.hpp @@ -82,7 +82,7 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, return os; } - // get number of warning + // get number of warnings Key keyMetaWarnings = warnings.lookup ("meta:/warnings"); int cntWarnings = 0; if (!keyMetaWarnings.isNull () && keyMetaWarnings.isValid ()) From 1eaebfcefb0170b74602de638098f0ce3d484202 Mon Sep 17 00:00:00 2001 From: flo91 Date: Sun, 16 Jan 2022 15:25:37 +0100 Subject: [PATCH 04/23] libtools: extend and refactor error/warning API, add some basic tests, add iterator functionality for Warnings to Error class --- src/libs/elektra/symbols.map | 21 ++ src/libs/highlevel/symbols.map | 10 - .../tools/include/errors/baseNotification.hpp | 56 ++++ src/libs/tools/include/errors/errbase.hpp | 50 ---- src/libs/tools/include/errors/error.hpp | 48 ++-- src/libs/tools/include/errors/errorTypes.hpp | 111 ++++++++ src/libs/tools/include/errors/warning.hpp | 25 +- .../tools/include/errors/warningTypes.hpp | 110 ++++++++ src/libs/tools/src/CMakeLists.txt | 2 +- .../tools/src/errors/baseNotification.cpp | 61 +++++ src/libs/tools/src/errors/errbase.cpp | 90 ------- src/libs/tools/src/errors/error.cpp | 76 +----- src/libs/tools/src/errors/errorTypes.cpp | 40 +++ src/libs/tools/src/errors/warning.cpp | 29 --- src/libs/tools/src/errors/warningTypes.cpp | 41 +++ src/libs/tools/tests/testtool_error.cpp | 241 ++++++++++++++++++ 16 files changed, 711 insertions(+), 300 deletions(-) create mode 100644 src/libs/tools/include/errors/baseNotification.hpp delete mode 100644 src/libs/tools/include/errors/errbase.hpp create mode 100644 src/libs/tools/include/errors/errorTypes.hpp create mode 100644 src/libs/tools/include/errors/warningTypes.hpp create mode 100644 src/libs/tools/src/errors/baseNotification.cpp delete mode 100644 src/libs/tools/src/errors/errbase.cpp create mode 100644 src/libs/tools/src/errors/errorTypes.cpp delete mode 100644 src/libs/tools/src/errors/warning.cpp create mode 100644 src/libs/tools/src/errors/warningTypes.cpp create mode 100644 src/libs/tools/tests/testtool_error.cpp diff --git a/src/libs/elektra/symbols.map b/src/libs/elektra/symbols.map index aa992f37570..53c0705667c 100644 --- a/src/libs/elektra/symbols.map +++ b/src/libs/elektra/symbols.map @@ -134,6 +134,27 @@ libelektra_1.0 { ELEKTRA_WARNING_VALIDATION_SEMANTIC; ELEKTRA_WARNING_VALIDATION_SYNTACTIC; + # export also names of errors and warnings + ELEKTRA_ERROR_CONFLICTING_STATE_NAME; + ELEKTRA_ERROR_INSTALLATION_NAME; + ELEKTRA_ERROR_INTERFACE_NAME; + ELEKTRA_ERROR_INTERNAL_NAME; + ELEKTRA_ERROR_OUT_OF_MEMORY_NAME; + ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME; + ELEKTRA_ERROR_RESOURCE_NAME; + ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME; + ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME; + + ELEKTRA_WARNING_CONFLICTING_STATE_NAME; + ELEKTRA_WARNING_INSTALLATION_NAME; + ELEKTRA_WARNING_INTERFACE_NAME; + ELEKTRA_WARNING_INTERNAL_NAME; + ELEKTRA_WARNING_OUT_OF_MEMORY_NAME; + ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME; + ELEKTRA_WARNING_RESOURCE_NAME; + ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME; + ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME; + # kdb.h elektraGOptsContract; elektraGOptsContractFromStrings; diff --git a/src/libs/highlevel/symbols.map b/src/libs/highlevel/symbols.map index 8f47667971c..30678b18b16 100644 --- a/src/libs/highlevel/symbols.map +++ b/src/libs/highlevel/symbols.map @@ -106,16 +106,6 @@ libelektra_0.9 { elektraFindReference; elektraFindReferenceArrayElement; elektraHelpKey; - - # elektra/error.h; - # added for C++ error api; - elektraErrorCode; - elektraErrorFromKey; - elektraErrorAddWarning; - elektraErrorCreate; - elektraErrorPureWarning; - elektraErrorAddWarning; - }; libelektraprivate_1.0 { diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp new file mode 100644 index 00000000000..5d356781684 --- /dev/null +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -0,0 +1,56 @@ +#ifndef ELEKTRA_BASENOTIFICATION_HPP +#define ELEKTRA_BASENOTIFICATION_HPP + +#include +#include +#include +#include // for kdb-types, code and description constants + +namespace kdb +{ +namespace tools +{ +namespace errors +{ +/* common abstract class for warnings and errors */ +/* Because warning and errors share the same data members, a method can accept a ErrBase argument and the caller + * can create an Error or a Warning based on the provided object. */ +class BaseNotification +{ +public: + /* setters */ + void setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line); + + /* get references (for getting and setting member values) */ + std::string & reason(); + std::string & module(); + std::string & file(); + kdb::long_t & line(); + + /* fixed values per Class, taken from C-makro definitions in /src/include/kdberrors.h */ + virtual std::string code() const = 0; + virtual std::string description() const = 0; + + /* string representation */ + friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb); + /* compare */ + friend bool operator== (const BaseNotification& lhs, const BaseNotification& rhs); + +protected: + BaseNotification () = default; + BaseNotification (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line); + + /* Can be overwritten by subclasses to change the text representation */ + std::ostream& toString (std::ostream& outputStream) const; + +private: + std::string m_reason; + std::string m_module; + std::string m_file; + kdb::long_t m_line = 0; +}; + +} // namespace errors +} // namespace tools +} // namespace kdb +#endif // ELEKTRA_BASENOTIFICATION_HPP \ No newline at end of file diff --git a/src/libs/tools/include/errors/errbase.hpp b/src/libs/tools/include/errors/errbase.hpp deleted file mode 100644 index 53789a34524..00000000000 --- a/src/libs/tools/include/errors/errbase.hpp +++ /dev/null @@ -1,50 +0,0 @@ -#ifndef ELEKTRA_ERRBASE_HPP -#define ELEKTRA_ERRBASE_HPP - -#include -#include -#include - -namespace kdb -{ - -namespace tools -{ - -namespace errors -{ - /* common abstract class for warnings and errors */ - -class ErrBase -{ -public: - ErrBase () = default; - ErrBase (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); - explicit ErrBase (kdb::Key & errKey); - explicit ErrBase (ElektraError *err); - virtual ~ErrBase () = default; // pure virtual destructor - - /* setters */ - void setData (kdb::Key & errKey); - void setData (ElektraError * err); - void setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); - - /* getters */ - std::string errorCode (); - std::string description (); - std::string module (); - std::string file (); - kdb::long_t line (); - kdb::boolean_t isNull(); - -protected: - ElektraError * err = nullptr; -}; - -} // namespace errors -} // namespace tools -} // namespace kdb - -#endif // ELEKTRA_ERRBASE_HPP \ No newline at end of file diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index e9557390a09..adfbba929ee 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -1,9 +1,8 @@ #ifndef ELEKTRA_ERROR_HPP #define ELEKTRA_ERROR_HPP -#include -#include -#include +#include +#include "errors/warning.hpp" namespace kdb { @@ -13,41 +12,30 @@ namespace tools namespace errors { -class Error : public ErrBase +class Error : public BaseNotification { public: - Error () : ErrBase{} - { - } - Error (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) - : ErrBase{ code, description, module, file, line } - { - } - explicit Error (kdb::Key & errorKey) : ErrBase{ errorKey } - { - } - explicit Error (ElektraError *err) : ErrBase { err } {} - ~Error () override; - - void addWarning (const std::string& code, const std::string& description, const std::string& module, const std::string& file, - kdb::long_t line); - void addWarning (Warning & warning); - void addWarning (ElektraError * warning); + + /* inherit constructors */ + using BaseNotification::BaseNotification; + + /* An Error can contain 0 to n warnings */ + void addWarning (Warning *warning); /* getters */ kdb::long_t warningCount (); - Warning getWarning(kdb::long_t index); - Warning copyWarning(kdb::long_t index); - - /* special types of errors and warnings */ - void setSemanticValidationError (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); - void setSyntacticValidationError (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); + /* iterator functionality */ + std::vector::iterator begin() { return warnings.begin(); } + std::vector::iterator end() { return warnings.end(); } + std::vector::const_iterator begin() const { return warnings.begin(); } + std::vector::const_iterator end() const { return warnings.begin(); } + std::vector::const_iterator cbegin() const { return warnings.cbegin(); } + std::vector::const_iterator cend() const { return warnings.cend(); } +private: + std::vector warnings; }; } // namespace errors } // namespace tools diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp new file mode 100644 index 00000000000..ed8e76ebfc4 --- /dev/null +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -0,0 +1,111 @@ + +#ifndef ELEKTRA_ERRORTYPES_HPP +#define ELEKTRA_ERRORTYPES_HPP + +#include "error.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +/* This file contains all concrete classes for the different Errors, based on the constants defined in /src/include/kdberrors.h */ + +class ResourceError : public Error +{ +public: + ResourceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class OutOfMemoryError : public Error +{ +public: + OutOfMemoryError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class InstallationError : public Error +{ +public: + InstallationError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + + +class InternalError: public Error +{ +public: + InternalError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class InterfaceError : public Error +{ +public: + InterfaceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class PluginMisbehaviorError : public Error +{ +public: + PluginMisbehaviorError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ConflictingStateError : public Error +{ +public: + ConflictingStateError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ValidationSyntacticError : public Error +{ +public: + ValidationSyntacticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ValidationSemanticError : public Error +{ +public: + ValidationSemanticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Error { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +} // namespace errors +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_ERRORTYPES_HPP diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp index e711fffd8c2..260f7eafaa6 100644 --- a/src/libs/tools/include/errors/warning.hpp +++ b/src/libs/tools/include/errors/warning.hpp @@ -2,35 +2,22 @@ #ifndef ELEKTRA_WARNING_HPP #define ELEKTRA_WARNING_HPP -#include "errors/errbase.hpp" +#include "baseNotification.hpp" namespace kdb { - - namespace tools { - namespace errors { -class Warning : public ErrBase +/* The warning class currently has no extra parts compared to the BaseNotification class, + * it's used for distiguishing Warnings and Errors by their type and allow to only add Warnings to Errors. */ +class Warning : public BaseNotification { public: - Warning (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) - : ErrBase { code, description, module, file, line } - { - } - Warning () = default; - explicit Warning (ElektraError *err) : ErrBase { err } {} - ~Warning () override = default; - void setSemanticValidationWarning (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); - void setSyntacticValidationWarning (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line); -private: - friend class Error; // Error has access to ElektraError *err (internal errors) of warnings + /* inherit constructors */ + using BaseNotification::BaseNotification; }; } // namespace errors diff --git a/src/libs/tools/include/errors/warningTypes.hpp b/src/libs/tools/include/errors/warningTypes.hpp new file mode 100644 index 00000000000..2a16ee7cdee --- /dev/null +++ b/src/libs/tools/include/errors/warningTypes.hpp @@ -0,0 +1,110 @@ + +#ifndef ELEKTRA_WARNINGTYPES_HPP +#define ELEKTRA_WARNINGTYPES_HPP + +#include "warning.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +/* This file contains all concrete classes for the different Warnings, based on the constants defined in /src/include/kdberrors.h */ + +class ResourceWarning : public Warning +{ +public: + ResourceWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class OutOfMemoryWarning : public Warning +{ +public: + OutOfMemoryWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class InstallationWarning : public Warning +{ +public: + InstallationWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class InternalWarning : public Warning +{ +public: + InternalWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class InterfaceWarning : public Warning +{ +public: + InterfaceWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class PluginMisbehaviorWarning : public Warning +{ +public: + PluginMisbehaviorWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ConflictingStateWarning : public Warning +{ +public: + ConflictingStateWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ValidationSyntacticWarning : public Warning +{ +public: + ValidationSyntacticWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +class ValidationSemanticWarning : public Warning +{ +public: + ValidationSemanticWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) + : Warning { reason, module, file, line} {} + + std::string code() const override; + std::string description() const override; +}; + +} // namespace errors +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_WARNINGTYPES_HPP diff --git a/src/libs/tools/src/CMakeLists.txt b/src/libs/tools/src/CMakeLists.txt index ac393ede041..5a17b10ca83 100644 --- a/src/libs/tools/src/CMakeLists.txt +++ b/src/libs/tools/src/CMakeLists.txt @@ -23,7 +23,7 @@ set (__symbols_file "${CMAKE_CURRENT_SOURCE_DIR}/libelektratools-symbols.map") if (BUILD_SHARED) add_library (elektratools SHARED ${SOURCES}) - target_link_libraries (elektratools elektra-core elektra-kdb elektra-plugin elektra-ease elektra-meta elektra-highlevel) + target_link_libraries (elektratools elektra-core elektra-kdb elektra-plugin elektra-ease elektra-meta) set_target_properties ( elektratools diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp new file mode 100644 index 00000000000..14cec9e5239 --- /dev/null +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -0,0 +1,61 @@ + +#include +#include +#include "errors/baseNotification.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +BaseNotification::BaseNotification (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) +{ + setData (reason, module, file, line); +} + +void BaseNotification::setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) +{ + this->m_reason = reason; + this->m_module = module; + this->m_file = file; + this->m_line = line; +} + +std::ostream& BaseNotification::toString (std::ostream & outputStream) const +{ + return outputStream << "Code: " << code() + << "\nDescription: " << description() + << "\nReason: " << m_reason + << "\nModule: " << m_module + << "\nFile: " << m_file + << "\nLine: " << std::to_string (m_line); +} + +std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb) +{ + eb.toString (outputStream); + return outputStream; +} + +bool operator== (const BaseNotification& lhs, const BaseNotification& rhs) +{ + return lhs.code() == rhs.code() + && lhs.description() == rhs.description() + && lhs.m_reason == rhs.m_reason + && lhs.m_module == rhs.m_module + && lhs.m_file == rhs.m_file + && lhs.m_line == rhs.m_line; +} + +/* getters */ +std::string& BaseNotification::reason() { return m_reason; } +std::string& BaseNotification::module() { return m_module; } +std::string& BaseNotification::file() { return m_file; } +kdb::long_t& BaseNotification::line() { return m_line; } + + +} // namespace errors +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/errbase.cpp b/src/libs/tools/src/errors/errbase.cpp deleted file mode 100644 index 5766b918390..00000000000 --- a/src/libs/tools/src/errors/errbase.cpp +++ /dev/null @@ -1,90 +0,0 @@ - -#include -#include "errors/errbase.hpp" - -namespace kdb -{ -namespace tools -{ -namespace errors -{ -ErrBase::ErrBase (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - setData (code, description, module, file, line); -} - -ErrBase::ErrBase (kdb::Key & errKey) -{ - setData (errKey); -} - -ErrBase::ErrBase (ElektraError * err) -{ - this->err = err; -} - - -void ErrBase::setData (ElektraError * _err) -{ - this->err = _err; -} - -void ErrBase::setData (kdb::Key & errKey) -{ - if (err) - { - elektraErrorReset (&err); - } - err = elektraErrorFromKey (errKey.getKey ()); -} - - -void ErrBase::setData (const std::string & code, const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - if (err) - { - elektraErrorReset (&err); - } - // strings get duplicated by elektraErrorCreate - err = elektraErrorCreate (code.c_str (), description.c_str (), module.c_str (), file.c_str (), line); -} - -/* getters */ -std::string ErrBase::errorCode () -{ - elektraErrorReset (&err); - return elektraErrorCode (err); -} - -std::string ErrBase::description () -{ - return elektraErrorDescription (err); -} - -/* TODO: maybe implement getter functions in C (currently in highlevel-API (elektra_error.c)) - * and call them like for code and description */ -std::string ErrBase::module () -{ - return err->module; -} - -std::string ErrBase::file () -{ - return err->file; -} - -kdb::long_t ErrBase::line () -{ - return err->line; -} - -kdb::boolean_t ErrBase::isNull () -{ - return (err == nullptr); -} - -} // namespace errors -} // namespace tools -} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index 57f46ae6ad3..b2f5142f892 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -1,8 +1,8 @@ -#include -#include + #include "errors/error.hpp" +#include namespace kdb { @@ -11,83 +11,17 @@ namespace tools namespace errors { - -Error::~Error () +void Error::addWarning (Warning *warning) { - if (err) - { - elektraErrorReset (&err); - } -} - - -void Error::addWarning (const std::string & code, const std::string & description, const std::string & module, - const std::string & file, kdb::long_t line) -{ - Warning w { code, description, module, file, line }; - addWarning (w); -} - -void Error::addWarning (ElektraError * warning) -{ - if (!err) - { - // create a dummy error key for only storing warnings - setData (elektraErrorPureWarning ()); - } - elektraErrorAddWarning (err, warning); -} - -void Error::addWarning (Warning & warning) -{ - addWarning (warning.err); + warnings.push_back (warning); } /* getters */ kdb::long_t Error::warningCount () { - return err->warningCount; -} - - -void Error::setSemanticValidationError (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - setData (ELEKTRA_ERROR_VALIDATION_SEMANTIC, description, module, file, line); + return warnings.size(); } -void Error::setSyntacticValidationError (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - setData (ELEKTRA_ERROR_VALIDATION_SYNTACTIC, description, module, file, line); -} - -/* Extract Warning from Error object - * Caution: No deep copy of the underlying ElektraError* err is created! - * The memory for it must be freed by the destructor of this Error object! - * The returned Warning must not be used after the Error object it originated from was destructed! - * This method can be used for conveniently iterating over the Warnings of an Error */ -Warning Error::getWarning(kdb::long_t index) -{ - return (err->warningCount>index) ? (Warning (err->warnings[index])) : Warning (); -} - -/* Copy a Warning from an Error Object, a deep copy of the underlying ElektraError *err is made. - * Can be used for adding it to another Error object, the memory for the Warning - * than gets freed by the Destructor of the other Error. */ -Warning Error::copyWarning(kdb::long_t index) -{ - if (err->warningCount > index) - { - return { err->code, err->description, err->module, err->file, err->line }; - } - else - { - return {}; - } -} - - } // namespace errors } // namespace tools } // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/errorTypes.cpp b/src/libs/tools/src/errors/errorTypes.cpp new file mode 100644 index 00000000000..4e84347a369 --- /dev/null +++ b/src/libs/tools/src/errors/errorTypes.cpp @@ -0,0 +1,40 @@ + +#include + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +std::string ResourceError::code() const { return ELEKTRA_ERROR_RESOURCE; } +std::string ResourceError::description() const { return ELEKTRA_ERROR_RESOURCE_NAME; } + +std::string OutOfMemoryError::code() const { return ELEKTRA_ERROR_OUT_OF_MEMORY; } +std::string OutOfMemoryError::description() const { return ELEKTRA_ERROR_OUT_OF_MEMORY_NAME; } + +std::string InstallationError::code() const { return ELEKTRA_ERROR_INSTALLATION; } +std::string InstallationError::description() const { return ELEKTRA_ERROR_INSTALLATION_NAME; } + +std::string InternalError::code() const { return ELEKTRA_ERROR_INTERNAL; } +std::string InternalError::description() const { return ELEKTRA_ERROR_INTERNAL_NAME; } + +std::string InterfaceError::code() const { return ELEKTRA_ERROR_INTERFACE; } +std::string InterfaceError::description() const { return ELEKTRA_ERROR_INTERFACE_NAME; } + +std::string PluginMisbehaviorError::code() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR; } +std::string PluginMisbehaviorError::description() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME; } + +std::string ConflictingStateError::code() const { return ELEKTRA_ERROR_CONFLICTING_STATE; } +std::string ConflictingStateError::description() const { return ELEKTRA_ERROR_CONFLICTING_STATE_NAME; } + +std::string ValidationSyntacticError::code() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC; } +std::string ValidationSyntacticError::description() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME; } + +std::string ValidationSemanticError::code() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC; } +std::string ValidationSemanticError::description() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME; } + +} // namespace errors +} // namespace tools +} // namespace kdb diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp deleted file mode 100644 index 92e949c8c97..00000000000 --- a/src/libs/tools/src/errors/warning.cpp +++ /dev/null @@ -1,29 +0,0 @@ - -#include -#include "errors/warning.hpp" - - -namespace kdb -{ - -namespace tools -{ -namespace errors -{ - -void Warning::setSemanticValidationWarning (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - setData(ELEKTRA_WARNING_VALIDATION_SEMANTIC, description, module, file, line); -} - -void Warning::setSyntacticValidationWarning (const std::string & description, const std::string & module, const std::string & file, - kdb::long_t line) -{ - setData(ELEKTRA_WARNING_VALIDATION_SYNTACTIC, description, module, file, line); -} - - -} // namespace errors -} // namespace tools -} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/warningTypes.cpp b/src/libs/tools/src/errors/warningTypes.cpp new file mode 100644 index 00000000000..b1d2573e202 --- /dev/null +++ b/src/libs/tools/src/errors/warningTypes.cpp @@ -0,0 +1,41 @@ + + +#include + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +std::string ResourceWarning::code() const { return ELEKTRA_WARNING_RESOURCE; } +std::string ResourceWarning::description() const { return ELEKTRA_WARNING_RESOURCE_NAME; } + +std::string OutOfMemoryWarning::code() const { return ELEKTRA_WARNING_OUT_OF_MEMORY; } +std::string OutOfMemoryWarning::description() const { return ELEKTRA_WARNING_OUT_OF_MEMORY_NAME; } + +std::string InstallationWarning::code() const { return ELEKTRA_WARNING_INSTALLATION; } +std::string InstallationWarning::description() const { return ELEKTRA_WARNING_INSTALLATION_NAME; } + +std::string InternalWarning::code() const { return ELEKTRA_WARNING_INTERNAL; } +std::string InternalWarning::description() const { return ELEKTRA_WARNING_INTERNAL_NAME; } + +std::string InterfaceWarning::code() const { return ELEKTRA_WARNING_INTERFACE; } +std::string InterfaceWarning::description() const { return ELEKTRA_WARNING_INTERFACE_NAME; } + +std::string PluginMisbehaviorWarning::code() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR; } +std::string PluginMisbehaviorWarning::description() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME; } + +std::string ConflictingStateWarning::code() const { return ELEKTRA_WARNING_CONFLICTING_STATE; } +std::string ConflictingStateWarning::description() const { return ELEKTRA_WARNING_CONFLICTING_STATE_NAME; } + +std::string ValidationSyntacticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC; } +std::string ValidationSyntacticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME; } + +std::string ValidationSemanticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC; } +std::string ValidationSemanticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME; } + +} // namespace errors +} // namespace tools +} // namespace kdb diff --git a/src/libs/tools/tests/testtool_error.cpp b/src/libs/tools/tests/testtool_error.cpp new file mode 100644 index 00000000000..53570d56bac --- /dev/null +++ b/src/libs/tools/tests/testtool_error.cpp @@ -0,0 +1,241 @@ +/** + * @file + * + * @brief Tests for the errors and warnings + * + * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) + * + */ + +#include +#include +#include + +TEST (Error, TestWarnings) +{ + /* Resource warning */ + std::cout << std::endl << "TEST WARNINGS:" << std::endl; + kdb::tools::errors::ResourceWarning resourceWarning ("resourceWarningReason", + "resourceWarningModule", + "resourceWarningFile", 0); + ASSERT_EQ(resourceWarning.code(), ELEKTRA_WARNING_RESOURCE); + ASSERT_EQ(resourceWarning.description(), ELEKTRA_WARNING_RESOURCE_NAME); + ASSERT_EQ(resourceWarning.reason(), "resourceWarningReason"); + ASSERT_EQ(resourceWarning.module(), "resourceWarningModule"); + ASSERT_EQ(resourceWarning.file(), "resourceWarningFile"); + ASSERT_EQ(resourceWarning.line(), 0); + + /* Out of memory warning */ + kdb::tools::errors::OutOfMemoryWarning outOfMemoryWarning ("outOfMemoryWarningReason", + "outOfMemoryWarningModule", + "outOfMemoryWarningFile", 1); + ASSERT_EQ(outOfMemoryWarning.code(), ELEKTRA_WARNING_OUT_OF_MEMORY); + ASSERT_EQ(outOfMemoryWarning.description(), ELEKTRA_WARNING_OUT_OF_MEMORY_NAME); + ASSERT_EQ(outOfMemoryWarning.reason(), "outOfMemoryWarningReason"); + ASSERT_EQ(outOfMemoryWarning.module(), "outOfMemoryWarningModule"); + ASSERT_EQ(outOfMemoryWarning.file(), "outOfMemoryWarningFile"); + ASSERT_EQ(outOfMemoryWarning.line(), 1); + + /* Installation warning */ + kdb::tools::errors::InstallationWarning installationWarning ("installationWarningReason", + "installationWarningModule", + "installationWarningFile", -1); + ASSERT_EQ(installationWarning.code(), ELEKTRA_WARNING_INSTALLATION); + ASSERT_EQ(installationWarning.description(), ELEKTRA_WARNING_INSTALLATION_NAME); + ASSERT_EQ(installationWarning.reason(), "installationWarningReason"); + ASSERT_EQ(installationWarning.module(), "installationWarningModule"); + ASSERT_EQ(installationWarning.file(), "installationWarningFile"); + ASSERT_EQ(installationWarning.line(), -1); + + /* Internal warning */ + kdb::tools::errors::InternalWarning internalWarning ("internalWarningReason", + "internalWarningModule", + "internalWarningFile", 999); + ASSERT_EQ(internalWarning.code(), ELEKTRA_WARNING_INTERNAL); + ASSERT_EQ(internalWarning.description(), ELEKTRA_WARNING_INTERNAL_NAME); + ASSERT_EQ(internalWarning.reason(), "internalWarningReason"); + ASSERT_EQ(internalWarning.module(), "internalWarningModule"); + ASSERT_EQ(internalWarning.file(), "internalWarningFile"); + ASSERT_EQ(internalWarning.line(), 999); + + /* Interface warning */ + kdb::tools::errors::InterfaceWarning interfaceWarning ("interfaceWarningReason", + "interfaceWarningModule", + "interfaceWarningFile", 2); + ASSERT_EQ(interfaceWarning.code(), ELEKTRA_WARNING_INTERFACE); + ASSERT_EQ(interfaceWarning.description(), ELEKTRA_WARNING_INTERFACE_NAME); + ASSERT_EQ(interfaceWarning.reason(), "interfaceWarningReason"); + ASSERT_EQ(interfaceWarning.module(), "interfaceWarningModule"); + ASSERT_EQ(interfaceWarning.file(), "interfaceWarningFile"); + ASSERT_EQ(interfaceWarning.line(), 2); + + /* PluginMisbehavior warning */ + kdb::tools::errors::PluginMisbehaviorWarning pluginMisbehaviorWarning ("pluginMisbehaviorWarningReason", + "pluginMisbehaviorWarningModule", + "pluginMisbehaviorWarningFile", 3); + ASSERT_EQ(pluginMisbehaviorWarning.code(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR); + ASSERT_EQ(pluginMisbehaviorWarning.description(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME); + ASSERT_EQ(pluginMisbehaviorWarning.reason(), "pluginMisbehaviorWarningReason"); + ASSERT_EQ(pluginMisbehaviorWarning.module(), "pluginMisbehaviorWarningModule"); + ASSERT_EQ(pluginMisbehaviorWarning.file(), "pluginMisbehaviorWarningFile"); + ASSERT_EQ(pluginMisbehaviorWarning.line(), 3); + + /* ConflictingStatie warning */ + kdb::tools::errors::ConflictingStateWarning conflictingStateWarning ("conflictingStateWarningReason", + "conflictingStateWarningModule", + "conflictingStateWarningFile", 4); + ASSERT_EQ(conflictingStateWarning.code(), ELEKTRA_WARNING_CONFLICTING_STATE); + ASSERT_EQ(conflictingStateWarning.description(), ELEKTRA_WARNING_CONFLICTING_STATE_NAME); + ASSERT_EQ(conflictingStateWarning.reason(), "conflictingStateWarningReason"); + ASSERT_EQ(conflictingStateWarning.module(), "conflictingStateWarningModule"); + ASSERT_EQ(conflictingStateWarning.file(), "conflictingStateWarningFile"); + ASSERT_EQ(conflictingStateWarning.line(), 4); + + /* ValidationSyntactic warning */ + kdb::tools::errors::ValidationSyntacticWarning validationSyntacticWarning ("validationSyntacticWarningReason", + "validationSyntacticWarningModule", + "validationSyntacticWarningFile", 5); + ASSERT_EQ(validationSyntacticWarning.code(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC); + ASSERT_EQ(validationSyntacticWarning.description(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME); + ASSERT_EQ(validationSyntacticWarning.reason(), "validationSyntacticWarningReason"); + ASSERT_EQ(validationSyntacticWarning.module(), "validationSyntacticWarningModule"); + ASSERT_EQ(validationSyntacticWarning.file(), "validationSyntacticWarningFile"); + ASSERT_EQ(validationSyntacticWarning.line(), 5); + + /* ValidationSemantic warning */ + kdb::tools::errors::ValidationSemanticWarning validationSemanticWarning ("validationSemanticWarningReason", + "validationSemanticWarningModule", + "validationSemanticWarningFile", 6); + ASSERT_EQ(validationSemanticWarning.code(), ELEKTRA_WARNING_VALIDATION_SEMANTIC); + ASSERT_EQ(validationSemanticWarning.description(), ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME); + ASSERT_EQ(validationSemanticWarning.reason(), "validationSemanticWarningReason"); + ASSERT_EQ(validationSemanticWarning.module(), "validationSemanticWarningModule"); + ASSERT_EQ(validationSemanticWarning.file(), "validationSemanticWarningFile"); + ASSERT_EQ(validationSemanticWarning.line(), 6); +} + +TEST (Error, TestErrors) +{ + /* Resource error */ + std::cout << std::endl << "TEST ERRORS:" << std::endl; + kdb::tools::errors::ResourceError resourceError ("resourceErrorReason", + "resourceErrorModule", + "resourceErrorFile", 0); + ASSERT_EQ(resourceError.code(), ELEKTRA_ERROR_RESOURCE); + ASSERT_EQ(resourceError.description(), ELEKTRA_ERROR_RESOURCE_NAME); + ASSERT_EQ(resourceError.reason(), "resourceErrorReason"); + ASSERT_EQ(resourceError.module(), "resourceErrorModule"); + ASSERT_EQ(resourceError.file(), "resourceErrorFile"); + ASSERT_EQ(resourceError.line(), 0); + + /* Out of memory error */ + kdb::tools::errors::OutOfMemoryError outOfMemoryError ("outOfMemoryErrorReason", + "outOfMemoryErrorModule", + "outOfMemoryErrorFile", 1); + ASSERT_EQ(outOfMemoryError.code(), ELEKTRA_ERROR_OUT_OF_MEMORY); + ASSERT_EQ(outOfMemoryError.description(), ELEKTRA_ERROR_OUT_OF_MEMORY_NAME); + ASSERT_EQ(outOfMemoryError.reason(), "outOfMemoryErrorReason"); + ASSERT_EQ(outOfMemoryError.module(), "outOfMemoryErrorModule"); + ASSERT_EQ(outOfMemoryError.file(), "outOfMemoryErrorFile"); + ASSERT_EQ(outOfMemoryError.line(), 1); + + /* Installation error */ + kdb::tools::errors::InstallationError installationError ("installationErrorReason", + "installationErrorModule", + "installationErrorFile", -1); + ASSERT_EQ(installationError.code(), ELEKTRA_ERROR_INSTALLATION); + ASSERT_EQ(installationError.description(), ELEKTRA_ERROR_INSTALLATION_NAME); + ASSERT_EQ(installationError.reason(), "installationErrorReason"); + ASSERT_EQ(installationError.module(), "installationErrorModule"); + ASSERT_EQ(installationError.file(), "installationErrorFile"); + ASSERT_EQ(installationError.line(), -1); + + /* Internal error */ + kdb::tools::errors::InternalError internalError ("internalErrorReason", + "internalErrorModule", + "internalErrorFile", 999); + ASSERT_EQ(internalError.code(), ELEKTRA_ERROR_INTERNAL); + ASSERT_EQ(internalError.description(), ELEKTRA_ERROR_INTERNAL_NAME); + ASSERT_EQ(internalError.reason(), "internalErrorReason"); + ASSERT_EQ(internalError.module(), "internalErrorModule"); + ASSERT_EQ(internalError.file(), "internalErrorFile"); + ASSERT_EQ(internalError.line(), 999); + + /* Interface error */ + kdb::tools::errors::InterfaceError interfaceError ("interfaceErrorReason", + "interfaceErrorModule", + "interfaceErrorFile", 2); + ASSERT_EQ(interfaceError.code(), ELEKTRA_ERROR_INTERFACE); + ASSERT_EQ(interfaceError.description(), ELEKTRA_ERROR_INTERFACE_NAME); + ASSERT_EQ(interfaceError.reason(), "interfaceErrorReason"); + ASSERT_EQ(interfaceError.module(), "interfaceErrorModule"); + ASSERT_EQ(interfaceError.file(), "interfaceErrorFile"); + ASSERT_EQ(interfaceError.line(), 2); + + /* PluginMisbehavior error */ + kdb::tools::errors::PluginMisbehaviorError pluginMisbehaviorError ("pluginMisbehaviorErrorReason", + "pluginMisbehaviorErrorModule", + "pluginMisbehaviorErrorFile", 3); + ASSERT_EQ(pluginMisbehaviorError.code(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR); + ASSERT_EQ(pluginMisbehaviorError.description(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME); + ASSERT_EQ(pluginMisbehaviorError.reason(), "pluginMisbehaviorErrorReason"); + ASSERT_EQ(pluginMisbehaviorError.module(), "pluginMisbehaviorErrorModule"); + ASSERT_EQ(pluginMisbehaviorError.file(), "pluginMisbehaviorErrorFile"); + ASSERT_EQ(pluginMisbehaviorError.line(), 3); + + /* ConflictingStatie error */ + kdb::tools::errors::ConflictingStateError conflictingStateError ("conflictingStateErrorReason", + "conflictingStateErrorModule", + "conflictingStateErrorFile", 4); + ASSERT_EQ(conflictingStateError.code(), ELEKTRA_ERROR_CONFLICTING_STATE); + ASSERT_EQ(conflictingStateError.description(), ELEKTRA_ERROR_CONFLICTING_STATE_NAME); + ASSERT_EQ(conflictingStateError.reason(), "conflictingStateErrorReason"); + ASSERT_EQ(conflictingStateError.module(), "conflictingStateErrorModule"); + ASSERT_EQ(conflictingStateError.file(), "conflictingStateErrorFile"); + ASSERT_EQ(conflictingStateError.line(), 4); + + /* ValidationSyntactic error */ + kdb::tools::errors::ValidationSyntacticError validationSyntacticError ("validationSyntacticErrorReason", + "validationSyntacticErrorModule", + "validationSyntacticErrorFile", 5); + ASSERT_EQ(validationSyntacticError.code(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC); + ASSERT_EQ(validationSyntacticError.description(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME); + ASSERT_EQ(validationSyntacticError.reason(), "validationSyntacticErrorReason"); + ASSERT_EQ(validationSyntacticError.module(), "validationSyntacticErrorModule"); + ASSERT_EQ(validationSyntacticError.file(), "validationSyntacticErrorFile"); + ASSERT_EQ(validationSyntacticError.line(), 5); + + /* ValidationSemantic error */ + kdb::tools::errors::ValidationSemanticError validationSemanticError ("validationSemanticErrorReason", + "validationSemanticErrorModule", + "validationSemanticErrorFile", 6); + ASSERT_EQ(validationSemanticError.code(), ELEKTRA_ERROR_VALIDATION_SEMANTIC); + ASSERT_EQ(validationSemanticError.description(), ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME); + ASSERT_EQ(validationSemanticError.reason(), "validationSemanticErrorReason"); + ASSERT_EQ(validationSemanticError.module(), "validationSemanticErrorModule"); + ASSERT_EQ(validationSemanticError.file(), "validationSemanticErrorFile"); + ASSERT_EQ(validationSemanticError.line(), 6); +} + +TEST (Error, ErrWarn) +{ + std::cout << std::endl << "TEST ERRORS WITH WARNINGS" << std::endl; + kdb::tools::errors::PluginMisbehaviorError pmE ("pmeReason", "pmeModule", "pmeFile", 100); + kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", 101); + kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", 102); + + pmE.addWarning (&csW); + pmE.addWarning (&ifW); + + ASSERT_EQ (pmE.warningCount(), 2); + + int i = 0; + for(kdb::tools::errors::Warning* w : pmE) { + std::cout << std::endl << "WARNING IN ERROR: " << *w << std::endl; + + if (i++ == 0) + ASSERT_EQ (*w, csW); + else + ASSERT_EQ (*w, ifW); + } +} \ No newline at end of file From 8b662a180f1c3d95cccb78b05972fa1f61ff6937 Mon Sep 17 00:00:00 2001 From: flo91 Date: Sun, 16 Jan 2022 20:21:53 +0100 Subject: [PATCH 05/23] libtools: warnings/errors API, add comparing of ojbects & cloning of warnigns Warnings get now cloned when they are stored in an Error object, add further tests --- .../tools/include/errors/baseNotification.hpp | 12 +- src/libs/tools/include/errors/error.hpp | 8 +- src/libs/tools/include/errors/errorTypes.hpp | 18 +++ src/libs/tools/include/errors/warning.hpp | 7 ++ .../tools/include/errors/warningTypes.hpp | 27 +++++ .../tools/src/errors/baseNotification.cpp | 36 ++++-- src/libs/tools/src/errors/error.cpp | 75 +++++++++++- src/libs/tools/src/errors/errorTypes.cpp | 64 ++++++++++ src/libs/tools/src/errors/warning.cpp | 19 +++ src/libs/tools/src/errors/warningTypes.cpp | 113 ++++++++++++++++++ src/libs/tools/tests/testtool_error.cpp | 53 +++++++- 11 files changed, 414 insertions(+), 18 deletions(-) create mode 100644 src/libs/tools/src/errors/warning.cpp diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index 5d356781684..8f3bf8e0963 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -21,11 +21,15 @@ class BaseNotification /* setters */ void setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line); - /* get references (for getting and setting member values) */ + /* get references (for setting and getting member values) */ std::string & reason(); std::string & module(); std::string & file(); kdb::long_t & line(); + const std::string & reason () const; + const std::string & module () const; + const std::string & file () const; + const kdb::long_t & line () const; /* fixed values per Class, taken from C-makro definitions in /src/include/kdberrors.h */ virtual std::string code() const = 0; @@ -34,7 +38,8 @@ class BaseNotification /* string representation */ friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb); /* compare */ - friend bool operator== (const BaseNotification& lhs, const BaseNotification& rhs); + bool operator== (const BaseNotification& other) const; + bool operator!= (const BaseNotification& other) const; protected: BaseNotification () = default; @@ -43,6 +48,9 @@ class BaseNotification /* Can be overwritten by subclasses to change the text representation */ std::ostream& toString (std::ostream& outputStream) const; + /* for supporting polymorphism in comparisons */ + virtual bool compare(const BaseNotification& other) const; + private: std::string m_reason; std::string m_module; diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index adfbba929ee..44618c17ae7 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -18,9 +18,10 @@ class Error : public BaseNotification /* inherit constructors */ using BaseNotification::BaseNotification; + ~Error(); /* An Error can contain 0 to n warnings */ - void addWarning (Warning *warning); + void addWarning (Warning & warning); /* getters */ kdb::long_t warningCount (); @@ -33,9 +34,14 @@ class Error : public BaseNotification std::vector::const_iterator cbegin() const { return warnings.cbegin(); } std::vector::const_iterator cend() const { return warnings.cend(); } + /* get warning by index */ + Warning& operator[](int index); private: std::vector warnings; + +protected: + bool compare(const BaseNotification& other) const override; }; } // namespace errors } // namespace tools diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp index ed8e76ebfc4..3e487396d95 100644 --- a/src/libs/tools/include/errors/errorTypes.hpp +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -21,6 +21,8 @@ class ResourceError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class OutOfMemoryError : public Error @@ -31,6 +33,8 @@ class OutOfMemoryError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class InstallationError : public Error @@ -41,6 +45,8 @@ class InstallationError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; @@ -52,6 +58,8 @@ class InternalError: public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class InterfaceError : public Error @@ -62,6 +70,8 @@ class InterfaceError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class PluginMisbehaviorError : public Error @@ -72,6 +82,8 @@ class PluginMisbehaviorError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ConflictingStateError : public Error @@ -82,6 +94,8 @@ class ConflictingStateError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ValidationSyntacticError : public Error @@ -92,6 +106,8 @@ class ValidationSyntacticError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ValidationSemanticError : public Error @@ -102,6 +118,8 @@ class ValidationSemanticError : public Error std::string code() const override; std::string description() const override; +private: + bool compare(const BaseNotification& other) const final; }; } // namespace errors diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp index 260f7eafaa6..22320f9e3dd 100644 --- a/src/libs/tools/include/errors/warning.hpp +++ b/src/libs/tools/include/errors/warning.hpp @@ -18,6 +18,13 @@ class Warning : public BaseNotification public: /* inherit constructors */ using BaseNotification::BaseNotification; + virtual Warning* clone() const = 0; + + /* needed for freeing the elements of the Warning-container in Error-class */ + virtual ~Warning () = default; + +protected: + bool compare(const BaseNotification& other) const override; }; } // namespace errors diff --git a/src/libs/tools/include/errors/warningTypes.hpp b/src/libs/tools/include/errors/warningTypes.hpp index 2a16ee7cdee..9fd5af6237c 100644 --- a/src/libs/tools/include/errors/warningTypes.hpp +++ b/src/libs/tools/include/errors/warningTypes.hpp @@ -21,6 +21,9 @@ class ResourceWarning : public Warning std::string code() const override; std::string description() const override; + ResourceWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class OutOfMemoryWarning : public Warning @@ -31,6 +34,9 @@ class OutOfMemoryWarning : public Warning std::string code() const override; std::string description() const override; + OutOfMemoryWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class InstallationWarning : public Warning @@ -41,6 +47,9 @@ class InstallationWarning : public Warning std::string code() const override; std::string description() const override; + InstallationWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class InternalWarning : public Warning @@ -51,6 +60,9 @@ class InternalWarning : public Warning std::string code() const override; std::string description() const override; + InternalWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class InterfaceWarning : public Warning @@ -61,6 +73,9 @@ class InterfaceWarning : public Warning std::string code() const override; std::string description() const override; + InterfaceWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class PluginMisbehaviorWarning : public Warning @@ -71,6 +86,9 @@ class PluginMisbehaviorWarning : public Warning std::string code() const override; std::string description() const override; + PluginMisbehaviorWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ConflictingStateWarning : public Warning @@ -81,6 +99,9 @@ class ConflictingStateWarning : public Warning std::string code() const override; std::string description() const override; + ConflictingStateWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ValidationSyntacticWarning : public Warning @@ -91,6 +112,9 @@ class ValidationSyntacticWarning : public Warning std::string code() const override; std::string description() const override; + ValidationSyntacticWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; class ValidationSemanticWarning : public Warning @@ -101,6 +125,9 @@ class ValidationSemanticWarning : public Warning std::string code() const override; std::string description() const override; + ValidationSemanticWarning* clone() const override; +private: + bool compare(const BaseNotification& other) const final; }; } // namespace errors diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index 14cec9e5239..6617722b768 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -23,6 +23,7 @@ void BaseNotification::setData (const std::string & reason, const std::string & this->m_line = line; } +/* String representation */ std::ostream& BaseNotification::toString (std::ostream & outputStream) const { return outputStream << "Code: " << code() @@ -39,22 +40,41 @@ std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb return outputStream; } -bool operator== (const BaseNotification& lhs, const BaseNotification& rhs) +/* Comparison */ +bool BaseNotification::compare(const BaseNotification& other ELEKTRA_UNUSED) const { - return lhs.code() == rhs.code() - && lhs.description() == rhs.description() - && lhs.m_reason == rhs.m_reason - && lhs.m_module == rhs.m_module - && lhs.m_file == rhs.m_file - && lhs.m_line == rhs.m_line; + /* Can be overloaded by subclasses to check additional constraints. + * At least the types of the two objects that get compared should be checked for equality! */ + return true; } -/* getters */ +bool BaseNotification::operator== (const BaseNotification& other) const +{ + return code() == other.code() + && description() == other.description() + && reason() == other.reason() + && module() == other.module() + && file() == other.file() + && line() == other.line() + && this->compare(other); +} + +bool BaseNotification::operator!= (const BaseNotification& other) const +{ + return !(*this == other); +} + +/* setters */ std::string& BaseNotification::reason() { return m_reason; } std::string& BaseNotification::module() { return m_module; } std::string& BaseNotification::file() { return m_file; } kdb::long_t& BaseNotification::line() { return m_line; } +/* getters */ +const std::string & BaseNotification::reason () const { return m_reason; } +const std::string & BaseNotification::module () const { return m_module; } +const std::string & BaseNotification::file () const { return m_file; } +const kdb::long_t & BaseNotification::line () const { return m_line; } } // namespace errors } // namespace tools diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index b2f5142f892..0afda04982c 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -1,8 +1,7 @@ - - +#include "iostream" #include "errors/error.hpp" -#include + namespace kdb { @@ -11,9 +10,13 @@ namespace tools namespace errors { -void Error::addWarning (Warning *warning) + +/* copy warning to make it independent from source object * + * if the same warning gets added to two different errors, they can be changed independently */ +void Error::addWarning (Warning & warning) { - warnings.push_back (warning); + /* TODO: Decide if we should create copies or store the original warnings */ + warnings.push_back (warning.clone()); } /* getters */ @@ -22,6 +25,68 @@ kdb::long_t Error::warningCount () return warnings.size(); } +Warning& Error::operator[](int index) +{ + if(index >= warningCount()) + { + throw std::out_of_range ("The warning with index " + std::to_string (index) + " was accessed, but there are only " + + std::to_string(warningCount()) + " warnings stored in the Error-object!"); + } + else + { + return (*(warnings[index])); + } + +} + + +bool Error::compare(const BaseNotification& other) const +{ + /* comparison of data fields is done by operator== in BaseNotification class */ + const Error* pOtherError = dynamic_cast (&other); + if(!pOtherError || warnings.size() != pOtherError->warnings.size()) + { + return false; + } + else + { + /* compare warnings */ + for (Warning *w : warnings) + { + /*TODO: Decide if ordering of warnings should be considered for equality. + * Currently two errors are equal if they contain the same warnings (compared by member values), + * even if they have different orders in the internal vector.*/ + + /*TODO: Currently copies of warnings are stored by the addWarning(Warning&) method. + * If we decide to store the original warnings, then we have to decide if + * two different Warnings (not the same address in mem) are considered equal + * if the member values are equal.*/ + bool equalWarningFound = false; + for (Warning *ow : pOtherError->warnings) + { + if (*w == *ow) + { + equalWarningFound = true; + break; + } + } + + if (!equalWarningFound) + { + return false; + } + } + return true; + } + +} +Error::~Error () +{ + for (Warning *w : warnings) + delete w; + +} + } // namespace errors } // namespace tools } // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/errorTypes.cpp b/src/libs/tools/src/errors/errorTypes.cpp index 4e84347a369..e06fb391408 100644 --- a/src/libs/tools/src/errors/errorTypes.cpp +++ b/src/libs/tools/src/errors/errorTypes.cpp @@ -10,30 +10,94 @@ namespace errors std::string ResourceError::code() const { return ELEKTRA_ERROR_RESOURCE; } std::string ResourceError::description() const { return ELEKTRA_ERROR_RESOURCE_NAME; } +bool ResourceError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} + std::string OutOfMemoryError::code() const { return ELEKTRA_ERROR_OUT_OF_MEMORY; } std::string OutOfMemoryError::description() const { return ELEKTRA_ERROR_OUT_OF_MEMORY_NAME; } +bool OutOfMemoryError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string InstallationError::code() const { return ELEKTRA_ERROR_INSTALLATION; } std::string InstallationError::description() const { return ELEKTRA_ERROR_INSTALLATION_NAME; } +bool InstallationError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string InternalError::code() const { return ELEKTRA_ERROR_INTERNAL; } std::string InternalError::description() const { return ELEKTRA_ERROR_INTERNAL_NAME; } +bool InternalError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string InterfaceError::code() const { return ELEKTRA_ERROR_INTERFACE; } std::string InterfaceError::description() const { return ELEKTRA_ERROR_INTERFACE_NAME; } +bool InterfaceError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string PluginMisbehaviorError::code() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR; } std::string PluginMisbehaviorError::description() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME; } +bool PluginMisbehaviorError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string ConflictingStateError::code() const { return ELEKTRA_ERROR_CONFLICTING_STATE; } std::string ConflictingStateError::description() const { return ELEKTRA_ERROR_CONFLICTING_STATE_NAME; } +bool ConflictingStateError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string ValidationSyntacticError::code() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC; } std::string ValidationSyntacticError::description() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME; } +bool ValidationSyntacticError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} std::string ValidationSemanticError::code() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC; } std::string ValidationSemanticError::description() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME; } +bool ValidationSemanticError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} } // namespace errors } // namespace tools diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp new file mode 100644 index 00000000000..a4f6063af68 --- /dev/null +++ b/src/libs/tools/src/errors/warning.cpp @@ -0,0 +1,19 @@ + +#include "errors/warning.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +bool Warning::compare(const BaseNotification& other) const +{ + /* comparison of data fields is done by operator== in BaseNotification class */ + return (dynamic_cast (&other)) ? true : false; +} + +} // namespace errors +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/warningTypes.cpp b/src/libs/tools/src/errors/warningTypes.cpp index b1d2573e202..8d6b97b0a15 100644 --- a/src/libs/tools/src/errors/warningTypes.cpp +++ b/src/libs/tools/src/errors/warningTypes.cpp @@ -11,30 +11,143 @@ namespace errors std::string ResourceWarning::code() const { return ELEKTRA_WARNING_RESOURCE; } std::string ResourceWarning::description() const { return ELEKTRA_WARNING_RESOURCE_NAME; } +bool ResourceWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +ResourceWarning* ResourceWarning::clone () const +{ + return new ResourceWarning(*this); +} std::string OutOfMemoryWarning::code() const { return ELEKTRA_WARNING_OUT_OF_MEMORY; } std::string OutOfMemoryWarning::description() const { return ELEKTRA_WARNING_OUT_OF_MEMORY_NAME; } +bool OutOfMemoryWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +OutOfMemoryWarning* OutOfMemoryWarning::clone () const +{ + return new OutOfMemoryWarning(*this); +} + + std::string InstallationWarning::code() const { return ELEKTRA_WARNING_INSTALLATION; } std::string InstallationWarning::description() const { return ELEKTRA_WARNING_INSTALLATION_NAME; } +bool InstallationWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +InstallationWarning* InstallationWarning::clone () const +{ + return new InstallationWarning(*this); +} + + std::string InternalWarning::code() const { return ELEKTRA_WARNING_INTERNAL; } std::string InternalWarning::description() const { return ELEKTRA_WARNING_INTERNAL_NAME; } +bool InternalWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +InternalWarning* InternalWarning::clone () const +{ + return new InternalWarning(*this); +} + + std::string InterfaceWarning::code() const { return ELEKTRA_WARNING_INTERFACE; } std::string InterfaceWarning::description() const { return ELEKTRA_WARNING_INTERFACE_NAME; } +bool InterfaceWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +InterfaceWarning* InterfaceWarning::clone () const +{ + return new InterfaceWarning(*this); +} + + std::string PluginMisbehaviorWarning::code() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR; } std::string PluginMisbehaviorWarning::description() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME; } +bool PluginMisbehaviorWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +PluginMisbehaviorWarning* PluginMisbehaviorWarning::clone () const +{ + return new PluginMisbehaviorWarning(*this); +} std::string ConflictingStateWarning::code() const { return ELEKTRA_WARNING_CONFLICTING_STATE; } std::string ConflictingStateWarning::description() const { return ELEKTRA_WARNING_CONFLICTING_STATE_NAME; } +bool ConflictingStateWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +ConflictingStateWarning* ConflictingStateWarning::clone () const +{ + return new ConflictingStateWarning(*this); +} + + std::string ValidationSyntacticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC; } std::string ValidationSyntacticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME; } +bool ValidationSyntacticWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +ValidationSyntacticWarning* ValidationSyntacticWarning::clone () const +{ + return new ValidationSyntacticWarning(*this); +} + + std::string ValidationSemanticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC; } std::string ValidationSemanticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME; } +bool ValidationSemanticWarning::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Warning::compare (other); +} +ValidationSemanticWarning* ValidationSemanticWarning::clone () const +{ + return new ValidationSemanticWarning(*this); +} + + } // namespace errors } // namespace tools diff --git a/src/libs/tools/tests/testtool_error.cpp b/src/libs/tools/tests/testtool_error.cpp index 53570d56bac..053bedea17a 100644 --- a/src/libs/tools/tests/testtool_error.cpp +++ b/src/libs/tools/tests/testtool_error.cpp @@ -224,8 +224,8 @@ TEST (Error, ErrWarn) kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", 101); kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", 102); - pmE.addWarning (&csW); - pmE.addWarning (&ifW); + pmE.addWarning (csW); + pmE.addWarning (ifW); ASSERT_EQ (pmE.warningCount(), 2); @@ -238,4 +238,53 @@ TEST (Error, ErrWarn) else ASSERT_EQ (*w, ifW); } +} + +TEST (Error, Equality) +{ + std::cout << std::endl << "TEST EQUALITY OF WARNINGS AND ERRORS" << std::endl; + + kdb::tools::errors::InstallationWarning installationWarning ("Reason", "Module", "File", 42); + kdb::tools::errors::InstallationWarning installationWarning2 ("Reason", "Module", "File", 42); + kdb::tools::errors::InterfaceWarning interfaceWarning ("Reason", "Module", "File", 42); + kdb::tools::errors::InstallationError installationError ("Reason", "Module", "File", 42); + + ASSERT_EQ (installationWarning, installationWarning); + ASSERT_EQ (installationWarning, installationWarning2); + ASSERT_EQ (installationError, installationError); + ASSERT_NE (installationWarning, interfaceWarning); + ASSERT_NE (installationWarning, installationError); + ASSERT_NE (installationError, installationWarning); + + kdb::tools::errors::InstallationError installationError1 ("Reason", "Module", "File", 42); + kdb::tools::errors::ResourceError resourceError ("Reason", "Module", "File", 42); + + ASSERT_NE (installationError, resourceError); + ASSERT_EQ (installationError, installationError1); + + installationError.addWarning (installationWarning); + installationError.addWarning (installationWarning2); + installationError.addWarning (interfaceWarning); + ASSERT_NE (installationError, installationError1); + + installationError1.addWarning (interfaceWarning); + installationError1.addWarning (installationWarning); + ASSERT_NE (installationError, installationError1); + + installationError1.addWarning (installationWarning2); + /* Should be equal because same ordering of warnings is not necessary for equality */ + ASSERT_EQ (installationError, installationError1); + + resourceError.addWarning (installationWarning); + resourceError.addWarning (installationWarning2); + resourceError.addWarning (interfaceWarning); + ASSERT_NE (installationError, resourceError); + + /* currently the Warning gets copied when it is added to the Error */ + ASSERT_EQ (installationError[2].file(), installationError1[0].file()); + installationError[2].file() = "changed file in warning"; + ASSERT_NE (installationError[2].file(), installationError1[0].file()); + + + } \ No newline at end of file From 100b8d2192376dd205a229ff2f00867d5ecddf5a Mon Sep 17 00:00:00 2001 From: flo91 Date: Mon, 17 Jan 2022 01:16:13 +0100 Subject: [PATCH 06/23] libtools: add Factories for Errors and Warnings, add MountPoint and ConfigFile members, add a PureWarning ErrorType for a dummy Error that only encapsulats Warnings, add some testcode to /src/tools/kdb/validate.cpp --- .../tools/include/errors/baseNotification.hpp | 16 +- src/libs/tools/include/errors/error.hpp | 2 +- .../tools/include/errors/errorFactory.hpp | 37 +++++ src/libs/tools/include/errors/errorTypes.hpp | 54 +++---- .../tools/include/errors/warningFactory.hpp | 34 ++++ .../tools/include/errors/warningTypes.hpp | 27 ++-- .../tools/src/errors/baseNotification.cpp | 22 ++- src/libs/tools/src/errors/errorFactory.cpp | 149 ++++++++++++++++++ src/libs/tools/src/errors/errorTypes.cpp | 11 ++ src/libs/tools/src/errors/warningFactory.cpp | 64 ++++++++ src/libs/tools/src/errors/warningTypes.cpp | 2 +- src/libs/tools/tests/testtool_error.cpp | 147 +++++++++++++---- src/tools/kdb/coloredkdbio.hpp | 4 +- src/tools/kdb/validate.cpp | 43 +++-- 14 files changed, 499 insertions(+), 113 deletions(-) create mode 100644 src/libs/tools/include/errors/errorFactory.hpp create mode 100644 src/libs/tools/include/errors/warningFactory.hpp create mode 100644 src/libs/tools/src/errors/errorFactory.cpp create mode 100644 src/libs/tools/src/errors/warningFactory.cpp diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index 8f3bf8e0963..96ce12b4e4a 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -4,7 +4,7 @@ #include #include #include -#include // for kdb-types, code and description constants +#include // for kdb-types, code and description constants namespace kdb { @@ -18,17 +18,26 @@ namespace errors class BaseNotification { public: + /* constructor */ + BaseNotification (std::string reason, std::string module, std::string file, std::string mountPoint, + std::string configFile, kdb::long_t line); + /* setters */ - void setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line); + void setData (const std::string & reason, const std::string & module, const std::string & file, + const std::string & mountPoint, const std::string & configFile, kdb::long_t line); /* get references (for setting and getting member values) */ std::string & reason(); std::string & module(); std::string & file(); + std::string & mountPoint(); + std::string & configFile(); kdb::long_t & line(); const std::string & reason () const; const std::string & module () const; const std::string & file () const; + const std::string & mountPoint () const; + const std::string & configFile () const; const kdb::long_t & line () const; /* fixed values per Class, taken from C-makro definitions in /src/include/kdberrors.h */ @@ -43,7 +52,6 @@ class BaseNotification protected: BaseNotification () = default; - BaseNotification (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line); /* Can be overwritten by subclasses to change the text representation */ std::ostream& toString (std::ostream& outputStream) const; @@ -55,6 +63,8 @@ class BaseNotification std::string m_reason; std::string m_module; std::string m_file; + std::string m_mountPoint; + std::string m_configFile; kdb::long_t m_line = 0; }; diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 44618c17ae7..8bfb7c0dc5e 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -18,7 +18,7 @@ class Error : public BaseNotification /* inherit constructors */ using BaseNotification::BaseNotification; - ~Error(); + virtual ~Error(); /* An Error can contain 0 to n warnings */ void addWarning (Warning & warning); diff --git a/src/libs/tools/include/errors/errorFactory.hpp b/src/libs/tools/include/errors/errorFactory.hpp new file mode 100644 index 00000000000..b89633911ca --- /dev/null +++ b/src/libs/tools/include/errors/errorFactory.hpp @@ -0,0 +1,37 @@ +// +// Created by flo on 1/16/22. +// + +#ifndef ELEKTRA_ERRORFACTORY_HPP +#define ELEKTRA_ERRORFACTORY_HPP + +#include "error.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +/* make sure to delete (free) the returned Errors */ +class ErrorFactory +{ +public: + /* takes one of the ELEKTRA_ERROR_* constants (e.g. ELEKTRA_ERROR_OUT_OF_MEMORY) + * from /src/include/kdberrors.h as a parameter */ + static Error * create (const std::string & type, const std::string & reason, const std::string & module, + const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line); + + /* Creates an Error object including Warnings as provided by the given key. */ + static Error * fromKey(kdb::Key key); + + /* checks if a code and description fit together */ + static bool checkErrorCodeDesc(const std::string & code, const std::string & description); +}; + +} // namespace errors +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_ERRORFACTORY_HPP diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp index 3e487396d95..2c1484892c6 100644 --- a/src/libs/tools/include/errors/errorTypes.hpp +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -11,14 +11,26 @@ namespace tools namespace errors { -/* This file contains all concrete classes for the different Errors, based on the constants defined in /src/include/kdberrors.h */ +/* Not an error by itself, but a container for multiple warnings */ +class PureWarningError : public Error +{ +public: + PureWarningError () + : Error { "No error, only warnings.", "", "", "", "", 0} {} + + std::string code() const override; + std::string description() const override; + +private: + bool compare(const BaseNotification& other) const final; +}; + +/* Concrete classes for the different Errors, based on the constants defined in /src/include/kdberrors.h */ class ResourceError : public Error { public: - ResourceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -28,10 +40,8 @@ class ResourceError : public Error class OutOfMemoryError : public Error { public: - OutOfMemoryError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - - std::string code() const override; + using Error::Error; + std::string code() const override; std::string description() const override; private: bool compare(const BaseNotification& other) const final; @@ -40,9 +50,7 @@ class OutOfMemoryError : public Error class InstallationError : public Error { public: - InstallationError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -53,9 +61,7 @@ class InstallationError : public Error class InternalError: public Error { public: - InternalError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -65,9 +71,7 @@ class InternalError: public Error class InterfaceError : public Error { public: - InterfaceError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -77,9 +81,7 @@ class InterfaceError : public Error class PluginMisbehaviorError : public Error { public: - PluginMisbehaviorError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -89,9 +91,7 @@ class PluginMisbehaviorError : public Error class ConflictingStateError : public Error { public: - ConflictingStateError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -101,9 +101,7 @@ class ConflictingStateError : public Error class ValidationSyntacticError : public Error { public: - ValidationSyntacticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: @@ -113,9 +111,7 @@ class ValidationSyntacticError : public Error class ValidationSemanticError : public Error { public: - ValidationSemanticError (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Error { reason, module, file, line} {} - + using Error::Error; std::string code() const override; std::string description() const override; private: diff --git a/src/libs/tools/include/errors/warningFactory.hpp b/src/libs/tools/include/errors/warningFactory.hpp new file mode 100644 index 00000000000..a4ec52786ed --- /dev/null +++ b/src/libs/tools/include/errors/warningFactory.hpp @@ -0,0 +1,34 @@ +// +// Created by flo on 1/16/22. +// + +#ifndef ELEKTRA_WARNINGFACTORY_HPP +#define ELEKTRA_WARNINGFACTORY_HPP + +#include "warning.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +class WarningFactory +{ +public: + /* takes one of the ELEKTRA_WARNING_* constants (e.g. ELEKTRA_WARNING_OUT_OF_MEMORY) + * from /src/include/kdberrors.h as a parameter */ + /* You must delete the object that is returned by this function! */ + static Warning * create (const std::string & type, const std::string & reason, const std::string & module, const std::string & file, + const std::string & mountPoint, const std::string & configFile, kdb::long_t line); + + /* checks if a code and description fit together */ + static bool checkWarningCodeDesc(const std::string & code, const std::string & description); +}; + +} // namespace errors +} // namespace tools +} // namespace kdb + +#endif // ELEKTRA_WARNINGFACTORY_HPP diff --git a/src/libs/tools/include/errors/warningTypes.hpp b/src/libs/tools/include/errors/warningTypes.hpp index 9fd5af6237c..c34dfc51678 100644 --- a/src/libs/tools/include/errors/warningTypes.hpp +++ b/src/libs/tools/include/errors/warningTypes.hpp @@ -16,8 +16,7 @@ namespace errors class ResourceWarning : public Warning { public: - ResourceWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -29,8 +28,7 @@ class ResourceWarning : public Warning class OutOfMemoryWarning : public Warning { public: - OutOfMemoryWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -42,8 +40,7 @@ class OutOfMemoryWarning : public Warning class InstallationWarning : public Warning { public: - InstallationWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -55,8 +52,7 @@ class InstallationWarning : public Warning class InternalWarning : public Warning { public: - InternalWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -68,8 +64,7 @@ class InternalWarning : public Warning class InterfaceWarning : public Warning { public: - InterfaceWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -81,8 +76,7 @@ class InterfaceWarning : public Warning class PluginMisbehaviorWarning : public Warning { public: - PluginMisbehaviorWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -94,8 +88,7 @@ class PluginMisbehaviorWarning : public Warning class ConflictingStateWarning : public Warning { public: - ConflictingStateWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -107,8 +100,7 @@ class ConflictingStateWarning : public Warning class ValidationSyntacticWarning : public Warning { public: - ValidationSyntacticWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; @@ -120,8 +112,7 @@ class ValidationSyntacticWarning : public Warning class ValidationSemanticWarning : public Warning { public: - ValidationSemanticWarning (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) - : Warning { reason, module, file, line} {} + using Warning::Warning; std::string code() const override; std::string description() const override; diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index 6617722b768..9549f1d43dd 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -1,6 +1,7 @@ #include #include +#include #include "errors/baseNotification.hpp" namespace kdb @@ -10,16 +11,19 @@ namespace tools namespace errors { -BaseNotification::BaseNotification (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) -{ - setData (reason, module, file, line); -} +BaseNotification::BaseNotification (std::string reason, std::string module, std::string file, + std::string mountPoint, std::string configFile, kdb::long_t line) +: m_reason (std::move (reason)), m_module (std::move (module)), m_file (std::move (file)), m_mountPoint (std::move (mountPoint)), + m_configFile (std::move (configFile)), m_line (line) {} -void BaseNotification::setData (const std::string & reason, const std::string & module, const std::string & file, kdb::long_t line) +void BaseNotification::setData (const std::string & reason, const std::string & module, const std::string & file, + const std::string & mountPoint, const std::string & configFile, kdb::long_t line) { this->m_reason = reason; this->m_module = module; this->m_file = file; + this->m_mountPoint = mountPoint; + this->m_configFile = configFile; this->m_line = line; } @@ -31,6 +35,8 @@ std::ostream& BaseNotification::toString (std::ostream & outputStream) const << "\nReason: " << m_reason << "\nModule: " << m_module << "\nFile: " << m_file + << "\nMount point: " << m_mountPoint + << "\nConfig file: " << m_configFile << "\nLine: " << std::to_string (m_line); } @@ -55,6 +61,8 @@ bool BaseNotification::operator== (const BaseNotification& other) const && reason() == other.reason() && module() == other.module() && file() == other.file() + && mountPoint() == other.mountPoint() + && configFile() == other.configFile() && line() == other.line() && this->compare(other); } @@ -68,12 +76,16 @@ bool BaseNotification::operator!= (const BaseNotification& other) const std::string& BaseNotification::reason() { return m_reason; } std::string& BaseNotification::module() { return m_module; } std::string& BaseNotification::file() { return m_file; } +std::string& BaseNotification::mountPoint() { return m_mountPoint; } +std::string& BaseNotification::configFile() { return m_configFile; } kdb::long_t& BaseNotification::line() { return m_line; } /* getters */ const std::string & BaseNotification::reason () const { return m_reason; } const std::string & BaseNotification::module () const { return m_module; } const std::string & BaseNotification::file () const { return m_file; } +const std::string & BaseNotification::mountPoint () const { return m_mountPoint; } +const std::string & BaseNotification::configFile () const { return m_configFile; } const kdb::long_t & BaseNotification::line () const { return m_line; } } // namespace errors diff --git a/src/libs/tools/src/errors/errorFactory.cpp b/src/libs/tools/src/errors/errorFactory.cpp new file mode 100644 index 00000000000..f6396852be3 --- /dev/null +++ b/src/libs/tools/src/errors/errorFactory.cpp @@ -0,0 +1,149 @@ + +#include +#include // for code and description constants +#include "errors/errorFactory.hpp" +#include "errors/warningFactory.hpp" +#include "errors/errorTypes.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +Error* ErrorFactory::create(const std::string & type, const std::string & reason, const std::string & module, + const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line) +{ + if (type == ELEKTRA_ERROR_RESOURCE || type == ELEKTRA_ERROR_RESOURCE_NAME) + return new ResourceError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_OUT_OF_MEMORY || type == ELEKTRA_ERROR_OUT_OF_MEMORY_NAME) + return new OutOfMemoryError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_INSTALLATION || type == ELEKTRA_ERROR_INSTALLATION_NAME) + return new InstallationError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_INTERNAL || type == ELEKTRA_ERROR_INTERNAL_NAME) + return new InternalError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_INTERFACE || type == ELEKTRA_ERROR_INTERFACE_NAME) + return new InterfaceError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR || type == ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME) + return new PluginMisbehaviorError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_CONFLICTING_STATE || type == ELEKTRA_ERROR_CONFLICTING_STATE_NAME) + return new ConflictingStateError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_VALIDATION_SYNTACTIC || type == ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME) + return new ValidationSyntacticError (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_ERROR_VALIDATION_SEMANTIC || type == ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME) + return new ValidationSemanticError (reason, module, file, mountPoint, configFile, line); + else + return nullptr; +} + +/* TODO: Test method */ +Error * ErrorFactory::fromKey (kdb::Key key) +{ + Error *err = nullptr; + if (key.isNull() || !key.isValid() || key.isBinary() || (!key.hasMeta("error") && !key.hasMeta("warnings"))) + { + return nullptr; + } + + if (!key.hasMeta ("error")) + { + err = new PureWarningError(); + } + else + { + std::string errCode = key.getMeta("error/number"); + std::string errDesc = key.getMeta("error/description"); + + if (!checkErrorCodeDesc (errCode, errDesc)) + { + /* TODO: throw exception */ + return nullptr; + } + else + { + std::string module = key.getMeta("error/module"); + std::string file = key.getMeta("error/file"); + std::string reason = key.getMeta("error/reason"); + std::string mountPoint = key.getMeta("error/mountpoint"); + std::string configFile = key.getMeta("error/configfile"); + kdb::long_t line = key.getMeta("error/line"); + + err = create (errCode, reason, module, file, mountPoint, configFile, line); + } + } + + /* process warnings * + * Code for extracting warnings was adapted from src/libs/highlevel/elektra_error.c:elektraErrorFromKey (Key * key) + * and /src/tools/kdb/coloredkdbio.h:printWarnings() + // TODO: use C++ binding version of keyMeta */ + KeySet metaKeys (ckdb::ksDup (ckdb::keyMeta (key.getKey()))); + Key warningsParent ("meta:/warnings", KEY_END); + KeySet warningKeys = metaKeys.cut (warningsParent); + + if (warningKeys.size() > 0) + { + for(auto it = warningKeys.begin() + 1; it != warningKeys.end (); ++it) + { + if (it->isDirectBelow(warningsParent)) + { + auto name = it->getName (); + + std::string warnCode = warningKeys.get(name + "/number"); + std::string warnDescription = warningKeys.get(name + "/description"); + + if (!WarningFactory::checkWarningCodeDesc (warnCode, warnDescription)) + { + // TODO: throw exception + return nullptr; + } + + std::string warnReason = warningKeys.get(name + "/reason"); + std::string warnModule = warningKeys.get(name + "/module"); + std::string warnFile = warningKeys.get(name + "/file"); + + std::string warnMountPoint = warningKeys.get(name + "/mountpoint"); + std::string warnConfigFile = warningKeys.get(name + "/configfile"); + + kdb::long_t warnLine = warningKeys.get(name + "/line"); + + Warning *w = WarningFactory::create (warnCode, warnReason, warnModule, warnFile, + warnMountPoint, warnConfigFile, warnLine); + err->addWarning (*w); + + /* Warning gets copied by addWarning(Warning &) */ + delete w; + } + } + } + + return err; +} + +bool ErrorFactory::checkErrorCodeDesc(const std::string & code, const std::string & description) +{ + if (code == ELEKTRA_ERROR_RESOURCE) + return (description == ELEKTRA_ERROR_RESOURCE_NAME); + else if (code == ELEKTRA_ERROR_OUT_OF_MEMORY) + return (description == ELEKTRA_ERROR_OUT_OF_MEMORY_NAME); + else if (code == ELEKTRA_ERROR_INSTALLATION) + return (description == ELEKTRA_ERROR_INSTALLATION_NAME); + else if (code == ELEKTRA_ERROR_INTERNAL) + return (description == ELEKTRA_ERROR_INTERNAL_NAME); + else if (code == ELEKTRA_ERROR_INTERFACE) + return (description == ELEKTRA_ERROR_INTERFACE_NAME); + else if (code == ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR) + return (description == ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME); + else if (code == ELEKTRA_ERROR_CONFLICTING_STATE) + return (description == ELEKTRA_ERROR_CONFLICTING_STATE_NAME); + else if (code == ELEKTRA_ERROR_VALIDATION_SYNTACTIC) + return (description == ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME); + else if (code == ELEKTRA_ERROR_VALIDATION_SEMANTIC) + return (description == ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME); + else + return false; +} + +} // namespace errors +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/errorTypes.cpp b/src/libs/tools/src/errors/errorTypes.cpp index e06fb391408..fa58e33dc7d 100644 --- a/src/libs/tools/src/errors/errorTypes.cpp +++ b/src/libs/tools/src/errors/errorTypes.cpp @@ -1,4 +1,5 @@ +#include // for code and description constants #include namespace kdb @@ -8,6 +9,16 @@ namespace tools namespace errors { +std::string PureWarningError::code() const { return ""; } +std::string PureWarningError::description() const { return "Warnings"; } +bool PureWarningError::compare(const BaseNotification& other) const +{ + if(!(dynamic_cast (&other))) + return false; + else + return Error::compare (other); +} + std::string ResourceError::code() const { return ELEKTRA_ERROR_RESOURCE; } std::string ResourceError::description() const { return ELEKTRA_ERROR_RESOURCE_NAME; } bool ResourceError::compare(const BaseNotification& other) const diff --git a/src/libs/tools/src/errors/warningFactory.cpp b/src/libs/tools/src/errors/warningFactory.cpp new file mode 100644 index 00000000000..c69d81fa3f5 --- /dev/null +++ b/src/libs/tools/src/errors/warningFactory.cpp @@ -0,0 +1,64 @@ + +#include // for code and description constants +#include "errors/warningFactory.hpp" +#include "errors/warningTypes.hpp" + +namespace kdb +{ +namespace tools +{ +namespace errors +{ + +Warning* WarningFactory::create(const std::string & type, const std::string & reason, const std::string & module, + const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line) +{ + if (type == ELEKTRA_WARNING_RESOURCE || type == ELEKTRA_WARNING_RESOURCE_NAME) + return new ResourceWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_OUT_OF_MEMORY || type == ELEKTRA_WARNING_OUT_OF_MEMORY_NAME) + return new OutOfMemoryWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_INSTALLATION || type == ELEKTRA_WARNING_INSTALLATION_NAME) + return new InstallationWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_INTERNAL || type == ELEKTRA_WARNING_INTERNAL_NAME) + return new InternalWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_INTERFACE || type == ELEKTRA_WARNING_INTERFACE_NAME) + return new InterfaceWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR || type == ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME) + return new PluginMisbehaviorWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_CONFLICTING_STATE || type == ELEKTRA_WARNING_CONFLICTING_STATE_NAME) + return new ConflictingStateWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_VALIDATION_SYNTACTIC || type == ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME) + return new ValidationSyntacticWarning (reason, module, file, mountPoint, configFile, line); + else if (type == ELEKTRA_WARNING_VALIDATION_SEMANTIC || type == ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME) + return new ValidationSemanticWarning (reason, module, file, mountPoint, configFile, line); + else + return nullptr; +} + +bool WarningFactory::checkWarningCodeDesc(const std::string & code, const std::string & description) +{ + if (code == ELEKTRA_WARNING_RESOURCE) + return (description == ELEKTRA_WARNING_RESOURCE_NAME); + else if (code == ELEKTRA_WARNING_OUT_OF_MEMORY) + return (description == ELEKTRA_WARNING_OUT_OF_MEMORY_NAME); + else if (code == ELEKTRA_WARNING_INSTALLATION) + return (description == ELEKTRA_WARNING_INSTALLATION_NAME); + else if (code == ELEKTRA_WARNING_INTERNAL) + return (description == ELEKTRA_WARNING_INTERNAL_NAME); + else if (code == ELEKTRA_WARNING_INTERFACE) + return (description == ELEKTRA_WARNING_INTERFACE_NAME); + else if (code == ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR) + return (description == ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME); + else if (code == ELEKTRA_WARNING_CONFLICTING_STATE) + return (description == ELEKTRA_WARNING_CONFLICTING_STATE_NAME); + else if (code == ELEKTRA_WARNING_VALIDATION_SYNTACTIC) + return (description == ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME); + else if (code == ELEKTRA_WARNING_VALIDATION_SEMANTIC) + return (description == ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME); + else + return false; +} + +} // namespace errors +} // namespace tools +} // namespace kdb \ No newline at end of file diff --git a/src/libs/tools/src/errors/warningTypes.cpp b/src/libs/tools/src/errors/warningTypes.cpp index 8d6b97b0a15..ca1b4debd57 100644 --- a/src/libs/tools/src/errors/warningTypes.cpp +++ b/src/libs/tools/src/errors/warningTypes.cpp @@ -1,5 +1,5 @@ - +#include // for code and description constants #include namespace kdb diff --git a/src/libs/tools/tests/testtool_error.cpp b/src/libs/tools/tests/testtool_error.cpp index 053bedea17a..89a58352521 100644 --- a/src/libs/tools/tests/testtool_error.cpp +++ b/src/libs/tools/tests/testtool_error.cpp @@ -8,6 +8,7 @@ */ #include +#include // for code and description constants #include #include @@ -17,100 +18,136 @@ TEST (Error, TestWarnings) std::cout << std::endl << "TEST WARNINGS:" << std::endl; kdb::tools::errors::ResourceWarning resourceWarning ("resourceWarningReason", "resourceWarningModule", - "resourceWarningFile", 0); + "resourceWarningFile", + "resourceWarningMountPoint", + "resourceWarningConfigFile", 0); ASSERT_EQ(resourceWarning.code(), ELEKTRA_WARNING_RESOURCE); ASSERT_EQ(resourceWarning.description(), ELEKTRA_WARNING_RESOURCE_NAME); ASSERT_EQ(resourceWarning.reason(), "resourceWarningReason"); ASSERT_EQ(resourceWarning.module(), "resourceWarningModule"); ASSERT_EQ(resourceWarning.file(), "resourceWarningFile"); + ASSERT_EQ(resourceWarning.mountPoint(), "resourceWarningMountPoint"); + ASSERT_EQ(resourceWarning.configFile(), "resourceWarningConfigFile"); ASSERT_EQ(resourceWarning.line(), 0); /* Out of memory warning */ kdb::tools::errors::OutOfMemoryWarning outOfMemoryWarning ("outOfMemoryWarningReason", "outOfMemoryWarningModule", - "outOfMemoryWarningFile", 1); + "outOfMemoryWarningFile", + "outOfMemoryWarningMountPoint", + "outOfMemoryWarningConfigFile", 1); ASSERT_EQ(outOfMemoryWarning.code(), ELEKTRA_WARNING_OUT_OF_MEMORY); ASSERT_EQ(outOfMemoryWarning.description(), ELEKTRA_WARNING_OUT_OF_MEMORY_NAME); ASSERT_EQ(outOfMemoryWarning.reason(), "outOfMemoryWarningReason"); ASSERT_EQ(outOfMemoryWarning.module(), "outOfMemoryWarningModule"); ASSERT_EQ(outOfMemoryWarning.file(), "outOfMemoryWarningFile"); + ASSERT_EQ(outOfMemoryWarning.mountPoint(), "outOfMemoryWarningMountPoint"); + ASSERT_EQ(outOfMemoryWarning.configFile(), "outOfMemoryWarningConfigFile"); ASSERT_EQ(outOfMemoryWarning.line(), 1); /* Installation warning */ kdb::tools::errors::InstallationWarning installationWarning ("installationWarningReason", "installationWarningModule", - "installationWarningFile", -1); + "installationWarningFile", + "installationWarningMountPoint", + "installationWarningConfigFile", -1); ASSERT_EQ(installationWarning.code(), ELEKTRA_WARNING_INSTALLATION); ASSERT_EQ(installationWarning.description(), ELEKTRA_WARNING_INSTALLATION_NAME); ASSERT_EQ(installationWarning.reason(), "installationWarningReason"); ASSERT_EQ(installationWarning.module(), "installationWarningModule"); ASSERT_EQ(installationWarning.file(), "installationWarningFile"); + ASSERT_EQ(installationWarning.mountPoint(), "installationWarningMountPoint"); + ASSERT_EQ(installationWarning.configFile(), "installationWarningConfigFile"); ASSERT_EQ(installationWarning.line(), -1); /* Internal warning */ kdb::tools::errors::InternalWarning internalWarning ("internalWarningReason", "internalWarningModule", - "internalWarningFile", 999); + "internalWarningFile", + "internalWarningMountPoint", + "internalWarningConfigFile", 999); ASSERT_EQ(internalWarning.code(), ELEKTRA_WARNING_INTERNAL); ASSERT_EQ(internalWarning.description(), ELEKTRA_WARNING_INTERNAL_NAME); ASSERT_EQ(internalWarning.reason(), "internalWarningReason"); ASSERT_EQ(internalWarning.module(), "internalWarningModule"); ASSERT_EQ(internalWarning.file(), "internalWarningFile"); + ASSERT_EQ(internalWarning.mountPoint(), "internalWarningMountPoint"); + ASSERT_EQ(internalWarning.configFile(), "internalWarningConfigFile"); ASSERT_EQ(internalWarning.line(), 999); /* Interface warning */ kdb::tools::errors::InterfaceWarning interfaceWarning ("interfaceWarningReason", "interfaceWarningModule", - "interfaceWarningFile", 2); + "interfaceWarningFile", + "interfaceWarningMountPoint", + "interfaceWarningConfigFile", 2); ASSERT_EQ(interfaceWarning.code(), ELEKTRA_WARNING_INTERFACE); ASSERT_EQ(interfaceWarning.description(), ELEKTRA_WARNING_INTERFACE_NAME); ASSERT_EQ(interfaceWarning.reason(), "interfaceWarningReason"); ASSERT_EQ(interfaceWarning.module(), "interfaceWarningModule"); ASSERT_EQ(interfaceWarning.file(), "interfaceWarningFile"); + ASSERT_EQ(interfaceWarning.mountPoint(), "interfaceWarningMountPoint"); + ASSERT_EQ(interfaceWarning.configFile(), "interfaceWarningConfigFile"); ASSERT_EQ(interfaceWarning.line(), 2); /* PluginMisbehavior warning */ kdb::tools::errors::PluginMisbehaviorWarning pluginMisbehaviorWarning ("pluginMisbehaviorWarningReason", "pluginMisbehaviorWarningModule", - "pluginMisbehaviorWarningFile", 3); + "pluginMisbehaviorWarningFile", + "pluginMisbehaviorWarningMountPoint", + "pluginMisbehaviorWarningConfigFile", 3); ASSERT_EQ(pluginMisbehaviorWarning.code(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR); ASSERT_EQ(pluginMisbehaviorWarning.description(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME); ASSERT_EQ(pluginMisbehaviorWarning.reason(), "pluginMisbehaviorWarningReason"); ASSERT_EQ(pluginMisbehaviorWarning.module(), "pluginMisbehaviorWarningModule"); ASSERT_EQ(pluginMisbehaviorWarning.file(), "pluginMisbehaviorWarningFile"); + ASSERT_EQ(pluginMisbehaviorWarning.mountPoint(), "pluginMisbehaviorWarningMountPoint"); + ASSERT_EQ(pluginMisbehaviorWarning.configFile(), "pluginMisbehaviorWarningConfigFile"); ASSERT_EQ(pluginMisbehaviorWarning.line(), 3); /* ConflictingStatie warning */ kdb::tools::errors::ConflictingStateWarning conflictingStateWarning ("conflictingStateWarningReason", "conflictingStateWarningModule", - "conflictingStateWarningFile", 4); + "conflictingStateWarningFile", + "conflictingStateWarningMountPoint", + "conflictingStateWarningConfigFile",4); ASSERT_EQ(conflictingStateWarning.code(), ELEKTRA_WARNING_CONFLICTING_STATE); ASSERT_EQ(conflictingStateWarning.description(), ELEKTRA_WARNING_CONFLICTING_STATE_NAME); ASSERT_EQ(conflictingStateWarning.reason(), "conflictingStateWarningReason"); ASSERT_EQ(conflictingStateWarning.module(), "conflictingStateWarningModule"); ASSERT_EQ(conflictingStateWarning.file(), "conflictingStateWarningFile"); + ASSERT_EQ(conflictingStateWarning.mountPoint(), "conflictingStateWarningMountPoint"); + ASSERT_EQ(conflictingStateWarning.configFile(), "conflictingStateWarningConfigFile"); ASSERT_EQ(conflictingStateWarning.line(), 4); /* ValidationSyntactic warning */ kdb::tools::errors::ValidationSyntacticWarning validationSyntacticWarning ("validationSyntacticWarningReason", "validationSyntacticWarningModule", - "validationSyntacticWarningFile", 5); + "validationSyntacticWarningFile", + "validationSyntacticMountPoint", + "validationSyntacticWarningConfigFile", 5); ASSERT_EQ(validationSyntacticWarning.code(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC); ASSERT_EQ(validationSyntacticWarning.description(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME); ASSERT_EQ(validationSyntacticWarning.reason(), "validationSyntacticWarningReason"); ASSERT_EQ(validationSyntacticWarning.module(), "validationSyntacticWarningModule"); ASSERT_EQ(validationSyntacticWarning.file(), "validationSyntacticWarningFile"); + ASSERT_EQ(validationSyntacticWarning.mountPoint(), "validationSyntacticMountPoint"); + ASSERT_EQ(validationSyntacticWarning.configFile(), "validationSyntacticWarningConfigFile"); ASSERT_EQ(validationSyntacticWarning.line(), 5); /* ValidationSemantic warning */ kdb::tools::errors::ValidationSemanticWarning validationSemanticWarning ("validationSemanticWarningReason", "validationSemanticWarningModule", - "validationSemanticWarningFile", 6); + "validationSemanticWarningFile", + "validationSemanticWarningMountPoint", + "validationSemanticWarningConfigFile", 6); ASSERT_EQ(validationSemanticWarning.code(), ELEKTRA_WARNING_VALIDATION_SEMANTIC); ASSERT_EQ(validationSemanticWarning.description(), ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME); ASSERT_EQ(validationSemanticWarning.reason(), "validationSemanticWarningReason"); ASSERT_EQ(validationSemanticWarning.module(), "validationSemanticWarningModule"); ASSERT_EQ(validationSemanticWarning.file(), "validationSemanticWarningFile"); + ASSERT_EQ(validationSemanticWarning.mountPoint(), "validationSemanticWarningMountPoint"); + ASSERT_EQ(validationSemanticWarning.configFile(), "validationSemanticWarningConfigFile"); ASSERT_EQ(validationSemanticWarning.line(), 6); } @@ -119,110 +156,150 @@ TEST (Error, TestErrors) /* Resource error */ std::cout << std::endl << "TEST ERRORS:" << std::endl; kdb::tools::errors::ResourceError resourceError ("resourceErrorReason", - "resourceErrorModule", - "resourceErrorFile", 0); + "resourceErrorModule", + "resourceErrorFile", + "resourceErrorMountPoint", + "resourceErrorConfigFile", 0); ASSERT_EQ(resourceError.code(), ELEKTRA_ERROR_RESOURCE); ASSERT_EQ(resourceError.description(), ELEKTRA_ERROR_RESOURCE_NAME); ASSERT_EQ(resourceError.reason(), "resourceErrorReason"); ASSERT_EQ(resourceError.module(), "resourceErrorModule"); ASSERT_EQ(resourceError.file(), "resourceErrorFile"); + ASSERT_EQ(resourceError.mountPoint(), "resourceErrorMountPoint"); + ASSERT_EQ(resourceError.configFile(), "resourceErrorConfigFile"); ASSERT_EQ(resourceError.line(), 0); /* Out of memory error */ kdb::tools::errors::OutOfMemoryError outOfMemoryError ("outOfMemoryErrorReason", - "outOfMemoryErrorModule", - "outOfMemoryErrorFile", 1); + "outOfMemoryErrorModule", + "outOfMemoryErrorFile", + "outOfMemoryErrorMountPoint", + "outOfMemoryErrorConfigFile", 1); ASSERT_EQ(outOfMemoryError.code(), ELEKTRA_ERROR_OUT_OF_MEMORY); ASSERT_EQ(outOfMemoryError.description(), ELEKTRA_ERROR_OUT_OF_MEMORY_NAME); ASSERT_EQ(outOfMemoryError.reason(), "outOfMemoryErrorReason"); ASSERT_EQ(outOfMemoryError.module(), "outOfMemoryErrorModule"); ASSERT_EQ(outOfMemoryError.file(), "outOfMemoryErrorFile"); + ASSERT_EQ(outOfMemoryError.mountPoint(), "outOfMemoryErrorMountPoint"); + ASSERT_EQ(outOfMemoryError.configFile(), "outOfMemoryErrorConfigFile"); ASSERT_EQ(outOfMemoryError.line(), 1); /* Installation error */ kdb::tools::errors::InstallationError installationError ("installationErrorReason", - "installationErrorModule", - "installationErrorFile", -1); + "installationErrorModule", + "installationErrorFile", + "installationErrorMountPoint", + "installationErrorConfigFile", -1); ASSERT_EQ(installationError.code(), ELEKTRA_ERROR_INSTALLATION); ASSERT_EQ(installationError.description(), ELEKTRA_ERROR_INSTALLATION_NAME); ASSERT_EQ(installationError.reason(), "installationErrorReason"); ASSERT_EQ(installationError.module(), "installationErrorModule"); ASSERT_EQ(installationError.file(), "installationErrorFile"); + ASSERT_EQ(installationError.mountPoint(), "installationErrorMountPoint"); + ASSERT_EQ(installationError.configFile(), "installationErrorConfigFile"); ASSERT_EQ(installationError.line(), -1); /* Internal error */ kdb::tools::errors::InternalError internalError ("internalErrorReason", - "internalErrorModule", - "internalErrorFile", 999); + "internalErrorModule", + "internalErrorFile", + "internalErrorMountPoint", + "internalErrorConfigFile", 999); + ASSERT_EQ(internalError.code(), ELEKTRA_ERROR_INTERNAL); ASSERT_EQ(internalError.description(), ELEKTRA_ERROR_INTERNAL_NAME); ASSERT_EQ(internalError.reason(), "internalErrorReason"); ASSERT_EQ(internalError.module(), "internalErrorModule"); ASSERT_EQ(internalError.file(), "internalErrorFile"); + ASSERT_EQ(internalError.mountPoint(), "internalErrorMountPoint"); + ASSERT_EQ(internalError.configFile(), "internalErrorConfigFile"); ASSERT_EQ(internalError.line(), 999); /* Interface error */ kdb::tools::errors::InterfaceError interfaceError ("interfaceErrorReason", - "interfaceErrorModule", - "interfaceErrorFile", 2); + "interfaceErrorModule", + "interfaceErrorFile", + "interfaceErrorMountPoint", + "interfaceErrorConfigFile", 2); ASSERT_EQ(interfaceError.code(), ELEKTRA_ERROR_INTERFACE); ASSERT_EQ(interfaceError.description(), ELEKTRA_ERROR_INTERFACE_NAME); ASSERT_EQ(interfaceError.reason(), "interfaceErrorReason"); ASSERT_EQ(interfaceError.module(), "interfaceErrorModule"); ASSERT_EQ(interfaceError.file(), "interfaceErrorFile"); + ASSERT_EQ(interfaceError.mountPoint(), "interfaceErrorMountPoint"); + ASSERT_EQ(interfaceError.configFile(), "interfaceErrorConfigFile"); ASSERT_EQ(interfaceError.line(), 2); /* PluginMisbehavior error */ kdb::tools::errors::PluginMisbehaviorError pluginMisbehaviorError ("pluginMisbehaviorErrorReason", "pluginMisbehaviorErrorModule", - "pluginMisbehaviorErrorFile", 3); + "pluginMisbehaviorErrorFile", + "pluginMisbehaviorErrorMountPoint", + "pluginMisbehaviorErrorConfigFile", 3); ASSERT_EQ(pluginMisbehaviorError.code(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR); ASSERT_EQ(pluginMisbehaviorError.description(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME); ASSERT_EQ(pluginMisbehaviorError.reason(), "pluginMisbehaviorErrorReason"); ASSERT_EQ(pluginMisbehaviorError.module(), "pluginMisbehaviorErrorModule"); ASSERT_EQ(pluginMisbehaviorError.file(), "pluginMisbehaviorErrorFile"); + ASSERT_EQ(pluginMisbehaviorError.mountPoint(), "pluginMisbehaviorErrorMountPoint"); + ASSERT_EQ(pluginMisbehaviorError.configFile(), "pluginMisbehaviorErrorConfigFile"); ASSERT_EQ(pluginMisbehaviorError.line(), 3); /* ConflictingStatie error */ kdb::tools::errors::ConflictingStateError conflictingStateError ("conflictingStateErrorReason", "conflictingStateErrorModule", - "conflictingStateErrorFile", 4); + "conflictingStateErrorFile", + "conflictingStateErrorMountPoint", + "conflictingStateErrorConfigFile", 4); ASSERT_EQ(conflictingStateError.code(), ELEKTRA_ERROR_CONFLICTING_STATE); ASSERT_EQ(conflictingStateError.description(), ELEKTRA_ERROR_CONFLICTING_STATE_NAME); ASSERT_EQ(conflictingStateError.reason(), "conflictingStateErrorReason"); ASSERT_EQ(conflictingStateError.module(), "conflictingStateErrorModule"); ASSERT_EQ(conflictingStateError.file(), "conflictingStateErrorFile"); + ASSERT_EQ(conflictingStateError.mountPoint(), "conflictingStateErrorMountPoint"); + ASSERT_EQ(conflictingStateError.configFile(), "conflictingStateErrorConfigFile"); ASSERT_EQ(conflictingStateError.line(), 4); /* ValidationSyntactic error */ kdb::tools::errors::ValidationSyntacticError validationSyntacticError ("validationSyntacticErrorReason", "validationSyntacticErrorModule", - "validationSyntacticErrorFile", 5); + "validationSyntacticErrorFile", + "validationSyntacticErrorMountPoint", + "validationSyntacticErrorConfigFile", 5); ASSERT_EQ(validationSyntacticError.code(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC); ASSERT_EQ(validationSyntacticError.description(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME); ASSERT_EQ(validationSyntacticError.reason(), "validationSyntacticErrorReason"); ASSERT_EQ(validationSyntacticError.module(), "validationSyntacticErrorModule"); ASSERT_EQ(validationSyntacticError.file(), "validationSyntacticErrorFile"); + ASSERT_EQ(validationSyntacticError.mountPoint(), "validationSyntacticErrorMountPoint"); + ASSERT_EQ(validationSyntacticError.configFile(), "validationSyntacticErrorConfigFile"); ASSERT_EQ(validationSyntacticError.line(), 5); /* ValidationSemantic error */ kdb::tools::errors::ValidationSemanticError validationSemanticError ("validationSemanticErrorReason", "validationSemanticErrorModule", - "validationSemanticErrorFile", 6); + "validationSemanticErrorFile", + "validationSemanticErrorMountPoint", + "validationSemanticErrorConfigFile", 6); ASSERT_EQ(validationSemanticError.code(), ELEKTRA_ERROR_VALIDATION_SEMANTIC); ASSERT_EQ(validationSemanticError.description(), ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME); ASSERT_EQ(validationSemanticError.reason(), "validationSemanticErrorReason"); ASSERT_EQ(validationSemanticError.module(), "validationSemanticErrorModule"); ASSERT_EQ(validationSemanticError.file(), "validationSemanticErrorFile"); + ASSERT_EQ(validationSemanticError.mountPoint(), "validationSemanticErrorMountPoint"); + ASSERT_EQ(validationSemanticError.configFile(), "validationSemanticErrorConfigFile"); ASSERT_EQ(validationSemanticError.line(), 6); } TEST (Error, ErrWarn) { std::cout << std::endl << "TEST ERRORS WITH WARNINGS" << std::endl; - kdb::tools::errors::PluginMisbehaviorError pmE ("pmeReason", "pmeModule", "pmeFile", 100); - kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", 101); - kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", 102); + kdb::tools::errors::PluginMisbehaviorError pmE ("pmeReason", "pmeModule", "pmeFile", "pmeMountPoint", + "pmeConfigFile", 100); + kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", "cswMountPoint", + "cswConfigFile", 101); + kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", "ifwMountPoint", + "ifwConfigFile", 102); pmE.addWarning (csW); pmE.addWarning (ifW); @@ -244,10 +321,14 @@ TEST (Error, Equality) { std::cout << std::endl << "TEST EQUALITY OF WARNINGS AND ERRORS" << std::endl; - kdb::tools::errors::InstallationWarning installationWarning ("Reason", "Module", "File", 42); - kdb::tools::errors::InstallationWarning installationWarning2 ("Reason", "Module", "File", 42); - kdb::tools::errors::InterfaceWarning interfaceWarning ("Reason", "Module", "File", 42); - kdb::tools::errors::InstallationError installationError ("Reason", "Module", "File", 42); + kdb::tools::errors::InstallationWarning installationWarning ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); + kdb::tools::errors::InstallationWarning installationWarning2 ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); + kdb::tools::errors::InterfaceWarning interfaceWarning ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); + kdb::tools::errors::InstallationError installationError ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); ASSERT_EQ (installationWarning, installationWarning); ASSERT_EQ (installationWarning, installationWarning2); @@ -256,8 +337,10 @@ TEST (Error, Equality) ASSERT_NE (installationWarning, installationError); ASSERT_NE (installationError, installationWarning); - kdb::tools::errors::InstallationError installationError1 ("Reason", "Module", "File", 42); - kdb::tools::errors::ResourceError resourceError ("Reason", "Module", "File", 42); + kdb::tools::errors::InstallationError installationError1 ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); + kdb::tools::errors::ResourceError resourceError ("Reason", "Module", "File", "MountPoint", + "ConfigFile", 42); ASSERT_NE (installationError, resourceError); ASSERT_EQ (installationError, installationError1); diff --git a/src/tools/kdb/coloredkdbio.hpp b/src/tools/kdb/coloredkdbio.hpp index 55a4e932db9..ade9b68d036 100644 --- a/src/tools/kdb/coloredkdbio.hpp +++ b/src/tools/kdb/coloredkdbio.hpp @@ -103,9 +103,11 @@ inline std::ostream & printWarnings (std::ostream & os, kdb::Key const & error, cntWarnings = 0; for (auto it = warnings.begin () + 1; it != warnings.end (); ++it) { - auto name = it->getName (); + if (it->isDirectBelow (parent)) { + auto name = it->getName (); + os << ' ' << ++cntWarnings << ": Module " << getErrorColor (ANSI_COLOR::BOLD) << getErrorColor (ANSI_COLOR::BLUE) << warnings.get (name + "/module") << getErrorColor (ANSI_COLOR::RESET) << " issued the warning " << getErrorColor (ANSI_COLOR::BOLD) diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index 1efc4b99916..fcf6dd0fdf2 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -11,7 +11,7 @@ #include #include #include - +#include #include using namespace std; @@ -46,10 +46,10 @@ int ValidateCommand::execute (Cmdline const & cl) kdb.get (ksUnfiltered, root); stringstream streamWarnings; - printWarnings (streamWarnings, root, cl.verbose, cl.debug); + //printWarnings (streamWarnings, root, cl.verbose, cl.debug); string strWarnings = streamWarnings.str (); - if (strWarnings.empty ()) + if (strWarnings.empty ()) //TODO: currently not working (always as with -f) because of disabled printWarnings above { cout << getFormattedSuccessString ("No warnings were issued! :)") << endl; } @@ -65,11 +65,8 @@ int ValidateCommand::execute (Cmdline const & cl) "despite warnings during getting them...") << endl; - - root = cl.createKey (0); // use a fresh root key for kdb.set, so that no warnings from kdb.get remain - // cout << "Now no warnings should be printed: " << endl; - // printWarnings (cerr, root, cl.verbose, cl.debug); - // return 0; + // TODO: Check how to handle, printWarnings consumed the warnings + //root = cl.createKey (0); } else { @@ -82,13 +79,9 @@ int ValidateCommand::execute (Cmdline const & cl) } } - - // cout << "size of all keys in mount point: " << ksUnfiltered.size () << endl; KeySet ksPart (ksUnfiltered.cut (root)); - // cout << "size of requested keys: " << ksPart.size () << endl; /* iterate over the result keys */ - // cout << "Iterating over keys:" << endl; for (Key curKey : ksPart) { /* do lookup (needed for resolving cascading keys) */ @@ -96,31 +89,35 @@ int ValidateCommand::execute (Cmdline const & cl) if (lookupKey.isBinary ()) continue; // only validate string keys - // cout << "1. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() - // << ", sync: " << lookupKey.needSync() << endl; - /* change value (to enable sync flag */ lookupKey.setString (lookupKey.getString () + "^"); - - // cout << "2. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() - // << ", sync: " << lookupKey.needSync() << endl; - /* change value back to original */ std::string tmpStr = lookupKey.getString (); /* remove last char that was added in the previous step */ tmpStr.pop_back (); lookupKey.setString (tmpStr); - - // cout << "3. curKey name: " << lookupKey.getName() << ", value: " << lookupKey.getString() - // << ", sync: " << lookupKey.needSync() << endl; } + /* write back values */ - kdb.set (ksPart, root); + try + { + kdb.set (ksPart, root); + }catch (KDBException & k) { + /* TODO: remove test code & refactor to use new c++ classes for whole function (instead of printWarnigns and printError) */ + std::cout << std::endl << "################" << std::endl << "TEST NEW C++ CLASS:" << std::endl; + tools::errors::Error *err = tools::errors::ErrorFactory::fromKey (root); + std::cout << *err << std::endl << "INCLUDED WARNINGS: " << std::endl; + for (tools::errors::Warning *w : *err) + std::cout << *w << std::endl << "---------------" << std::endl; + delete err; + std::cout << "err deleted!" << std::endl << "################" << std::endl << std::endl; + } printWarnings (streamWarnings, root, cl.verbose, cl.debug); printError (cerr, root, cl.verbose, cl.debug); + kdb.close (); return 0; } From ea3c01d57c52897477dd4932d46cf17f9c45672b Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 17 Jan 2022 17:58:35 +0100 Subject: [PATCH 07/23] doc: add kdb validate tutorial --- doc/tutorials/validation.md | 48 +++++++++++++++++++ .../tutorial_wrapper/CMakeLists.txt | 2 +- 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/doc/tutorials/validation.md b/doc/tutorials/validation.md index fd4ec99c7db..e73be731710 100644 --- a/doc/tutorials/validation.md +++ b/doc/tutorials/validation.md @@ -301,3 +301,51 @@ rm $(kdb get system:/tests/userbackup) kdb rm system:/tests/specbackup kdb rm system:/tests/userbackup ``` + +## Validate Existing Keys + +To check if an existing set of keys can be read and written with the current validation rules `kdb validate` should be used. Validate will read the values of all keys under the point defined as argument in the command line, sets the key value to something different, then back to the original and finally writes that original value back to the key database. All loaded [validation plugins](/src/plugins/README.md) are now used to validate the values of keys with the necessary meta-keys (see above). + +```sh +# mount test config file and set a value +sudo kdb mount test.ini user:/tests/validate range dump +kdb set user:/tests/validate/x 10 + +# add range check to value +kdb meta-set user:/tests/validate/x check/range "1-10" + +# check if validate passes +kdb validate user:/tests/validate + +# get actual path to mounted file +CONFIG_FILE=$(kdb file user:/tests/validate) +echo $CONFIG_FILE + +# unmount to change to invalid configuration +sudo kdb umount user:/tests/validate + +# overwrite config with invalid config +sudo truncate -s 0 $CONFIG_FILE +# force add range check that that makes value invalid +sudo echo 'kdbOpen 2' >> $CONFIG_FILE +sudo echo '$key string 1 2' >> $CONFIG_FILE +sudo echo 'x' >> $CONFIG_FILE +sudo echo '51' >> $CONFIG_FILE +sudo echo '$meta 11 4' >> $CONFIG_FILE +sudo echo 'check/range' >> $CONFIG_FILE +sudo echo '1-50' >> $CONFIG_FILE + +# mount again +sudo kdb mount test.ini user:/tests/validate range dump + +# validation fails now +kdb validate user:/tests/validate +# RET:1 + +# clean up +kdb rm -r user:/tests/validate +sudo kdb umount user:/tests/validate + +$end + +``` \ No newline at end of file diff --git a/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt b/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt index 196880a8477..e912d49d96c 100644 --- a/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt +++ b/tests/shell/shell_recorder/tutorial_wrapper/CMakeLists.txt @@ -35,7 +35,7 @@ add_msr_test (cmerge "${CMAKE_SOURCE_DIR}/doc/tutorials/cmerge.md" REQUIRED_PLUG if (ENABLE_ASAN) message (STATUS "Excluding Markdown Shell Recorder test for `validation`, as it leaks memory and fails with ASAN enabled") else (ENABLE_ASAN) - add_msr_test (tutorial_validation "${CMAKE_SOURCE_DIR}/doc/tutorials/validation.md" REQUIRED_PLUGINS ni validation) + add_msr_test (tutorial_validation "${CMAKE_SOURCE_DIR}/doc/tutorials/validation.md" REQUIRED_PLUGINS ni validation range) add_msr_test ( tutorial_crypto "${CMAKE_SOURCE_DIR}/doc/tutorials/crypto.md" REQUIRED_PLUGINS crypto fcrypt From e2e63809f385f7ae4e0cb0d070dbe7002bed2c92 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 17 Jan 2022 18:10:46 +0100 Subject: [PATCH 08/23] doc: add kdb validate man page --- doc/help/kdb-validate.md | 39 +++++++++++++++++++++++++++++++++++++ doc/man/man1/kdb-validate.1 | 38 ++++++++++++++++++++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 doc/help/kdb-validate.md create mode 100644 doc/man/man1/kdb-validate.1 diff --git a/doc/help/kdb-validate.md b/doc/help/kdb-validate.md new file mode 100644 index 00000000000..c534b051419 --- /dev/null +++ b/doc/help/kdb-validate.md @@ -0,0 +1,39 @@ +# kdb-validate(1) - Validate key values + +## SYNOPSIS + +`kdb validate` + +## DESCRIPTION + +Validate the values of keys below a given name using the loaded validation plugins (eg. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database. + +This command is useful for validating configuration files against +their specifications. + +For keys to be validated, they must contain the 'check'-metakeys +and the respective plugins for validation must be loaded +for the backend that was used while mounting. +If a validation is done while using 'kdb set' +the same validation is also done by 'kdb validate' + +Use -f to do a write test even if the previous read +from the key database has issued warnings. + +## OPTIONS + +- `-d`,`--debug`: + Give debug information or ask debug questions (in interactive mode). +- `-f`, `--force`: + Force the action to be done. +- `-H`, `--help`: + Show the man page. +- `-p `, `--profile `: + Use a different profile for kdb configuration. +- `-v`, `--verbose`: + Explain what is happening. +- `-V`, `--version`: + Print version info. +- `-C `, `--color `: + Print `never/auto(default)/always` colored output. + diff --git a/doc/man/man1/kdb-validate.1 b/doc/man/man1/kdb-validate.1 new file mode 100644 index 00000000000..551d491eec9 --- /dev/null +++ b/doc/man/man1/kdb-validate.1 @@ -0,0 +1,38 @@ +.\" generated with Ronn-NG/v0.10.1 +.\" http://github.com/apjanke/ronn-ng/tree/0.10.1.pre1 +.TH "KDB\-VALIDATE" "1" "January 2022" "" +.SH "NAME" +\fBkdb\-validate\fR \- Validate key values +.SH "SYNOPSIS" +\fBkdb validate\fR +.SH "DESCRIPTION" +Validate the values of keys below a given name using the loaded validation plugins (eg\. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database\. +.P +This command is useful for validating configuration files against their specifications\. +.P +For keys to be validated, they must contain the 'check'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using 'kdb set' the same validation is also done by 'kdb validate' +.P +Use \-f to do a write test even if the previous read from the key database has issued warnings\. +.SH "OPTIONS" +.TP +\fB\-d\fR,\fB\-\-debug\fR +Give debug information or ask debug questions (in interactive mode)\. +.TP +\fB\-f\fR, \fB\-\-force\fR +Force the action to be done\. +.TP +\fB\-H\fR, \fB\-\-help\fR +Show the man page\. +.TP +\fB\-p \fR, \fB\-\-profile \fR +Use a different profile for kdb configuration\. +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Explain what is happening\. +.TP +\fB\-V\fR, \fB\-\-version\fR +Print version info\. +.TP +\fB\-C \fR, \fB\-\-color \fR +Print \fBnever/auto(default)/always\fR colored output\. + From f415a62111734b73e90086668fb986b4c8207ded Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 17 Jan 2022 18:58:02 +0100 Subject: [PATCH 09/23] doc: fix kdb validate shell recorder test --- doc/tutorials/validation.md | 34 ++++++++-------------------------- 1 file changed, 8 insertions(+), 26 deletions(-) diff --git a/doc/tutorials/validation.md b/doc/tutorials/validation.md index e73be731710..8047f991a79 100644 --- a/doc/tutorials/validation.md +++ b/doc/tutorials/validation.md @@ -308,43 +308,25 @@ To check if an existing set of keys can be read and written with the current val ```sh # mount test config file and set a value -sudo kdb mount test.ini user:/tests/validate range dump +sudo kdb mount testvalidate.ini /tests/validate range dump kdb set user:/tests/validate/x 10 # add range check to value -kdb meta-set user:/tests/validate/x check/range "1-10" +kdb meta-set spec:/tests/validate/x check/range "1-10" # check if validate passes -kdb validate user:/tests/validate +kdb validate /tests/validate -# get actual path to mounted file -CONFIG_FILE=$(kdb file user:/tests/validate) -echo $CONFIG_FILE - -# unmount to change to invalid configuration -sudo kdb umount user:/tests/validate - -# overwrite config with invalid config -sudo truncate -s 0 $CONFIG_FILE -# force add range check that that makes value invalid -sudo echo 'kdbOpen 2' >> $CONFIG_FILE -sudo echo '$key string 1 2' >> $CONFIG_FILE -sudo echo 'x' >> $CONFIG_FILE -sudo echo '51' >> $CONFIG_FILE -sudo echo '$meta 11 4' >> $CONFIG_FILE -sudo echo 'check/range' >> $CONFIG_FILE -sudo echo '1-50' >> $CONFIG_FILE - -# mount again -sudo kdb mount test.ini user:/tests/validate range dump +# change allowed range +kdb meta-set -f spec:/tests/validate/x check/range "1-5" # validation fails now -kdb validate user:/tests/validate +kdb validate /tests/validate # RET:1 # clean up -kdb rm -r user:/tests/validate -sudo kdb umount user:/tests/validate +kdb rm -r /tests/validate +sudo kdb umount /tests/validate $end From 542fe705c174e3d6d6ffd1c73bdc4c82ae792366 Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 17 Jan 2022 20:02:15 +0100 Subject: [PATCH 10/23] doc: add doxygen descriptions --- .../tools/src/errors/baseNotification.cpp | 24 +++++++++++++++---- src/libs/tools/src/errors/error.cpp | 20 ++++++++++++++-- src/libs/tools/src/errors/errorFactory.cpp | 11 +++++++++ src/libs/tools/src/errors/warning.cpp | 10 ++++++++ src/tools/kdb/validate.cpp | 12 ++++++++++ 5 files changed, 71 insertions(+), 6 deletions(-) diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index 9549f1d43dd..1b60694bcad 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -45,15 +45,31 @@ std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb eb.toString (outputStream); return outputStream; } - -/* Comparison */ +/** + * @brief Compare to another notification object + * + * Is used in operator==. + * Can be overloaded by subclasses to check additional constraints. + * At least the types of the two objects that get compared should be checked for equality! + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ bool BaseNotification::compare(const BaseNotification& other ELEKTRA_UNUSED) const { - /* Can be overloaded by subclasses to check additional constraints. - * At least the types of the two objects that get compared should be checked for equality! */ return true; } +/** + * @brief Compare fields of notification objects + * + * Also incorporates the compare method to enable subclasses to add constraints to the comparison. + * + * @param other the notification to compare + * + * @return true if objects are equal + */ bool BaseNotification::operator== (const BaseNotification& other) const { return code() == other.code() diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index 0afda04982c..9bbbd0d81bd 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -11,8 +11,14 @@ namespace errors { -/* copy warning to make it independent from source object * - * if the same warning gets added to two different errors, they can be changed independently */ +/** + * @brief Add a warning to an error + * + * The warning is copied to make it independent from the source object. This way the same warning added to two different errors can be + * changed independently. + * + * @param warning the warning to add + */ void Error::addWarning (Warning & warning) { /* TODO: Decide if we should create copies or store the original warnings */ @@ -40,6 +46,16 @@ Warning& Error::operator[](int index) } +/** + * @brief Compare errors + * + * The comparison of data fields is done by operator== in the BaseNotification class. + * This function compares an errors warnings in addition to the notification fields. + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ bool Error::compare(const BaseNotification& other) const { /* comparison of data fields is done by operator== in BaseNotification class */ diff --git a/src/libs/tools/src/errors/errorFactory.cpp b/src/libs/tools/src/errors/errorFactory.cpp index f6396852be3..c007ab08d13 100644 --- a/src/libs/tools/src/errors/errorFactory.cpp +++ b/src/libs/tools/src/errors/errorFactory.cpp @@ -37,6 +37,17 @@ Error* ErrorFactory::create(const std::string & type, const std::string & reason return nullptr; } + +/** + * @brief Create an error from a given key + * + * Reads meta-keys of given key to find error and warnings meta-keys. If no error exists a PureWarningError is created that contains the + * key's warnings. + * + * @param key the key that has the error and warnings + * + * @return the error with warnings + */ /* TODO: Test method */ Error * ErrorFactory::fromKey (kdb::Key key) { diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp index a4f6063af68..43ddfc8224c 100644 --- a/src/libs/tools/src/errors/warning.cpp +++ b/src/libs/tools/src/errors/warning.cpp @@ -8,6 +8,16 @@ namespace tools namespace errors { +/** + * @brief Compare warnings + * + * The comparison of data fields is done by operator== in the BaseNotification class. + * This function compares the type of BaseNotification in addition to the notification fields. + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ bool Warning::compare(const BaseNotification& other) const { /* comparison of data fields is done by operator== in BaseNotification class */ diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index fcf6dd0fdf2..6076d0c694e 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -18,6 +18,18 @@ using namespace std; using namespace kdb; +/** + * @brief Validate key database subtree + * + * Validate the part of the database that is rooted by the key given in the first argument. + * This is done by reading all key values, writing a different value, rewriting the original value and then re-setting the values + * in the key database. All loaded validation plugins are hereby triggered and their warnings are returned. + * + * @param cl the command line + * + * @return 0 if no warnings or errors were found and validation was therefore successful + * @return 1 if some warnings and errors occurred and validation therefore failed + */ int ValidateCommand::execute (Cmdline const & cl) { int argc = cl.arguments.size (); From ea626463495f63ba26ff061fcfe7ae057ce22c3a Mon Sep 17 00:00:00 2001 From: Jakob Date: Mon, 17 Jan 2022 20:48:30 +0100 Subject: [PATCH 11/23] doc: move doxygen to header files --- .../tools/include/errors/baseNotification.hpp | 22 ++++++++++++++++++- src/libs/tools/include/errors/error.hpp | 22 ++++++++++++++++++- .../tools/include/errors/errorFactory.hpp | 11 +++++++++- src/libs/tools/include/errors/warning.hpp | 11 ++++++++++ .../tools/src/errors/baseNotification.cpp | 21 +----------------- src/libs/tools/src/errors/error.cpp | 19 ---------------- src/libs/tools/src/errors/errorFactory.cpp | 10 --------- src/libs/tools/src/errors/warning.cpp | 10 --------- 8 files changed, 64 insertions(+), 62 deletions(-) diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index 96ce12b4e4a..80a6c21be6f 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -47,6 +47,16 @@ class BaseNotification /* string representation */ friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb); /* compare */ + + /** + * @brief Compare fields of notification objects + * + * Also incorporates the compare method to enable subclasses to add constraints to the comparison. + * + * @param other the notification to compare + * + * @return true if objects are equal + */ bool operator== (const BaseNotification& other) const; bool operator!= (const BaseNotification& other) const; @@ -56,7 +66,17 @@ class BaseNotification /* Can be overwritten by subclasses to change the text representation */ std::ostream& toString (std::ostream& outputStream) const; - /* for supporting polymorphism in comparisons */ + /** + * @brief Compare to another notification object + * + * Is used in operator==. + * Can be overloaded by subclasses to check additional constraints. + * At least the types of the two objects that get compared should be checked for equality! + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ virtual bool compare(const BaseNotification& other) const; private: diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 8bfb7c0dc5e..5ed171b9cd0 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -20,7 +20,16 @@ class Error : public BaseNotification using BaseNotification::BaseNotification; virtual ~Error(); - /* An Error can contain 0 to n warnings */ + /** + * @brief Add a warning to an error + * + * The warning is copied to make it independent from the source object. This way the same warning added to two different errors can be + * changed independently. + * + * An Error can contain 0 to n warnings. + * + * @param warning the warning to add + */ void addWarning (Warning & warning); /* getters */ @@ -41,6 +50,17 @@ class Error : public BaseNotification std::vector warnings; protected: + + /** + * @brief Compare errors + * + * The comparison of data fields is done by operator== in the BaseNotification class. + * This function compares an errors warnings in addition to the notification fields. + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ bool compare(const BaseNotification& other) const override; }; } // namespace errors diff --git a/src/libs/tools/include/errors/errorFactory.hpp b/src/libs/tools/include/errors/errorFactory.hpp index b89633911ca..e3fca5021d8 100644 --- a/src/libs/tools/include/errors/errorFactory.hpp +++ b/src/libs/tools/include/errors/errorFactory.hpp @@ -23,7 +23,16 @@ class ErrorFactory static Error * create (const std::string & type, const std::string & reason, const std::string & module, const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line); - /* Creates an Error object including Warnings as provided by the given key. */ + /** + * @brief Create an error from a given key + * + * Reads meta-keys of given key to find error and warnings meta-keys. If no error exists a PureWarningError is created that contains the + * key's warnings. + * + * @param key the key that has the error and warnings + * + * @return the error with warnings + */ static Error * fromKey(kdb::Key key); /* checks if a code and description fit together */ diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp index 22320f9e3dd..56ca84538cf 100644 --- a/src/libs/tools/include/errors/warning.hpp +++ b/src/libs/tools/include/errors/warning.hpp @@ -24,6 +24,17 @@ class Warning : public BaseNotification virtual ~Warning () = default; protected: + + /** + * @brief Compare warnings + * + * The comparison of data fields is done by operator== in the BaseNotification class. + * This function compares the type of BaseNotification in addition to the notification fields. + * + * @param other the notification to compare to + * + * @return true if objects are equal + */ bool compare(const BaseNotification& other) const override; }; diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index 1b60694bcad..96a06cf99d2 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -45,31 +45,12 @@ std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb eb.toString (outputStream); return outputStream; } -/** - * @brief Compare to another notification object - * - * Is used in operator==. - * Can be overloaded by subclasses to check additional constraints. - * At least the types of the two objects that get compared should be checked for equality! - * - * @param other the notification to compare to - * - * @return true if objects are equal - */ + bool BaseNotification::compare(const BaseNotification& other ELEKTRA_UNUSED) const { return true; } -/** - * @brief Compare fields of notification objects - * - * Also incorporates the compare method to enable subclasses to add constraints to the comparison. - * - * @param other the notification to compare - * - * @return true if objects are equal - */ bool BaseNotification::operator== (const BaseNotification& other) const { return code() == other.code() diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index 9bbbd0d81bd..61638f867d0 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -11,14 +11,6 @@ namespace errors { -/** - * @brief Add a warning to an error - * - * The warning is copied to make it independent from the source object. This way the same warning added to two different errors can be - * changed independently. - * - * @param warning the warning to add - */ void Error::addWarning (Warning & warning) { /* TODO: Decide if we should create copies or store the original warnings */ @@ -45,17 +37,6 @@ Warning& Error::operator[](int index) } - -/** - * @brief Compare errors - * - * The comparison of data fields is done by operator== in the BaseNotification class. - * This function compares an errors warnings in addition to the notification fields. - * - * @param other the notification to compare to - * - * @return true if objects are equal - */ bool Error::compare(const BaseNotification& other) const { /* comparison of data fields is done by operator== in BaseNotification class */ diff --git a/src/libs/tools/src/errors/errorFactory.cpp b/src/libs/tools/src/errors/errorFactory.cpp index c007ab08d13..7810ee17aad 100644 --- a/src/libs/tools/src/errors/errorFactory.cpp +++ b/src/libs/tools/src/errors/errorFactory.cpp @@ -38,16 +38,6 @@ Error* ErrorFactory::create(const std::string & type, const std::string & reason } -/** - * @brief Create an error from a given key - * - * Reads meta-keys of given key to find error and warnings meta-keys. If no error exists a PureWarningError is created that contains the - * key's warnings. - * - * @param key the key that has the error and warnings - * - * @return the error with warnings - */ /* TODO: Test method */ Error * ErrorFactory::fromKey (kdb::Key key) { diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp index 43ddfc8224c..a4f6063af68 100644 --- a/src/libs/tools/src/errors/warning.cpp +++ b/src/libs/tools/src/errors/warning.cpp @@ -8,16 +8,6 @@ namespace tools namespace errors { -/** - * @brief Compare warnings - * - * The comparison of data fields is done by operator== in the BaseNotification class. - * This function compares the type of BaseNotification in addition to the notification fields. - * - * @param other the notification to compare to - * - * @return true if objects are equal - */ bool Warning::compare(const BaseNotification& other) const { /* comparison of data fields is done by operator== in BaseNotification class */ From 255217072569626ed541dc92d78932c1b2f63f4a Mon Sep 17 00:00:00 2001 From: flo91 Date: Mon, 17 Jan 2022 18:39:38 +0100 Subject: [PATCH 12/23] libtool: remove small unnecassary comment --- src/libs/tools/include/errors/baseNotification.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index 80a6c21be6f..b819542d6a1 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -4,7 +4,7 @@ #include #include #include -#include // for kdb-types, code and description constants +#include namespace kdb { @@ -91,4 +91,4 @@ class BaseNotification } // namespace errors } // namespace tools } // namespace kdb -#endif // ELEKTRA_BASENOTIFICATION_HPP \ No newline at end of file +#endif // ELEKTRA_BASENOTIFICATION_HPP From ddb59fd2274d4ba5a0746c8e287961a895b422ba Mon Sep 17 00:00:00 2001 From: Jakob Date: Wed, 19 Jan 2022 14:49:01 +0100 Subject: [PATCH 13/23] doc: add string keys only to validate documentation --- doc/help/kdb-validate.md | 3 ++- doc/man/man1/kdb-validate.1 | 4 ++-- doc/tutorials/validation.md | 4 +++- src/tools/kdb/validate.hpp | 3 ++- 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/doc/help/kdb-validate.md b/doc/help/kdb-validate.md index c534b051419..87745c308c4 100644 --- a/doc/help/kdb-validate.md +++ b/doc/help/kdb-validate.md @@ -6,7 +6,7 @@ ## DESCRIPTION -Validate the values of keys below a given name using the loaded validation plugins (eg. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database. +Validate the values of string keys below a given name using the loaded validation plugins (eg. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database. This command is useful for validating configuration files against their specifications. @@ -16,6 +16,7 @@ and the respective plugins for validation must be loaded for the backend that was used while mounting. If a validation is done while using 'kdb set' the same validation is also done by 'kdb validate' +Only string keys are validated! Binary keys are skipped! Use -f to do a write test even if the previous read from the key database has issued warnings. diff --git a/doc/man/man1/kdb-validate.1 b/doc/man/man1/kdb-validate.1 index 551d491eec9..288bfbf5f43 100644 --- a/doc/man/man1/kdb-validate.1 +++ b/doc/man/man1/kdb-validate.1 @@ -6,11 +6,11 @@ .SH "SYNOPSIS" \fBkdb validate\fR .SH "DESCRIPTION" -Validate the values of keys below a given name using the loaded validation plugins (eg\. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database\. +Validate the values of string keys below a given name using the loaded validation plugins (eg\. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database\. .P This command is useful for validating configuration files against their specifications\. .P -For keys to be validated, they must contain the 'check'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using 'kdb set' the same validation is also done by 'kdb validate' +For keys to be validated, they must contain the 'check'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using 'kdb set' the same validation is also done by 'kdb validate' Only string keys are validated! Binary keys are skipped! .P Use \-f to do a write test even if the previous read from the key database has issued warnings\. .SH "OPTIONS" diff --git a/doc/tutorials/validation.md b/doc/tutorials/validation.md index 8047f991a79..0e8101e8c80 100644 --- a/doc/tutorials/validation.md +++ b/doc/tutorials/validation.md @@ -304,7 +304,9 @@ kdb rm system:/tests/userbackup ## Validate Existing Keys -To check if an existing set of keys can be read and written with the current validation rules `kdb validate` should be used. Validate will read the values of all keys under the point defined as argument in the command line, sets the key value to something different, then back to the original and finally writes that original value back to the key database. All loaded [validation plugins](/src/plugins/README.md) are now used to validate the values of keys with the necessary meta-keys (see above). +To check if an existing set of keys can be read and written with the current validation rules `kdb validate` should be used. Validate will read the values of all string keys under the point defined as argument in the command line, sets the key value to something different, then back to the original and finally writes that original value back to the key database. All loaded [validation plugins](/src/plugins/README.md) are now used to validate the values of keys with the necessary meta-keys (see above). + +Only string keys are validated! Binary keys are skipped! ```sh # mount test config file and set a value diff --git a/src/tools/kdb/validate.hpp b/src/tools/kdb/validate.hpp index d5943f1f1ec..8d98e01a730 100644 --- a/src/tools/kdb/validate.hpp +++ b/src/tools/kdb/validate.hpp @@ -33,7 +33,7 @@ class ValidateCommand : public Command virtual std::string getShortHelpText () override { - return "Validate the values of keys below a given name."; + return "Validate the values of string keys below a given name."; } virtual std::string getLongHelpText () override @@ -45,6 +45,7 @@ class ValidateCommand : public Command "for the backend that was used while mounting.\n" "If a validation is done while using 'kdb set'\n" "the same validation is also done by 'kdb validate'\n" + "Only string keys are validated!\n" "\n" "Use -f to do a write test even if the previous read\n" "from the key database has issued warnings."; From 8974542fcb7cc210cd0c20ce28be9f158f65ecc5 Mon Sep 17 00:00:00 2001 From: Jakob Date: Wed, 19 Jan 2022 14:50:48 +0100 Subject: [PATCH 14/23] doc: add string keys only to doxygen as well --- src/tools/kdb/validate.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index 6076d0c694e..962d8fd0245 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -22,8 +22,9 @@ using namespace kdb; * @brief Validate key database subtree * * Validate the part of the database that is rooted by the key given in the first argument. - * This is done by reading all key values, writing a different value, rewriting the original value and then re-setting the values + * This is done by reading all string key values, writing a different value, rewriting the original value and then re-setting the values * in the key database. All loaded validation plugins are hereby triggered and their warnings are returned. + * Only string keys are validated! Binary keys will be skipped! * * @param cl the command line * From 9894405847d2b67e0c3f1fd479e0274be1ba99bb Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 19 Jan 2022 18:26:30 +0100 Subject: [PATCH 15/23] libtools: fix or clarify most aspects that were reviewd in the PR #4224 by @markus2330 --- doc/help/kdb-validate.md | 10 +++---- doc/man/man1/kdb-validate.1 | 29 ++++++++++++++----- .../tools/include/errors/baseNotification.hpp | 9 +++--- src/libs/tools/include/errors/error.hpp | 7 +++-- src/libs/tools/include/errors/errorTypes.hpp | 5 +++- src/tools/kdb/validate.cpp | 5 ++-- 6 files changed, 44 insertions(+), 21 deletions(-) diff --git a/doc/help/kdb-validate.md b/doc/help/kdb-validate.md index 87745c308c4..fc4bf811d83 100644 --- a/doc/help/kdb-validate.md +++ b/doc/help/kdb-validate.md @@ -14,19 +14,19 @@ their specifications. For keys to be validated, they must contain the 'check'-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting. -If a validation is done while using 'kdb set' -the same validation is also done by 'kdb validate' +If a validation is done while using `kdb set` or `kdb get` +the same validation is also done by `kdb validate` Only string keys are validated! Binary keys are skipped! -Use -f to do a write test even if the previous read +Use `-f` to do a write-test even if the previous read from the key database has issued warnings. ## OPTIONS - `-d`,`--debug`: - Give debug information or ask debug questions (in interactive mode). + Give debug information. - `-f`, `--force`: - Force the action to be done. +Force writing the configuration even on warnings. - `-H`, `--help`: Show the man page. - `-p `, `--profile `: diff --git a/doc/man/man1/kdb-validate.1 b/doc/man/man1/kdb-validate.1 index 288bfbf5f43..f129462a002 100644 --- a/doc/man/man1/kdb-validate.1 +++ b/doc/man/man1/kdb-validate.1 @@ -1,37 +1,52 @@ -.\" generated with Ronn-NG/v0.10.1 -.\" http://github.com/apjanke/ronn-ng/tree/0.10.1.pre1 -.TH "KDB\-VALIDATE" "1" "January 2022" "" +.\" generated with Ronn/v0.7.3 +.\" http://github.com/rtomayko/ronn/tree/0.7.3 +. +.TH "KDB\-VALIDATE" "1" "January 2022" "" "" +. .SH "NAME" \fBkdb\-validate\fR \- Validate key values +. .SH "SYNOPSIS" \fBkdb validate\fR +. .SH "DESCRIPTION" Validate the values of string keys below a given name using the loaded validation plugins (eg\. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database\. +. .P This command is useful for validating configuration files against their specifications\. +. .P -For keys to be validated, they must contain the 'check'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using 'kdb set' the same validation is also done by 'kdb validate' Only string keys are validated! Binary keys are skipped! +For keys to be validated, they must contain the \'check\'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using \fBkdb set\fR or \fBkdb get\fR the same validation is also done by \fBkdb validate\fR Only string keys are validated! Binary keys are skipped! +. .P -Use \-f to do a write test even if the previous read from the key database has issued warnings\. +Use \fB\-f\fR to do a write\-test even if the previous read from the key database has issued warnings\. +. .SH "OPTIONS" +. .TP \fB\-d\fR,\fB\-\-debug\fR -Give debug information or ask debug questions (in interactive mode)\. +Give debug information\. +. .TP \fB\-f\fR, \fB\-\-force\fR -Force the action to be done\. +Force writing the configuration even on warnings\. +. .TP \fB\-H\fR, \fB\-\-help\fR Show the man page\. +. .TP \fB\-p \fR, \fB\-\-profile \fR Use a different profile for kdb configuration\. +. .TP \fB\-v\fR, \fB\-\-verbose\fR Explain what is happening\. +. .TP \fB\-V\fR, \fB\-\-version\fR Print version info\. +. .TP \fB\-C \fR, \fB\-\-color \fR Print \fBnever/auto(default)/always\fR colored output\. diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index b819542d6a1..af2908d5682 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -12,9 +12,11 @@ namespace tools { namespace errors { -/* common abstract class for warnings and errors */ -/* Because warning and errors share the same data members, a method can accept a ErrBase argument and the caller - * can create an Error or a Warning based on the provided object. */ +/* Common abstract class for warnings and errors. + * Because warning and errors share the same data members, + * a method can accept a BaseNotification argument and + * the caller can create an Error or a Warning + * based on the provided object. */ class BaseNotification { public: @@ -46,7 +48,6 @@ class BaseNotification /* string representation */ friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb); - /* compare */ /** * @brief Compare fields of notification objects diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 5ed171b9cd0..649dd11dc55 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -23,10 +23,13 @@ class Error : public BaseNotification /** * @brief Add a warning to an error * - * The warning is copied to make it independent from the source object. This way the same warning added to two different errors can be + * The warning is copied to make it independent from the source object. + * This way the same warning added to two different errors can be * changed independently. * - * An Error can contain 0 to n warnings. + * An object of type `Error` can contain 0 to n warnings, + * like Keys in the C-API can contain one error, + * but multiple warnings. * * @param warning the warning to add */ diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp index 2c1484892c6..d66f46a4a7b 100644 --- a/src/libs/tools/include/errors/errorTypes.hpp +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -11,7 +11,10 @@ namespace tools namespace errors { -/* Not an error by itself, but a container for multiple warnings */ +/* Not an Error by itself, but a container for multiple Warnings, + * like keys in the C-API have at most one error, but 0 to n warnings. + * This way the content of such a key can be stored in a single Error object, + * even if the key doesn't contain an actual error. */ class PureWarningError : public Error { public: diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index 962d8fd0245..e971c6863b5 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -100,9 +100,10 @@ int ValidateCommand::execute (Cmdline const & cl) /* do lookup (needed for resolving cascading keys) */ Key lookupKey = ksPart.lookup (curKey); - if (lookupKey.isBinary ()) continue; // only validate string keys + /* only validate string keys */ + if (lookupKey.isBinary ()) continue; - /* change value (to enable sync flag */ + /* change value to enable sync flag */ lookupKey.setString (lookupKey.getString () + "^"); /* change value back to original */ From 06d5ccd82c017b229dbbf9dc79bd50adaca992f1 Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 19 Jan 2022 23:16:46 +0100 Subject: [PATCH 16/23] libtools: bring the branch into a working state --- doc/tutorials/validation.md | 24 +++--- .../tools/include/errors/baseNotification.hpp | 18 +++- src/libs/tools/include/errors/error.hpp | 16 ++++ src/libs/tools/include/errors/errorTypes.hpp | 1 - .../tools/src/errors/baseNotification.cpp | 46 +++++----- src/libs/tools/src/errors/error.cpp | 29 +++++-- src/tools/kdb/validate.cpp | 86 +++++++++---------- 7 files changed, 131 insertions(+), 89 deletions(-) diff --git a/doc/tutorials/validation.md b/doc/tutorials/validation.md index 0e8101e8c80..5ae1438822c 100644 --- a/doc/tutorials/validation.md +++ b/doc/tutorials/validation.md @@ -310,26 +310,26 @@ Only string keys are validated! Binary keys are skipped! ```sh # mount test config file and set a value -sudo kdb mount testvalidate.ini /tests/validate range dump -kdb set user:/tests/validate/x 10 +sudo kdb mount range.ecf /tests/range range dump -# add range check to value -kdb meta-set spec:/tests/validate/x check/range "1-10" +# set value +kdb set user:/tests/range/value 5 + +# add range check to all keys under /tests/range/ +kdb meta-set spec:/tests/range/_ check/range "1-10" # check if validate passes -kdb validate /tests/validate +kdb validate /tests/range -# change allowed range -kdb meta-set -f spec:/tests/validate/x check/range "1-5" +# set new key to invalid value (with kdb set -f) +kdb set -f user:/tests/range/value2 11 # validation fails now -kdb validate /tests/validate +kdb validate /tests/range # RET:1 # clean up -kdb rm -r /tests/validate -sudo kdb umount /tests/validate - -$end +kdb rm -r /tests/range/ +sudo kdb umount /tests/range ``` \ No newline at end of file diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index af2908d5682..cc94ab4400b 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -64,9 +64,6 @@ class BaseNotification protected: BaseNotification () = default; - /* Can be overwritten by subclasses to change the text representation */ - std::ostream& toString (std::ostream& outputStream) const; - /** * @brief Compare to another notification object * @@ -80,6 +77,21 @@ class BaseNotification */ virtual bool compare(const BaseNotification& other) const; + /* Can be overwritten by subclasses to change the text representation */ + + /** + * @brief Get a text representation of the notification. + * + * Is used in operator<<. + * Can be overloaded by subclasses to append additional text. + * + * @param outputStream The stream to append the text to, + * used by the operator `<<` + * + * @return The given stream with additional text appended. + */ + virtual std::ostream& toString (std::ostream& outputStream) const; + private: std::string m_reason; std::string m_module; diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 649dd11dc55..1bd23987d0d 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -65,6 +65,22 @@ class Error : public BaseNotification * @return true if objects are equal */ bool compare(const BaseNotification& other) const override; + + /** + * @brief Create a text representation of the Error + * + * A string that contains the text representation + * defined by baseNotification.cpp as well as the + * text representation of all warnings that the + * Error object contains appended to the given stream. + * + * @param outputStream The stream were the string representations + * should be appended. + * + * @return The given stream with the created string + * written to it. + */ + std::ostream & toString (std::ostream & outputStream) const override; }; } // namespace errors } // namespace tools diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp index d66f46a4a7b..18f09221227 100644 --- a/src/libs/tools/include/errors/errorTypes.hpp +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -1,7 +1,6 @@ #ifndef ELEKTRA_ERRORTYPES_HPP #define ELEKTRA_ERRORTYPES_HPP - #include "error.hpp" namespace kdb diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index 96a06cf99d2..e3213641f96 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -30,14 +30,14 @@ void BaseNotification::setData (const std::string & reason, const std::string & /* String representation */ std::ostream& BaseNotification::toString (std::ostream & outputStream) const { - return outputStream << "Code: " << code() - << "\nDescription: " << description() - << "\nReason: " << m_reason - << "\nModule: " << m_module - << "\nFile: " << m_file - << "\nMount point: " << m_mountPoint - << "\nConfig file: " << m_configFile - << "\nLine: " << std::to_string (m_line); + return outputStream << "Code: " << code () << std::endl + << "Description: " << description () << std::endl + << "Reason: " << m_reason << std::endl + << "Module: " << m_module << std::endl + << "File: " << m_file << std::endl + << "Mount point: " << m_mountPoint << std::endl + << "Config file: " << m_configFile << std::endl + << "Line: " << std::to_string (m_line); } std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb) @@ -53,15 +53,15 @@ bool BaseNotification::compare(const BaseNotification& other ELEKTRA_UNUSED) con bool BaseNotification::operator== (const BaseNotification& other) const { - return code() == other.code() - && description() == other.description() - && reason() == other.reason() - && module() == other.module() - && file() == other.file() - && mountPoint() == other.mountPoint() - && configFile() == other.configFile() - && line() == other.line() - && this->compare(other); + return code () == other.code () + && description () == other.description () + && reason () == other.reason () + && module () == other.module () + && file () == other.file () + && mountPoint () == other.mountPoint () + && configFile () == other.configFile () + && line () == other.line () + && this->compare (other); } bool BaseNotification::operator!= (const BaseNotification& other) const @@ -70,12 +70,12 @@ bool BaseNotification::operator!= (const BaseNotification& other) const } /* setters */ -std::string& BaseNotification::reason() { return m_reason; } -std::string& BaseNotification::module() { return m_module; } -std::string& BaseNotification::file() { return m_file; } -std::string& BaseNotification::mountPoint() { return m_mountPoint; } -std::string& BaseNotification::configFile() { return m_configFile; } -kdb::long_t& BaseNotification::line() { return m_line; } +std::string & BaseNotification::reason() { return m_reason; } +std::string & BaseNotification::module() { return m_module; } +std::string & BaseNotification::file() { return m_file; } +std::string & BaseNotification::mountPoint() { return m_mountPoint; } +std::string & BaseNotification::configFile() { return m_configFile; } +kdb::long_t & BaseNotification::line() { return m_line; } /* getters */ const std::string & BaseNotification::reason () const { return m_reason; } diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index 61638f867d0..490989f6627 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -48,18 +48,17 @@ bool Error::compare(const BaseNotification& other) const else { /* compare warnings */ - for (Warning *w : warnings) + for (const Warning *w : warnings) { - /*TODO: Decide if ordering of warnings should be considered for equality. - * Currently two errors are equal if they contain the same warnings (compared by member values), - * even if they have different orders in the internal vector.*/ + /* Two errors are equal if they contain the same warnings (compared by member values), + * even if they have different orders in the internal vector. */ - /*TODO: Currently copies of warnings are stored by the addWarning(Warning&) method. + /* TODO: Currently copies of warnings are stored by the addWarning(Warning&) method. * If we decide to store the original warnings, then we have to decide if * two different Warnings (not the same address in mem) are considered equal - * if the member values are equal.*/ + * if the member values are equal. */ bool equalWarningFound = false; - for (Warning *ow : pOtherError->warnings) + for (const Warning *ow : pOtherError->warnings) { if (*w == *ow) { @@ -83,6 +82,22 @@ Error::~Error () delete w; } +std::ostream & Error::toString (std::ostream & outputStream) const +{ + BaseNotification::toString (outputStream); + + kdb::long_t i = 0; + if (warnings.size() > 0) + { + outputStream << std::endl << std::endl << "The following warnings were attachted to the Error: " << std::endl << std::endl; + for (const Warning *w : warnings) + outputStream << "Warning " << ++i << ": " << std::endl << *w << std::endl; + } + + + return outputStream; +} + } // namespace errors } // namespace tools diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index e971c6863b5..83763a71612 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -7,12 +7,11 @@ */ #include - #include #include -#include #include #include +#include /* for removeNamespace (Key) */ using namespace std; using namespace kdb; @@ -39,37 +38,38 @@ int ValidateCommand::execute (Cmdline const & cl) throw invalid_argument ("1 argument needed"); } - cout << "The given path was: " << cl.arguments[0] << endl; - KeySet ksUnfiltered; + /* use the given cmd line argument as the start key */ Key root = cl.createKey (0); - /* if -f (force) was given, the namespace is kept - * and check-constraints in the spec:/ namespace - * are not considered --> the key can be set to a - * value that does not pass the validation criteria, - * otherwise a cascading key is created for the - * following kdb.get() */ - // Key parentKey = cl.getParentKey (root); + if (cl.verbose) + { + cout << "The name of the root key is: " + root.getName() << endl; + } + + /* Remove namespace -> create cascading key, so that + * check-constraints in the spec:/ namespace are considered. */ + Key parentKey = removeNamespace (root); // do not resume on any get errors // otherwise the user might break // the config kdb.get (ksUnfiltered, root); - stringstream streamWarnings; - //printWarnings (streamWarnings, root, cl.verbose, cl.debug); + /* Convert result of kdb.get to Error object of the C++ errors/warnings API */ + tools::errors::Error *result = tools::errors::ErrorFactory::fromKey (root); - string strWarnings = streamWarnings.str (); - if (strWarnings.empty ()) //TODO: currently not working (always as with -f) because of disabled printWarnings above + /* If no warnings or errors occurred, the ErrorFactory returns a nullptr. */ + if (result) { - cout << getFormattedSuccessString ("No warnings were issued! :)") << endl; - } - else - { - cerr << strWarnings; - root.clear (); + cout << getFormattedInfoString ("The following warnings were issued while" + " trying to get the values of the keys: ") << endl << endl; + + cerr << *result << endl << endl; + + /* After printing the Warnings, the object is no longer needed. */ + delete result; if (cl.force) { @@ -77,9 +77,6 @@ int ValidateCommand::execute (Cmdline const & cl) "Because -f was given, we now try to set the values " "despite warnings during getting them...") << endl; - - // TODO: Check how to handle, printWarnings consumed the warnings - //root = cl.createKey (0); } else { @@ -87,10 +84,13 @@ int ValidateCommand::execute (Cmdline const & cl) "The validation was stopped because of warnings " "while getting the values!") << endl; - kdb.close (); return 1; } } + else + { + cout << getFormattedSuccessString ("No warnings were issued! :)") << endl; + } KeySet ksPart (ksUnfiltered.cut (root)); @@ -118,36 +118,36 @@ int ValidateCommand::execute (Cmdline const & cl) try { kdb.set (ksPart, root); - }catch (KDBException & k) { - /* TODO: remove test code & refactor to use new c++ classes for whole function (instead of printWarnigns and printError) */ - std::cout << std::endl << "################" << std::endl << "TEST NEW C++ CLASS:" << std::endl; - tools::errors::Error *err = tools::errors::ErrorFactory::fromKey (root); - std::cout << *err << std::endl << "INCLUDED WARNINGS: " << std::endl; - for (tools::errors::Warning *w : *err) - std::cout << *w << std::endl << "---------------" << std::endl; - delete err; - std::cout << "err deleted!" << std::endl << "################" << std::endl << std::endl; + return 0; + } + catch (KDBException & k) + { + cout << getFormattedInfoString ("The following error was issued while trying to set the values back: ") << endl << endl; + result = tools::errors::ErrorFactory::fromKey (root); + cerr << *result << endl; + delete result; + return 1; } - - printWarnings (streamWarnings, root, cl.verbose, cl.debug); - printError (cerr, root, cl.verbose, cl.debug); - - kdb.close (); - return 0; } std::string ValidateCommand::getFormattedErrorString (const std::string & str) { - return getErrorColor (ANSI_COLOR::BOLD) + getErrorColor (ANSI_COLOR::MAGENTA) + str + getErrorColor (ANSI_COLOR::RESET); + return getErrorColor (ANSI_COLOR::BOLD) + + getErrorColor (ANSI_COLOR::MAGENTA) + str + + getErrorColor (ANSI_COLOR::RESET); } std::string ValidateCommand::getFormattedSuccessString (const std::string & str) { - return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::GREEN) + str + getStdColor (ANSI_COLOR::RESET); + return getStdColor (ANSI_COLOR::BOLD) + + getStdColor (ANSI_COLOR::GREEN) + str + + getStdColor (ANSI_COLOR::RESET); } std::string ValidateCommand::getFormattedInfoString (const std::string & str) { - return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::YELLOW) + str + getStdColor (ANSI_COLOR::RESET); + return getStdColor (ANSI_COLOR::BOLD) + + getStdColor (ANSI_COLOR::YELLOW) + str + + getStdColor (ANSI_COLOR::RESET); } From 9daf8015a21728eb02fba30f8545f40d9ca4af3e Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 19 Jan 2022 23:27:53 +0100 Subject: [PATCH 17/23] libtools: reformat code, close #3674 --- doc/help/kdb-validate.md | 3 +- doc/tutorials/validation.md | 2 +- .../tools/include/errors/baseNotification.hpp | 38 +- src/libs/tools/include/errors/error.hpp | 42 +- .../tools/include/errors/errorFactory.hpp | 12 +- src/libs/tools/include/errors/errorTypes.hpp | 76 +-- src/libs/tools/include/errors/warning.hpp | 5 +- .../tools/include/errors/warningFactory.hpp | 2 +- .../tools/include/errors/warningTypes.hpp | 81 +-- .../tools/src/errors/baseNotification.cpp | 96 ++-- src/libs/tools/src/errors/error.cpp | 33 +- src/libs/tools/src/errors/errorFactory.cpp | 62 +-- src/libs/tools/src/errors/errorTypes.cpp | 142 ++++-- src/libs/tools/src/errors/warning.cpp | 2 +- src/libs/tools/src/errors/warningFactory.cpp | 9 +- src/libs/tools/src/errors/warningTypes.cpp | 171 ++++--- src/libs/tools/tests/testtool_error.cpp | 462 ++++++++---------- src/tools/kdb/validate.cpp | 31 +- 18 files changed, 691 insertions(+), 578 deletions(-) diff --git a/doc/help/kdb-validate.md b/doc/help/kdb-validate.md index fc4bf811d83..599aea5fd60 100644 --- a/doc/help/kdb-validate.md +++ b/doc/help/kdb-validate.md @@ -26,7 +26,7 @@ from the key database has issued warnings. - `-d`,`--debug`: Give debug information. - `-f`, `--force`: -Force writing the configuration even on warnings. + Force writing the configuration even on warnings. - `-H`, `--help`: Show the man page. - `-p `, `--profile `: @@ -37,4 +37,3 @@ Force writing the configuration even on warnings. Print version info. - `-C `, `--color `: Print `never/auto(default)/always` colored output. - diff --git a/doc/tutorials/validation.md b/doc/tutorials/validation.md index 5ae1438822c..63f6b31634b 100644 --- a/doc/tutorials/validation.md +++ b/doc/tutorials/validation.md @@ -332,4 +332,4 @@ kdb validate /tests/range kdb rm -r /tests/range/ sudo kdb umount /tests/range -``` \ No newline at end of file +``` diff --git a/src/libs/tools/include/errors/baseNotification.hpp b/src/libs/tools/include/errors/baseNotification.hpp index cc94ab4400b..41adc81fe55 100644 --- a/src/libs/tools/include/errors/baseNotification.hpp +++ b/src/libs/tools/include/errors/baseNotification.hpp @@ -1,10 +1,10 @@ #ifndef ELEKTRA_BASENOTIFICATION_HPP #define ELEKTRA_BASENOTIFICATION_HPP -#include +#include #include +#include #include -#include namespace kdb { @@ -21,20 +21,20 @@ class BaseNotification { public: /* constructor */ - BaseNotification (std::string reason, std::string module, std::string file, std::string mountPoint, - std::string configFile, kdb::long_t line); + BaseNotification (std::string reason, std::string module, std::string file, std::string mountPoint, std::string configFile, + kdb::long_t line); /* setters */ - void setData (const std::string & reason, const std::string & module, const std::string & file, - const std::string & mountPoint, const std::string & configFile, kdb::long_t line); + void setData (const std::string & reason, const std::string & module, const std::string & file, const std::string & mountPoint, + const std::string & configFile, kdb::long_t line); /* get references (for setting and getting member values) */ - std::string & reason(); - std::string & module(); - std::string & file(); - std::string & mountPoint(); - std::string & configFile(); - kdb::long_t & line(); + std::string & reason (); + std::string & module (); + std::string & file (); + std::string & mountPoint (); + std::string & configFile (); + kdb::long_t & line (); const std::string & reason () const; const std::string & module () const; const std::string & file () const; @@ -43,11 +43,11 @@ class BaseNotification const kdb::long_t & line () const; /* fixed values per Class, taken from C-makro definitions in /src/include/kdberrors.h */ - virtual std::string code() const = 0; - virtual std::string description() const = 0; + virtual std::string code () const = 0; + virtual std::string description () const = 0; /* string representation */ - friend std::ostream& operator<< (std::ostream& outputStream, const BaseNotification& eb); + friend std::ostream & operator<< (std::ostream & outputStream, const BaseNotification & eb); /** * @brief Compare fields of notification objects @@ -58,8 +58,8 @@ class BaseNotification * * @return true if objects are equal */ - bool operator== (const BaseNotification& other) const; - bool operator!= (const BaseNotification& other) const; + bool operator== (const BaseNotification & other) const; + bool operator!= (const BaseNotification & other) const; protected: BaseNotification () = default; @@ -75,7 +75,7 @@ class BaseNotification * * @return true if objects are equal */ - virtual bool compare(const BaseNotification& other) const; + virtual bool compare (const BaseNotification & other) const; /* Can be overwritten by subclasses to change the text representation */ @@ -90,7 +90,7 @@ class BaseNotification * * @return The given stream with additional text appended. */ - virtual std::ostream& toString (std::ostream& outputStream) const; + virtual std::ostream & toString (std::ostream & outputStream) const; private: std::string m_reason; diff --git a/src/libs/tools/include/errors/error.hpp b/src/libs/tools/include/errors/error.hpp index 1bd23987d0d..c7c69fdf555 100644 --- a/src/libs/tools/include/errors/error.hpp +++ b/src/libs/tools/include/errors/error.hpp @@ -1,8 +1,8 @@ #ifndef ELEKTRA_ERROR_HPP #define ELEKTRA_ERROR_HPP -#include #include "errors/warning.hpp" +#include namespace kdb { @@ -15,10 +15,9 @@ namespace errors class Error : public BaseNotification { public: - /* inherit constructors */ using BaseNotification::BaseNotification; - virtual ~Error(); + virtual ~Error (); /** * @brief Add a warning to an error @@ -39,21 +38,38 @@ class Error : public BaseNotification kdb::long_t warningCount (); /* iterator functionality */ - std::vector::iterator begin() { return warnings.begin(); } - std::vector::iterator end() { return warnings.end(); } - std::vector::const_iterator begin() const { return warnings.begin(); } - std::vector::const_iterator end() const { return warnings.begin(); } - std::vector::const_iterator cbegin() const { return warnings.cbegin(); } - std::vector::const_iterator cend() const { return warnings.cend(); } + std::vector::iterator begin () + { + return warnings.begin (); + } + std::vector::iterator end () + { + return warnings.end (); + } + std::vector::const_iterator begin () const + { + return warnings.begin (); + } + std::vector::const_iterator end () const + { + return warnings.begin (); + } + std::vector::const_iterator cbegin () const + { + return warnings.cbegin (); + } + std::vector::const_iterator cend () const + { + return warnings.cend (); + } /* get warning by index */ - Warning& operator[](int index); + Warning & operator[] (int index); private: - std::vector warnings; + std::vector warnings; protected: - /** * @brief Compare errors * @@ -64,7 +80,7 @@ class Error : public BaseNotification * * @return true if objects are equal */ - bool compare(const BaseNotification& other) const override; + bool compare (const BaseNotification & other) const override; /** * @brief Create a text representation of the Error diff --git a/src/libs/tools/include/errors/errorFactory.hpp b/src/libs/tools/include/errors/errorFactory.hpp index e3fca5021d8..dd52731bdea 100644 --- a/src/libs/tools/include/errors/errorFactory.hpp +++ b/src/libs/tools/include/errors/errorFactory.hpp @@ -20,23 +20,23 @@ class ErrorFactory public: /* takes one of the ELEKTRA_ERROR_* constants (e.g. ELEKTRA_ERROR_OUT_OF_MEMORY) * from /src/include/kdberrors.h as a parameter */ - static Error * create (const std::string & type, const std::string & reason, const std::string & module, - const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line); + static Error * create (const std::string & type, const std::string & reason, const std::string & module, const std::string & file, + const std::string & mountPoint, const std::string & configFile, kdb::long_t line); /** * @brief Create an error from a given key * - * Reads meta-keys of given key to find error and warnings meta-keys. If no error exists a PureWarningError is created that contains the - * key's warnings. + * Reads meta-keys of given key to find error and warnings meta-keys. If no error exists a PureWarningError is created that contains + * the key's warnings. * * @param key the key that has the error and warnings * * @return the error with warnings */ - static Error * fromKey(kdb::Key key); + static Error * fromKey (kdb::Key key); /* checks if a code and description fit together */ - static bool checkErrorCodeDesc(const std::string & code, const std::string & description); + static bool checkErrorCodeDesc (const std::string & code, const std::string & description); }; } // namespace errors diff --git a/src/libs/tools/include/errors/errorTypes.hpp b/src/libs/tools/include/errors/errorTypes.hpp index 18f09221227..da3ba859d44 100644 --- a/src/libs/tools/include/errors/errorTypes.hpp +++ b/src/libs/tools/include/errors/errorTypes.hpp @@ -17,14 +17,15 @@ namespace errors class PureWarningError : public Error { public: - PureWarningError () - : Error { "No error, only warnings.", "", "", "", "", 0} {} + PureWarningError () : Error{ "No error, only warnings.", "", "", "", "", 0 } + { + } - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; @@ -33,91 +34,100 @@ class ResourceError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class OutOfMemoryError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class InstallationError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; -class InternalError: public Error +class InternalError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class InterfaceError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class PluginMisbehaviorError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ConflictingStateError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ValidationSyntacticError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ValidationSemanticError : public Error { public: using Error::Error; - std::string code() const override; - std::string description() const override; + std::string code () const override; + std::string description () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; } // namespace errors diff --git a/src/libs/tools/include/errors/warning.hpp b/src/libs/tools/include/errors/warning.hpp index 56ca84538cf..2e2ddf20f89 100644 --- a/src/libs/tools/include/errors/warning.hpp +++ b/src/libs/tools/include/errors/warning.hpp @@ -18,13 +18,12 @@ class Warning : public BaseNotification public: /* inherit constructors */ using BaseNotification::BaseNotification; - virtual Warning* clone() const = 0; + virtual Warning * clone () const = 0; /* needed for freeing the elements of the Warning-container in Error-class */ virtual ~Warning () = default; protected: - /** * @brief Compare warnings * @@ -35,7 +34,7 @@ class Warning : public BaseNotification * * @return true if objects are equal */ - bool compare(const BaseNotification& other) const override; + bool compare (const BaseNotification & other) const override; }; } // namespace errors diff --git a/src/libs/tools/include/errors/warningFactory.hpp b/src/libs/tools/include/errors/warningFactory.hpp index a4ec52786ed..91895d5c17e 100644 --- a/src/libs/tools/include/errors/warningFactory.hpp +++ b/src/libs/tools/include/errors/warningFactory.hpp @@ -24,7 +24,7 @@ class WarningFactory const std::string & mountPoint, const std::string & configFile, kdb::long_t line); /* checks if a code and description fit together */ - static bool checkWarningCodeDesc(const std::string & code, const std::string & description); + static bool checkWarningCodeDesc (const std::string & code, const std::string & description); }; } // namespace errors diff --git a/src/libs/tools/include/errors/warningTypes.hpp b/src/libs/tools/include/errors/warningTypes.hpp index c34dfc51678..30033d0c743 100644 --- a/src/libs/tools/include/errors/warningTypes.hpp +++ b/src/libs/tools/include/errors/warningTypes.hpp @@ -18,11 +18,12 @@ class ResourceWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - ResourceWarning* clone() const override; + std::string code () const override; + std::string description () const override; + ResourceWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class OutOfMemoryWarning : public Warning @@ -30,11 +31,12 @@ class OutOfMemoryWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - OutOfMemoryWarning* clone() const override; + std::string code () const override; + std::string description () const override; + OutOfMemoryWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class InstallationWarning : public Warning @@ -42,11 +44,12 @@ class InstallationWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - InstallationWarning* clone() const override; + std::string code () const override; + std::string description () const override; + InstallationWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class InternalWarning : public Warning @@ -54,11 +57,12 @@ class InternalWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - InternalWarning* clone() const override; + std::string code () const override; + std::string description () const override; + InternalWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class InterfaceWarning : public Warning @@ -66,11 +70,12 @@ class InterfaceWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - InterfaceWarning* clone() const override; + std::string code () const override; + std::string description () const override; + InterfaceWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class PluginMisbehaviorWarning : public Warning @@ -78,11 +83,12 @@ class PluginMisbehaviorWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - PluginMisbehaviorWarning* clone() const override; + std::string code () const override; + std::string description () const override; + PluginMisbehaviorWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ConflictingStateWarning : public Warning @@ -90,11 +96,12 @@ class ConflictingStateWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - ConflictingStateWarning* clone() const override; + std::string code () const override; + std::string description () const override; + ConflictingStateWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ValidationSyntacticWarning : public Warning @@ -102,11 +109,12 @@ class ValidationSyntacticWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - ValidationSyntacticWarning* clone() const override; + std::string code () const override; + std::string description () const override; + ValidationSyntacticWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; class ValidationSemanticWarning : public Warning @@ -114,11 +122,12 @@ class ValidationSemanticWarning : public Warning public: using Warning::Warning; - std::string code() const override; - std::string description() const override; - ValidationSemanticWarning* clone() const override; + std::string code () const override; + std::string description () const override; + ValidationSemanticWarning * clone () const override; + private: - bool compare(const BaseNotification& other) const final; + bool compare (const BaseNotification & other) const final; }; } // namespace errors diff --git a/src/libs/tools/src/errors/baseNotification.cpp b/src/libs/tools/src/errors/baseNotification.cpp index e3213641f96..ff5e656c321 100644 --- a/src/libs/tools/src/errors/baseNotification.cpp +++ b/src/libs/tools/src/errors/baseNotification.cpp @@ -1,8 +1,8 @@ -#include +#include "errors/baseNotification.hpp" #include +#include #include -#include "errors/baseNotification.hpp" namespace kdb { @@ -11,10 +11,12 @@ namespace tools namespace errors { -BaseNotification::BaseNotification (std::string reason, std::string module, std::string file, - std::string mountPoint, std::string configFile, kdb::long_t line) +BaseNotification::BaseNotification (std::string reason, std::string module, std::string file, std::string mountPoint, + std::string configFile, kdb::long_t line) : m_reason (std::move (reason)), m_module (std::move (module)), m_file (std::move (file)), m_mountPoint (std::move (mountPoint)), - m_configFile (std::move (configFile)), m_line (line) {} + m_configFile (std::move (configFile)), m_line (line) +{ +} void BaseNotification::setData (const std::string & reason, const std::string & module, const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line) @@ -28,7 +30,7 @@ void BaseNotification::setData (const std::string & reason, const std::string & } /* String representation */ -std::ostream& BaseNotification::toString (std::ostream & outputStream) const +std::ostream & BaseNotification::toString (std::ostream & outputStream) const { return outputStream << "Code: " << code () << std::endl << "Description: " << description () << std::endl @@ -40,50 +42,80 @@ std::ostream& BaseNotification::toString (std::ostream & outputStream) const << "Line: " << std::to_string (m_line); } -std::ostream& operator <<(std::ostream& outputStream, const BaseNotification& eb) +std::ostream & operator<< (std::ostream & outputStream, const BaseNotification & eb) { eb.toString (outputStream); return outputStream; } -bool BaseNotification::compare(const BaseNotification& other ELEKTRA_UNUSED) const +bool BaseNotification::compare (const BaseNotification & other ELEKTRA_UNUSED) const { return true; } -bool BaseNotification::operator== (const BaseNotification& other) const -{ - return code () == other.code () - && description () == other.description () - && reason () == other.reason () - && module () == other.module () - && file () == other.file () - && mountPoint () == other.mountPoint () - && configFile () == other.configFile () - && line () == other.line () - && this->compare (other); +bool BaseNotification::operator== (const BaseNotification & other) const +{ + return code () == other.code () && description () == other.description () && reason () == other.reason () && + module () == other.module () && file () == other.file () && mountPoint () == other.mountPoint () && + configFile () == other.configFile () && line () == other.line () && this->compare (other); } -bool BaseNotification::operator!= (const BaseNotification& other) const +bool BaseNotification::operator!= (const BaseNotification & other) const { return !(*this == other); } /* setters */ -std::string & BaseNotification::reason() { return m_reason; } -std::string & BaseNotification::module() { return m_module; } -std::string & BaseNotification::file() { return m_file; } -std::string & BaseNotification::mountPoint() { return m_mountPoint; } -std::string & BaseNotification::configFile() { return m_configFile; } -kdb::long_t & BaseNotification::line() { return m_line; } +std::string & BaseNotification::reason () +{ + return m_reason; +} +std::string & BaseNotification::module () +{ + return m_module; +} +std::string & BaseNotification::file () +{ + return m_file; +} +std::string & BaseNotification::mountPoint () +{ + return m_mountPoint; +} +std::string & BaseNotification::configFile () +{ + return m_configFile; +} +kdb::long_t & BaseNotification::line () +{ + return m_line; +} /* getters */ -const std::string & BaseNotification::reason () const { return m_reason; } -const std::string & BaseNotification::module () const { return m_module; } -const std::string & BaseNotification::file () const { return m_file; } -const std::string & BaseNotification::mountPoint () const { return m_mountPoint; } -const std::string & BaseNotification::configFile () const { return m_configFile; } -const kdb::long_t & BaseNotification::line () const { return m_line; } +const std::string & BaseNotification::reason () const +{ + return m_reason; +} +const std::string & BaseNotification::module () const +{ + return m_module; +} +const std::string & BaseNotification::file () const +{ + return m_file; +} +const std::string & BaseNotification::mountPoint () const +{ + return m_mountPoint; +} +const std::string & BaseNotification::configFile () const +{ + return m_configFile; +} +const kdb::long_t & BaseNotification::line () const +{ + return m_line; +} } // namespace errors } // namespace tools diff --git a/src/libs/tools/src/errors/error.cpp b/src/libs/tools/src/errors/error.cpp index 490989f6627..d6d1ca2f14b 100644 --- a/src/libs/tools/src/errors/error.cpp +++ b/src/libs/tools/src/errors/error.cpp @@ -1,6 +1,6 @@ -#include "iostream" #include "errors/error.hpp" +#include "iostream" namespace kdb @@ -14,41 +14,40 @@ namespace errors void Error::addWarning (Warning & warning) { /* TODO: Decide if we should create copies or store the original warnings */ - warnings.push_back (warning.clone()); + warnings.push_back (warning.clone ()); } /* getters */ kdb::long_t Error::warningCount () { - return warnings.size(); + return warnings.size (); } -Warning& Error::operator[](int index) +Warning & Error::operator[] (int index) { - if(index >= warningCount()) + if (index >= warningCount ()) { - throw std::out_of_range ("The warning with index " + std::to_string (index) + " was accessed, but there are only " - + std::to_string(warningCount()) + " warnings stored in the Error-object!"); + throw std::out_of_range ("The warning with index " + std::to_string (index) + " was accessed, but there are only " + + std::to_string (warningCount ()) + " warnings stored in the Error-object!"); } else { return (*(warnings[index])); } - } -bool Error::compare(const BaseNotification& other) const +bool Error::compare (const BaseNotification & other) const { /* comparison of data fields is done by operator== in BaseNotification class */ - const Error* pOtherError = dynamic_cast (&other); - if(!pOtherError || warnings.size() != pOtherError->warnings.size()) + const Error * pOtherError = dynamic_cast (&other); + if (!pOtherError || warnings.size () != pOtherError->warnings.size ()) { return false; } else { /* compare warnings */ - for (const Warning *w : warnings) + for (const Warning * w : warnings) { /* Two errors are equal if they contain the same warnings (compared by member values), * even if they have different orders in the internal vector. */ @@ -58,7 +57,7 @@ bool Error::compare(const BaseNotification& other) const * two different Warnings (not the same address in mem) are considered equal * if the member values are equal. */ bool equalWarningFound = false; - for (const Warning *ow : pOtherError->warnings) + for (const Warning * ow : pOtherError->warnings) { if (*w == *ow) { @@ -74,23 +73,21 @@ bool Error::compare(const BaseNotification& other) const } return true; } - } Error::~Error () { - for (Warning *w : warnings) + for (Warning * w : warnings) delete w; - } std::ostream & Error::toString (std::ostream & outputStream) const { BaseNotification::toString (outputStream); kdb::long_t i = 0; - if (warnings.size() > 0) + if (warnings.size () > 0) { outputStream << std::endl << std::endl << "The following warnings were attachted to the Error: " << std::endl << std::endl; - for (const Warning *w : warnings) + for (const Warning * w : warnings) outputStream << "Warning " << ++i << ": " << std::endl << *w << std::endl; } diff --git a/src/libs/tools/src/errors/errorFactory.cpp b/src/libs/tools/src/errors/errorFactory.cpp index 7810ee17aad..698b6e04b80 100644 --- a/src/libs/tools/src/errors/errorFactory.cpp +++ b/src/libs/tools/src/errors/errorFactory.cpp @@ -1,9 +1,9 @@ -#include -#include // for code and description constants #include "errors/errorFactory.hpp" -#include "errors/warningFactory.hpp" #include "errors/errorTypes.hpp" +#include "errors/warningFactory.hpp" +#include // for code and description constants +#include namespace kdb { @@ -12,8 +12,8 @@ namespace tools namespace errors { -Error* ErrorFactory::create(const std::string & type, const std::string & reason, const std::string & module, - const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line) +Error * ErrorFactory::create (const std::string & type, const std::string & reason, const std::string & module, const std::string & file, + const std::string & mountPoint, const std::string & configFile, kdb::long_t line) { if (type == ELEKTRA_ERROR_RESOURCE || type == ELEKTRA_ERROR_RESOURCE_NAME) return new ResourceError (reason, module, file, mountPoint, configFile, line); @@ -41,20 +41,20 @@ Error* ErrorFactory::create(const std::string & type, const std::string & reason /* TODO: Test method */ Error * ErrorFactory::fromKey (kdb::Key key) { - Error *err = nullptr; - if (key.isNull() || !key.isValid() || key.isBinary() || (!key.hasMeta("error") && !key.hasMeta("warnings"))) + Error * err = nullptr; + if (key.isNull () || !key.isValid () || key.isBinary () || (!key.hasMeta ("error") && !key.hasMeta ("warnings"))) { return nullptr; } if (!key.hasMeta ("error")) { - err = new PureWarningError(); + err = new PureWarningError (); } else { - std::string errCode = key.getMeta("error/number"); - std::string errDesc = key.getMeta("error/description"); + std::string errCode = key.getMeta ("error/number"); + std::string errDesc = key.getMeta ("error/description"); if (!checkErrorCodeDesc (errCode, errDesc)) { @@ -63,12 +63,12 @@ Error * ErrorFactory::fromKey (kdb::Key key) } else { - std::string module = key.getMeta("error/module"); - std::string file = key.getMeta("error/file"); - std::string reason = key.getMeta("error/reason"); - std::string mountPoint = key.getMeta("error/mountpoint"); - std::string configFile = key.getMeta("error/configfile"); - kdb::long_t line = key.getMeta("error/line"); + std::string module = key.getMeta ("error/module"); + std::string file = key.getMeta ("error/file"); + std::string reason = key.getMeta ("error/reason"); + std::string mountPoint = key.getMeta ("error/mountpoint"); + std::string configFile = key.getMeta ("error/configfile"); + kdb::long_t line = key.getMeta ("error/line"); err = create (errCode, reason, module, file, mountPoint, configFile, line); } @@ -78,20 +78,20 @@ Error * ErrorFactory::fromKey (kdb::Key key) * Code for extracting warnings was adapted from src/libs/highlevel/elektra_error.c:elektraErrorFromKey (Key * key) * and /src/tools/kdb/coloredkdbio.h:printWarnings() // TODO: use C++ binding version of keyMeta */ - KeySet metaKeys (ckdb::ksDup (ckdb::keyMeta (key.getKey()))); + KeySet metaKeys (ckdb::ksDup (ckdb::keyMeta (key.getKey ()))); Key warningsParent ("meta:/warnings", KEY_END); KeySet warningKeys = metaKeys.cut (warningsParent); - if (warningKeys.size() > 0) + if (warningKeys.size () > 0) { - for(auto it = warningKeys.begin() + 1; it != warningKeys.end (); ++it) + for (auto it = warningKeys.begin () + 1; it != warningKeys.end (); ++it) { - if (it->isDirectBelow(warningsParent)) + if (it->isDirectBelow (warningsParent)) { auto name = it->getName (); - std::string warnCode = warningKeys.get(name + "/number"); - std::string warnDescription = warningKeys.get(name + "/description"); + std::string warnCode = warningKeys.get (name + "/number"); + std::string warnDescription = warningKeys.get (name + "/description"); if (!WarningFactory::checkWarningCodeDesc (warnCode, warnDescription)) { @@ -99,17 +99,17 @@ Error * ErrorFactory::fromKey (kdb::Key key) return nullptr; } - std::string warnReason = warningKeys.get(name + "/reason"); - std::string warnModule = warningKeys.get(name + "/module"); - std::string warnFile = warningKeys.get(name + "/file"); + std::string warnReason = warningKeys.get (name + "/reason"); + std::string warnModule = warningKeys.get (name + "/module"); + std::string warnFile = warningKeys.get (name + "/file"); - std::string warnMountPoint = warningKeys.get(name + "/mountpoint"); - std::string warnConfigFile = warningKeys.get(name + "/configfile"); + std::string warnMountPoint = warningKeys.get (name + "/mountpoint"); + std::string warnConfigFile = warningKeys.get (name + "/configfile"); - kdb::long_t warnLine = warningKeys.get(name + "/line"); + kdb::long_t warnLine = warningKeys.get (name + "/line"); - Warning *w = WarningFactory::create (warnCode, warnReason, warnModule, warnFile, - warnMountPoint, warnConfigFile, warnLine); + Warning * w = WarningFactory::create (warnCode, warnReason, warnModule, warnFile, warnMountPoint, + warnConfigFile, warnLine); err->addWarning (*w); /* Warning gets copied by addWarning(Warning &) */ @@ -121,7 +121,7 @@ Error * ErrorFactory::fromKey (kdb::Key key) return err; } -bool ErrorFactory::checkErrorCodeDesc(const std::string & code, const std::string & description) +bool ErrorFactory::checkErrorCodeDesc (const std::string & code, const std::string & description) { if (code == ELEKTRA_ERROR_RESOURCE) return (description == ELEKTRA_ERROR_RESOURCE_NAME); diff --git a/src/libs/tools/src/errors/errorTypes.cpp b/src/libs/tools/src/errors/errorTypes.cpp index fa58e33dc7d..f40d329d637 100644 --- a/src/libs/tools/src/errors/errorTypes.cpp +++ b/src/libs/tools/src/errors/errorTypes.cpp @@ -1,6 +1,6 @@ -#include // for code and description constants #include +#include // for code and description constants namespace kdb { @@ -9,102 +9,162 @@ namespace tools namespace errors { -std::string PureWarningError::code() const { return ""; } -std::string PureWarningError::description() const { return "Warnings"; } -bool PureWarningError::compare(const BaseNotification& other) const +std::string PureWarningError::code () const +{ + return ""; +} +std::string PureWarningError::description () const +{ + return "Warnings"; +} +bool PureWarningError::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string ResourceError::code() const { return ELEKTRA_ERROR_RESOURCE; } -std::string ResourceError::description() const { return ELEKTRA_ERROR_RESOURCE_NAME; } -bool ResourceError::compare(const BaseNotification& other) const +std::string ResourceError::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_RESOURCE; +} +std::string ResourceError::description () const +{ + return ELEKTRA_ERROR_RESOURCE_NAME; +} +bool ResourceError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string OutOfMemoryError::code() const { return ELEKTRA_ERROR_OUT_OF_MEMORY; } -std::string OutOfMemoryError::description() const { return ELEKTRA_ERROR_OUT_OF_MEMORY_NAME; } -bool OutOfMemoryError::compare(const BaseNotification& other) const +std::string OutOfMemoryError::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_OUT_OF_MEMORY; +} +std::string OutOfMemoryError::description () const +{ + return ELEKTRA_ERROR_OUT_OF_MEMORY_NAME; +} +bool OutOfMemoryError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string InstallationError::code() const { return ELEKTRA_ERROR_INSTALLATION; } -std::string InstallationError::description() const { return ELEKTRA_ERROR_INSTALLATION_NAME; } -bool InstallationError::compare(const BaseNotification& other) const +std::string InstallationError::code () const +{ + return ELEKTRA_ERROR_INSTALLATION; +} +std::string InstallationError::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_INSTALLATION_NAME; +} +bool InstallationError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string InternalError::code() const { return ELEKTRA_ERROR_INTERNAL; } -std::string InternalError::description() const { return ELEKTRA_ERROR_INTERNAL_NAME; } -bool InternalError::compare(const BaseNotification& other) const +std::string InternalError::code () const +{ + return ELEKTRA_ERROR_INTERNAL; +} +std::string InternalError::description () const +{ + return ELEKTRA_ERROR_INTERNAL_NAME; +} +bool InternalError::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string InterfaceError::code() const { return ELEKTRA_ERROR_INTERFACE; } -std::string InterfaceError::description() const { return ELEKTRA_ERROR_INTERFACE_NAME; } -bool InterfaceError::compare(const BaseNotification& other) const +std::string InterfaceError::code () const +{ + return ELEKTRA_ERROR_INTERFACE; +} +std::string InterfaceError::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_INTERFACE_NAME; +} +bool InterfaceError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string PluginMisbehaviorError::code() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR; } -std::string PluginMisbehaviorError::description() const { return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME; } -bool PluginMisbehaviorError::compare(const BaseNotification& other) const +std::string PluginMisbehaviorError::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR; +} +std::string PluginMisbehaviorError::description () const +{ + return ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME; +} +bool PluginMisbehaviorError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string ConflictingStateError::code() const { return ELEKTRA_ERROR_CONFLICTING_STATE; } -std::string ConflictingStateError::description() const { return ELEKTRA_ERROR_CONFLICTING_STATE_NAME; } -bool ConflictingStateError::compare(const BaseNotification& other) const +std::string ConflictingStateError::code () const +{ + return ELEKTRA_ERROR_CONFLICTING_STATE; +} +std::string ConflictingStateError::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_ERROR_CONFLICTING_STATE_NAME; +} +bool ConflictingStateError::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string ValidationSyntacticError::code() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC; } -std::string ValidationSyntacticError::description() const { return ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME; } -bool ValidationSyntacticError::compare(const BaseNotification& other) const +std::string ValidationSyntacticError::code () const +{ + return ELEKTRA_ERROR_VALIDATION_SYNTACTIC; +} +std::string ValidationSyntacticError::description () const +{ + return ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME; +} +bool ValidationSyntacticError::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); } -std::string ValidationSemanticError::code() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC; } -std::string ValidationSemanticError::description() const { return ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME; } -bool ValidationSemanticError::compare(const BaseNotification& other) const +std::string ValidationSemanticError::code () const +{ + return ELEKTRA_ERROR_VALIDATION_SEMANTIC; +} +std::string ValidationSemanticError::description () const +{ + return ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME; +} +bool ValidationSemanticError::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Error::compare (other); diff --git a/src/libs/tools/src/errors/warning.cpp b/src/libs/tools/src/errors/warning.cpp index a4f6063af68..679f39b7417 100644 --- a/src/libs/tools/src/errors/warning.cpp +++ b/src/libs/tools/src/errors/warning.cpp @@ -8,7 +8,7 @@ namespace tools namespace errors { -bool Warning::compare(const BaseNotification& other) const +bool Warning::compare (const BaseNotification & other) const { /* comparison of data fields is done by operator== in BaseNotification class */ return (dynamic_cast (&other)) ? true : false; diff --git a/src/libs/tools/src/errors/warningFactory.cpp b/src/libs/tools/src/errors/warningFactory.cpp index c69d81fa3f5..0f355e77c6f 100644 --- a/src/libs/tools/src/errors/warningFactory.cpp +++ b/src/libs/tools/src/errors/warningFactory.cpp @@ -1,7 +1,7 @@ -#include // for code and description constants #include "errors/warningFactory.hpp" #include "errors/warningTypes.hpp" +#include // for code and description constants namespace kdb { @@ -10,8 +10,9 @@ namespace tools namespace errors { -Warning* WarningFactory::create(const std::string & type, const std::string & reason, const std::string & module, - const std::string & file, const std::string & mountPoint, const std::string & configFile, kdb::long_t line) +Warning * WarningFactory::create (const std::string & type, const std::string & reason, const std::string & module, + const std::string & file, const std::string & mountPoint, const std::string & configFile, + kdb::long_t line) { if (type == ELEKTRA_WARNING_RESOURCE || type == ELEKTRA_WARNING_RESOURCE_NAME) return new ResourceWarning (reason, module, file, mountPoint, configFile, line); @@ -35,7 +36,7 @@ Warning* WarningFactory::create(const std::string & type, const std::string & re return nullptr; } -bool WarningFactory::checkWarningCodeDesc(const std::string & code, const std::string & description) +bool WarningFactory::checkWarningCodeDesc (const std::string & code, const std::string & description) { if (code == ELEKTRA_WARNING_RESOURCE) return (description == ELEKTRA_WARNING_RESOURCE_NAME); diff --git a/src/libs/tools/src/errors/warningTypes.cpp b/src/libs/tools/src/errors/warningTypes.cpp index ca1b4debd57..ba2943ca007 100644 --- a/src/libs/tools/src/errors/warningTypes.cpp +++ b/src/libs/tools/src/errors/warningTypes.cpp @@ -1,6 +1,6 @@ -#include // for code and description constants #include +#include // for code and description constants namespace kdb { @@ -9,146 +9,193 @@ namespace tools namespace errors { -std::string ResourceWarning::code() const { return ELEKTRA_WARNING_RESOURCE; } -std::string ResourceWarning::description() const { return ELEKTRA_WARNING_RESOURCE_NAME; } -bool ResourceWarning::compare(const BaseNotification& other) const +std::string ResourceWarning::code () const +{ + return ELEKTRA_WARNING_RESOURCE; +} +std::string ResourceWarning::description () const +{ + return ELEKTRA_WARNING_RESOURCE_NAME; +} +bool ResourceWarning::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -ResourceWarning* ResourceWarning::clone () const +ResourceWarning * ResourceWarning::clone () const { - return new ResourceWarning(*this); + return new ResourceWarning (*this); } -std::string OutOfMemoryWarning::code() const { return ELEKTRA_WARNING_OUT_OF_MEMORY; } -std::string OutOfMemoryWarning::description() const { return ELEKTRA_WARNING_OUT_OF_MEMORY_NAME; } -bool OutOfMemoryWarning::compare(const BaseNotification& other) const +std::string OutOfMemoryWarning::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_OUT_OF_MEMORY; +} +std::string OutOfMemoryWarning::description () const +{ + return ELEKTRA_WARNING_OUT_OF_MEMORY_NAME; +} +bool OutOfMemoryWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -OutOfMemoryWarning* OutOfMemoryWarning::clone () const +OutOfMemoryWarning * OutOfMemoryWarning::clone () const { - return new OutOfMemoryWarning(*this); + return new OutOfMemoryWarning (*this); } - -std::string InstallationWarning::code() const { return ELEKTRA_WARNING_INSTALLATION; } -std::string InstallationWarning::description() const { return ELEKTRA_WARNING_INSTALLATION_NAME; } -bool InstallationWarning::compare(const BaseNotification& other) const +std::string InstallationWarning::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_INSTALLATION; +} +std::string InstallationWarning::description () const +{ + return ELEKTRA_WARNING_INSTALLATION_NAME; +} +bool InstallationWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -InstallationWarning* InstallationWarning::clone () const +InstallationWarning * InstallationWarning::clone () const { - return new InstallationWarning(*this); + return new InstallationWarning (*this); } - -std::string InternalWarning::code() const { return ELEKTRA_WARNING_INTERNAL; } -std::string InternalWarning::description() const { return ELEKTRA_WARNING_INTERNAL_NAME; } -bool InternalWarning::compare(const BaseNotification& other) const +std::string InternalWarning::code () const +{ + return ELEKTRA_WARNING_INTERNAL; +} +std::string InternalWarning::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_INTERNAL_NAME; +} +bool InternalWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -InternalWarning* InternalWarning::clone () const +InternalWarning * InternalWarning::clone () const { - return new InternalWarning(*this); + return new InternalWarning (*this); } - -std::string InterfaceWarning::code() const { return ELEKTRA_WARNING_INTERFACE; } -std::string InterfaceWarning::description() const { return ELEKTRA_WARNING_INTERFACE_NAME; } -bool InterfaceWarning::compare(const BaseNotification& other) const +std::string InterfaceWarning::code () const +{ + return ELEKTRA_WARNING_INTERFACE; +} +std::string InterfaceWarning::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_INTERFACE_NAME; +} +bool InterfaceWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -InterfaceWarning* InterfaceWarning::clone () const +InterfaceWarning * InterfaceWarning::clone () const { - return new InterfaceWarning(*this); + return new InterfaceWarning (*this); } - -std::string PluginMisbehaviorWarning::code() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR; } -std::string PluginMisbehaviorWarning::description() const { return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME; } -bool PluginMisbehaviorWarning::compare(const BaseNotification& other) const +std::string PluginMisbehaviorWarning::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR; +} +std::string PluginMisbehaviorWarning::description () const +{ + return ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME; +} +bool PluginMisbehaviorWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -PluginMisbehaviorWarning* PluginMisbehaviorWarning::clone () const +PluginMisbehaviorWarning * PluginMisbehaviorWarning::clone () const { - return new PluginMisbehaviorWarning(*this); + return new PluginMisbehaviorWarning (*this); } -std::string ConflictingStateWarning::code() const { return ELEKTRA_WARNING_CONFLICTING_STATE; } -std::string ConflictingStateWarning::description() const { return ELEKTRA_WARNING_CONFLICTING_STATE_NAME; } -bool ConflictingStateWarning::compare(const BaseNotification& other) const +std::string ConflictingStateWarning::code () const +{ + return ELEKTRA_WARNING_CONFLICTING_STATE; +} +std::string ConflictingStateWarning::description () const +{ + return ELEKTRA_WARNING_CONFLICTING_STATE_NAME; +} +bool ConflictingStateWarning::compare (const BaseNotification & other) const { - if(!(dynamic_cast (&other))) + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -ConflictingStateWarning* ConflictingStateWarning::clone () const +ConflictingStateWarning * ConflictingStateWarning::clone () const { - return new ConflictingStateWarning(*this); + return new ConflictingStateWarning (*this); } - -std::string ValidationSyntacticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC; } -std::string ValidationSyntacticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME; } -bool ValidationSyntacticWarning::compare(const BaseNotification& other) const +std::string ValidationSyntacticWarning::code () const +{ + return ELEKTRA_WARNING_VALIDATION_SYNTACTIC; +} +std::string ValidationSyntacticWarning::description () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME; +} +bool ValidationSyntacticWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -ValidationSyntacticWarning* ValidationSyntacticWarning::clone () const +ValidationSyntacticWarning * ValidationSyntacticWarning::clone () const { - return new ValidationSyntacticWarning(*this); + return new ValidationSyntacticWarning (*this); } - -std::string ValidationSemanticWarning::code() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC; } -std::string ValidationSemanticWarning::description() const { return ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME; } -bool ValidationSemanticWarning::compare(const BaseNotification& other) const +std::string ValidationSemanticWarning::code () const { - if(!(dynamic_cast (&other))) + return ELEKTRA_WARNING_VALIDATION_SEMANTIC; +} +std::string ValidationSemanticWarning::description () const +{ + return ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME; +} +bool ValidationSemanticWarning::compare (const BaseNotification & other) const +{ + if (!(dynamic_cast (&other))) return false; else return Warning::compare (other); } -ValidationSemanticWarning* ValidationSemanticWarning::clone () const +ValidationSemanticWarning * ValidationSemanticWarning::clone () const { - return new ValidationSemanticWarning(*this); + return new ValidationSemanticWarning (*this); } - } // namespace errors } // namespace tools } // namespace kdb diff --git a/src/libs/tools/tests/testtool_error.cpp b/src/libs/tools/tests/testtool_error.cpp index 89a58352521..6745c61fed8 100644 --- a/src/libs/tools/tests/testtool_error.cpp +++ b/src/libs/tools/tests/testtool_error.cpp @@ -7,307 +7,262 @@ * */ +#include +#include #include #include // for code and description constants -#include -#include TEST (Error, TestWarnings) { /* Resource warning */ std::cout << std::endl << "TEST WARNINGS:" << std::endl; - kdb::tools::errors::ResourceWarning resourceWarning ("resourceWarningReason", - "resourceWarningModule", - "resourceWarningFile", - "resourceWarningMountPoint", - "resourceWarningConfigFile", 0); - ASSERT_EQ(resourceWarning.code(), ELEKTRA_WARNING_RESOURCE); - ASSERT_EQ(resourceWarning.description(), ELEKTRA_WARNING_RESOURCE_NAME); - ASSERT_EQ(resourceWarning.reason(), "resourceWarningReason"); - ASSERT_EQ(resourceWarning.module(), "resourceWarningModule"); - ASSERT_EQ(resourceWarning.file(), "resourceWarningFile"); - ASSERT_EQ(resourceWarning.mountPoint(), "resourceWarningMountPoint"); - ASSERT_EQ(resourceWarning.configFile(), "resourceWarningConfigFile"); - ASSERT_EQ(resourceWarning.line(), 0); + kdb::tools::errors::ResourceWarning resourceWarning ("resourceWarningReason", "resourceWarningModule", "resourceWarningFile", + "resourceWarningMountPoint", "resourceWarningConfigFile", 0); + ASSERT_EQ (resourceWarning.code (), ELEKTRA_WARNING_RESOURCE); + ASSERT_EQ (resourceWarning.description (), ELEKTRA_WARNING_RESOURCE_NAME); + ASSERT_EQ (resourceWarning.reason (), "resourceWarningReason"); + ASSERT_EQ (resourceWarning.module (), "resourceWarningModule"); + ASSERT_EQ (resourceWarning.file (), "resourceWarningFile"); + ASSERT_EQ (resourceWarning.mountPoint (), "resourceWarningMountPoint"); + ASSERT_EQ (resourceWarning.configFile (), "resourceWarningConfigFile"); + ASSERT_EQ (resourceWarning.line (), 0); /* Out of memory warning */ - kdb::tools::errors::OutOfMemoryWarning outOfMemoryWarning ("outOfMemoryWarningReason", - "outOfMemoryWarningModule", - "outOfMemoryWarningFile", - "outOfMemoryWarningMountPoint", + kdb::tools::errors::OutOfMemoryWarning outOfMemoryWarning ("outOfMemoryWarningReason", "outOfMemoryWarningModule", + "outOfMemoryWarningFile", "outOfMemoryWarningMountPoint", "outOfMemoryWarningConfigFile", 1); - ASSERT_EQ(outOfMemoryWarning.code(), ELEKTRA_WARNING_OUT_OF_MEMORY); - ASSERT_EQ(outOfMemoryWarning.description(), ELEKTRA_WARNING_OUT_OF_MEMORY_NAME); - ASSERT_EQ(outOfMemoryWarning.reason(), "outOfMemoryWarningReason"); - ASSERT_EQ(outOfMemoryWarning.module(), "outOfMemoryWarningModule"); - ASSERT_EQ(outOfMemoryWarning.file(), "outOfMemoryWarningFile"); - ASSERT_EQ(outOfMemoryWarning.mountPoint(), "outOfMemoryWarningMountPoint"); - ASSERT_EQ(outOfMemoryWarning.configFile(), "outOfMemoryWarningConfigFile"); - ASSERT_EQ(outOfMemoryWarning.line(), 1); + ASSERT_EQ (outOfMemoryWarning.code (), ELEKTRA_WARNING_OUT_OF_MEMORY); + ASSERT_EQ (outOfMemoryWarning.description (), ELEKTRA_WARNING_OUT_OF_MEMORY_NAME); + ASSERT_EQ (outOfMemoryWarning.reason (), "outOfMemoryWarningReason"); + ASSERT_EQ (outOfMemoryWarning.module (), "outOfMemoryWarningModule"); + ASSERT_EQ (outOfMemoryWarning.file (), "outOfMemoryWarningFile"); + ASSERT_EQ (outOfMemoryWarning.mountPoint (), "outOfMemoryWarningMountPoint"); + ASSERT_EQ (outOfMemoryWarning.configFile (), "outOfMemoryWarningConfigFile"); + ASSERT_EQ (outOfMemoryWarning.line (), 1); /* Installation warning */ - kdb::tools::errors::InstallationWarning installationWarning ("installationWarningReason", - "installationWarningModule", - "installationWarningFile", - "installationWarningMountPoint", + kdb::tools::errors::InstallationWarning installationWarning ("installationWarningReason", "installationWarningModule", + "installationWarningFile", "installationWarningMountPoint", "installationWarningConfigFile", -1); - ASSERT_EQ(installationWarning.code(), ELEKTRA_WARNING_INSTALLATION); - ASSERT_EQ(installationWarning.description(), ELEKTRA_WARNING_INSTALLATION_NAME); - ASSERT_EQ(installationWarning.reason(), "installationWarningReason"); - ASSERT_EQ(installationWarning.module(), "installationWarningModule"); - ASSERT_EQ(installationWarning.file(), "installationWarningFile"); - ASSERT_EQ(installationWarning.mountPoint(), "installationWarningMountPoint"); - ASSERT_EQ(installationWarning.configFile(), "installationWarningConfigFile"); - ASSERT_EQ(installationWarning.line(), -1); + ASSERT_EQ (installationWarning.code (), ELEKTRA_WARNING_INSTALLATION); + ASSERT_EQ (installationWarning.description (), ELEKTRA_WARNING_INSTALLATION_NAME); + ASSERT_EQ (installationWarning.reason (), "installationWarningReason"); + ASSERT_EQ (installationWarning.module (), "installationWarningModule"); + ASSERT_EQ (installationWarning.file (), "installationWarningFile"); + ASSERT_EQ (installationWarning.mountPoint (), "installationWarningMountPoint"); + ASSERT_EQ (installationWarning.configFile (), "installationWarningConfigFile"); + ASSERT_EQ (installationWarning.line (), -1); /* Internal warning */ - kdb::tools::errors::InternalWarning internalWarning ("internalWarningReason", - "internalWarningModule", - "internalWarningFile", - "internalWarningMountPoint", - "internalWarningConfigFile", 999); - ASSERT_EQ(internalWarning.code(), ELEKTRA_WARNING_INTERNAL); - ASSERT_EQ(internalWarning.description(), ELEKTRA_WARNING_INTERNAL_NAME); - ASSERT_EQ(internalWarning.reason(), "internalWarningReason"); - ASSERT_EQ(internalWarning.module(), "internalWarningModule"); - ASSERT_EQ(internalWarning.file(), "internalWarningFile"); - ASSERT_EQ(internalWarning.mountPoint(), "internalWarningMountPoint"); - ASSERT_EQ(internalWarning.configFile(), "internalWarningConfigFile"); - ASSERT_EQ(internalWarning.line(), 999); + kdb::tools::errors::InternalWarning internalWarning ("internalWarningReason", "internalWarningModule", "internalWarningFile", + "internalWarningMountPoint", "internalWarningConfigFile", 999); + ASSERT_EQ (internalWarning.code (), ELEKTRA_WARNING_INTERNAL); + ASSERT_EQ (internalWarning.description (), ELEKTRA_WARNING_INTERNAL_NAME); + ASSERT_EQ (internalWarning.reason (), "internalWarningReason"); + ASSERT_EQ (internalWarning.module (), "internalWarningModule"); + ASSERT_EQ (internalWarning.file (), "internalWarningFile"); + ASSERT_EQ (internalWarning.mountPoint (), "internalWarningMountPoint"); + ASSERT_EQ (internalWarning.configFile (), "internalWarningConfigFile"); + ASSERT_EQ (internalWarning.line (), 999); /* Interface warning */ - kdb::tools::errors::InterfaceWarning interfaceWarning ("interfaceWarningReason", - "interfaceWarningModule", - "interfaceWarningFile", - "interfaceWarningMountPoint", - "interfaceWarningConfigFile", 2); - ASSERT_EQ(interfaceWarning.code(), ELEKTRA_WARNING_INTERFACE); - ASSERT_EQ(interfaceWarning.description(), ELEKTRA_WARNING_INTERFACE_NAME); - ASSERT_EQ(interfaceWarning.reason(), "interfaceWarningReason"); - ASSERT_EQ(interfaceWarning.module(), "interfaceWarningModule"); - ASSERT_EQ(interfaceWarning.file(), "interfaceWarningFile"); - ASSERT_EQ(interfaceWarning.mountPoint(), "interfaceWarningMountPoint"); - ASSERT_EQ(interfaceWarning.configFile(), "interfaceWarningConfigFile"); - ASSERT_EQ(interfaceWarning.line(), 2); + kdb::tools::errors::InterfaceWarning interfaceWarning ("interfaceWarningReason", "interfaceWarningModule", "interfaceWarningFile", + "interfaceWarningMountPoint", "interfaceWarningConfigFile", 2); + ASSERT_EQ (interfaceWarning.code (), ELEKTRA_WARNING_INTERFACE); + ASSERT_EQ (interfaceWarning.description (), ELEKTRA_WARNING_INTERFACE_NAME); + ASSERT_EQ (interfaceWarning.reason (), "interfaceWarningReason"); + ASSERT_EQ (interfaceWarning.module (), "interfaceWarningModule"); + ASSERT_EQ (interfaceWarning.file (), "interfaceWarningFile"); + ASSERT_EQ (interfaceWarning.mountPoint (), "interfaceWarningMountPoint"); + ASSERT_EQ (interfaceWarning.configFile (), "interfaceWarningConfigFile"); + ASSERT_EQ (interfaceWarning.line (), 2); /* PluginMisbehavior warning */ - kdb::tools::errors::PluginMisbehaviorWarning pluginMisbehaviorWarning ("pluginMisbehaviorWarningReason", - "pluginMisbehaviorWarningModule", - "pluginMisbehaviorWarningFile", - "pluginMisbehaviorWarningMountPoint", - "pluginMisbehaviorWarningConfigFile", 3); - ASSERT_EQ(pluginMisbehaviorWarning.code(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR); - ASSERT_EQ(pluginMisbehaviorWarning.description(), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME); - ASSERT_EQ(pluginMisbehaviorWarning.reason(), "pluginMisbehaviorWarningReason"); - ASSERT_EQ(pluginMisbehaviorWarning.module(), "pluginMisbehaviorWarningModule"); - ASSERT_EQ(pluginMisbehaviorWarning.file(), "pluginMisbehaviorWarningFile"); - ASSERT_EQ(pluginMisbehaviorWarning.mountPoint(), "pluginMisbehaviorWarningMountPoint"); - ASSERT_EQ(pluginMisbehaviorWarning.configFile(), "pluginMisbehaviorWarningConfigFile"); - ASSERT_EQ(pluginMisbehaviorWarning.line(), 3); + kdb::tools::errors::PluginMisbehaviorWarning pluginMisbehaviorWarning ( + "pluginMisbehaviorWarningReason", "pluginMisbehaviorWarningModule", "pluginMisbehaviorWarningFile", + "pluginMisbehaviorWarningMountPoint", "pluginMisbehaviorWarningConfigFile", 3); + ASSERT_EQ (pluginMisbehaviorWarning.code (), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR); + ASSERT_EQ (pluginMisbehaviorWarning.description (), ELEKTRA_WARNING_PLUGIN_MISBEHAVIOR_NAME); + ASSERT_EQ (pluginMisbehaviorWarning.reason (), "pluginMisbehaviorWarningReason"); + ASSERT_EQ (pluginMisbehaviorWarning.module (), "pluginMisbehaviorWarningModule"); + ASSERT_EQ (pluginMisbehaviorWarning.file (), "pluginMisbehaviorWarningFile"); + ASSERT_EQ (pluginMisbehaviorWarning.mountPoint (), "pluginMisbehaviorWarningMountPoint"); + ASSERT_EQ (pluginMisbehaviorWarning.configFile (), "pluginMisbehaviorWarningConfigFile"); + ASSERT_EQ (pluginMisbehaviorWarning.line (), 3); /* ConflictingStatie warning */ - kdb::tools::errors::ConflictingStateWarning conflictingStateWarning ("conflictingStateWarningReason", - "conflictingStateWarningModule", - "conflictingStateWarningFile", - "conflictingStateWarningMountPoint", - "conflictingStateWarningConfigFile",4); - ASSERT_EQ(conflictingStateWarning.code(), ELEKTRA_WARNING_CONFLICTING_STATE); - ASSERT_EQ(conflictingStateWarning.description(), ELEKTRA_WARNING_CONFLICTING_STATE_NAME); - ASSERT_EQ(conflictingStateWarning.reason(), "conflictingStateWarningReason"); - ASSERT_EQ(conflictingStateWarning.module(), "conflictingStateWarningModule"); - ASSERT_EQ(conflictingStateWarning.file(), "conflictingStateWarningFile"); - ASSERT_EQ(conflictingStateWarning.mountPoint(), "conflictingStateWarningMountPoint"); - ASSERT_EQ(conflictingStateWarning.configFile(), "conflictingStateWarningConfigFile"); - ASSERT_EQ(conflictingStateWarning.line(), 4); + kdb::tools::errors::ConflictingStateWarning conflictingStateWarning ( + "conflictingStateWarningReason", "conflictingStateWarningModule", "conflictingStateWarningFile", + "conflictingStateWarningMountPoint", "conflictingStateWarningConfigFile", 4); + ASSERT_EQ (conflictingStateWarning.code (), ELEKTRA_WARNING_CONFLICTING_STATE); + ASSERT_EQ (conflictingStateWarning.description (), ELEKTRA_WARNING_CONFLICTING_STATE_NAME); + ASSERT_EQ (conflictingStateWarning.reason (), "conflictingStateWarningReason"); + ASSERT_EQ (conflictingStateWarning.module (), "conflictingStateWarningModule"); + ASSERT_EQ (conflictingStateWarning.file (), "conflictingStateWarningFile"); + ASSERT_EQ (conflictingStateWarning.mountPoint (), "conflictingStateWarningMountPoint"); + ASSERT_EQ (conflictingStateWarning.configFile (), "conflictingStateWarningConfigFile"); + ASSERT_EQ (conflictingStateWarning.line (), 4); /* ValidationSyntactic warning */ - kdb::tools::errors::ValidationSyntacticWarning validationSyntacticWarning ("validationSyntacticWarningReason", - "validationSyntacticWarningModule", - "validationSyntacticWarningFile", - "validationSyntacticMountPoint", - "validationSyntacticWarningConfigFile", 5); - ASSERT_EQ(validationSyntacticWarning.code(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC); - ASSERT_EQ(validationSyntacticWarning.description(), ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME); - ASSERT_EQ(validationSyntacticWarning.reason(), "validationSyntacticWarningReason"); - ASSERT_EQ(validationSyntacticWarning.module(), "validationSyntacticWarningModule"); - ASSERT_EQ(validationSyntacticWarning.file(), "validationSyntacticWarningFile"); - ASSERT_EQ(validationSyntacticWarning.mountPoint(), "validationSyntacticMountPoint"); - ASSERT_EQ(validationSyntacticWarning.configFile(), "validationSyntacticWarningConfigFile"); - ASSERT_EQ(validationSyntacticWarning.line(), 5); + kdb::tools::errors::ValidationSyntacticWarning validationSyntacticWarning ( + "validationSyntacticWarningReason", "validationSyntacticWarningModule", "validationSyntacticWarningFile", + "validationSyntacticMountPoint", "validationSyntacticWarningConfigFile", 5); + ASSERT_EQ (validationSyntacticWarning.code (), ELEKTRA_WARNING_VALIDATION_SYNTACTIC); + ASSERT_EQ (validationSyntacticWarning.description (), ELEKTRA_WARNING_VALIDATION_SYNTACTIC_NAME); + ASSERT_EQ (validationSyntacticWarning.reason (), "validationSyntacticWarningReason"); + ASSERT_EQ (validationSyntacticWarning.module (), "validationSyntacticWarningModule"); + ASSERT_EQ (validationSyntacticWarning.file (), "validationSyntacticWarningFile"); + ASSERT_EQ (validationSyntacticWarning.mountPoint (), "validationSyntacticMountPoint"); + ASSERT_EQ (validationSyntacticWarning.configFile (), "validationSyntacticWarningConfigFile"); + ASSERT_EQ (validationSyntacticWarning.line (), 5); /* ValidationSemantic warning */ - kdb::tools::errors::ValidationSemanticWarning validationSemanticWarning ("validationSemanticWarningReason", - "validationSemanticWarningModule", - "validationSemanticWarningFile", - "validationSemanticWarningMountPoint", - "validationSemanticWarningConfigFile", 6); - ASSERT_EQ(validationSemanticWarning.code(), ELEKTRA_WARNING_VALIDATION_SEMANTIC); - ASSERT_EQ(validationSemanticWarning.description(), ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME); - ASSERT_EQ(validationSemanticWarning.reason(), "validationSemanticWarningReason"); - ASSERT_EQ(validationSemanticWarning.module(), "validationSemanticWarningModule"); - ASSERT_EQ(validationSemanticWarning.file(), "validationSemanticWarningFile"); - ASSERT_EQ(validationSemanticWarning.mountPoint(), "validationSemanticWarningMountPoint"); - ASSERT_EQ(validationSemanticWarning.configFile(), "validationSemanticWarningConfigFile"); - ASSERT_EQ(validationSemanticWarning.line(), 6); + kdb::tools::errors::ValidationSemanticWarning validationSemanticWarning ( + "validationSemanticWarningReason", "validationSemanticWarningModule", "validationSemanticWarningFile", + "validationSemanticWarningMountPoint", "validationSemanticWarningConfigFile", 6); + ASSERT_EQ (validationSemanticWarning.code (), ELEKTRA_WARNING_VALIDATION_SEMANTIC); + ASSERT_EQ (validationSemanticWarning.description (), ELEKTRA_WARNING_VALIDATION_SEMANTIC_NAME); + ASSERT_EQ (validationSemanticWarning.reason (), "validationSemanticWarningReason"); + ASSERT_EQ (validationSemanticWarning.module (), "validationSemanticWarningModule"); + ASSERT_EQ (validationSemanticWarning.file (), "validationSemanticWarningFile"); + ASSERT_EQ (validationSemanticWarning.mountPoint (), "validationSemanticWarningMountPoint"); + ASSERT_EQ (validationSemanticWarning.configFile (), "validationSemanticWarningConfigFile"); + ASSERT_EQ (validationSemanticWarning.line (), 6); } TEST (Error, TestErrors) { /* Resource error */ std::cout << std::endl << "TEST ERRORS:" << std::endl; - kdb::tools::errors::ResourceError resourceError ("resourceErrorReason", - "resourceErrorModule", - "resourceErrorFile", - "resourceErrorMountPoint", - "resourceErrorConfigFile", 0); - ASSERT_EQ(resourceError.code(), ELEKTRA_ERROR_RESOURCE); - ASSERT_EQ(resourceError.description(), ELEKTRA_ERROR_RESOURCE_NAME); - ASSERT_EQ(resourceError.reason(), "resourceErrorReason"); - ASSERT_EQ(resourceError.module(), "resourceErrorModule"); - ASSERT_EQ(resourceError.file(), "resourceErrorFile"); - ASSERT_EQ(resourceError.mountPoint(), "resourceErrorMountPoint"); - ASSERT_EQ(resourceError.configFile(), "resourceErrorConfigFile"); - ASSERT_EQ(resourceError.line(), 0); + kdb::tools::errors::ResourceError resourceError ("resourceErrorReason", "resourceErrorModule", "resourceErrorFile", + "resourceErrorMountPoint", "resourceErrorConfigFile", 0); + ASSERT_EQ (resourceError.code (), ELEKTRA_ERROR_RESOURCE); + ASSERT_EQ (resourceError.description (), ELEKTRA_ERROR_RESOURCE_NAME); + ASSERT_EQ (resourceError.reason (), "resourceErrorReason"); + ASSERT_EQ (resourceError.module (), "resourceErrorModule"); + ASSERT_EQ (resourceError.file (), "resourceErrorFile"); + ASSERT_EQ (resourceError.mountPoint (), "resourceErrorMountPoint"); + ASSERT_EQ (resourceError.configFile (), "resourceErrorConfigFile"); + ASSERT_EQ (resourceError.line (), 0); /* Out of memory error */ - kdb::tools::errors::OutOfMemoryError outOfMemoryError ("outOfMemoryErrorReason", - "outOfMemoryErrorModule", - "outOfMemoryErrorFile", - "outOfMemoryErrorMountPoint", - "outOfMemoryErrorConfigFile", 1); - ASSERT_EQ(outOfMemoryError.code(), ELEKTRA_ERROR_OUT_OF_MEMORY); - ASSERT_EQ(outOfMemoryError.description(), ELEKTRA_ERROR_OUT_OF_MEMORY_NAME); - ASSERT_EQ(outOfMemoryError.reason(), "outOfMemoryErrorReason"); - ASSERT_EQ(outOfMemoryError.module(), "outOfMemoryErrorModule"); - ASSERT_EQ(outOfMemoryError.file(), "outOfMemoryErrorFile"); - ASSERT_EQ(outOfMemoryError.mountPoint(), "outOfMemoryErrorMountPoint"); - ASSERT_EQ(outOfMemoryError.configFile(), "outOfMemoryErrorConfigFile"); - ASSERT_EQ(outOfMemoryError.line(), 1); + kdb::tools::errors::OutOfMemoryError outOfMemoryError ("outOfMemoryErrorReason", "outOfMemoryErrorModule", "outOfMemoryErrorFile", + "outOfMemoryErrorMountPoint", "outOfMemoryErrorConfigFile", 1); + ASSERT_EQ (outOfMemoryError.code (), ELEKTRA_ERROR_OUT_OF_MEMORY); + ASSERT_EQ (outOfMemoryError.description (), ELEKTRA_ERROR_OUT_OF_MEMORY_NAME); + ASSERT_EQ (outOfMemoryError.reason (), "outOfMemoryErrorReason"); + ASSERT_EQ (outOfMemoryError.module (), "outOfMemoryErrorModule"); + ASSERT_EQ (outOfMemoryError.file (), "outOfMemoryErrorFile"); + ASSERT_EQ (outOfMemoryError.mountPoint (), "outOfMemoryErrorMountPoint"); + ASSERT_EQ (outOfMemoryError.configFile (), "outOfMemoryErrorConfigFile"); + ASSERT_EQ (outOfMemoryError.line (), 1); /* Installation error */ - kdb::tools::errors::InstallationError installationError ("installationErrorReason", - "installationErrorModule", - "installationErrorFile", - "installationErrorMountPoint", + kdb::tools::errors::InstallationError installationError ("installationErrorReason", "installationErrorModule", + "installationErrorFile", "installationErrorMountPoint", "installationErrorConfigFile", -1); - ASSERT_EQ(installationError.code(), ELEKTRA_ERROR_INSTALLATION); - ASSERT_EQ(installationError.description(), ELEKTRA_ERROR_INSTALLATION_NAME); - ASSERT_EQ(installationError.reason(), "installationErrorReason"); - ASSERT_EQ(installationError.module(), "installationErrorModule"); - ASSERT_EQ(installationError.file(), "installationErrorFile"); - ASSERT_EQ(installationError.mountPoint(), "installationErrorMountPoint"); - ASSERT_EQ(installationError.configFile(), "installationErrorConfigFile"); - ASSERT_EQ(installationError.line(), -1); + ASSERT_EQ (installationError.code (), ELEKTRA_ERROR_INSTALLATION); + ASSERT_EQ (installationError.description (), ELEKTRA_ERROR_INSTALLATION_NAME); + ASSERT_EQ (installationError.reason (), "installationErrorReason"); + ASSERT_EQ (installationError.module (), "installationErrorModule"); + ASSERT_EQ (installationError.file (), "installationErrorFile"); + ASSERT_EQ (installationError.mountPoint (), "installationErrorMountPoint"); + ASSERT_EQ (installationError.configFile (), "installationErrorConfigFile"); + ASSERT_EQ (installationError.line (), -1); /* Internal error */ - kdb::tools::errors::InternalError internalError ("internalErrorReason", - "internalErrorModule", - "internalErrorFile", - "internalErrorMountPoint", - "internalErrorConfigFile", 999); - - ASSERT_EQ(internalError.code(), ELEKTRA_ERROR_INTERNAL); - ASSERT_EQ(internalError.description(), ELEKTRA_ERROR_INTERNAL_NAME); - ASSERT_EQ(internalError.reason(), "internalErrorReason"); - ASSERT_EQ(internalError.module(), "internalErrorModule"); - ASSERT_EQ(internalError.file(), "internalErrorFile"); - ASSERT_EQ(internalError.mountPoint(), "internalErrorMountPoint"); - ASSERT_EQ(internalError.configFile(), "internalErrorConfigFile"); - ASSERT_EQ(internalError.line(), 999); + kdb::tools::errors::InternalError internalError ("internalErrorReason", "internalErrorModule", "internalErrorFile", + "internalErrorMountPoint", "internalErrorConfigFile", 999); + + ASSERT_EQ (internalError.code (), ELEKTRA_ERROR_INTERNAL); + ASSERT_EQ (internalError.description (), ELEKTRA_ERROR_INTERNAL_NAME); + ASSERT_EQ (internalError.reason (), "internalErrorReason"); + ASSERT_EQ (internalError.module (), "internalErrorModule"); + ASSERT_EQ (internalError.file (), "internalErrorFile"); + ASSERT_EQ (internalError.mountPoint (), "internalErrorMountPoint"); + ASSERT_EQ (internalError.configFile (), "internalErrorConfigFile"); + ASSERT_EQ (internalError.line (), 999); /* Interface error */ - kdb::tools::errors::InterfaceError interfaceError ("interfaceErrorReason", - "interfaceErrorModule", - "interfaceErrorFile", - "interfaceErrorMountPoint", - "interfaceErrorConfigFile", 2); - ASSERT_EQ(interfaceError.code(), ELEKTRA_ERROR_INTERFACE); - ASSERT_EQ(interfaceError.description(), ELEKTRA_ERROR_INTERFACE_NAME); - ASSERT_EQ(interfaceError.reason(), "interfaceErrorReason"); - ASSERT_EQ(interfaceError.module(), "interfaceErrorModule"); - ASSERT_EQ(interfaceError.file(), "interfaceErrorFile"); - ASSERT_EQ(interfaceError.mountPoint(), "interfaceErrorMountPoint"); - ASSERT_EQ(interfaceError.configFile(), "interfaceErrorConfigFile"); - ASSERT_EQ(interfaceError.line(), 2); + kdb::tools::errors::InterfaceError interfaceError ("interfaceErrorReason", "interfaceErrorModule", "interfaceErrorFile", + "interfaceErrorMountPoint", "interfaceErrorConfigFile", 2); + ASSERT_EQ (interfaceError.code (), ELEKTRA_ERROR_INTERFACE); + ASSERT_EQ (interfaceError.description (), ELEKTRA_ERROR_INTERFACE_NAME); + ASSERT_EQ (interfaceError.reason (), "interfaceErrorReason"); + ASSERT_EQ (interfaceError.module (), "interfaceErrorModule"); + ASSERT_EQ (interfaceError.file (), "interfaceErrorFile"); + ASSERT_EQ (interfaceError.mountPoint (), "interfaceErrorMountPoint"); + ASSERT_EQ (interfaceError.configFile (), "interfaceErrorConfigFile"); + ASSERT_EQ (interfaceError.line (), 2); /* PluginMisbehavior error */ - kdb::tools::errors::PluginMisbehaviorError pluginMisbehaviorError ("pluginMisbehaviorErrorReason", - "pluginMisbehaviorErrorModule", - "pluginMisbehaviorErrorFile", - "pluginMisbehaviorErrorMountPoint", + kdb::tools::errors::PluginMisbehaviorError pluginMisbehaviorError ("pluginMisbehaviorErrorReason", "pluginMisbehaviorErrorModule", + "pluginMisbehaviorErrorFile", "pluginMisbehaviorErrorMountPoint", "pluginMisbehaviorErrorConfigFile", 3); - ASSERT_EQ(pluginMisbehaviorError.code(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR); - ASSERT_EQ(pluginMisbehaviorError.description(), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME); - ASSERT_EQ(pluginMisbehaviorError.reason(), "pluginMisbehaviorErrorReason"); - ASSERT_EQ(pluginMisbehaviorError.module(), "pluginMisbehaviorErrorModule"); - ASSERT_EQ(pluginMisbehaviorError.file(), "pluginMisbehaviorErrorFile"); - ASSERT_EQ(pluginMisbehaviorError.mountPoint(), "pluginMisbehaviorErrorMountPoint"); - ASSERT_EQ(pluginMisbehaviorError.configFile(), "pluginMisbehaviorErrorConfigFile"); - ASSERT_EQ(pluginMisbehaviorError.line(), 3); + ASSERT_EQ (pluginMisbehaviorError.code (), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR); + ASSERT_EQ (pluginMisbehaviorError.description (), ELEKTRA_ERROR_PLUGIN_MISBEHAVIOR_NAME); + ASSERT_EQ (pluginMisbehaviorError.reason (), "pluginMisbehaviorErrorReason"); + ASSERT_EQ (pluginMisbehaviorError.module (), "pluginMisbehaviorErrorModule"); + ASSERT_EQ (pluginMisbehaviorError.file (), "pluginMisbehaviorErrorFile"); + ASSERT_EQ (pluginMisbehaviorError.mountPoint (), "pluginMisbehaviorErrorMountPoint"); + ASSERT_EQ (pluginMisbehaviorError.configFile (), "pluginMisbehaviorErrorConfigFile"); + ASSERT_EQ (pluginMisbehaviorError.line (), 3); /* ConflictingStatie error */ - kdb::tools::errors::ConflictingStateError conflictingStateError ("conflictingStateErrorReason", - "conflictingStateErrorModule", - "conflictingStateErrorFile", - "conflictingStateErrorMountPoint", + kdb::tools::errors::ConflictingStateError conflictingStateError ("conflictingStateErrorReason", "conflictingStateErrorModule", + "conflictingStateErrorFile", "conflictingStateErrorMountPoint", "conflictingStateErrorConfigFile", 4); - ASSERT_EQ(conflictingStateError.code(), ELEKTRA_ERROR_CONFLICTING_STATE); - ASSERT_EQ(conflictingStateError.description(), ELEKTRA_ERROR_CONFLICTING_STATE_NAME); - ASSERT_EQ(conflictingStateError.reason(), "conflictingStateErrorReason"); - ASSERT_EQ(conflictingStateError.module(), "conflictingStateErrorModule"); - ASSERT_EQ(conflictingStateError.file(), "conflictingStateErrorFile"); - ASSERT_EQ(conflictingStateError.mountPoint(), "conflictingStateErrorMountPoint"); - ASSERT_EQ(conflictingStateError.configFile(), "conflictingStateErrorConfigFile"); - ASSERT_EQ(conflictingStateError.line(), 4); + ASSERT_EQ (conflictingStateError.code (), ELEKTRA_ERROR_CONFLICTING_STATE); + ASSERT_EQ (conflictingStateError.description (), ELEKTRA_ERROR_CONFLICTING_STATE_NAME); + ASSERT_EQ (conflictingStateError.reason (), "conflictingStateErrorReason"); + ASSERT_EQ (conflictingStateError.module (), "conflictingStateErrorModule"); + ASSERT_EQ (conflictingStateError.file (), "conflictingStateErrorFile"); + ASSERT_EQ (conflictingStateError.mountPoint (), "conflictingStateErrorMountPoint"); + ASSERT_EQ (conflictingStateError.configFile (), "conflictingStateErrorConfigFile"); + ASSERT_EQ (conflictingStateError.line (), 4); /* ValidationSyntactic error */ - kdb::tools::errors::ValidationSyntacticError validationSyntacticError ("validationSyntacticErrorReason", - "validationSyntacticErrorModule", - "validationSyntacticErrorFile", - "validationSyntacticErrorMountPoint", - "validationSyntacticErrorConfigFile", 5); - ASSERT_EQ(validationSyntacticError.code(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC); - ASSERT_EQ(validationSyntacticError.description(), ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME); - ASSERT_EQ(validationSyntacticError.reason(), "validationSyntacticErrorReason"); - ASSERT_EQ(validationSyntacticError.module(), "validationSyntacticErrorModule"); - ASSERT_EQ(validationSyntacticError.file(), "validationSyntacticErrorFile"); - ASSERT_EQ(validationSyntacticError.mountPoint(), "validationSyntacticErrorMountPoint"); - ASSERT_EQ(validationSyntacticError.configFile(), "validationSyntacticErrorConfigFile"); - ASSERT_EQ(validationSyntacticError.line(), 5); + kdb::tools::errors::ValidationSyntacticError validationSyntacticError ( + "validationSyntacticErrorReason", "validationSyntacticErrorModule", "validationSyntacticErrorFile", + "validationSyntacticErrorMountPoint", "validationSyntacticErrorConfigFile", 5); + ASSERT_EQ (validationSyntacticError.code (), ELEKTRA_ERROR_VALIDATION_SYNTACTIC); + ASSERT_EQ (validationSyntacticError.description (), ELEKTRA_ERROR_VALIDATION_SYNTACTIC_NAME); + ASSERT_EQ (validationSyntacticError.reason (), "validationSyntacticErrorReason"); + ASSERT_EQ (validationSyntacticError.module (), "validationSyntacticErrorModule"); + ASSERT_EQ (validationSyntacticError.file (), "validationSyntacticErrorFile"); + ASSERT_EQ (validationSyntacticError.mountPoint (), "validationSyntacticErrorMountPoint"); + ASSERT_EQ (validationSyntacticError.configFile (), "validationSyntacticErrorConfigFile"); + ASSERT_EQ (validationSyntacticError.line (), 5); /* ValidationSemantic error */ - kdb::tools::errors::ValidationSemanticError validationSemanticError ("validationSemanticErrorReason", - "validationSemanticErrorModule", - "validationSemanticErrorFile", - "validationSemanticErrorMountPoint", - "validationSemanticErrorConfigFile", 6); - ASSERT_EQ(validationSemanticError.code(), ELEKTRA_ERROR_VALIDATION_SEMANTIC); - ASSERT_EQ(validationSemanticError.description(), ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME); - ASSERT_EQ(validationSemanticError.reason(), "validationSemanticErrorReason"); - ASSERT_EQ(validationSemanticError.module(), "validationSemanticErrorModule"); - ASSERT_EQ(validationSemanticError.file(), "validationSemanticErrorFile"); - ASSERT_EQ(validationSemanticError.mountPoint(), "validationSemanticErrorMountPoint"); - ASSERT_EQ(validationSemanticError.configFile(), "validationSemanticErrorConfigFile"); - ASSERT_EQ(validationSemanticError.line(), 6); + kdb::tools::errors::ValidationSemanticError validationSemanticError ( + "validationSemanticErrorReason", "validationSemanticErrorModule", "validationSemanticErrorFile", + "validationSemanticErrorMountPoint", "validationSemanticErrorConfigFile", 6); + ASSERT_EQ (validationSemanticError.code (), ELEKTRA_ERROR_VALIDATION_SEMANTIC); + ASSERT_EQ (validationSemanticError.description (), ELEKTRA_ERROR_VALIDATION_SEMANTIC_NAME); + ASSERT_EQ (validationSemanticError.reason (), "validationSemanticErrorReason"); + ASSERT_EQ (validationSemanticError.module (), "validationSemanticErrorModule"); + ASSERT_EQ (validationSemanticError.file (), "validationSemanticErrorFile"); + ASSERT_EQ (validationSemanticError.mountPoint (), "validationSemanticErrorMountPoint"); + ASSERT_EQ (validationSemanticError.configFile (), "validationSemanticErrorConfigFile"); + ASSERT_EQ (validationSemanticError.line (), 6); } TEST (Error, ErrWarn) { std::cout << std::endl << "TEST ERRORS WITH WARNINGS" << std::endl; - kdb::tools::errors::PluginMisbehaviorError pmE ("pmeReason", "pmeModule", "pmeFile", "pmeMountPoint", - "pmeConfigFile", 100); - kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", "cswMountPoint", - "cswConfigFile", 101); - kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", "ifwMountPoint", - "ifwConfigFile", 102); + kdb::tools::errors::PluginMisbehaviorError pmE ("pmeReason", "pmeModule", "pmeFile", "pmeMountPoint", "pmeConfigFile", 100); + kdb::tools::errors::ConflictingStateWarning csW ("cswReason", "cswModule", "cswFile", "cswMountPoint", "cswConfigFile", 101); + kdb::tools::errors::InterfaceWarning ifW ("ifwReason", "ifwModule", "ifwFile", "ifwMountPoint", "ifwConfigFile", 102); pmE.addWarning (csW); pmE.addWarning (ifW); - ASSERT_EQ (pmE.warningCount(), 2); + ASSERT_EQ (pmE.warningCount (), 2); int i = 0; - for(kdb::tools::errors::Warning* w : pmE) { + for (kdb::tools::errors::Warning * w : pmE) + { std::cout << std::endl << "WARNING IN ERROR: " << *w << std::endl; if (i++ == 0) @@ -321,14 +276,10 @@ TEST (Error, Equality) { std::cout << std::endl << "TEST EQUALITY OF WARNINGS AND ERRORS" << std::endl; - kdb::tools::errors::InstallationWarning installationWarning ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); - kdb::tools::errors::InstallationWarning installationWarning2 ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); - kdb::tools::errors::InterfaceWarning interfaceWarning ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); - kdb::tools::errors::InstallationError installationError ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); + kdb::tools::errors::InstallationWarning installationWarning ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); + kdb::tools::errors::InstallationWarning installationWarning2 ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); + kdb::tools::errors::InterfaceWarning interfaceWarning ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); + kdb::tools::errors::InstallationError installationError ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); ASSERT_EQ (installationWarning, installationWarning); ASSERT_EQ (installationWarning, installationWarning2); @@ -337,10 +288,8 @@ TEST (Error, Equality) ASSERT_NE (installationWarning, installationError); ASSERT_NE (installationError, installationWarning); - kdb::tools::errors::InstallationError installationError1 ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); - kdb::tools::errors::ResourceError resourceError ("Reason", "Module", "File", "MountPoint", - "ConfigFile", 42); + kdb::tools::errors::InstallationError installationError1 ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); + kdb::tools::errors::ResourceError resourceError ("Reason", "Module", "File", "MountPoint", "ConfigFile", 42); ASSERT_NE (installationError, resourceError); ASSERT_EQ (installationError, installationError1); @@ -364,10 +313,7 @@ TEST (Error, Equality) ASSERT_NE (installationError, resourceError); /* currently the Warning gets copied when it is added to the Error */ - ASSERT_EQ (installationError[2].file(), installationError1[0].file()); - installationError[2].file() = "changed file in warning"; - ASSERT_NE (installationError[2].file(), installationError1[0].file()); - - - + ASSERT_EQ (installationError[2].file (), installationError1[0].file ()); + installationError[2].file () = "changed file in warning"; + ASSERT_NE (installationError[2].file (), installationError1[0].file ()); } \ No newline at end of file diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index 83763a71612..ae111f67072 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -6,12 +6,12 @@ * @copyright BSD License (see LICENSE.md or https://www.libelektra.org) */ -#include #include -#include #include #include +#include #include /* for removeNamespace (Key) */ +#include using namespace std; using namespace kdb; @@ -45,11 +45,11 @@ int ValidateCommand::execute (Cmdline const & cl) if (cl.verbose) { - cout << "The name of the root key is: " + root.getName() << endl; + cout << "The name of the root key is: " + root.getName () << endl; } - /* Remove namespace -> create cascading key, so that - * check-constraints in the spec:/ namespace are considered. */ + /* Remove namespace -> create cascading key, so that + * check-constraints in the spec:/ namespace are considered. */ Key parentKey = removeNamespace (root); // do not resume on any get errors @@ -58,13 +58,16 @@ int ValidateCommand::execute (Cmdline const & cl) kdb.get (ksUnfiltered, root); /* Convert result of kdb.get to Error object of the C++ errors/warnings API */ - tools::errors::Error *result = tools::errors::ErrorFactory::fromKey (root); + tools::errors::Error * result = tools::errors::ErrorFactory::fromKey (root); /* If no warnings or errors occurred, the ErrorFactory returns a nullptr. */ if (result) { - cout << getFormattedInfoString ("The following warnings were issued while" - " trying to get the values of the keys: ") << endl << endl; + cout << getFormattedInfoString ( + "The following warnings were issued while" + " trying to get the values of the keys: ") + << endl + << endl; cerr << *result << endl << endl; @@ -133,21 +136,15 @@ int ValidateCommand::execute (Cmdline const & cl) std::string ValidateCommand::getFormattedErrorString (const std::string & str) { - return getErrorColor (ANSI_COLOR::BOLD) - + getErrorColor (ANSI_COLOR::MAGENTA) + str - + getErrorColor (ANSI_COLOR::RESET); + return getErrorColor (ANSI_COLOR::BOLD) + getErrorColor (ANSI_COLOR::MAGENTA) + str + getErrorColor (ANSI_COLOR::RESET); } std::string ValidateCommand::getFormattedSuccessString (const std::string & str) { - return getStdColor (ANSI_COLOR::BOLD) - + getStdColor (ANSI_COLOR::GREEN) + str - + getStdColor (ANSI_COLOR::RESET); + return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::GREEN) + str + getStdColor (ANSI_COLOR::RESET); } std::string ValidateCommand::getFormattedInfoString (const std::string & str) { - return getStdColor (ANSI_COLOR::BOLD) - + getStdColor (ANSI_COLOR::YELLOW) + str - + getStdColor (ANSI_COLOR::RESET); + return getStdColor (ANSI_COLOR::BOLD) + getStdColor (ANSI_COLOR::YELLOW) + str + getStdColor (ANSI_COLOR::RESET); } From 18edf392e8203989834a5c13debf5c1e4e7f8ab5 Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 19 Jan 2022 23:40:04 +0100 Subject: [PATCH 18/23] kdb validate: info output when warnigns occured while kdb.get() and -f was set only when also -v was given --- src/tools/kdb/validate.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index ae111f67072..38a58250169 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -74,7 +74,7 @@ int ValidateCommand::execute (Cmdline const & cl) /* After printing the Warnings, the object is no longer needed. */ delete result; - if (cl.force) + if (cl.force && cl.verbose) { cout << getFormattedInfoString ( "Because -f was given, we now try to set the values " From 937b3298f1c733b885afa2d959c11e10a9eeffd9 Mon Sep 17 00:00:00 2001 From: flo91 Date: Wed, 19 Jan 2022 23:58:21 +0100 Subject: [PATCH 19/23] doc: update links in manpages as reported by the buildserver check --- doc/decisions/error_codes.md | 2 +- doc/tutorials/highlevel-bindings.md | 2 +- doc/tutorials/install-webui.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/doc/decisions/error_codes.md b/doc/decisions/error_codes.md index e5040eadc8d..64c4359d45a 100644 --- a/doc/decisions/error_codes.md +++ b/doc/decisions/error_codes.md @@ -58,7 +58,7 @@ Various projects and standards: Currently they have 43 classes which all come from SQLSTATE. Postgres also throws additional errors but have to subclass it to one of the current 43 classes and have a special naming convention which have to start with a `P` in the subclass. - [etcd](https://github.com/etcd-io/etcd): Etcd's approach for errors are tightly coupled to the programming language Go as well as the [gRPC](https://grpc.io/) standard which currently has - [16 codes](https://godoc.org/google.golang.org/grpc/codes) defined. Some of these errors are similar or identical to those which will be used in elektra. + [16 codes](https://pkg.go.dev/google.golang.org/grpc/codes?utm_source=godoc) defined. Some of these errors are similar or identical to those which will be used in elektra. Every error of etcd is associated with one of these categories and gets its own error message which is specified in [this](https://github.com/etcd-io/etcd/blob/master/etcdserver/api/v3rpc/rpctypes/error.go) file. This concept though does not allow easy subclassing which might be useful (eg. further split FailedPrecondition into more specific errors like semantic and syntactic errors) - [Windows Registry](https://docs.microsoft.com/en-us/windows/desktop/sysinfo/registry): The registry does not use any specific error concept but takes the standard [Win32 Error Codes](https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-erref/18d8fbe8-a967-4f1c-ae50-99ca8e491d2d). These are neither hierarchical nor have any special ordering. Basically it is the same as elektra has now except for no duplicated diff --git a/doc/tutorials/highlevel-bindings.md b/doc/tutorials/highlevel-bindings.md index 06f9b0240c9..ceb12b91fac 100644 --- a/doc/tutorials/highlevel-bindings.md +++ b/doc/tutorials/highlevel-bindings.md @@ -56,7 +56,7 @@ direct mapping, significantly simplifies the generated code (and maybe also the ### Bindings for the `lowlevel` Library -Your programming language of choice must provide a way to call into C code (like [cgo](https://golang.org/cmd/cgo/)). +Your programming language of choice must provide a way to call into C code (like [cgo](https://pkg.go.dev/cmd/cgo/)). In general we prefer (in this order): diff --git a/doc/tutorials/install-webui.md b/doc/tutorials/install-webui.md index d3ea262ca52..0ba1516bbac 100644 --- a/doc/tutorials/install-webui.md +++ b/doc/tutorials/install-webui.md @@ -12,7 +12,7 @@ Elektra-web requires: - [Elektra](https://libelektra.org/) with the [`yajl` plugin](https://master.libelektra.org/src/plugins/yajl/) installed - A recent [node.js](https://nodejs.org/en/) installation (at least 6.x) -- [Go](https://golang.org/) with version > 1.13 +- [Go](https://go.dev/) with version > 1.13 ## Building with elektra-web Tool From c8525a71579f20b10594bdccd47ae0b8b4fbfca7 Mon Sep 17 00:00:00 2001 From: flo91 Date: Thu, 20 Jan 2022 00:00:28 +0100 Subject: [PATCH 20/23] doc: remove broken links as reported by the buildserver (checked also manually - they are currently broken!) --- tests/linkchecker.whitelist | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/linkchecker.whitelist b/tests/linkchecker.whitelist index 72a8a1c9c03..629e9c17b40 100644 --- a/tests/linkchecker.whitelist +++ b/tests/linkchecker.whitelist @@ -33,6 +33,7 @@ https://doc.libelektra.org/coverage/master/debian-buster-full https://crates.io/crates/elektra https://crates.io/crates/elektra-sys https://www.sciencedaily.com/releases/2005/11/051103080801.htm +https://lgtm.com # Certificate issues https://oyranos.org From b498283e5d492e445b460ddaa59d35fd3aff4106 Mon Sep 17 00:00:00 2001 From: flo91 Date: Thu, 20 Jan 2022 13:24:44 +0100 Subject: [PATCH 21/23] doc: create manpage and remove broken links from READMEs as reported by the CI --- doc/man/man1/kdb-validate.1 | 23 ++++------------------- src/plugins/README.md | 1 - src/plugins/ni/README.md | 2 -- 3 files changed, 4 insertions(+), 22 deletions(-) diff --git a/doc/man/man1/kdb-validate.1 b/doc/man/man1/kdb-validate.1 index f129462a002..29a6ff8294a 100644 --- a/doc/man/man1/kdb-validate.1 +++ b/doc/man/man1/kdb-validate.1 @@ -1,52 +1,37 @@ -.\" generated with Ronn/v0.7.3 -.\" http://github.com/rtomayko/ronn/tree/0.7.3 -. -.TH "KDB\-VALIDATE" "1" "January 2022" "" "" -. +.\" generated with Ronn-NG/v0.10.1 +.\" http://github.com/apjanke/ronn-ng/tree/0.10.1.pre1 +.TH "KDB\-VALIDATE" "1" "January 2022" "" .SH "NAME" \fBkdb\-validate\fR \- Validate key values -. .SH "SYNOPSIS" \fBkdb validate\fR -. .SH "DESCRIPTION" Validate the values of string keys below a given name using the loaded validation plugins (eg\. range or validation) by reading all values, making them dirty by changing to another value, changing back to original and then writing that back to the key database\. -. .P This command is useful for validating configuration files against their specifications\. -. .P -For keys to be validated, they must contain the \'check\'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using \fBkdb set\fR or \fBkdb get\fR the same validation is also done by \fBkdb validate\fR Only string keys are validated! Binary keys are skipped! -. +For keys to be validated, they must contain the 'check'\-metakeys and the respective plugins for validation must be loaded for the backend that was used while mounting\. If a validation is done while using \fBkdb set\fR or \fBkdb get\fR the same validation is also done by \fBkdb validate\fR Only string keys are validated! Binary keys are skipped! .P Use \fB\-f\fR to do a write\-test even if the previous read from the key database has issued warnings\. -. .SH "OPTIONS" -. .TP \fB\-d\fR,\fB\-\-debug\fR Give debug information\. -. .TP \fB\-f\fR, \fB\-\-force\fR Force writing the configuration even on warnings\. -. .TP \fB\-H\fR, \fB\-\-help\fR Show the man page\. -. .TP \fB\-p \fR, \fB\-\-profile \fR Use a different profile for kdb configuration\. -. .TP \fB\-v\fR, \fB\-\-verbose\fR Explain what is happening\. -. .TP \fB\-V\fR, \fB\-\-version\fR Print version info\. -. .TP \fB\-C \fR, \fB\-\-color \fR Print \fBnever/auto(default)/always\fR colored output\. diff --git a/src/plugins/README.md b/src/plugins/README.md index 7fce748b7bb..5b37d3f3844 100644 --- a/src/plugins/README.md +++ b/src/plugins/README.md @@ -101,7 +101,6 @@ Using semi-structured data for config files, mainly suitable for spec-namespace (put a focus on having nice syntax for metadata): - [ni](ni/) parses INI files based on (including metadata) - [ni](https://lab.burn.capital/chaz-attic/bohr/-/blob/main/include/bohr/ni.h). Only suited for import/export: diff --git a/src/plugins/ni/README.md b/src/plugins/ni/README.md index 4f153a30f09..853be1018f1 100644 --- a/src/plugins/ni/README.md +++ b/src/plugins/ni/README.md @@ -127,5 +127,3 @@ The memory footprint is for a 190.000 (reduced to 35.000 when rewrote first ) line ini file with 1.1MB size is 16.88 MB. The sort order is not stable, even not with the same file rewritten again. - -[bohr libraries](https://lab.burn.capital/chaz-attic/bohr) From b0f940c1f44d7f9d82945c3331b32256068c191b Mon Sep 17 00:00:00 2001 From: flo91 Date: Thu, 20 Jan 2022 14:01:35 +0100 Subject: [PATCH 22/23] doc: patch manpage for changed readme --- doc/man/man7/elektra-plugins.7 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/man/man7/elektra-plugins.7 b/doc/man/man7/elektra-plugins.7 index 36caeb40d5b..5d9aa42463b 100644 --- a/doc/man/man7/elektra-plugins.7 +++ b/doc/man/man7/elektra-plugins.7 @@ -1,6 +1,6 @@ .\" generated with Ronn-NG/v0.10.1 .\" http://github.com/apjanke/ronn-ng/tree/0.10.1.pre1 -.TH "ELEKTRA\-PLUGINS" "7" "July 2021" "" +.TH "ELEKTRA\-PLUGINS" "7" "January 2022" "" .SH "NAME" \fBelektra\-plugins\fR \- plugins overview .P @@ -94,7 +94,7 @@ augeas \fIaugeas/\fR reads/writes many different configuration files using the A .P Using semi\-structured data for config files, mainly suitable for spec\-namespace (put a focus on having nice syntax for metadata): .IP "\(bu" 4 -ni \fIni/\fR parses INI files based on (including metadata) ni \fIhttps://lab\.burn\.capital/chaz\-attic/bohr/\-/blob/main/include/bohr/ni\.h\fR\. +ni \fIni/\fR parses INI files based on (including metadata) .IP "" 0 .P Only suited for import/export: From 4cab540198637b4fee52acf0919836baece3e17d Mon Sep 17 00:00:00 2001 From: flo91 Date: Thu, 20 Jan 2022 18:07:57 +0100 Subject: [PATCH 23/23] kdb: fix error so that -f without -v is still correctly handled libtools: make error and warning factories not instantiable (by deleting their default constructors and destructors) --- .../tools/include/errors/errorFactory.hpp | 3 +++ .../tools/include/errors/warningFactory.hpp | 3 +++ src/tools/kdb/validate.cpp | 20 +++++++++---------- src/tools/kdb/validate.hpp | 2 +- 4 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/libs/tools/include/errors/errorFactory.hpp b/src/libs/tools/include/errors/errorFactory.hpp index dd52731bdea..33f291aeb4c 100644 --- a/src/libs/tools/include/errors/errorFactory.hpp +++ b/src/libs/tools/include/errors/errorFactory.hpp @@ -18,6 +18,9 @@ namespace errors class ErrorFactory { public: + ErrorFactory () = delete; + ~ErrorFactory () = delete; + /* takes one of the ELEKTRA_ERROR_* constants (e.g. ELEKTRA_ERROR_OUT_OF_MEMORY) * from /src/include/kdberrors.h as a parameter */ static Error * create (const std::string & type, const std::string & reason, const std::string & module, const std::string & file, diff --git a/src/libs/tools/include/errors/warningFactory.hpp b/src/libs/tools/include/errors/warningFactory.hpp index 91895d5c17e..306a9d2009f 100644 --- a/src/libs/tools/include/errors/warningFactory.hpp +++ b/src/libs/tools/include/errors/warningFactory.hpp @@ -17,6 +17,9 @@ namespace errors class WarningFactory { public: + WarningFactory () = delete; + ~WarningFactory () = delete; + /* takes one of the ELEKTRA_WARNING_* constants (e.g. ELEKTRA_WARNING_OUT_OF_MEMORY) * from /src/include/kdberrors.h as a parameter */ /* You must delete the object that is returned by this function! */ diff --git a/src/tools/kdb/validate.cpp b/src/tools/kdb/validate.cpp index 38a58250169..7715b8c6bb8 100644 --- a/src/tools/kdb/validate.cpp +++ b/src/tools/kdb/validate.cpp @@ -50,7 +50,7 @@ int ValidateCommand::execute (Cmdline const & cl) /* Remove namespace -> create cascading key, so that * check-constraints in the spec:/ namespace are considered. */ - Key parentKey = removeNamespace (root); + root = removeNamespace (root); // do not resume on any get errors // otherwise the user might break @@ -74,14 +74,7 @@ int ValidateCommand::execute (Cmdline const & cl) /* After printing the Warnings, the object is no longer needed. */ delete result; - if (cl.force && cl.verbose) - { - cout << getFormattedInfoString ( - "Because -f was given, we now try to set the values " - "despite warnings during getting them...") - << endl; - } - else + if (!cl.force) { cerr << getFormattedErrorString ( "The validation was stopped because of warnings " @@ -89,6 +82,13 @@ int ValidateCommand::execute (Cmdline const & cl) << endl; return 1; } + else if (cl.verbose) + { + cout << getFormattedInfoString ( + "Because -f was given, we now try to set the values " + "despite warnings during getting them...") + << endl; + } } else { @@ -127,7 +127,7 @@ int ValidateCommand::execute (Cmdline const & cl) { cout << getFormattedInfoString ("The following error was issued while trying to set the values back: ") << endl << endl; result = tools::errors::ErrorFactory::fromKey (root); - cerr << *result << endl; + cerr << *result << endl << endl; delete result; return 1; } diff --git a/src/tools/kdb/validate.hpp b/src/tools/kdb/validate.hpp index 8d98e01a730..3925cae0c22 100644 --- a/src/tools/kdb/validate.hpp +++ b/src/tools/kdb/validate.hpp @@ -48,7 +48,7 @@ class ValidateCommand : public Command "Only string keys are validated!\n" "\n" "Use -f to do a write test even if the previous read\n" - "from the key database has issued warnings."; + "from the key database has issued warnings.\n"; } virtual int execute (Cmdline const & cmdline) override;