knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
KnowRobError.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_KNOWROB_ERROR_H
7 #define KNOWROB_KNOWROB_ERROR_H
8 
9 #include <fmt/core.h>
10 #include <optional>
11 
12 namespace knowrob {
16  class KnowRobError : public std::runtime_error {
17  public:
22  KnowRobError(std::string_view exc_type, std::string_view msg);
23 
29  KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view trace);
30 
37  KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view file, int line);
38 
46  KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view file, int line, std::string_view trace);
47 
51  void setFile(std::string_view file) { file_ = file; }
52 
56  void setLine(int line) { line_ = line; }
57 
61  auto file() const { return file_; }
62 
66  auto line() const { return line_; }
67 
71  bool hasFile() const { return file_.has_value(); }
72 
73  protected:
74  std::optional<std::string> file_;
75  int line_;
76  };
77 }
78 
79 #define LOG_KNOWROB_ERROR(err, msg, ...) \
80  if(err.hasFile()) { \
81  KB_ERROR("{}. Origin of error is:", fmt::format(msg, ##__VA_ARGS__)); \
82  KB_ERROR1(err.file()->c_str(), err.line(), err.what()); \
83  } else { \
84  KB_ERROR("{}. {}", fmt::format(msg, ##__VA_ARGS__), err.what()); \
85  }
86 
87 #endif //KNOWROB_KNOWROB_ERROR_H
KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view file, int line, std::string_view trace)
KnowRobError(std::string_view exc_type, std::string_view msg)
KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view file, int line)
std::optional< std::string > file_
Definition: KnowRobError.h:74
KnowRobError(std::string_view exc_type, std::string_view msg, std::string_view trace)
bool hasFile() const
Definition: KnowRobError.h:71
void setLine(int line)
Definition: KnowRobError.h:56
void setFile(std::string_view file)
Definition: KnowRobError.h:51