2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2020-09-08 12:55:01 +00:00
|
|
|
Copyright (C) 2017-2020 Fredrik Öhrström
|
2018-04-01 06:53:37 +00:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2017-09-02 21:26:57 +00:00
|
|
|
|
|
|
|
#include"printer.h"
|
2018-11-02 12:50:12 +00:00
|
|
|
#include"shell.h"
|
2017-09-02 21:26:57 +00:00
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
|
2019-02-26 22:40:44 +00:00
|
|
|
Printer::Printer(bool json, bool fields, char separator,
|
|
|
|
bool use_meterfiles, string &meterfiles_dir,
|
|
|
|
bool use_logfile, string &logfile,
|
2019-06-20 12:28:52 +00:00
|
|
|
vector<string> shell_cmdlines, bool overwrite,
|
2019-12-11 17:56:34 +00:00
|
|
|
MeterFileNaming naming,
|
|
|
|
MeterFileTimestamp timestamp)
|
2017-09-02 21:26:57 +00:00
|
|
|
{
|
2018-03-05 10:29:25 +00:00
|
|
|
json_ = json;
|
|
|
|
fields_ = fields;
|
|
|
|
separator_ = separator;
|
2019-02-26 22:40:44 +00:00
|
|
|
use_meterfiles_ = use_meterfiles;
|
2018-03-05 12:42:56 +00:00
|
|
|
meterfiles_dir_ = meterfiles_dir;
|
2019-02-26 22:40:44 +00:00
|
|
|
use_logfile_ = use_logfile;
|
|
|
|
logfile_ = logfile;
|
2018-11-02 12:50:12 +00:00
|
|
|
shell_cmdlines_ = shell_cmdlines;
|
2019-02-24 16:31:32 +00:00
|
|
|
overwrite_ = overwrite;
|
2019-06-20 12:28:52 +00:00
|
|
|
naming_ = naming;
|
2019-12-11 17:56:34 +00:00
|
|
|
timestamp_ = timestamp;
|
2017-09-02 21:26:57 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:43:30 +00:00
|
|
|
void Printer::print(Telegram *t, Meter *meter,
|
|
|
|
vector<string> *more_json,
|
2021-08-08 15:16:52 +00:00
|
|
|
vector<string> *selected_fields)
|
2018-11-02 12:50:12 +00:00
|
|
|
{
|
|
|
|
string human_readable, fields, json;
|
|
|
|
vector<string> envs;
|
2019-02-26 22:40:44 +00:00
|
|
|
bool printed = false;
|
2018-11-02 12:50:12 +00:00
|
|
|
|
2022-01-25 19:10:38 +00:00
|
|
|
meter->printMeter(t, &human_readable, &fields, separator_, &json, &envs, more_json, selected_fields, false);
|
2018-11-02 12:50:12 +00:00
|
|
|
|
2019-05-21 12:19:54 +00:00
|
|
|
if (shell_cmdlines_.size() > 0 || meter->shellCmdlines().size() > 0) {
|
2018-11-02 12:50:12 +00:00
|
|
|
printShells(meter, envs);
|
2019-02-26 22:40:44 +00:00
|
|
|
printed = true;
|
|
|
|
}
|
|
|
|
if (use_meterfiles_) {
|
2019-06-20 12:28:52 +00:00
|
|
|
printFiles(meter, t, human_readable, fields, json);
|
2019-02-26 22:40:44 +00:00
|
|
|
printed = true;
|
|
|
|
}
|
|
|
|
if (!printed) {
|
|
|
|
// This will print on stdout or in the logfile.
|
2019-06-20 12:28:52 +00:00
|
|
|
printFiles(meter, t, human_readable, fields, json);
|
2019-11-03 22:43:29 +00:00
|
|
|
fflush(stdout);
|
2018-11-02 12:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::printShells(Meter *meter, vector<string> &envs)
|
|
|
|
{
|
2019-05-21 12:19:54 +00:00
|
|
|
vector<string> *shells = &shell_cmdlines_;
|
|
|
|
if (meter->shellCmdlines().size() > 0) {
|
|
|
|
shells = &meter->shellCmdlines();
|
|
|
|
}
|
|
|
|
for (auto &s : *shells) {
|
2018-11-02 12:50:12 +00:00
|
|
|
vector<string> args;
|
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back(s);
|
|
|
|
invokeShell("/bin/sh", args, envs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-20 12:28:52 +00:00
|
|
|
void Printer::printFiles(Meter *meter, Telegram *t, string &human_readable, string &fields, string &json)
|
2017-09-02 21:26:57 +00:00
|
|
|
{
|
|
|
|
FILE *output = stdout;
|
2018-03-05 10:29:25 +00:00
|
|
|
|
2019-02-26 22:40:44 +00:00
|
|
|
if (use_meterfiles_) {
|
2019-12-11 17:56:34 +00:00
|
|
|
char filename[256];
|
2018-11-02 12:50:12 +00:00
|
|
|
memset(filename, 0, sizeof(filename));
|
2019-06-20 12:28:52 +00:00
|
|
|
switch (naming_) {
|
|
|
|
case MeterFileNaming::Name:
|
|
|
|
snprintf(filename, 127, "%s/%s", meterfiles_dir_.c_str(), meter->name().c_str());
|
|
|
|
break;
|
|
|
|
case MeterFileNaming::Id:
|
2021-01-30 16:58:00 +00:00
|
|
|
snprintf(filename, 127, "%s/%s", meterfiles_dir_.c_str(), t->ids.back().c_str());
|
2019-06-20 12:28:52 +00:00
|
|
|
break;
|
|
|
|
case MeterFileNaming::NameId:
|
2021-01-30 16:58:00 +00:00
|
|
|
snprintf(filename, 127, "%s/%s-%s", meterfiles_dir_.c_str(), meter->name().c_str(), t->ids.back().c_str());
|
2019-06-20 12:28:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2019-12-11 17:56:34 +00:00
|
|
|
string stamp;
|
|
|
|
|
|
|
|
switch (timestamp_) {
|
|
|
|
case MeterFileTimestamp::Never:
|
|
|
|
// Append nothing.
|
|
|
|
break;
|
|
|
|
case MeterFileTimestamp::Day:
|
|
|
|
stamp = currentDay();
|
|
|
|
break;
|
|
|
|
case MeterFileTimestamp::Hour:
|
|
|
|
stamp = currentHour();
|
|
|
|
break;
|
|
|
|
case MeterFileTimestamp::Minute:
|
|
|
|
stamp = currentMinute();
|
|
|
|
break;
|
|
|
|
case MeterFileTimestamp::Micros:
|
|
|
|
stamp = currentMicros();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stamp.length() > 0)
|
|
|
|
{
|
|
|
|
// There is a timestamp, lets append it.
|
|
|
|
strcat(filename, "_");
|
|
|
|
strcat(filename, stamp.c_str());
|
|
|
|
}
|
|
|
|
|
2019-02-24 16:31:32 +00:00
|
|
|
const char *mode = overwrite_ ? "w" : "a";
|
|
|
|
output = fopen(filename, mode);
|
2019-02-23 22:11:09 +00:00
|
|
|
if (!output) {
|
|
|
|
warning("Could not open file \"%s\" for writing!\n", filename);
|
2019-02-26 22:40:44 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (use_logfile_) {
|
|
|
|
output = fopen(logfile_.c_str(), "a");
|
|
|
|
if (!output) {
|
|
|
|
warning("Could not open file \"%s\" for writing!\n", logfile_.c_str());
|
|
|
|
return;
|
2019-02-23 22:11:09 +00:00
|
|
|
}
|
2017-09-02 21:26:57 +00:00
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
if (json_) {
|
2019-02-24 14:20:55 +00:00
|
|
|
if (output) {
|
|
|
|
fprintf(output, "%s\n", json.c_str());
|
|
|
|
} else {
|
|
|
|
notice("%s\n", json.c_str());
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
else if (fields_) {
|
2019-02-24 14:20:55 +00:00
|
|
|
if (output) {
|
|
|
|
fprintf(output, "%s\n", fields.c_str());
|
|
|
|
} else {
|
|
|
|
notice("%s\n", fields.c_str());
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
else {
|
2019-02-24 14:20:55 +00:00
|
|
|
if (output) {
|
|
|
|
fprintf(output, "%s\n", human_readable.c_str());
|
|
|
|
} else {
|
|
|
|
notice("%s\n", human_readable.c_str());
|
|
|
|
}
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-24 14:20:55 +00:00
|
|
|
if (output != stdout && output) {
|
2018-11-02 12:50:12 +00:00
|
|
|
fclose(output);
|
2018-03-05 10:29:25 +00:00
|
|
|
}
|
2017-09-02 21:26:57 +00:00
|
|
|
}
|