Code improvements and fixes

This commit is contained in:
Valentino Orlandi 2022-08-19 22:39:46 +02:00
parent f8e4963583
commit d9c966704c
Signed by: elB4RTO
GPG key ID: 1719E976DB2D4E71

View file

@ -27,12 +27,21 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
QString rich_line="", class_name=""; QString rich_line="", class_name="";
std::string sep, fld, fld_str, aux_sep1, aux_sep2, aux_fld_str; std::string sep, fld, fld_str, aux_sep1, aux_sep2, aux_fld_str;
bool missing=false; bool missing=false;
int start, stop, i, aux_start1, aux_start2, aux_stop, size_t start, stop, i, aux_start, aux_stop,
line_size, sep_size, line_size, sep_size;
n_sep = logs_format.separators.size()-1; const size_t n_sep = logs_format.separators.size()-1;
std::vector<std::string> lines; std::vector<std::string> lines;
StringOps::splitrip( lines, content ); StringOps::splitrip( lines, content );
for ( const std::string& line : lines ) { for ( const std::string& line : lines ) {
// 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; i = 0;
line_size = line.size()-1; line_size = line.size()-1;
rich_line = "<p>"; rich_line = "<p>";
@ -48,11 +57,11 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
} else if ( i == n_sep+1 ) { } else if ( i == n_sep+1 ) {
// final separator // final separator
sep = logs_format.final; sep = logs_format.final;
if ( sep == "" ) { if ( sep.size() == 0 ) {
stop = line_size+1; stop = line_size+1;
} else { } else {
stop = line.find( sep, start ); stop = line.find( sep, start );
if ( stop > line_size+1 || stop < 0 ) { if ( stop == std::string::npos ) {
stop = line_size +1; stop = line_size +1;
} }
} }
@ -60,105 +69,77 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
// no more separators // no more separators
break; break;
} }
if ( stop > line_size+1 || stop < 0 ) { if ( stop == std::string::npos ) {
// separator not found, skip to the next one // separator not found, skip to the next one
i++; i++;
stop = start; stop = start;
continue; continue;
} }
// get fields
fld = logs_format.fields.at( i );
fld_str = line.substr(start, stop-start);
if ( i+1 <= n_sep ) { if ( i+1 <= n_sep ) {
// not the last separator, check for mistakes
bool ok = true;
aux_stop = stop;
// tricky error messages from apache if ( sep == " " ) {
aux_fld_str = StringOps::strip( line.substr(start, stop-start), " " ); // whitespace-separated-values fields
if ( StringOps::startsWith( aux_fld_str, "AH0" ) int c = StringOps::count( fld_str, sep ),
|| StringOps::startsWith( aux_fld_str, "PHP " ) ) { n = 0;
if ( logs_format.fields.at( i ) != "error_message" ) { if ( fld == "request_full" ) {
// this field is missing, step to the right field n = 2;
for ( int j=i+1; j<logs_format.fields.size(); j++ ) { } else if ( fld == "date_time_mcs" ) {
if ( logs_format.fields.at( j ) == "error_message" ) { n = 4;
i = j; } 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;
break; break;
} }
aux_start = aux_stop + 1;
c++;
} }
} }
// correct field, check if the next separators has columns. in case, update the stop position
int c_count = StringOps::count( aux_fld_str, ": " ), } else if ( fld == "user_agent" && StringOps::startsWith( sep, "\"" ) ) {
c_max = 2; // atm the only support is for escaped quotes
if ( StringOps::startsWith( aux_fld_str, "AH00171" ) if ( fld_str.back() == '\\' ) {
|| StringOps::startsWith( aux_fld_str, "AH00163" ) ) { aux_start = stop + sep.size();
c_max = 1; while (true) {
} aux_stop = line.find( sep, aux_start );
if ( c_count < c_max ) { if ( aux_stop == std::string::npos ) {
// supposed to contain at least 2 columns // not found
aux_stop = stop; break;
int j; } else if ( line.at( aux_stop-1 ) != '\\' ) {
for ( j=0; j<c_count; j++ ) { // non-backslashed quotes
aux_stop = line.find( ": ", aux_stop );
if ( aux_stop != 0 || aux_stop >= line_size ) {
// column not not found
break; break;
} else {
// one_setp_further
aux_stop ++;
}
}
// finally update the stop to the next sep
if ( i <= n_sep ) {
sep = logs_format.separators.at( i );
stop = line.find( sep, aux_stop );
} else if ( i == n_sep+1 ) {
// final separator
sep = logs_format.final;
if ( sep == "" ) {
stop = line_size+1;
} else {
stop = line.find( sep, aux_stop );
if ( stop > line_size+1 || stop < 0 ) {
stop = line_size +1;
}
} }
aux_start = aux_stop + sep.size();
} }
} }
}
} else { // finally update if needed
if ( ok == true && aux_stop >= stop ) {
// not the last separator, check the possibility of missing stop = aux_stop;
aux_sep1 = sep; fld_str = StringOps::strip( line.substr(start, stop-start), " " );
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.at( 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.at( j );
missing = true;
}
break;
}
} }
} }
sep_size = sep.size(); // do not remove, holdss the corretc size to increase stop sep_size = sep.size(); // do not remove, holdss the corretc size to increase stop
// color the fields // color the fields
fld = logs_format.fields.at( i );
fld_str = line.substr(start, stop-start);
if ( StringOps::startsWith( fld, "date_time" ) ) { if ( StringOps::startsWith( fld, "date_time" ) ) {
if ( StringOps::startsWith( fld_str, "[" ) ) { if ( StringOps::startsWith( fld_str, "[" ) ) {
fld_str = fld_str.substr( 1, fld_str.size()-1 ); fld_str = fld_str.substr( 1, fld_str.size()-1 );
@ -221,7 +202,6 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
rich_line += "<br/>"; rich_line += "<br/>";
} }
rich_content.push_back( rich_line ); rich_content.push_back( rich_line );
rich_line.clear();
} }
lines.clear(); lines.clear();
rich_content.push_back("</body></html>"); rich_content.push_back("</body></html>");