From 435d697b7b56089b19f016452913e5f04e367950 Mon Sep 17 00:00:00 2001 From: Christoph Lehmann <christoph.lehmann@ufz.de> Date: Sun, 24 Jan 2016 11:47:27 +0100 Subject: [PATCH] [BL] added filename member --- BaseLib/ConfigTreeNew.cpp | 22 +++++++++++++++------- BaseLib/ConfigTreeNew.h | 13 +++++++++---- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/BaseLib/ConfigTreeNew.cpp b/BaseLib/ConfigTreeNew.cpp index 1943d1baa4a..cf78d169720 100644 --- a/BaseLib/ConfigTreeNew.cpp +++ b/BaseLib/ConfigTreeNew.cpp @@ -18,9 +18,10 @@ const std::string ConfigTreeNew::key_chars = key_chars_start + "_0123456789"; ConfigTreeNew:: ConfigTreeNew(PTree const& tree, + std::string const& filename, Callback const& error_cb, Callback const& warning_cb) - : _tree(&tree), _onerror(error_cb), _onwarning(warning_cb) + : _tree(&tree), _filename(filename), _onerror(error_cb), _onwarning(warning_cb) { if (!_onerror) { ERR("ConfigTree: No valid error handler provided."); @@ -36,6 +37,7 @@ ConfigTreeNew:: ConfigTreeNew(PTree const& tree, ConfigTreeNew const& parent, std::string const& root) : _tree(&tree), _path(joinPaths(parent._path, root)), + _filename(parent._filename), _onerror(parent._onerror), _onwarning(parent._onwarning) { checkKeyname(root); @@ -45,6 +47,7 @@ ConfigTreeNew:: ConfigTreeNew(ConfigTreeNew && other) : _tree (other._tree) , _path (std::move(other._path)) + , _filename (std::move(other._filename)) , _visited_params(std::move(other._visited_params)) , _have_read_data (other._have_read_data) , _onerror (std::move(other._onerror)) @@ -67,6 +70,7 @@ operator=(ConfigTreeNew&& other) _tree = other._tree; other._tree = nullptr; _path = std::move(other._path); + _filename = std::move(other._filename); _visited_params = std::move(other._visited_params); _have_read_data = other._have_read_data; _onerror = std::move(other._onerror); @@ -157,25 +161,29 @@ void ConfigTreeNew::ignoreConfParamAll(const std::string ¶m) const void ConfigTreeNew::error(const std::string& message) const { - _onerror(_path, message); + _onerror(_filename, _path, message); std::abort(); } void ConfigTreeNew::warning(const std::string& message) const { - _onwarning(_path, message); + _onwarning(_filename, _path, message); } -void ConfigTreeNew::onerror(const std::string& path, const std::string& message) +void ConfigTreeNew::onerror(const std::string& filename, const std::string& path, + const std::string& message) { - ERR("ConfigTree: At path <%s>: %s", path.c_str(), message.c_str()); + ERR("ConfigTree: In file `%s' at path <%s>: %s", + filename.c_str(), path.c_str(), message.c_str()); std::abort(); } -void ConfigTreeNew::onwarning(const std::string& path, const std::string& message) +void ConfigTreeNew::onwarning(const std::string& filename, const std::string& path, + const std::string& message) { - WARN("ConfigTree: At path <%s>: %s", path.c_str(), message.c_str()); + WARN("ConfigTree: In file `%s' at path <%s>: %s", + filename.c_str(), path.c_str(), message.c_str()); } std::string ConfigTreeNew::shortString(const std::string &s) diff --git a/BaseLib/ConfigTreeNew.h b/BaseLib/ConfigTreeNew.h index 291ea5f16e0..aaf6d7f8d05 100644 --- a/BaseLib/ConfigTreeNew.h +++ b/BaseLib/ConfigTreeNew.h @@ -172,7 +172,8 @@ public: //! Type of the function objects used as callbacks. //! The first argument denotes the path in the tree at which an event (warning/error) //! occured, the second argument is the associated message - using Callback = std::function<void(const std::string& path, + using Callback = std::function<void(const std::string& filename, + const std::string& path, const std::string& message)>; /*! @@ -194,6 +195,7 @@ public: * i.e., warnings will also result in program abortion! */ explicit ConfigTreeNew(PTree const& tree, + std::string const& filename, Callback const& error_cb = onerror, Callback const& warning_cb = onerror); @@ -340,11 +342,13 @@ public: //! Default error callback function //! Will print an error message and call std::abort() - static void onerror(std::string const& path, std::string const& message); + static void onerror(std::string const& filename, std::string const& path, + std::string const& message); //! Default warning callback function //! Will print a warning message - static void onwarning(std::string const& path, std::string const& message); + static void onwarning(std::string const& filename, std::string const& path, + std::string const& message); private: struct CountType @@ -417,7 +421,8 @@ private: //! A path printed in error/warning messages. std::string _path; - //! \todo add file name + //! TODO doc + std::string _filename; using KeyType = std::pair<bool, std::string>; -- GitLab