LogDoctor/craplog/utilities/rtf.cpp

162 lines
6.1 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-27 21:55:14 +02:00
using std::string, std::unordered_map;
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-06-27 21:55:14 +02:00
QString RichText::enrichLogs( string content, FormatOps::LogsFormat logs_format, TextBrowser TB )
2022-06-23 04:00:37 +02:00
{
2022-06-27 21:55:14 +02:00
std::unordered_map<std::string, QString> colors = TB.getColorScheme();
int color_scheme = TB.getColorSchemeID();
bool wide_lines = TB.getWideLinesUsage();
// enrich the text
2022-06-25 18:48:08 +02:00
QString rich_content = QString("<!DOCTYPE html><html><head></head><body");
if ( color_scheme > 0 ) {
2022-06-27 21:55:14 +02:00
rich_content += " style=\"background:" + colors["background"] + "; color:" + colors["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-06-29 21:46:35 +02:00
string sep, fld, fld_str, aux_sep1, aux_sep2;
2022-06-28 20:48:42 +02:00
bool missing=false;
int start=0, stop=0, i=0, aux_start1=0, aux_start2=0, aux_stop=0;
int line_size;
2022-06-26 22:31:45 +02:00
int n_sep = logs_format.separators.size()-1;
2022-06-27 21:55:14 +02:00
for ( string& line : StringOps::splitrip( content ) ) {
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-06-26 22:31:45 +02:00
sep = logs_format.separators[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-06-28 20:48:42 +02:00
stop = line_size;
2022-06-25 18:48:08 +02:00
} else {
// no more separators
break;
}
2022-06-28 20:48:42 +02:00
if ( stop > line_size+1 || stop < 0 ) {
// separator not found, skip to the next one
i++;
2022-06-25 18:48:08 +02:00
stop = start;
continue;
}
2022-06-28 20:48:42 +02:00
if ( i+1 <= n_sep ) {
2022-06-27 21:55:14 +02:00
// not the last separator, check the possibility of missing
2022-06-28 20:48:42 +02:00
aux_sep1 = sep;
aux_start1 = aux_sep1.find(' ');
if ( aux_start1 >= 0 && aux_start1 < aux_sep1.size() ) {
aux_sep1 = StringOps::lstripUntil( aux_sep1, " " );
}
// iterate over following separators
for ( int j=i+1; j<n_sep; j++ ) {
aux_sep2 = logs_format.separators[j];
aux_start2 = aux_sep2.find(' ');
if ( aux_start2 > aux_sep2.size() || aux_start2 < 0 ) {
aux_start2 = stop;
} else {
aux_start2 = stop + aux_start2 + 1;
aux_sep2 = StringOps::lstripUntil( aux_sep2, " " );
}
// if the 2 seps are identical, skip (for uncertainty)
if ( aux_sep1 == aux_sep2 || aux_sep2 == "" ) {
continue;
}
// check if the next sep is found in the same position of the current one
if ( line.find( aux_sep2, aux_start2 ) == aux_start2 ) {
// probably the current field is missing, skip to this one
i = j;
aux_stop = aux_start2 + aux_sep2.size();
aux_sep2 = logs_format.separators[j];
missing = true;
}
break;
2022-06-27 21:55:14 +02:00
}
}
// color the fields
2022-06-29 21:46:35 +02:00
fld = logs_format.fields[i];
fld_str = line.substr(start, stop-start);
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-06-27 21:55:14 +02:00
class_name += colors["ip"];
} else if ( fld == "port" ) {
2022-06-27 21:55:14 +02:00
class_name += colors["pt"];
2022-06-29 21:46:35 +02:00
} else if ( StringOps::startsWith( fld, "date_time" ) ) {
2022-06-27 21:55:14 +02:00
class_name += colors["time"];
} else if ( fld == "user_agent" || fld == "source_file" ) {
2022-06-27 21:55:14 +02:00
class_name += colors["ua_src"];
} else if ( fld == "request" || fld == "error_message" ) {
2022-06-27 21:55:14 +02:00
class_name += colors["req_err"];
} else if ( fld == "response_code" || fld == "error_level" ) {
2022-06-27 21:55:14 +02:00
class_name += colors["res_lev"];
} else {
2022-06-27 21:55:14 +02:00
class_name += colors["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 {
stop = stop + sep.size();
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 );
rich_line.clear();
2022-06-23 20:07:59 +02:00
}
rich_content.push_back("</body></html>");
2022-06-23 04:00:37 +02:00
return rich_content;
}