2018-04-01 06:53:37 +00:00
|
|
|
/*
|
2019-02-24 13:08:51 +00:00
|
|
|
Copyright (C) 2017-2019 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-01-27 23:03:25 +00:00
|
|
|
Printer::Printer(bool json, bool fields, char separator, bool meterfiles, string &meterfiles_dir,
|
2018-11-02 12:50:12 +00:00
|
|
|
vector<string> shell_cmdlines)
|
2017-09-02 21:26:57 +00:00
|
|
|
{
|
2018-03-05 10:29:25 +00:00
|
|
|
json_ = json;
|
|
|
|
fields_ = fields;
|
|
|
|
separator_ = separator;
|
2017-09-02 21:26:57 +00:00
|
|
|
meterfiles_ = meterfiles;
|
2018-03-05 12:42:56 +00:00
|
|
|
meterfiles_dir_ = meterfiles_dir;
|
2018-11-02 12:50:12 +00:00
|
|
|
shell_cmdlines_ = shell_cmdlines;
|
2017-09-02 21:26:57 +00:00
|
|
|
}
|
|
|
|
|
2018-03-05 10:29:25 +00:00
|
|
|
void Printer::print(Meter *meter)
|
2018-11-02 12:50:12 +00:00
|
|
|
{
|
|
|
|
string human_readable, fields, json;
|
|
|
|
vector<string> envs;
|
|
|
|
|
|
|
|
meter->printMeter(&human_readable, &fields, separator_, &json, &envs);
|
|
|
|
|
|
|
|
if (shell_cmdlines_.size() > 0) {
|
|
|
|
printShells(meter, envs);
|
|
|
|
} else {
|
|
|
|
printFiles(meter, human_readable, fields, json);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::printShells(Meter *meter, vector<string> &envs)
|
|
|
|
{
|
|
|
|
for (auto &s : shell_cmdlines_) {
|
|
|
|
vector<string> args;
|
|
|
|
args.push_back("-c");
|
|
|
|
args.push_back(s);
|
|
|
|
invokeShell("/bin/sh", args, envs);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Printer::printFiles(Meter *meter, 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
|
|
|
|
2017-09-02 21:26:57 +00:00
|
|
|
if (meterfiles_) {
|
2018-11-02 12:50:12 +00:00
|
|
|
char filename[128];
|
|
|
|
memset(filename, 0, sizeof(filename));
|
2019-01-27 23:03:25 +00:00
|
|
|
snprintf(filename, 127, "%s/%s", meterfiles_dir_.c_str(), meter->name().c_str());
|
2018-11-02 12:50:12 +00:00
|
|
|
output = fopen(filename, "w");
|
2019-02-23 22:11:09 +00:00
|
|
|
if (!output) {
|
|
|
|
warning("Could not open file \"%s\" for writing!\n", filename);
|
|
|
|
}
|
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
|
|
|
}
|