Addes Strings and Logs utilities

This commit is contained in:
Valentino Orlandi 2022-06-24 01:26:00 +02:00
parent b8d7ea298f
commit fef96496cf
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
7 changed files with 142 additions and 26 deletions

View File

@ -6,12 +6,12 @@ MainWindow::MainWindow(QWidget *parent)
, ui(new Ui::MainWindow)
{
this->ui->setupUi(this);
// initialize the colors map
this->COLORS["black"] = QColor(0,0,0,255);
this->COLORS["grey"] = QColor(127,127,127,255);
this->COLORS["white"] = QColor(255,255,255,255);
// define text sizes
this->font_size = 13;
this->font_size_big = 16;
@ -26,10 +26,10 @@ MainWindow::MainWindow(QWidget *parent)
this->FONTS["main"] = QFont( main_font_family, this->font_size );
this->FONTS["main_italic"] = QFont( main_font_family, this->font_size, -1, true );
this->FONTS["script"] = QFont( script_font_family, this->font_size );
// initialize the TextBrowser's color scheme
this->TB_color_scheme = 1;
// get a fresh list of log files
this->ui->listLogFiles->header()->resizeSection(0,200);
this->ui->listLogFiles->header()->resizeSection(1,100);
@ -41,6 +41,7 @@ MainWindow::~MainWindow()
delete ui;
}
//////////////
//// LOGS ////
//////////////
@ -184,9 +185,9 @@ bool MainWindow::runCraplog()
if ( proceed == false ) {
return proceed;
}
return proceed;
}

View File

@ -57,19 +57,20 @@ int Craplog::setLogFileSelected( QString file_name )
return result;
}
// scan the logs path to update the log files list
void Craplog::scanLogsDir()
{
this->logs_list.clear();
// iterate over entries in the logs folder
for ( const std::filesystem::directory_entry& dir_entry : std::filesystem::directory_iterator{this->logs_path}) {
for ( const auto& dir_entry : std::filesystem::directory_iterator{this->logs_path}) {
// get the attributes
int size = dir_entry.file_size();
std::string path = dir_entry.path().string();
std::string name = dir_entry.path().filename().string();
// match only files having ".log." in their name
if ( ! dir_entry.is_regular_file()
|| name.find(".log.") == -1 ) {
|| LogOp::isNameValid( name ) ) {
continue;
}
// push in the list

View File

@ -15,7 +15,7 @@ class Craplog
{
public:
Craplog();
// logs formats
enum LogsFormat {
AccessCommon,
@ -24,9 +24,10 @@ public:
};
LogsFormat access_logs_format;
LogsFormat error_logs_format;
// log file infoes
enum LogType {
Failed,
Access,
Error
};
@ -48,11 +49,11 @@ private:
// logs related
std::string logs_path;
std::vector<LogFile> logs_list;
// logs list related
// logs list related
void scanLogsDir();
void readConfigs();
void loadFileContent();
};

View File

@ -1,6 +1,62 @@
#include "logs.h"
#include "utilities/strings.h"
LogOp::LogOp()
{
}
bool LogOp::isNameValid(std::string name)
{
bool valid = false;
if ( StringOp::startsWith( name, "access.log." )
|| StringOp::startsWith( name, "error.log." ) ) {
}
return valid;
}
Craplog::LogType LogOp::defineLogType( std::vector<std::string> lines )
{
int n_acc=0, n_err=0;
auto type = Craplog::LogType::Failed;
for ( const std::string& line : lines ) {
if ( line[0] == '[' ) {
n_err ++;
} else {
if ( line.find('"') < line.size() ) {
n_acc ++;
}
}
}
if ( n_acc > 0 && n_err == 0 ) {
// access logs
type = Craplog::LogType::Access;
} else if ( n_err > 0 && n_acc == 0 ) {
// error logs
type = Craplog::LogType::Error;
} else {
// something is wrong with these logs, put a warnin
if ( n_acc > 0 && n_err > 0 ) {
// both access and error types found
std::cout << "WRONG!" << std::endl; // !!! PUT A DIALOG ERROR MESSAGE HERE !!!
} else {
// every line was invalid
std::cout << "WRONG!" << std::endl; // !!! PUT A DIALOG ERROR MESSAGE HERE !!!
}
}
return type;
}
std::vector<std::string> LogOp::splitLine( std::string line, Craplog::LogType type )
{
}
std::vector<std::string> LogOp::splitLines( std::string line, Craplog::LogType type )
{
}

View File

@ -10,11 +10,13 @@ class LogOp
{
public:
LogOp();
Craplog::LogType defineLogType( std::string line );
std::vector<std::string> splitLine( std::string line );
std::vector<std::string> splitLines( std::string line );
static bool isNameValid( std::string name );
static Craplog::LogType defineLogType( std::vector<std::string> lines );
static std::vector<std::string> splitLine( std::string line, Craplog::LogType type );
static std::vector<std::string> splitLines( std::string line, Craplog::LogType type );
};
#endif // LOGOP_H

View File

@ -2,7 +2,56 @@
StringOp::StringOp()
{
}
bool StringOp::isNumeric( std::string str )
{
bool result = true;
for ( char& chr : str ) {
if ( StringOp::isNumeric( chr ) == false ) {
result = false;
}
}
}
bool StringOp::isNumeric( char chr )
{
if ( chr > 65 && chr < 99 ) {
return true;
} else {
return false;
}
}
bool StringOp::startsWith(std::string str, std::string flag)
{
bool result = true;
for ( int i=0; i<flag.size(); i++ ) {
if ( str[i] != flag[i] ) {
result = false;
break;
}
}
return result;
}
bool StringOp::endsWith(std::string str, std::string flag)
{
bool result = true;
int size = str.size()-1;
for ( int i=size; i>size-flag.size(); i-- ) {
if ( str[i] != flag[i] ) {
result = false;
break;
}
}
return result;
}

View File

@ -10,13 +10,19 @@ class StringOp
{
public:
StringOp();
static std::string strip( std::string str, std::string chars=" \n\t\b\r\v" );
static std::string lstrip( std::string str, std::string chars=" \n\t\b\r\v" );
static bool isNumeric( std::string str );
static bool isNumeric( char chr );
static bool startsWith( std::string str, std::string flag );
static bool endsWith( std::string str, std::string flag );
static std::string strip( std::string str, std::string chars=" \n\t\b\r\v" );
static std::string lstrip( std::string str, std::string chars=" \n\t\b\r\v" );
static std::string rstrip( std::string str, std::string chars=" \n\t\b\r\v" );
static std::vector<std::string> split( std::string str, std::string sep="\n" );
static std::vector<std::string> splitrip( std::string str, std::string sep="\n", std::string chars=" \n\t\b\r\v" );
};