2019-03-26 15:33:47 +01:00
|
|
|
/** Print hook.
|
|
|
|
*
|
2022-12-14 17:41:58 +01:00
|
|
|
* @author Steffen Vogel <post@steffenvogel.de>
|
2022-03-15 09:28:57 -04:00
|
|
|
* @copyright 2014-2022, Institute for Automation of Complex Power Systems, EONERC
|
2022-07-04 18:20:03 +02:00
|
|
|
* @license Apache 2.0
|
2019-03-26 15:33:47 +01:00
|
|
|
*********************************************************************************/
|
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <cstdio>
|
2019-06-23 16:57:00 +02:00
|
|
|
#include <cstring>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/node.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
#include <villas/hook.hpp>
|
2021-08-10 10:12:48 -04:00
|
|
|
#include <villas/path.hpp>
|
|
|
|
#include <villas/sample.hpp>
|
2021-05-10 00:12:30 +02:00
|
|
|
#include <villas/format.hpp>
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
namespace villas {
|
|
|
|
namespace node {
|
|
|
|
|
|
|
|
class PrintHook : public Hook {
|
|
|
|
|
|
|
|
protected:
|
2021-08-10 10:12:48 -04:00
|
|
|
Format::Ptr formatter;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
std::string prefix;
|
2021-08-10 10:12:48 -04:00
|
|
|
std::string output_path;
|
|
|
|
|
|
|
|
FILE *output;
|
2022-11-07 10:13:05 -05:00
|
|
|
std::vector<char> output_buffer;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
public:
|
2021-08-10 10:12:48 -04:00
|
|
|
PrintHook(Path *p, Node *n, int fl, int prio, bool en = true) :
|
2019-03-26 15:33:47 +01:00
|
|
|
Hook(p, n, fl, prio, en),
|
2021-08-10 10:12:48 -04:00
|
|
|
output(nullptr)
|
2021-05-10 00:12:30 +02:00
|
|
|
{ }
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
virtual
|
|
|
|
void start()
|
2019-03-26 15:33:47 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
assert(state == State::PREPARED ||
|
|
|
|
state == State::STOPPED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!output_path.empty()) {
|
|
|
|
output = fopen(output_path.c_str(), "w+");
|
|
|
|
if (!output)
|
|
|
|
throw SystemError("Failed to open file");
|
|
|
|
}
|
2022-11-07 10:13:05 -05:00
|
|
|
else
|
|
|
|
output_buffer = std::vector<char>(DEFAULT_FORMAT_BUFFER_LENGTH);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
formatter->start(signals);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::STARTED;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
virtual
|
|
|
|
void stop() {
|
|
|
|
if (output)
|
|
|
|
fclose(output);
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
virtual
|
|
|
|
void parse(json_t *json)
|
2019-03-26 15:33:47 +01:00
|
|
|
{
|
2021-08-10 10:12:48 -04:00
|
|
|
const char *p = nullptr;
|
|
|
|
const char *o = nullptr;
|
2019-03-26 15:33:47 +01:00
|
|
|
int ret;
|
|
|
|
json_error_t err;
|
2021-05-10 00:12:30 +02:00
|
|
|
json_t *json_format = nullptr;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
assert(state == State::INITIALIZED ||
|
|
|
|
state == State::CHECKED ||
|
|
|
|
state == State::PARSED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-02-16 14:15:14 +01:00
|
|
|
Hook::parse(json);
|
2020-08-28 19:40:23 +02:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
ret = json_unpack_ex(json, &err, 0, "{ s?: s, s?: o, s?: s }",
|
2019-03-26 15:33:47 +01:00
|
|
|
"prefix", &p,
|
2021-08-10 10:12:48 -04:00
|
|
|
"format", &json_format,
|
|
|
|
"output", &o
|
2019-03-26 15:33:47 +01:00
|
|
|
);
|
|
|
|
if (ret)
|
2021-02-16 14:15:14 +01:00
|
|
|
throw ConfigError(json, err, "node-config-hook-print");
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (p && o) {
|
|
|
|
throw ConfigError(json, "node-config-hook-print", "Prefix and output settings are exclusive.");
|
|
|
|
}
|
|
|
|
|
2019-03-26 15:33:47 +01:00
|
|
|
if (p)
|
2021-05-10 00:12:30 +02:00
|
|
|
prefix = p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (o)
|
|
|
|
output_path = o;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-05-10 00:12:30 +02:00
|
|
|
/* Format */
|
2021-08-10 10:12:48 -04:00
|
|
|
auto *fmt = json_format
|
2021-05-10 00:12:30 +02:00
|
|
|
? FormatFactory::make(json_format)
|
|
|
|
: FormatFactory::make("villas.human");
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
formatter = Format::Ptr(fmt);
|
2021-05-10 00:12:30 +02:00
|
|
|
if (!formatter)
|
|
|
|
throw ConfigError(json_format, "node-config-hook-print-format", "Invalid format configuration");
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
state = State::PARSED;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
virtual
|
|
|
|
Hook::Reason process(struct Sample *smp)
|
2019-03-26 15:33:47 +01:00
|
|
|
{
|
2019-06-23 16:13:23 +02:00
|
|
|
assert(state == State::STARTED);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2021-08-10 10:12:48 -04:00
|
|
|
if (!output) {
|
2022-11-07 10:13:05 -05:00
|
|
|
char *buf = output_buffer.data();
|
|
|
|
size_t buflen = output_buffer.size();
|
2021-08-10 10:12:48 -04:00
|
|
|
size_t wbytes;
|
|
|
|
|
2022-11-07 10:13:05 -05:00
|
|
|
formatter->sprint(buf, buflen, &wbytes, smp);
|
2021-08-10 10:12:48 -04:00
|
|
|
|
|
|
|
if (wbytes > 0 && buf[wbytes-1] == '\n')
|
|
|
|
buf[wbytes-1] = 0;
|
|
|
|
|
|
|
|
if (node)
|
2023-01-10 15:25:27 +01:00
|
|
|
logger->info("{}{} {}", prefix, node->getName(), buf);
|
2021-05-10 00:12:30 +02:00
|
|
|
else if (path)
|
2023-01-10 15:25:27 +01:00
|
|
|
logger->info("{}{} {}", prefix, path->toString(), buf);
|
2021-05-10 00:12:30 +02:00
|
|
|
}
|
2021-08-10 10:12:48 -04:00
|
|
|
else
|
|
|
|
formatter->print(output, smp);
|
2019-03-26 15:33:47 +01:00
|
|
|
|
2019-06-23 16:13:23 +02:00
|
|
|
return Reason::OK;
|
2019-03-26 15:33:47 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Register hook */
|
2020-06-14 15:00:02 +02:00
|
|
|
static char n[] = "print";
|
2021-08-10 10:12:48 -04:00
|
|
|
static char d[] = "Print the message to stdout or a file";
|
2020-06-14 15:00:02 +02:00
|
|
|
static HookPlugin<PrintHook, n, d, (int) Hook::Flags::NODE_READ | (int) Hook::Flags::NODE_WRITE | (int) Hook::Flags::PATH> p;
|
2019-03-26 15:33:47 +01:00
|
|
|
|
|
|
|
} /* namespace node */
|
|
|
|
} /* namespace villas */
|
|
|
|
|