Fixed LogFile's TextBrowser

This commit is contained in:
Valentino Orlandi 2022-06-25 18:48:08 +02:00
parent 5809f00b01
commit 67732665e9
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
8 changed files with 161 additions and 108 deletions

View File

@ -14,23 +14,47 @@ MainWindow::MainWindow(QWidget *parent)
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;
this->font_size_small = 10;
// load the main font
QString main_font_family = QFontDatabase::applicationFontFamilies(
this->main_font_family = QFontDatabase::applicationFontFamilies(
QFontDatabase::addApplicationFont(":/fonts/Metropolis")).at(0);
// load the script font
QString script_font_family = QFontDatabase::applicationFontFamilies(
this->script_font_family = QFontDatabase::applicationFontFamilies(
QFontDatabase::addApplicationFont(":/fonts/3270")).at(0);
// initialize the fonts map
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 );
this->FONTS["main"] = QFont(
this->main_font_family,
this->font_size );
this->FONTS["main_italic"] = QFont(
this->main_font_family,
this->font_size,
-1, true );
this->FONTS["main_bold"] = QFont(
this->main_font_family,
this->font_size,
1 );
this->FONTS["main_big"] = QFont(
this->main_font_family,
this->font_size_big );
this->FONTS["main_small"] = QFont(
this->main_font_family,
this->font_size_small );
this->FONTS["script"] = QFont(
this->script_font_family,
this->font_size );
// initialize the TextBrowser's color scheme
this->TB_color_scheme = 1;
// TreeView for the LogFiles
this->ui->checkAllLogFiles->setFont( this->FONTS["main_small"] );
this->ui->listLogFiles->setFont( this->FONTS["main"] );
// initialize the TextBrowser for the LogFiles
this->TB.wide_lines = false;
this->TB.color_scheme = 1;
this->TB.font_size = this->font_size;
this->TB.font_family = this->main_font_family;
this->TB.font = QFont(
this->TB.font_family,
this->TB.font_size );
this->ui->textLogFiles->setFont( this->TB.font );
// get a fresh list of log files
this->ui->listLogFiles->header()->resizeSection(0,200);
@ -53,16 +77,14 @@ void MainWindow::on_buttonRefreshList_clicked()
// clear the current tree
this->ui->listLogFiles->clear();
// iterate over elements of list
std::cout << "OOOOOOOOOOOOOOOOOK" << std::endl;
for ( const Craplog::LogFile& log_file : this->craplog.getLogsList(true) ) {
std::cout << "XXXXXXXXXXXXXXXXXXXXXXXK" << std::endl;
// new entry for the tree widget
QTreeWidgetItem * item = new QTreeWidgetItem();
// set unchecked
item->setCheckState(0, Qt::CheckState::Unchecked );
// set the name of the file
item->setText( 0, log_file.name );
item->setFont( 0, this->FONTS["main"] );
//item->setFont( 0, this->FONTS["main"] );
// prepare the size of the file
float size = (float)log_file.size / 1024;
std::string sfx = " KiB";
@ -128,27 +150,26 @@ void MainWindow::on_checkAllLogFiles_stateChanged(int arg1)
void MainWindow::on_buttonViewFile_clicked()
{
/*QString file_name = this->ui->listLogFiles->selectedItems().takeFirst()->text(0);
std::string file_path = this->craplog.getLogFilePath( file_name );
std::string content = IOutils::readFile( file_path );
this->ui->textLogFiles->setText( RichText::enrichLogs( content, this->TB_color_scheme ) );*/
Craplog::LogFile item = this->craplog.getLogFileItem(
this->ui->listLogFiles->selectedItems().takeFirst()->text(0) );
Craplog::LogsFormat format;
if ( item.type == Craplog::LogType::Access ) {
format = this->craplog.getAccessLogsFormat();
} else if ( item.type == Craplog::LogType::Error ) {
format = this->craplog.getErrorLogsFormat();
} else {
// this shouldn't be
// !!! PUT A DIALOG ERROR MESSAGE HERE !!!
if ( this->ui->listLogFiles->selectedItems().size() > 0 ) {
// display the selected item
Craplog::LogFile item = this->craplog.getLogFileItem(
this->ui->listLogFiles->selectedItems().takeFirst()->text(0) );
Craplog::LogsFormat format;
if ( item.type == Craplog::LogType::Access ) {
format = this->craplog.getAccessLogsFormat();
} else if ( item.type == Craplog::LogType::Error ) {
format = this->craplog.getErrorLogsFormat();
} else {
// this shouldn't be
// !!! PUT A DIALOG ERROR MESSAGE HERE !!!
}
this->ui->textLogFiles->setText(
RichText::enrichLogs(
IOutils::readFile( item.path ),
format,
this->TB.color_scheme,
this->TB.wide_lines ));
}
this->ui->textLogFiles->setText(
RichText::enrichLogs(
IOutils::readFile( item.path ),
format,
this->TB_color_scheme ));
this->ui->textLogFiles->setFont( this->FONTS["main"] );
}

View File

@ -40,9 +40,28 @@ private:
bool runCraplog();
std::unordered_map<std::string, QColor> COLORS;
std::unordered_map<std::string, QFont> FONTS;
int font_size, font_size_big, font_size_small;
std::unordered_map<std::string, QFont> FONTS;
int font_size = 13,
font_size_big = 16,
font_size_small = 10;
int TB_color_scheme;
// base font families, to build fonts from
QString main_font_family;
QString script_font_family;
// text browser related
struct LogFilesTB {
bool wide_lines;
int color_scheme;
int font_size;
QString font_family;
QFont font;
};
LogFilesTB TB;
void setTB_FontSize( int size );
void setTB_FontFamily( QString font_family );
void updateTB_Font();
int getTB_FontSize();
QString getTB_FontFamily();
};
#endif // MAINWINDOW_H

View File

@ -5,6 +5,6 @@
<file alias="Hack">fonts/Hack.ttf</file>
</qresource>
<qresource prefix="/icons">
<file alias="Craplogo">icons/craplogo.svg</file>
<file alias="Craplogo">icons/crapicon.svg</file>
</qresource>
</RCC>

View File

@ -159,7 +159,6 @@ Craplog::LogType Craplog::defineFileType( std::string name, std::vector<std::str
}
real_type = Craplog::LogType::Failed;
for ( const std::string& line : lines ) {
std::cout << "LINE: " << line << std::endl;
if ( supposed_type == Craplog::LogType::Access ) {
if ( this->access_logs_format.initial.size() > 0 ) {
// a fixed starter char is set
@ -194,7 +193,6 @@ Craplog::LogType Craplog::defineFileType( std::string name, std::vector<std::str
n_err++;
}
}
std::cout << "ACC: " << n_acc << " / ERR: " << n_err << std::endl;
if ( n_acc > 0 && n_err == 0 ) {
// access logs
real_type = Craplog::LogType::Access;

View File

@ -56,7 +56,7 @@ std::string IOutils::readFile( std::string path )
std::string content = std::string();
try {
constexpr std::size_t read_size = std::size_t(4096);
std::ifstream file = std::ifstream(path);
std::ifstream file(path);
if ( file.is_open() == false ) {
throw std::ios_base::failure( "file is not open" );
}
@ -67,14 +67,12 @@ std::string IOutils::readFile( std::string path )
file.exceptions(std::ifstream::failbit);
file.exceptions(std::ios_base::badbit);
// read the whole file
std::string buf = std::string(read_size, '\0');
while (file.read(& buf[0], read_size)) {
content.append(buf);
//content.append(buf, 0, file.gcount()); !!! REMOVE IF NOT NEEDED !!!
}
content.append(buf, 0, file.gcount());
content = std::string(
(std::istreambuf_iterator<char>( file )),
std::istreambuf_iterator<char>() );
} catch (const std::ios_base::failure& err) {
// failed reading
content = "";
// >> err.what() << // !!! PUT A DIALOG ERROR MESSAGE HERE !!!
}
if ( file.is_open() ) {

View File

@ -1,60 +1,66 @@
#include "rtf.h"
RichText::RichText()
{
}
QString RichText::enrichLogs( std::string content, Craplog::LogsFormat format, int color_scheme )
QString RichText::enrichLogs( std::string content, Craplog::LogsFormat format, int color_scheme, bool wide_lines )
{
QString style;
std::unordered_map<std::string, QString> style;
switch ( color_scheme ) {
case 1:
// breeze
style = QString("<style>"
"body{background-color:#ffffff}"
"p{color:#9c9c9b}"
"span.x{color:#1f1c1b}"
"span.ip{color:#644a9b}"
"span.pt{color:#d5bc79}"
"span.time{color:#d685d6}"
"span.ua_src{color:#006e28}"
"span.req_err{color:#54b8ff}"
"span.res_lev{color:#d24f4f}"
"/<style>");
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;
case 2:
// monokai
style = QString("<style>"
"body{background-color:#272822}"
"p{color:#706c5a}"
"span.x{color:#d1d1cb}"
"span.ip{color:#57adbc}"
"span.pt{color:#c1b864}"
"span.time{color:#9773db}"
"span.ua_src{color:#a6e22e}"
"span.req_err{color:#bebeb8}"
"span.res_lev{color:#f92672}"
"</style>");
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;
case 3:
// radical
style = QString("<style>"
"body{background-color:#141322}"
"p{color:#749295}"
"span.x{color:#7c9c9e}"
"span.ip{color:#fda8bc}"
"span.pt{color:#ff85a1}"
"span.time{color:#a8c0c2}"
"span.ua_src{color:#42a784}"
"span.req_err{color:#d5358f}"
"span.res_lev{color:#56e8e4}"
"</style>");
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;
default:
style = "";
// 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/>";
}
QString rich_content = QString("<!DOCTYPE html><html><head>%1</head><body>")
.arg( style );
QString rich_line="", class_name="";
std::string sep, fld;
int start=0, stop=0, i=0;
@ -63,7 +69,7 @@ QString RichText::enrichLogs( std::string content, Craplog::LogsFormat format, i
for ( std::string& line : StringOps::splitrip( content ) ) {
i = 0;
line_size = line.size()-1;
rich_line += "<p>";
rich_line = "<p>";
// add the initial chars
stop = format.initial.size();
rich_line += QString::fromStdString( format.initial );
@ -71,54 +77,65 @@ QString RichText::enrichLogs( std::string content, Craplog::LogsFormat format, i
// color fields
if ( i <= n_sep ) {
sep = format.separators[i];
} else {
} else if ( i == n_sep+1 ) {
// final separator
sep = format.final;
} else {
// no more separators
break;
}
start = stop;
start = stop; // stop updated at the end of this loop
stop = line.find( sep, start );
if ( stop > line_size ) {
if ( stop > line_size || stop < 0 ) {
// separator not found, skip to the next one
i++;
if ( i > n_sep+1 ) {
// break if no one left
break;
}
stop = start;
continue;
}
// color the fields
class_name.clear();
fld = format.fields[i];
rich_line += "<b>";
class_name = "";
if ( color_scheme > 0 ) {
class_name += "<span class=\"";
fld = format.fields[i];
class_name += "<span style=\"color:";
if ( fld == "client" ) {
class_name += "ip";
class_name += style["ip"];
} else if ( fld == "port" ) {
class_name += "pt";
class_name += style["pt"];
} else if ( fld == "date_time" ) {
class_name += "time";
class_name += style["time"];
} else if ( fld == "user_agent" || fld == "source_file" ) {
class_name += "ua_src";
class_name += style["ua_src"];
} else if ( fld == "request" || fld == "error_message" ) {
class_name = "req_err";
class_name += style["req_err"];
} else if ( fld == "response_code" || fld == "error_level" ) {
class_name += "res_lev";
class_name += style["res_lev"];
} else {
class_name += "x";
class_name += style["x"];
}
class_name += "\">";
}
// add the class name as apan
// 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>";
}
rich_line += "</b>";
// update the stop for the next start
stop = stop + sep.size();
if ( stop >= line_size ) {
if ( stop > line_size ) {
// this was the final separator
rich_line += QString::fromStdString( format.final );
break;
}
rich_line += QString::fromStdString( sep );
i++;
}
rich_line += "</p>";
if ( wide_lines == true ) {
rich_line += "<br/>";
}
rich_content.push_back( rich_line );
rich_line.clear();
}

View File

@ -12,7 +12,7 @@ class RichText
public:
RichText();
static QString enrichLogs( std::string content, Craplog::LogsFormat, int color_scheme );
static QString enrichLogs( std::string content, Craplog::LogsFormat format, int color_scheme, bool wide_lines );
};
#endif // RICHTEXT_H

View File

@ -126,7 +126,7 @@ std::vector<std::string> StringOps::split( std::string str, std::string sep )
std::string slice;
int start=0, stop=0;
while (true) {
stop = str.find( sep );
stop = str.find( sep, start );
if ( stop >= str.size() ) {
slice = str.substr( start );
if ( slice.empty() == false ) {