From 224c224e29d383219caa008f5a42beb512abf3ac Mon Sep 17 00:00:00 2001
From: Valentino Orlandi
Date: Fri, 27 Jan 2023 02:00:56 +0100
Subject: [PATCH] Code improvements
---
.../modules/craplog/modules/datetime.cpp | 20 ++++++++++-------
logdoctor/modules/craplog/modules/formats.cpp | 6 ++---
logdoctor/modules/craplog/modules/formats.h | 8 +++----
logdoctor/modules/craplog/modules/hash.h | 3 ---
logdoctor/modules/tb.cpp | 6 ++---
logdoctor/utilities/rtf.cpp | 22 ++++++++++---------
logdoctor/utilities/strings.cpp | 14 +++++++-----
7 files changed, 42 insertions(+), 37 deletions(-)
diff --git a/logdoctor/modules/craplog/modules/datetime.cpp b/logdoctor/modules/craplog/modules/datetime.cpp
index 203d32d1..492e9dc3 100644
--- a/logdoctor/modules/craplog/modules/datetime.cpp
+++ b/logdoctor/modules/craplog/modules/datetime.cpp
@@ -113,10 +113,10 @@ const std::vector DateTimeOps::processDateTime( const std::string&
// convert to seconds
if ( aux == "us" ) {
// from microseconds
- datetime = datetime.substr( 0, datetime.size()-6 );
+ datetime.resize( datetime.size()-6 );
} else if ( aux == "ms" ) {
// from milliseconds
- datetime = datetime.substr( 0, datetime.size()-3 );
+ datetime.resize( datetime.size()-3 );
} else if ( aux == "s.ms" ) {
// from seconds.milliseconds
datetime = std::to_string( std::stoi( datetime ) );
@@ -140,11 +140,13 @@ const std::vector DateTimeOps::processDateTime( const std::string&
day = datetime.substr( 8, 2 );
} else if ( format == "MMDDYY" ) {
+ int y = std::stoi( datetime.substr( 6, 2 ) );
month = datetime.substr( 0, 2 );
day = datetime.substr( 3, 2 );
- year = "20" + datetime.substr( 6, 2 );
+ year = (y<70) ? "20" : "19";
+ year += std::to_string( y );
- } else if ( format == "MDYY" ) {
+ } else if ( format == "MDYYYY" ) {
int aux_;
if ( datetime.at(2) == '/' ) {
month = datetime.substr( 0, 2 );
@@ -158,21 +160,23 @@ const std::vector DateTimeOps::processDateTime( const std::string&
aux_ += 3;
} else {
day = "0" + datetime.substr( aux_, 1 );
- aux_ = +2;
+ aux_ += 2;
}
- year = "20" + datetime.substr( aux_ );
+ year = datetime.substr( aux_ );
} else if ( StringOps::startsWith( format, "year" ) ) {
year = datetime;
if ( format == "year_short" ) {
- year = "20" + year;
+ int y = std::stoi( year );
+ year = (y<70) ? "20" : "19";
+ year += year;
}
} else if ( StringOps::startsWith( format, "month" ) ) {
if ( format.size() <= 5 ) {
month = datetime;
} else {
- datetime = datetime.substr( 0, 3 ); // may be the full name
+ datetime.resize( 3 ); // may be the full name
month = DateTimeOps::convertMonth( datetime );
}
diff --git a/logdoctor/modules/craplog/modules/formats.cpp b/logdoctor/modules/craplog/modules/formats.cpp
index f1fb7cf4..4270db72 100644
--- a/logdoctor/modules/craplog/modules/formats.cpp
+++ b/logdoctor/modules/craplog/modules/formats.cpp
@@ -12,9 +12,9 @@ FormatOps::FormatOps()
// count the new lines
-const int FormatOps::countNewLines( const std::string& initial, const std::string& final, const std::vector& separators ) const
+const unsigned FormatOps::countNewLines( const std::string& initial, const std::string& final, const std::vector& separators ) const
{
- int nl = 0;
+ unsigned nl = 0;
nl += StringOps::count( initial, "\n" );
nl += StringOps::count( final, "\n" );
for ( const std::string& sep : separators ) {
@@ -616,7 +616,7 @@ const FormatOps::LogsFormat FormatOps::processIisFormatString( const std::string
// IIS logging module
final = ",";
separators = {", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", ",", "};
- fields = {"client","NONE","date_time_MDYY","date_time_utc_t","NONE","NONE","NONE","time_taken_ms","bytes_received","bytes_sent","response_code","NONE","request_method","request_uri","request_query"};
+ fields = {"client","NONE","date_time_MDYYYY","date_time_utc_t","NONE","NONE","NONE","time_taken_ms","bytes_received","bytes_sent","response_code","NONE","request_method","request_uri","request_query"};
break;
case 1:
// NCSA logging module
diff --git a/logdoctor/modules/craplog/modules/formats.h b/logdoctor/modules/craplog/modules/formats.h
index 516c6a74..a8f5093a 100644
--- a/logdoctor/modules/craplog/modules/formats.h
+++ b/logdoctor/modules/craplog/modules/formats.h
@@ -24,7 +24,7 @@ public:
std::string final; //!< The final separator
std::vector separators; //!< The separators in the middle
std::vector fields; //!< The fields
- int new_lines; //!< The number of new lines
+ unsigned new_lines; //!< The number of new lines
};
@@ -117,7 +117,7 @@ private:
\return The number of new lines in a single log line
\see LogsFormat, processApacheFormatString(), processNginxFormatString()
*/
- const int countNewLines( const std::string& initial, const std::string& final, const std::vector& separators ) const;
+ const unsigned countNewLines( const std::string& initial, const std::string& final, const std::vector& separators ) const;
//! Finds the end of a Nginx log field
/*!
@@ -437,7 +437,7 @@ private:
/////////////
//// IIS ////
- //!< Access logs fields formats
+ //!< Access logs fields formats (W3C)
const std::unordered_map IIS_ALF = {
{"date", "date_time_utc_d"},
{"time", "date_time_utc_t"},
@@ -468,7 +468,7 @@ private:
const std::unordered_map IIS_ALF_SAMPLES = {
{"NONE", "DISCARDED"},
{"date_time_ncsa", "01/Jan/2000:23:59:59 +0000"},
- {"date_time_MDYY", "1/1/00"},
+ {"date_time_MDYYYY", "1/1/2000"},
{"date_time_utc_d", "2000-01-01"},
{"date_time_utc_t", "23:59:59"},
{"request_full", "GET /index.php?query=x HTTP/1.1"},
diff --git a/logdoctor/modules/craplog/modules/hash.h b/logdoctor/modules/craplog/modules/hash.h
index 94b7b9c9..c1433b4a 100644
--- a/logdoctor/modules/craplog/modules/hash.h
+++ b/logdoctor/modules/craplog/modules/hash.h
@@ -28,7 +28,6 @@ public:
/*!
\param db_path The path of the log files' Hashes database
\return Whether the operation has been successful or not
- \see hashes
*/
const bool loadUsedHashesLists( const std::string& db_path );
@@ -46,7 +45,6 @@ public:
\param file_hash The sha256 hash to compare
\param web_server_id The ID of the Web Server which generated the file
\return Whether the hash is already in the list or not
- \see hashes
*/
const bool hasBeenUsed( const std::string& file_hash, const int& web_server_id ) const;
@@ -56,7 +54,6 @@ public:
\param hashes The list of hashes to insert
\param web_server_id The ID of the Web Server which generated the file
\return Whether the operation has been successful or not
- \see insertUsedHash()
*/
const bool insertUsedHashes( const std::string& db_path, const std::vector& hashes, const int& web_server_id );
diff --git a/logdoctor/modules/tb.cpp b/logdoctor/modules/tb.cpp
index da2f6517..9867443e 100644
--- a/logdoctor/modules/tb.cpp
+++ b/logdoctor/modules/tb.cpp
@@ -79,9 +79,9 @@ void TextBrowser::makePreview( QString& content ) const
this->color_scheme.at("text") );
}
content += ">";
- if ( this->wide_lines ) {
+ /*if ( this->wide_lines ) {
content += "
";
- }
+ }*/
for ( int i=0; i<32; i++ ) {
content += "";
@@ -166,7 +166,7 @@ void TextBrowser::makePreview( QString& content ) const
content += "";
content += "
";
- if ( this->wide_lines ) {
+ if ( this->wide_lines && i < 31 ) {
content += "
";
}
}
diff --git a/logdoctor/utilities/rtf.cpp b/logdoctor/utilities/rtf.cpp
index 02e7c4a4..b542e87e 100644
--- a/logdoctor/utilities/rtf.cpp
+++ b/logdoctor/utilities/rtf.cpp
@@ -24,22 +24,24 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
.arg( colors.at("background"), colors.at("text") );
}
rich_content += ">";
- if ( wide_lines == true ) {
+ /*if ( wide_lines == true ) {
rich_content += "
";
- }
+ }*/
QString rich_line="", class_name="";
std::string sep, fld, fld_str, aux_sep2;
- bool missing=false;
+ bool missing = false;
size_t start, stop, i, aux_start, aux_stop,
line_size, sep_size;
const size_t n_sep = logs_format.separators.size()-1;
std::vector lines;
StringOps::splitrip( lines, content );
+ size_t lines_left = lines.size();
for ( const std::string& line : lines ) {
+ lines_left --;
// check if the line is commented, usually from IIS logs
if ( StringOps::startsWith( line, "#" ) && !StringOps::startsWith( logs_format.initial, "#" ) ) {
rich_line = QString("%1
").arg( QString::fromStdString( line ) );
- if ( wide_lines == true ) {
+ if ( wide_lines ) {
rich_line += "
";
}
rich_content.append( rich_line );
@@ -135,7 +137,7 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
}
// finally update if needed
- if ( ok == true && aux_stop >= stop ) {
+ if ( ok && aux_stop >= stop ) {
stop = aux_stop;
fld_str = StringOps::strip( line.substr(start, stop-start), " " );
}
@@ -148,7 +150,7 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
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 );
+ fld_str.resize( fld_str.size()-1 );
sep = "]" + sep;
}
}
@@ -182,7 +184,7 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
}
rich_line += "";
// update the stop for the next start
- if ( missing == true ) {
+ if ( missing ) {
stop = aux_stop;
} else {
stop += sep_size;
@@ -193,7 +195,7 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
rich_line += QString::fromStdString( logs_format.final );
break;
}
- if ( missing == true ) {
+ if ( missing ) {
missing = false;
rich_line += QString::fromStdString( aux_sep2 );
} else {
@@ -201,10 +203,10 @@ void RichText::enrichLogs( QString &rich_content, const std::string& content, co
}
}
rich_line += "
";
- if ( wide_lines == true ) {
+ if ( wide_lines && lines_left > 0 ) {
rich_line += "
";
}
- rich_content+= rich_line;
+ rich_content += rich_line;
}
lines.clear();
rich_content += "