knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
logging.cpp
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 #include <SWI-Prolog.h>
7 #include "knowrob/Logger.h"
8 #include "knowrob/integration/prolog/PrologTerm.h"
9 #include "knowrob/integration/prolog/logging.h"
10 
11 using namespace knowrob;
12 
13 static inline foreign_t pl_log_message_internal(term_t level_term, term_t msg_term, const char *file, int line) {
14  static atom_t ATOM_error = PL_new_atom("error");
15  static atom_t ATOM_warning = PL_new_atom("warning");
16  static atom_t ATOM_informational = PL_new_atom("informational");
17  static atom_t ATOM_debug = PL_new_atom("debug");
18 
19  // first argument is the log level
20  atom_t level_atom;
21  if (!PL_get_atom(level_term, &level_atom)) {
22  return FALSE;
23  }
24  // second argument holds the logged term
25  TermPtr msg = PrologTerm::toKnowRobTerm(msg_term);
26 
27  // remove single quotes from logged message which are added
28  // by the string conversion of Term objects.
29  std::stringstream ss;
30  if (msg->isAtomic()) {
31  ss << std::static_pointer_cast<Atomic>(msg)->stringForm();
32  }
33  std::string msg_str = ss.str();
34  if (msg_str.front() == '\'') {
35  msg_str.erase(0, 1);
36  msg_str.erase(msg_str.size() - 1);
37  }
38 
39  // finally call the logging macro according to the level
40  // of the log message
41  if (level_atom == ATOM_debug) {
42  KB_DEBUG1(file, line, "{}", msg_str);
43  } else if (level_atom == ATOM_informational) {
44  KB_INFO1(file, line, "{}", msg_str);
45  } else if (level_atom == ATOM_warning) {
46  KB_WARN1(file, line, "{}", msg_str);
47  } else if (level_atom == ATOM_error) {
48  KB_ERROR1(file, line, "{}", msg_str);
49  } else {
50  KB_WARN("unknown logging level '{}'.", PL_atom_chars(level_atom));
51  KB_INFO1(file, line, "{}", *msg);
52  }
53  return TRUE;
54 }
55 
56 namespace knowrob::prolog {
57  foreign_t log_message2(term_t level_term, term_t msg_term) {
58  return pl_log_message_internal(level_term, msg_term, __FILE__, __LINE__);
59  }
60 
61  foreign_t log_message4(term_t level_term, term_t msg_term, term_t file_term, term_t line_term) {
62  atom_t file_atom;
63  int line_number = 0;
64  if (PL_get_atom(file_term, &file_atom) &&
65  PL_get_integer(line_term, &line_number)) {
66  const char *file_string = PL_atom_chars(file_atom);
67  return pl_log_message_internal(level_term, msg_term, file_string, line_number);
68  } else {
69  return log_message2(level_term, msg_term);
70  }
71  }
72 }
#define KB_DEBUG1(file, line,...)
Definition: Logger.h:41
#define KB_WARN1(file, line,...)
Definition: Logger.h:53
#define KB_INFO1(file, line,...)
Definition: Logger.h:47
#define KB_ERROR1(file, line,...)
Definition: Logger.h:59
#define KB_WARN
Definition: Logger.h:27
TermPtr toKnowRobTerm() const
Definition: PrologTerm.cpp:691
TermRule & string()
Definition: terms.cpp:63
foreign_t log_message4(term_t level_term, term_t msg_term, term_t file_term, term_t line_term)
Definition: logging.cpp:61
foreign_t log_message2(term_t level_term, term_t msg_term)
Definition: logging.cpp:57
std::shared_ptr< Term > TermPtr
Definition: Term.h:117