LogDoctor/logdoctor/utilities/rtf.cpp

225 lines
12 KiB
C++
Raw Normal View History

2022-06-25 18:48:08 +02:00
2022-06-23 04:00:37 +02:00
#include "rtf.h"
2022-06-25 18:48:08 +02:00
2022-06-23 04:00:37 +02:00
RichText::RichText()
{
2022-06-23 04:00:37 +02:00
}
2022-08-03 21:30:17 +02:00
void RichText::enrichLogs( QString &rich_content, const std::string& content, const FormatOps::LogsFormat& logs_format, TextBrowser& TB )
2022-06-23 04:00:37 +02:00
{
2022-08-03 21:30:17 +02:00
const std::unordered_map<std::string, QString>& colors = TB.getColorScheme();
2022-06-27 21:55:14 +02:00
int color_scheme = TB.getColorSchemeID();
bool wide_lines = TB.getWideLinesUsage();
// enrich the text
2022-07-23 22:16:34 +02:00
rich_content.clear();
rich_content.reserve( content.size() );
rich_content += QString("<!DOCTYPE html><html><head></head><body");
2022-06-25 18:48:08 +02:00
if ( color_scheme > 0 ) {
2022-07-03 19:12:42 +02:00
rich_content += " style=\"background:" + colors.at("background") + "; color:" + colors.at("text") + "\"";
2022-06-25 18:48:08 +02:00
}
rich_content += ">";
if ( wide_lines == true ) {
rich_content += "<br/>";
2022-06-23 04:00:37 +02:00
}
QString rich_line="", class_name="";
2022-07-04 21:03:09 +02:00
std::string sep, fld, fld_str, aux_sep1, aux_sep2, aux_fld_str;
2022-06-28 20:48:42 +02:00
bool missing=false;
2022-08-19 22:39:46 +02:00
size_t start, stop, i, aux_start, aux_stop,
line_size, sep_size;
const size_t n_sep = logs_format.separators.size()-1;
2022-07-23 22:16:34 +02:00
std::vector<std::string> lines;
StringOps::splitrip( lines, content );
for ( const std::string& line : lines ) {
2022-08-19 22:39:46 +02:00
// check if the line is commented, usually from IIS logs
if ( StringOps::startsWith( line, "#" ) && !StringOps::startsWith( logs_format.initial, "#" ) ) {
rich_line = QString("<p>%1</p>").arg( QString::fromStdString( line ) );
if ( wide_lines == true ) {
rich_line += "<br/>";
}
rich_content.push_back( rich_line );
continue;
}
i = 0;
line_size = line.size()-1;
2022-06-25 18:48:08 +02:00
rich_line = "<p>";
// add the initial chars
2022-06-26 22:31:45 +02:00
stop = logs_format.initial.size();
rich_line += QString::fromStdString( logs_format.initial );
while (true) {
// color fields
2022-06-28 20:48:42 +02:00
start = stop; // stop updated at the end of this loop
if ( i <= n_sep ) {
2022-07-03 19:12:42 +02:00
sep = logs_format.separators.at( i );
2022-06-28 20:48:42 +02:00
stop = line.find( sep, start );
2022-06-25 18:48:08 +02:00
} else if ( i == n_sep+1 ) {
// final separator
2022-06-26 22:31:45 +02:00
sep = logs_format.final;
2022-08-19 22:39:46 +02:00
if ( sep.size() == 0 ) {
2022-07-04 21:03:09 +02:00
stop = line_size+1;
} else {
stop = line.find( sep, start );
2022-08-19 22:39:46 +02:00
if ( stop == std::string::npos ) {
2022-07-04 21:03:09 +02:00
stop = line_size +1;
}
}
2022-06-25 18:48:08 +02:00
} else {
// no more separators
break;
}
2022-08-19 22:39:46 +02:00
if ( stop == std::string::npos ) {
// separator not found, skip to the next one
i++;
2022-06-25 18:48:08 +02:00
stop = start;
continue;
}
2022-08-19 22:39:46 +02:00
// get fields
fld = logs_format.fields.at( i );
fld_str = line.substr(start, stop-start);
2022-06-28 20:48:42 +02:00
if ( i+1 <= n_sep ) {
2022-08-19 22:39:46 +02:00
// not the last separator, check for mistakes
bool ok = true;
aux_stop = stop;
2022-07-04 21:03:09 +02:00
2022-08-19 22:39:46 +02:00
if ( sep == " " ) {
// whitespace-separated-values fields
int c = StringOps::count( fld_str, sep ),
n = 0;
if ( fld == "request_full" ) {
n = 2;
} else if ( fld == "date_time_mcs" ) {
n = 4;
} else if ( fld == "date_time_ncsa" ) {
n = 1;
} else if ( fld == "date_time_gmt" ) {
n = 3;
}
if ( n > 0 && c < n ) {
// loop until the correct number of whitespaces is reached
aux_start = stop + 1;
while ( c < n ) {
aux_stop = line.find( sep, aux_start );
if ( aux_stop == std::string::npos ) {
// not found
ok = false;
2022-07-04 21:03:09 +02:00
break;
}
2022-08-19 22:39:46 +02:00
aux_start = aux_stop + 1;
c++;
2022-07-04 21:03:09 +02:00
}
2022-06-28 20:48:42 +02:00
}
2022-08-19 22:39:46 +02:00
} else if ( fld == "user_agent" && StringOps::startsWith( sep, "\"" ) ) {
// atm the only support is for escaped quotes
if ( fld_str.back() == '\\' ) {
aux_start = stop + sep.size();
while (true) {
aux_stop = line.find( sep, aux_start );
if ( aux_stop == std::string::npos ) {
// not found
break;
} else if ( line.at( aux_stop-1 ) != '\\' ) {
// non-backslashed quotes
2022-07-04 21:03:09 +02:00
break;
}
2022-08-19 22:39:46 +02:00
aux_start = aux_stop + sep.size();
2022-07-04 21:03:09 +02:00
}
}
2022-08-19 22:39:46 +02:00
}
2022-07-04 21:03:09 +02:00
2022-08-19 22:39:46 +02:00
// finally update if needed
if ( ok == true && aux_stop >= stop ) {
stop = aux_stop;
fld_str = StringOps::strip( line.substr(start, stop-start), " " );
2022-06-27 21:55:14 +02:00
}
}
2022-07-04 21:03:09 +02:00
2022-07-03 19:12:42 +02:00
sep_size = sep.size(); // do not remove, holdss the corretc size to increase stop
// color the fields
2022-06-29 21:46:35 +02:00
if ( StringOps::startsWith( fld, "date_time" ) ) {
if ( StringOps::startsWith( fld_str, "[" ) ) {
fld_str = fld_str.substr( 1, fld_str.size()-1 );
rich_line += "[";
if ( StringOps::endsWith( fld_str, "]" ) ) {
fld_str = fld_str.substr( 0, fld_str.size()-1 );
sep = "]" + sep;
}
}
}
2022-06-25 18:48:08 +02:00
rich_line += "<b>";
class_name = "";
if ( color_scheme > 0 ) {
2022-06-25 18:48:08 +02:00
class_name += "<span style=\"color:";
if ( fld == "client" ) {
2022-07-03 19:12:42 +02:00
class_name += colors.at("ip");
} else if ( fld == "port" ) {
2022-07-03 19:12:42 +02:00
class_name += colors.at("pt");
2022-06-29 21:46:35 +02:00
} else if ( StringOps::startsWith( fld, "date_time" ) ) {
2022-07-03 19:12:42 +02:00
class_name += colors.at("time");
2022-08-10 23:27:24 +02:00
} else if ( fld == "user_agent" ) {
class_name += colors.at("ua");
} else if ( StringOps::startsWith( fld, "request" ) ) {
class_name += colors.at("req");
} else if ( fld == "response_code" ) {
class_name += colors.at("res");
} else {
2022-07-03 19:12:42 +02:00
class_name += colors.at("x");
}
class_name += "\">";
}
2022-06-25 18:48:08 +02:00
// add the class name as span
rich_line += class_name;
2022-06-29 21:46:35 +02:00
rich_line += QString::fromStdString( fld_str );
if ( color_scheme > 0 ) {
rich_line += "</span>";
}
2022-06-25 18:48:08 +02:00
rich_line += "</b>";
// update the stop for the next start
2022-06-28 20:48:42 +02:00
if ( missing == true ) {
stop = aux_stop;
} else {
2022-07-03 19:12:42 +02:00
stop += sep_size;
2022-06-28 20:48:42 +02:00
i++;
}
2022-06-25 18:48:08 +02:00
if ( stop > line_size ) {
// this was the final separator
2022-06-26 22:31:45 +02:00
rich_line += QString::fromStdString( logs_format.final );
break;
}
2022-06-28 20:48:42 +02:00
if ( missing == true ) {
missing = false;
rich_line += QString::fromStdString( aux_sep2 );
} else {
rich_line += QString::fromStdString( sep );
}
2022-06-23 20:07:59 +02:00
}
rich_line += "</p>";
2022-06-25 18:48:08 +02:00
if ( wide_lines == true ) {
rich_line += "<br/>";
}
rich_content.push_back( rich_line );
2022-06-23 20:07:59 +02:00
}
2022-07-23 22:16:34 +02:00
lines.clear();
rich_content.push_back("</body></html>");
2022-06-23 04:00:37 +02:00
}
2022-07-23 22:16:34 +02:00
void RichText::richLogsDefault( QString& rich_str )
{
2022-07-23 22:16:34 +02:00
rich_str.clear();
rich_str += QString("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }hr { height: 1px; border-width: 0; }</style></head><body style=\" font-family:'Noto Sans'; font-size:16pt; font-weight:400; font-style:normal;\"><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; color:#6d6d6d;\">Select a file from the list</span></p><p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; color:#6d6d6d;\">to inspect its content</span></p></body></html>");
}
2022-07-23 22:16:34 +02:00
void RichText::richLogsFailure( QString &rich_str )
{
2022-07-23 22:16:34 +02:00
rich_str.clear();
rich_str += QString("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0//EN\" \"http://www.w3.org/TR/REC-html40/strict.dtd\"><html><head><meta name=\"qrichtext\" content=\"1\" /><meta charset=\"utf-8\" /><style type=\"text/css\">p, li { white-space: pre-wrap; }hr { height: 1px; border-width: 0; }</style></head><body style=\" font-family:'Noto Sans'; font-size:16pt; font-weight:400; font-style:normal;\"><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:14pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\"-qt-paragraph-type:empty; margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px; font-size:13pt;\"><br /></p><p align=\"center\" style=\" margin-top:0px; margin-bottom:0px; margin-left:0px; margin-right:0px; -qt-block-indent:0; text-indent:0px;\"><span style=\" font-size:14pt; color:#aa0000;\">Failed to read</span></p></body></html>");
}