Fawkes API Fawkes Development Version
log_adapter.cpp
1
2/***************************************************************************
3 * log_adapter.cpp - PLEXIL adapter for Fawkes' log
4 *
5 * Created: Mon Aug 13 15:49:25 2018
6 * Copyright 2006-2018 Tim Niemueller [www.niemueller.de]
7 *
8 ****************************************************************************/
9
10/* This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Library General Public License for more details.
19 *
20 * Read the full text in the LICENSE.GPL file in the doc directory.
21 */
22
23#include "log_adapter.h"
24
25#include <AdapterConfiguration.hh>
26#include <AdapterExecInterface.hh>
27#include <AdapterFactory.hh>
28#include <Command.hh>
29#include <functional>
30#include <plan-utils.hh>
31
32/** @class LoggingPlexilAdapter "log_adapter.h"
33 * Plexil adapter to provide logging facilities.
34 * @author Tim Niemueller
35 */
36
37/** Constructor.
38 * @param execInterface Reference to the parent AdapterExecInterface object.
39 */
40LoggingPlexilAdapter::LoggingPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
41: InterfaceAdapter(execInterface)
42{
43}
44
45/** Constructor from configuration XML.
46 * @param execInterface Reference to the parent AdapterExecInterface object.
47 * @param xml A const reference to the XML element describing this adapter
48 * @note The instance maintains a shared pointer to the XML.
49 */
50LoggingPlexilAdapter::LoggingPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface,
51 pugi::xml_node const xml)
52: InterfaceAdapter(execInterface, xml)
53{
54}
55
56/** Destructor. */
58{
59}
60
61/** Initialize adapter.
62 * @return true if initialization was successful, false otherwise.
63 */
64bool
66{
67 logger_ = reinterpret_cast<fawkes::Logger *>(m_execInterface.getProperty("::Fawkes::Logger"));
68
69 PLEXIL::g_configuration->registerCommandInterface("print", this);
70 PLEXIL::g_configuration->registerCommandInterface("pprint", this);
71 PLEXIL::g_configuration->registerCommandInterface("printToString", this);
72 PLEXIL::g_configuration->registerCommandInterface("pprintToString", this);
73 PLEXIL::g_configuration->registerCommandInterface("to_string", this);
74
75 PLEXIL::g_configuration->registerCommandInterface("log_debug", this);
76 PLEXIL::g_configuration->registerCommandInterface("log_info", this);
77 PLEXIL::g_configuration->registerCommandInterface("log_warn", this);
78 PLEXIL::g_configuration->registerCommandInterface("log_error", this);
79
80 return true;
81}
82
83/** Start adapter.
84 * @return true if starting was successful, false otherwise.
85 */
86bool
88{
89 return true;
90}
91
92/** Stop adapter.
93 * @return true if successful, false otherwise.
94 */
95bool
97{
98 return true;
99}
100
101/** Reset adapter.
102 * @return true if successful, false otherwise.
103 */
104bool
106{
107 return true;
108}
109
110/** Shut adapter down.
111 * @return true if successful, false otherwise.
112 */
113bool
115{
116 return true;
117}
118
119static std::string
120v_tostring(const std::vector<PLEXIL::Value> &values, bool pretty)
121{
122 std::string rv;
123 if (pretty) {
124 rv = pprintToString(values).valueToString();
125 } else {
126 rv = printToString(values).valueToString();
127 }
128 if (rv[rv.size() - 1] == '\n') {
129 rv.resize(rv.size() - 1);
130 }
131 return rv;
132}
133
134static PLEXIL::Value
135v_tovalue(const std::vector<PLEXIL::Value> &values, bool pretty)
136{
137 PLEXIL::Value rv;
138 if (pretty) {
139 rv = pprintToString(values).valueToString();
140 } else {
141 rv = printToString(values).valueToString();
142 }
143 return rv;
144}
145
146/** Perform given command.
147 * @param cmd command to execute
148 */
149void
151{
152 std::map<std::string, std::function<void(const std::vector<PLEXIL::Value> &)>> mapping_norv = {
153 {"print",
154 [this](const std::vector<PLEXIL::Value> &values) {
155 this->logger_->log_info("Plexil", "%s", v_tostring(values, false).c_str());
156 }},
157 {"pprint",
158 [this](const std::vector<PLEXIL::Value> &values) {
159 this->logger_->log_info("Plexil", "%s", v_tostring(values, true).c_str());
160 }},
161 {"log_debug",
162 [this](const std::vector<PLEXIL::Value> &values) {
163 this->logger_->log_debug("Plexil", "%s", v_tostring(values, true).c_str());
164 }},
165 {"log_info",
166 [this](const std::vector<PLEXIL::Value> &values) {
167 this->logger_->log_info("Plexil", "%s", v_tostring(values, true).c_str());
168 }},
169 {"log_warn",
170 [this](const std::vector<PLEXIL::Value> &values) {
171 this->logger_->log_warn("Plexil", "%s", v_tostring(values, true).c_str());
172 }},
173 {"log_error", [this](const std::vector<PLEXIL::Value> &values) {
174 this->logger_->log_error("Plexil", "%s", v_tostring(values, true).c_str());
175 }}};
176
177 std::map<std::string, std::function<PLEXIL::Value(const std::vector<PLEXIL::Value> &)>>
178 mapping_rv = {
179 {"printToString",
180 [](const std::vector<PLEXIL::Value> &values) { return v_tovalue(values, false); }},
181 {"pprintToString",
182 [](const std::vector<PLEXIL::Value> &values) { return v_tovalue(values, true); }},
183 {"to_string",
184 [](const std::vector<PLEXIL::Value> &values) { return v_tovalue(values, false); }},
185 };
186
187 const auto &entry_norv = mapping_norv.find(cmd->getName());
188 const auto &entry_rv = mapping_rv.find(cmd->getName());
189 if (entry_norv != mapping_norv.end()) {
190 entry_norv->second(cmd->getArgValues());
191 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
192 } else if (entry_rv != mapping_rv.end()) {
193 m_execInterface.handleCommandReturn(cmd, entry_rv->second(cmd->getArgValues()));
194 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_SUCCESS);
195 } else {
196 m_execInterface.handleCommandAck(cmd, PLEXIL::COMMAND_FAILED);
197 }
198
199 m_execInterface.notifyOfExternalEvent();
200}
201
202/** Abort currently running execution.
203 * @param cmd command to abort
204 */
205void
207{
208 m_execInterface.handleCommandAbortAck(cmd, false);
209 m_execInterface.notifyOfExternalEvent();
210}
211
212extern "C" {
213void
214initFawkesLoggingAdapter()
215{
216 REGISTER_ADAPTER(LoggingPlexilAdapter, "FawkesLoggingAdapter");
217}
218}
Interface adapter to provide logging facilities.
Definition: log_adapter.h:32
LoggingPlexilAdapter(PLEXIL::AdapterExecInterface &execInterface)
Constructor.
Definition: log_adapter.cpp:40
void executeCommand(PLEXIL::Command *cmd)
Perform given command.
virtual bool start()
Start adapter.
Definition: log_adapter.cpp:87
void invokeAbort(PLEXIL::Command *cmd)
Abort currently running execution.
virtual bool reset()
Reset adapter.
virtual bool shutdown()
Shut adapter down.
virtual ~LoggingPlexilAdapter()
Destructor.
Definition: log_adapter.cpp:57
virtual bool stop()
Stop adapter.
Definition: log_adapter.cpp:96
virtual bool initialize()
Initialize adapter.
Definition: log_adapter.cpp:65
Interface for logging.
Definition: logger.h:42
virtual void log_debug(const char *component, const char *format,...)=0
Log debug message.
virtual void log_warn(const char *component, const char *format,...)=0
Log warning message.
virtual void log_error(const char *component, const char *format,...)=0
Log error message.
virtual void log_info(const char *component, const char *format,...)=0
Log informational message.