Add html option for analyze.

pull/505/head
Fredrik Öhrström 2022-03-06 22:42:44 +01:00
rodzic 69e4f4c00e
commit 97efb421d8
8 zmienionych plików z 45 dodań i 21 usunięć

Wyświetl plik

@ -1,5 +1,5 @@
/*
Copyright (C) 2017-2020 Fredrik Öhrström (gpl-3.0-or-later)
Copyright (C) 2017-2022 Fredrik Öhrström (gpl-3.0-or-later)
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
@ -139,6 +139,7 @@ static shared_ptr<Configuration> parseNormalCommandLine(Configuration *c, int ar
else if (s == "plain") c->analyze_format = OutputFormat::PLAIN;
else if (s == "terminal") c->analyze_format = OutputFormat::TERMINAL;
else if (s == "json") c->analyze_format = OutputFormat::JSON;
else if (s == "html") c->analyze_format = OutputFormat::HTML;
else if (s == "verbose") c->analyze_verbose = true;
else
{
@ -236,6 +237,11 @@ static shared_ptr<Configuration> parseNormalCommandLine(Configuration *c, int ar
i++;
continue;
}
if (!strcmp(argv[i], "--logsummary")) {
c->logsummary = true;
i++;
continue;
}
if (!strncmp(argv[i], "--format=", 9))
{
if (!strcmp(argv[i]+9, "json"))

Wyświetl plik

@ -419,6 +419,15 @@ void handleLogtelegrams(Configuration *c, string logtelegrams)
}
}
void handleLogsummary(Configuration *c, string logsummary)
{
if (logsummary == "true") { c->logsummary = true; }
else if (logsummary == "false") { c->logsummary = false;}
else {
warning("No such logsummary setting: \"%s\"\n", logsummary.c_str());
}
}
void handleMeterfiles(Configuration *c, string meterfiles)
{
if (meterfiles.length() > 0)
@ -680,6 +689,7 @@ shared_ptr<Configuration> loadConfiguration(string root, ConfigOverrides overrid
else if (p.first == "exitafter") handleExitAfter(c, p.second);
else if (p.first == "oneshot") handleOneshot(c, p.second);
else if (p.first == "logtelegrams") handleLogtelegrams(c, p.second);
else if (p.first == "logsummary") handleLogsummary(c, p.second);
else if (p.first == "meterfiles") handleMeterfiles(c, p.second);
else if (p.first == "meterfilesaction") handleMeterfilesAction(c, p.second);
else if (p.first == "meterfilesnaming") handleMeterfilesNaming(c, p.second);

Wyświetl plik

@ -42,6 +42,11 @@ enum class MeterFileTimestamp
Never, Day, Hour, Minute, Micros
};
enum class LogSummary
{
All, Unknown
};
// These values can be overridden from the command line.
struct ConfigOverrides
{
@ -78,6 +83,7 @@ struct Configuration
AddLogTimestamps addtimestamps {};
bool internaltesting {}; // Not currently used. Was used for speeding up testing. I.e. it shortened all timeouts.
// Might be needed in the future. Therefore it is still here.
bool logsummary {};
bool logtelegrams {};
bool meterfiles {};
std::string meterfiles_dir;

Wyświetl plik

@ -560,7 +560,7 @@ bool start(Configuration *config)
}
bool stop_after_send = false;
if (!meter_manager_->hasMeters() && serial_manager_->isRunning())
if ((config->logsummary || !meter_manager_->hasMeters()) && serial_manager_->isRunning())
{
if (config->send_bus_content.size() != 0)
{
@ -568,7 +568,7 @@ bool start(Configuration *config)
}
else if (!config->analyze)
{
notice("No meters configured. Printing id:s of all telegrams heard!\n");
if (!config->logsummary) notice("No meters configured. Printing id:s of all telegrams heard!\n");
meter_manager_->onTelegram([](AboutTelegram &about, vector<uchar> frame) {
Telegram t;

Wyświetl plik

@ -126,7 +126,7 @@ private:
bool analyze_verbose_;
vector<MeterInfo> meter_templates_;
vector<shared_ptr<Meter>> meters_;
function<void(AboutTelegram&,vector<uchar>)> on_telegram_;
vector<function<bool(AboutTelegram&,vector<uchar>)>> telegram_listeners_;
function<void(Telegram*t,Meter*)> on_meter_updated_;
public:
@ -215,15 +215,6 @@ public:
return true;
}
if (!hasMeters())
{
if (on_telegram_)
{
on_telegram_(about, input_frame);
}
return true;
}
bool handled = false;
bool exact_id_match = false;
@ -336,6 +327,10 @@ public:
}
}
}
for (auto f : telegram_listeners_)
{
f(about, input_frame);
}
if (isVerboseEnabled() && !handled)
{
verbose("(wmbus) telegram from %s ignored by all configured meters!\n", ids.c_str());
@ -343,9 +338,9 @@ public:
return handled;
}
void onTelegram(function<void(AboutTelegram &about, vector<uchar>)> cb)
void onTelegram(function<bool(AboutTelegram &about, vector<uchar>)> cb)
{
on_telegram_ = cb;
telegram_listeners_.push_back(cb);
}
void whenMeterUpdated(std::function<void(Telegram*t,Meter*)> cb)

Wyświetl plik

@ -455,7 +455,7 @@ struct MeterManager
virtual bool handleTelegram(AboutTelegram &about, vector<uchar> data, bool simulated) = 0;
virtual bool hasAllMetersReceivedATelegram() = 0;
virtual bool hasMeters() = 0;
virtual void onTelegram(function<void(AboutTelegram&,vector<uchar>)> cb) = 0;
virtual void onTelegram(function<bool(AboutTelegram&,vector<uchar>)> cb) = 0;
virtual void whenMeterUpdated(std::function<void(Telegram*t,Meter*)> cb) = 0;
virtual void pollMeters(shared_ptr<BusManager> bus) = 0;
virtual void analyzeEnabled(bool b, OutputFormat f, string force_driver, string key, bool verbose) = 0;

Wyświetl plik

@ -242,7 +242,7 @@ size_t findBytes(std::vector<uchar> &v, uchar a, uchar b, uchar c);
enum class OutputFormat
{
NONE, PLAIN, TERMINAL, JSON
NONE, PLAIN, TERMINAL, JSON, HTML
};
#ifndef FUZZING

Wyświetl plik

@ -2040,7 +2040,7 @@ void Telegram::explainParse(string intro, int from)
}
}
string renderAnalysisAsText(vector<Explanation> &explanations, bool use_ansi)
string renderAnalysisAsText(vector<Explanation> &explanations, OutputFormat of)
{
string s;
@ -2049,13 +2049,20 @@ string renderAnalysisAsText(vector<Explanation> &explanations, bool use_ansi)
const char *red;
const char *reset;
if (use_ansi)
if (of == OutputFormat::TERMINAL)
{
green = "\033[0;97m\033[1;42m";
yellow = "\033[0;97m\033[0;43m";
red = "\033[0;97m\033[0;41m\033[1;37m";
reset = "\033[0m";
}
else if (of == OutputFormat::HTML)
{
green = "<span style=\"color:white;background-color:#008450;\">";
yellow = "<span style=\"color:white;background-color:#efb700;\">";
red = "<span style=\"color:white;background-color:#b81d13;\">";
reset = "</span>";
}
else
{
green = "";
@ -2133,10 +2140,10 @@ string Telegram::analyzeParse(OutputFormat format, int *content_length, int *und
switch(format)
{
case OutputFormat::PLAIN :
case OutputFormat::HTML :
case OutputFormat::TERMINAL:
{
bool use_ansi = format == OutputFormat::TERMINAL;
return renderAnalysisAsText(explanations, use_ansi);
return renderAnalysisAsText(explanations, format);
break;
}
case OutputFormat::JSON: