LogDoctor/logdoctor/utilities/rtf.cpp

231 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-10-24 23:12:23 +02:00
#include "utilities/strings.h"
2023-04-11 23:14:24 +02:00
#include "modules/craplog/modules/lib.h"
#include "modules/tb.h"
2022-10-24 23:12:23 +02:00
2023-04-11 23:14:24 +02:00
#include <QString>
2022-06-23 04:00:37 +02:00
2023-04-11 23:14:24 +02:00
void RichText::enrichLogs( QString& rich_content, const std::string& content, const LogsFormat& logs_format, TextBrowser& TB )
2022-06-23 04:00:37 +02:00
{
2023-04-11 23:14:24 +02:00
const std::unordered_map<std::string, QString>& colors{ TB.getColorScheme() };
const int color_scheme{ TB.getColorSchemeID() };
const bool wide_lines{ TB.getWideLinesUsage() };
2022-06-27 21:55:14 +02:00
// enrich the text
2022-07-23 22:16:34 +02:00
rich_content.clear();
2023-09-16 20:22:12 +02:00
rich_content.reserve( static_cast<int>(content.size()*2ul) );
2023-01-23 19:07:24 +01:00
rich_content += "<!DOCTYPE html><html><head></head><body";
2022-06-25 18:48:08 +02:00
if ( color_scheme > 0 ) {
2023-01-23 19:07:24 +01:00
rich_content += QString(" style=\"background:%1; color:%2\"")
.arg( colors.at("background"), colors.at("text") );
2022-06-25 18:48:08 +02:00
}
rich_content += ">";
2023-01-27 02:00:56 +01:00
/*if ( wide_lines == true ) {
2022-06-25 18:48:08 +02:00
rich_content += "<br/>";
2023-01-27 02:00:56 +01:00
}*/
2023-04-11 23:14:24 +02:00
QString rich_line{}, class_name{};
2022-10-23 16:41:36 +02:00
std::string sep, fld, fld_str, aux_sep2;
2023-04-11 23:14:24 +02:00
bool missing{ false };
size_t i, start, stop, aux_start, aux_stop,
2022-08-19 22:39:46 +02:00
line_size, sep_size;
2023-04-11 23:14:24 +02:00
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 );
2023-04-11 23:14:24 +02:00
size_t lines_left{ lines.size() };
2022-07-23 22:16:34 +02:00
for ( const std::string& line : lines ) {
-- lines_left;
2022-08-19 22:39:46 +02:00
// check if the line is commented, usually from IIS logs
2023-04-11 23:14:24 +02:00
if ( StringOps::startsWith( line, '#' ) && !StringOps::startsWith( logs_format.initial, '#' ) ) {
2022-08-19 22:39:46 +02:00
rich_line = QString("<p>%1</p>").arg( QString::fromStdString( line ) );
2023-01-27 02:00:56 +01:00
if ( wide_lines ) {
2022-08-19 22:39:46 +02:00
rich_line += "<br/>";
}
2023-01-23 19:07:24 +01:00
rich_content.append( rich_line );
2022-08-19 22:39:46 +02:00
continue;
}
i = 0;
line_size = line.size();
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;
2023-04-11 23:14:24 +02:00
if ( sep.empty() ) {
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
2023-04-11 23:14:24 +02:00
bool ok{ true };
2022-08-19 22:39:46 +02:00
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
2023-04-11 23:14:24 +02:00
size_t c{ StringOps::count( fld_str, ' ' ) },
n{ 0 };
2022-08-19 22:39:46 +02:00
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
2023-04-11 23:14:24 +02:00
} else if ( StringOps::startsWith( sep, '"' ) && fld == "user_agent" ) {
2022-08-19 22:39:46 +02:00
// 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
2023-01-27 02:00:56 +01:00
if ( ok && aux_stop >= stop ) {
2022-08-19 22:39:46 +02:00
stop = aux_stop;
2023-04-11 23:14:24 +02:00
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
sep_size = sep.size(); // do not remove, holds the corretc size to increase stop
// color the fields
2022-06-29 21:46:35 +02:00
if ( StringOps::startsWith( fld, "date_time" ) ) {
2023-04-11 23:14:24 +02:00
if ( StringOps::startsWith( fld_str, '[' ) ) {
2022-06-29 21:46:35 +02:00
fld_str = fld_str.substr( 1, fld_str.size()-1 );
rich_line += "[";
2023-04-11 23:14:24 +02:00
if ( StringOps::endsWith( fld_str, ']' ) ) {
2023-01-27 02:00:56 +01:00
fld_str.resize( fld_str.size()-1 );
2022-06-29 21:46:35 +02:00
sep = "]" + sep;
}
}
}
2022-06-25 18:48:08 +02:00
rich_line += "<b>";
2023-04-11 23:14:24 +02:00
class_name.clear();
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");
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 if ( StringOps::startsWith( fld, "bytes" ) ) {
class_name += colors.at("byte");
} 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
2023-01-27 02:00:56 +01:00
if ( missing ) {
2022-06-28 20:48:42 +02:00
stop = aux_stop;
} else {
2022-07-03 19:12:42 +02:00
stop += sep_size;
++i;
2022-06-28 20:48:42 +02:00
}
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;
}
2023-01-27 02:00:56 +01:00
if ( missing ) {
2022-06-28 20:48:42 +02:00
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>";
2023-01-27 02:00:56 +01:00
if ( wide_lines && lines_left > 0 ) {
2022-06-25 18:48:08 +02:00
rich_line += "<br/>";
}
2023-01-27 02:00:56 +01:00
rich_content += rich_line;
2022-06-23 20:07:59 +02:00
}
2022-07-23 22:16:34 +02:00
lines.clear();
2023-01-23 19:07:24 +01:00
rich_content += "</body></html>";
2023-04-11 23:14:24 +02:00
rich_content.shrink_to_fit();
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();
2022-08-22 21:16:53 +02:00
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;\">%1</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;\">%2</span></p></body></html>")
.arg( RichText::tr("Select a file from the list"),
RichText::tr("to inspect its content") );
}
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();
2022-08-22 21:16:53 +02:00
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;\">%1</span></p></body></html>")
.arg( RichText::tr("Failed to read") );
}