Added Craplogo icon

This commit is contained in:
Valentino Orlandi 2022-06-23 20:07:59 +02:00
parent 174e693ebe
commit b8d7ea298f
Signed by: elB4RTO
GPG Key ID: 1719E976DB2D4E71
16 changed files with 335 additions and 41 deletions

View File

@ -24,14 +24,16 @@ set(PROJECT_SOURCES
mainwindow.h
mainwindow.cpp
mainwindow.ui
windows/craplog.h
windows/craplog.cpp
tools/craplog.h
tools/craplog.cpp
utilities/io.h
utilities/io.cpp
utilities/strings.h
utilities/strings.cpp
utilities/rtf.h
utilities/rtf.cpp
utilities/logs.h
utilities/logs.cpp
resources/resources.qrc
${TS_FILES}
)

View File

@ -27,6 +27,9 @@ MainWindow::MainWindow(QWidget *parent)
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);
@ -52,12 +55,11 @@ void MainWindow::on_buttonRefreshList_clicked()
QTreeWidgetItem * item = new QTreeWidgetItem();
// set unchecked
item->setCheckState(0, Qt::CheckState::Unchecked );
// name to be showed
// set the name of the file
item->setText( 0, log_file.name );
item->setFont( 0, this->FONTS["main"] );
// size to be showed
// prepare the size of the file
float size = (float)log_file.size / 1024;
int n_decimals = 3;
std::string sfx = " KiB";
if (size > 1024) {
size /= 1024;
@ -65,30 +67,29 @@ void MainWindow::on_buttonRefreshList_clicked()
}
// cut decimals depending on how big the floor is
std::string size_str = std::to_string( size );
int point_i = size_str.find('.')+1;
if ( point_i == 0 ) {
point_i = size_str.find(',')+1;
int cut_index = size_str.find('.')+1;
if ( cut_index == 0 ) {
cut_index = size_str.find(',')+1;
}
int n_decimals = 3;
if ( size >= 100 ) {
n_decimals = 2;
if ( size >= 1000 ) {
n_decimals = 1;
if ( size >= 10000 ) {
n_decimals = 0;
point_i --;
cut_index --;
}
}
}
if ( point_i >= 1 ) {
for ( int i=0; i<n_decimals; i++ ) {
if ( i >= size_str.size() ) {
break;
}
point_i ++;
if ( cut_index >= 1 ) {
cut_index += n_decimals;
if ( cut_index > size_str.size()-1 ) {
cut_index = size_str.size()-1;
}
}
// apply text and color
item->setText( 1, QString::fromStdString( size_str.substr(0,point_i) + sfx ) );
// apply text and color to the size text
item->setText( 1, QString::fromStdString( size_str.substr(0,cut_index) + sfx ) );
item->setForeground( 1, this->COLORS["grey"] );
item->setFont( 1, this->FONTS["main_italic"] );
// append the item (on top, forced)
@ -125,7 +126,7 @@ 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->ui->textLogFiles->setText( RichText::enrichLogs( content, this->TB_color_scheme ) );
this->ui->textLogFiles->setFont( this->FONTS["main"] );
}

View File

@ -9,7 +9,7 @@
#include "utilities/io.h"
#include "utilities/rtf.h"
#include "windows/craplog.h"
#include "tools/craplog.h"
QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
@ -43,5 +43,7 @@ private:
std::unordered_map<std::string, QColor> COLORS;
std::unordered_map<std::string, QFont> FONTS;
int font_size, font_size_big, font_size_small;
int TB_color_scheme;
};
#endif // MAINWINDOW_H

View File

@ -19,6 +19,10 @@
<property name="windowTitle">
<string>Craplog</string>
</property>
<property name="windowIcon">
<iconset resource="resources/resources.qrc">
<normaloff>:/icons/Craplogo</normaloff>:/icons/Craplogo</iconset>
</property>
<property name="locale">
<locale language="English" country="UnitedStates"/>
</property>
@ -457,8 +461,25 @@ hr { height: 1px; border-width: 0; }
<height>27</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="acceptDrops">
<bool>false</bool>
</property>
<property name="title">
<string>Utilities</string>
</property>
<addaction name="actionCrapnote"/>
</widget>
<addaction name="menuFile"/>
</widget>
<action name="actionCrapnote">
<property name="text">
<string>Block-note</string>
</property>
</action>
</widget>
<resources/>
<resources>
<include location="resources/resources.qrc"/>
</resources>
<connections/>
</ui>

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 38 KiB

View File

@ -4,4 +4,7 @@
<file alias="3270">fonts/3270.ttf</file>
<file alias="Hack">fonts/Hack.ttf</file>
</qresource>
<qresource prefix="/icons">
<file alias="Craplogo">icons/craplogo.svg</file>
</qresource>
</RCC>

View File

@ -1,7 +1,14 @@
#include "craplog.h"
#include "utilities/io.h"
#include "utilities/logs.h"
Craplog::Craplog()
{
this->access_logs_format = Craplog::LogsFormat::AccessCombined;
this->error_logs_format = Craplog::LogsFormat::ErrorDefault;
this->logs_path = "/var/log/apache2";
//this->readConfigs();
this->scanLogsDir();
@ -71,7 +78,8 @@ void Craplog::scanLogsDir()
.selected = false,
.size = size,
.name = QString::fromStdString( name ),
.path = dir_entry.path().string(),
.path = path,
.type = LogOp::defineLogType( path )
});
}
}

View File

