LogDoctor/craplog/utilities/rtf.cpp

145 lines
4.9 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-06-26 17:40:06 +02:00
QString RichText::enrichLogs( std::string content, int logs_format, int color_scheme, bool wide_lines )
2022-06-23 04:00:37 +02:00
{
2022-06-25 18:48:08 +02:00
std::unordered_map<std::string, QString> style;
2022-06-23 04:00:37 +02:00
switch ( color_scheme ) {
case 1:
// breeze
2022-06-25 18:48:08 +02:00
style.emplace("background","#ffffff");
style.emplace("text","#9c9c9b");
style.emplace("x","#1f1c1b");
style.emplace("ip","#644a9b");
style.emplace("pt","#d5bc79");
style.emplace("time","#d685d6");
style.emplace("ua_src","#006e28");
style.emplace("req_err","#54b8ff");
style.emplace("res_lev","#d24f4f");
break;
2022-06-23 04:00:37 +02:00
case 2:
// monokai
2022-06-25 18:48:08 +02:00
style.emplace("background","#272822");
style.emplace("text","#706c5a");
style.emplace("x","#d1d1cb");
style.emplace("ip","#57adbc");
style.emplace("pt","#c1b864");
style.emplace("time","#9773db");
style.emplace("ua_src","#a6e22e");
style.emplace("req_err","#bebeb8");
style.emplace("res_lev","#f92672");
break;
2022-06-23 04:00:37 +02:00
case 3:
// radical
2022-06-25 18:48:08 +02:00
style.emplace("background","#141322");
style.emplace("text","#749295");
style.emplace("x","#7c9c9e");
style.emplace("ip","#fda8bc");
style.emplace("pt","#ff85a1");
style.emplace("time","#a8c0c2");
style.emplace("ua_src","#42a784");
style.emplace("req_err","#d5358f");
style.emplace("res_lev","#56e8e4");
break;
2022-06-23 04:00:37 +02:00
default:
2022-06-25 18:48:08 +02:00
// no style
break;
}
QString rich_content = QString("<!DOCTYPE html><html><head></head><body");
if ( color_scheme > 0 ) {
rich_content += " style=\"background:" + style["background"] + "; color:" + style["text"] + "\"";
}
rich_content += ">";
if ( wide_lines == true ) {
rich_content += "<br/>";
2022-06-23 04:00:37 +02:00
}
QString rich_line="", class_name="";
std::string sep, fld;
int start=0, stop=0, i=0;
int line_size;
int n_sep = format.separators.size()-1;
for ( std::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
stop = format.initial.size();
rich_line += QString::fromStdString( format.initial );
while (true) {
// color fields
if ( i <= n_sep ) {
sep = format.separators[i];
2022-06-25 18:48:08 +02:00
} else if ( i == n_sep+1 ) {
// final separator
sep = format.final;
2022-06-25 18:48:08 +02:00
} else {
// no more separators
break;
}
2022-06-25 18:48:08 +02:00
start = stop; // stop updated at the end of this loop
stop = line.find( sep, start );
2022-06-25 18:48:08 +02:00
if ( stop > line_size || stop < 0 ) {
// separator not found, skip to the next one
i++;
2022-06-25 18:48:08 +02:00
stop = start;
continue;
}
// color the fields
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
fld = format.fields[i];
class_name += "<span style=\"color:";
if ( fld == "client" ) {
2022-06-25 18:48:08 +02:00
class_name += style["ip"];
} else if ( fld == "port" ) {
2022-06-25 18:48:08 +02:00
class_name += style["pt"];
} else if ( fld == "date_time" ) {
2022-06-25 18:48:08 +02:00
class_name += style["time"];
} else if ( fld == "user_agent" || fld == "source_file" ) {
2022-06-25 18:48:08 +02:00
class_name += style["ua_src"];
} else if ( fld == "request" || fld == "error_message" ) {
2022-06-25 18:48:08 +02:00
class_name += style["req_err"];
} else if ( fld == "response_code" || fld == "error_level" ) {
2022-06-25 18:48:08 +02:00
class_name += style["res_lev"];
} else {
2022-06-25 18:48:08 +02:00
class_name += style["x"];
}
class_name += "\">";
}
2022-06-25 18:48:08 +02:00
// add the class name as span
rich_line += class_name;
rich_line += QString::fromStdString( line.substr(start, stop-start) );
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
stop = stop + sep.size();
2022-06-25 18:48:08 +02:00
if ( stop > line_size ) {
// this was the final separator
rich_line += QString::fromStdString( format.final );
break;
}
2022-06-25 18:48:08 +02:00
rich_line += QString::fromStdString( sep );
i++;
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;
}