Fawkes API  Fawkes Development Version
print_action_executor.cpp
1 /***************************************************************************
2  * print_action_executor.cpp - A simple action executor for printing
3  *
4  * Created: Tue 05 Nov 2019 14:38:48 CET 14:38
5  * Copyright 2019 Till Hofmann <hofmann@kbsg.rwth-aachen.de>
6  ****************************************************************************/
7 
8 /* This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU Library General Public License for more details.
17  *
18  * Read the full text in the LICENSE.GPL file in the doc directory.
19  */
20 
21 #include "print_action_executor.h"
22 
23 #include "utils.h"
24 
25 #include <logging/logger.h>
26 
27 namespace fawkes {
28 namespace gpp {
29 /** @class PrintActionExecutor
30  * A Golog++ action executor that just prints a message.
31  * @author Till Hofmann
32  */
33 
34 /** Constructor.
35  * Initialize the executor to print with the given logger.
36  * @param logger The logger to send messages to
37  */
39 {
40 }
41 
42 /** Destructor. */
44 {
45 }
46 
47 bool
48 PrintActionExecutor::can_execute_activity(std::shared_ptr<gologpp::Activity> activity) const
49 {
50  return activity->mapped_name() == "print";
51 }
52 
53 void
54 PrintActionExecutor::start(std::shared_ptr<gologpp::Activity> activity)
55 {
56  if (!can_execute_activity(activity)) {
57  throw Exception("Cannot execute activity '%s' with PrintActionExecutor",
58  activity->mapped_name().c_str());
59  }
60  activity->update(gologpp::Transition::Hook::START);
61  std::map<std::string, Logger::LogLevel> log_levels = {{"debug", Logger::LL_DEBUG},
62  {"info", Logger::LL_INFO},
63  {"warn", Logger::LL_WARN},
64  {"error", Logger::LL_ERROR},
65  {"none", Logger::LL_NONE}};
67  if (activity->target()->mapping().arg_mapping().count("level") > 0) {
68  std::string level_str = static_cast<std::string>(activity->mapped_arg_value("level"));
69  if (log_levels.count(level_str) > 0) {
70  log_level = log_levels[level_str];
71  }
72  }
73 
74  logger_->log(log_level,
75  "Golog++",
76  "%s",
77  static_cast<std::string>(activity->mapped_arg_value("message")).c_str());
78  activity->update(gologpp::Transition::Hook::FINISH);
79 }
80 
81 void
82 PrintActionExecutor::stop(std::shared_ptr<gologpp::Grounding<gologpp::Action>> activity)
83 {
84  logger_->log_error("PrintActionExecutor", "Cannot stop printing a message!");
85 }
86 } // namespace gpp
87 } // namespace fawkes
fawkes::Logger::log
virtual void log(LogLevel level, const char *component, const char *format,...)
Log message of given log level.
Definition: logger.cpp:326
fawkes::gpp::PrintActionExecutor::start
void start(std::shared_ptr< gologpp::Activity > activity) override
Start the given activity.
Definition: print_action_executor.cpp:54
fawkes::gpp::PrintActionExecutor::can_execute_activity
bool can_execute_activity(std::shared_ptr< gologpp::Activity > activity) const override
Determine if this executor can execute the given activity.
Definition: print_action_executor.cpp:48
fawkes::gpp::PrintActionExecutor::~PrintActionExecutor
virtual ~PrintActionExecutor()
Destructor.
Definition: print_action_executor.cpp:43
fawkes::Logger::LL_DEBUG
@ LL_DEBUG
debug output, relevant only when tracking down problems
Definition: logger.h:52
fawkes::Logger::log_error
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
fawkes::Logger::LL_INFO
@ LL_INFO
informational output about normal procedures
Definition: logger.h:53
fawkes::Logger
Interface for logging.
Definition: logger.h:42
fawkes
Fawkes library namespace.
fawkes::gpp::PrintActionExecutor::PrintActionExecutor
PrintActionExecutor(Logger *logger)
Constructor.
Definition: print_action_executor.cpp:38
fawkes::Logger::LL_ERROR
@ LL_ERROR
error, may be recoverable (software still running) or not (software has to terminate).
Definition: logger.h:57
fawkes::gpp::ActionExecutor::logger_
Logger * logger_
The logger to use for logging messages.
Definition: action_executor.h:44
fawkes::gpp::PrintActionExecutor::stop
void stop(std::shared_ptr< gologpp::Grounding< gologpp::Action >> activity) override
Stop the given activity.
Definition: print_action_executor.cpp:82
fawkes::Logger::LogLevel
LogLevel
Log level.
Definition: logger.h:51
fawkes::Logger::LL_NONE
@ LL_NONE
use this to disable log output
Definition: logger.h:60
fawkes::Logger::LL_WARN
@ LL_WARN
warning, should be investigated but software still functions, an example is that something was reques...
Definition: logger.h:54
fawkes::gpp::ActionExecutor
Abstract class to execute a Golog++ activity.
Definition: action_executor.h:35
fawkes::Exception
Base class for exceptions in Fawkes.
Definition: exception.h:36