@ -16,24 +16,43 @@ class Craplog
public:
Craplog();
// logs formats
enum LogsFormat {
AccessCommon,
AccessCombined,
ErrorDefault
};
LogsFormat access_logs_format;
LogsFormat error_logs_format;
// log file infoes
enum LogType {
Access,
Error
};
struct LogFile {
bool selected;
int size;
QString name;
std::string path;
LogType type;
};
// logs list related methods
std::vector<LogFile> getLogsList( bool fresh=false );
int getLogsListSize();
std::string getLogFilePath( QString file_name );
int setLogFileSelected( QString file_name );
private:
// logs related
std::string logs_path;
std::vector<LogFile> logs_list;
// logs list related
void scanLogsDir();
void readConfigs();
void scanLogsDir();
void loadFileContent();
};

View File

@ -1,25 +1,84 @@
#include "io.h"
#include "utilities/strings.h"
IOutils::IOutils()
{
}
auto IOutils::readFile( std::string path ) -> std::string
std::vector<std::string> IOutils::readLines( std::string path, int n_lines )
{
constexpr std::size_t read_size = std::size_t(4096);
std::ifstream file = std::ifstream(path.data());
file.exceptions(std::ios_base::badbit);
std::string content = std::string();
std::string buf = std::string(read_size, '\0');
while (file.read(& buf[0], read_size)) {
content.append(buf, 0, file.gcount());
// read rhe first line only
std::ifstream file;
std::vector<std::string> lines;
std::string line;
try {
constexpr std::size_t read_size = std::size_t(4096);
file = std::ifstream(path);
if ( file.is_open() == false ) {
throw std::ios_base::failure( "file is not open" );
}
// add bit exceptions
file.exceptions(std::ifstream::failbit);
file.exceptions(std::ios_base::badbit);
// get non-empty lines
int n=0;
while (n < n_lines) {
if ( file.good() == false ) {
throw std::ios_base::failure( "file is not good" );
}
getline(file, line);
if ( line.empty() ) {
continue;
}
// succesfully catched a line
lines.push_back( StringOp::strip( line ) );
n++;
}
} catch (const std::ios_base::failure& err) {
// failed reading
std::cout << err.what() << std::endl; // !!! PUT AN DIALOG ERROR MESSAGE HERE !!!
}
if ( file.is_open() ) {
file.close();
}
return lines;
}
std::string IOutils::readFile( std::string path )
{
// read the whole file
std::ifstream file;
std::string content = std::string();
try {
constexpr std::size_t read_size = std::size_t(4096);
std::ifstream file = std::ifstream(path);
if ( file.is_open() == false ) {
throw std::ios_base::failure( "file is not open" );
}
if ( file.good() == false ) {
throw std::ios_base::failure( "file is not good" );
}
// add bit exceptions
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());
} catch (const std::ios_base::failure& err) {
// failed reading
std::cout << err.what() << std::endl; // !!! PUT AN DIALOG ERROR MESSAGE HERE !!!
}
if ( file.is_open() ) {
file.close();
}
content.append(buf, 0, file.gcount());
file.close();
return content;
}

View File

@ -4,6 +4,7 @@
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
class IOutils
@ -11,6 +12,7 @@ class IOutils
public:
IOutils();
static std::vector<std::string> readLines( std::string path, int n_lines=10 );
static std::string readFile( std::string path );
};

View File

@ -0,0 +1,6 @@
#include "logs.h"
LogOp::LogOp()
{
}

20
craplog/utilities/logs.h Normal file
View File

@ -0,0 +1,20 @@
#ifndef LOGS_H
#define LOGS_H
#include <string>
#include <vector>
#include "tools/craplog.h"
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 );
};
#endif // LOGOP_H

View File

@ -6,7 +6,7 @@ RichText::RichText()
}
QString RichText::enrichLogs( std::string content, int color_scheme )
QString RichText::enrichLogs( std::string content, Craplog::LogsFormat logs_format, int color_scheme )
{
QString style;
switch ( color_scheme ) {
@ -52,5 +52,10 @@ QString RichText::enrichLogs( std::string content, int color_scheme )
QString rich_content = QString("<html><head>%1<head/><body>")
.arg( style );
for ( std::string& line : StringOp::splitrip( content ) ) {
if ( line[0] == '[' ) {
// error logs
}
}
return rich_content;
}

View File

@ -4,12 +4,15 @@
#include "qfont.h"
#include "qstring.h"
#include "utilities/strings.h"
#include "tools/craplog.h"
class RichText
{
public:
RichText();
static QString enrichLogs( std::string content, int color_scheme=0 );
static QString enrichLogs( std::string content, int color_scheme );
};
#endif // RICHTEXT_H

View File

@ -89,4 +89,20 @@ std::vector<std::string> StringOp::split( std::string str, std::string sep )
start = stop+sep.size();
}
}
return splitted;
}
std::vector<std::string> StringOp::splitrip( std::string str, std::string sep, std::string chars )
{
std::vector<std::string> splitted, aux;
str = StringOp::strip( str );
aux = StringOp::split( str );
for ( std::string &str : aux ) {
if ( str.empty() ) {
continue;
}
splitted.push_back( StringOp::strip( str ) );
}
return splitted;
}

View File

@ -3,6 +3,7 @@
#include <string>
#include <vector>
#include "qstring.h"
class StringOp
@ -10,11 +11,13 @@ class StringOp
public:
StringOp();
std::string strip( std::string str, std::string chars=" \n\t\b\r\v" );
std::string lstrip( std::string str, std::string chars=" \n\t\b\r\v" );
std::string rstrip( std::string str, std::string chars=" \n\t\b\r\v" );
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" );
std::vector<std::string> split( std::string str, std::string sep );
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" );
};
#endif // STRINGOP_H