Add basic implementation of SQL Writer and Modcall writer
This commit is contained in:
parent
e512897651
commit
a836d2f500
@ -24,7 +24,7 @@
|
||||
#include <QTextStream>
|
||||
|
||||
/**
|
||||
* @brief A class for handling all log writing in the full logging mode.
|
||||
* @brief A class to handle file interaction when writing in full log mode.
|
||||
*/
|
||||
class WriterFull : public QObject
|
||||
{
|
||||
@ -33,10 +33,7 @@ public:
|
||||
/**
|
||||
* @brief Constructor for full logwriter
|
||||
*
|
||||
* @details While this could've been a simple function, making it an object allows me to easier check runtime requirements
|
||||
* when reloading the logger. It also helps split the complex stucture of a logger into easier segments.
|
||||
*
|
||||
* @param QPObject pointer to the parent object.
|
||||
* @param QObject pointer to the parent object.
|
||||
*/
|
||||
WriterFull(QObject* parent = nullptr);;
|
||||
|
||||
@ -47,9 +44,8 @@ public:
|
||||
*/
|
||||
virtual ~WriterFull() {}
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Slot for u_logger to connect to when full logging is used.
|
||||
* @brief Function to write log entry into a logfile.
|
||||
* @param Preformatted QString which will be written into the logfile.
|
||||
*/
|
||||
void flush(const QString f_entry);
|
||||
|
@ -18,15 +18,51 @@
|
||||
#ifndef WRITER_MODCALL_H
|
||||
#define WRITER_MODCALL_H
|
||||
#include <QObject>
|
||||
#include <QFile>
|
||||
#include <QDir>
|
||||
#include <QDateTime>
|
||||
#include <QTextStream>
|
||||
#include <QQueue>
|
||||
|
||||
|
||||
/**
|
||||
* @brief A class to handle file interaction when writing the modcall buffer.
|
||||
*/
|
||||
class WriterModcall : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
/**
|
||||
* @brief Constructor for modcall logwriter
|
||||
*
|
||||
* @param QObject pointer to the parent object.
|
||||
*/
|
||||
WriterModcall(QObject* parent = nullptr);;
|
||||
|
||||
/**
|
||||
* @brief Deconstructor for modcall logwriter.
|
||||
*
|
||||
* @details Doesn't really do anything, but its here for completeness sake.
|
||||
*/
|
||||
virtual ~WriterModcall() {}
|
||||
|
||||
/**
|
||||
* @brief Function to write area buffer into a logfile.
|
||||
* @param QQueue of the area that will be written into the logfile.
|
||||
* @param Name of the area for the filename.
|
||||
*/
|
||||
void flush(const QString f_areaName, QQueue<QString> f_buffer);
|
||||
|
||||
private:
|
||||
/**
|
||||
* @brief Filename of the logfile used.
|
||||
*/
|
||||
QFile l_logfile;
|
||||
|
||||
/**
|
||||
* @brief Directory where logfiles will be stored.
|
||||
*/
|
||||
QDir l_dir;
|
||||
};
|
||||
|
||||
|
||||
#endif //WRITER_MODCALL_H
|
||||
|
@ -17,15 +17,62 @@
|
||||
//////////////////////////////////////////////////////////////////////////////////////
|
||||
#ifndef WRITER_SQL_H
|
||||
#define WRITER_SQL_H
|
||||
#include <QObject>
|
||||
|
||||
#include <QObject>
|
||||
#include <QDir>
|
||||
#include <QSqlDatabase>
|
||||
#include <QSqlDriver>
|
||||
#include <QSqlError>
|
||||
#include <QSqlQuery>
|
||||
#include <QFileInfo>
|
||||
#include <QDebug>
|
||||
|
||||
/**
|
||||
* @brief A class to handle database interaction when executing SQL statements in SQL mode.
|
||||
*/
|
||||
class WriterSQL : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
WriterSQL(QObject* parent = nullptr);;
|
||||
virtual ~WriterSQL() {}
|
||||
/**
|
||||
* @brief Constructor for SQL logwriter
|
||||
*
|
||||
* @param QObject pointer to the parent object.
|
||||
*/
|
||||
WriterSQL(QObject* parent = nullptr);
|
||||
|
||||
/**
|
||||
* @brief Deconstructor for SQL logwriter. Closes the underlying database
|
||||
*/
|
||||
virtual ~WriterSQL();;
|
||||
|
||||
/**
|
||||
* @brief Function to execute SQL queries on the log database.
|
||||
* @param SQL query execute d on the log database.
|
||||
*/
|
||||
void execLogScript(QSqlQuery query);
|
||||
|
||||
private:
|
||||
|
||||
/**
|
||||
* @brief The name of the database connection driver.
|
||||
*/
|
||||
const QString DRIVER;
|
||||
|
||||
/**
|
||||
* @brief The backing database that stores user details.
|
||||
*/
|
||||
QSqlDatabase log_db;
|
||||
|
||||
/**
|
||||
* @brief Filename of the logfile used.
|
||||
*/
|
||||
QFile l_logfile;
|
||||
|
||||
/**
|
||||
* @brief Directory where logfiles will be stored.
|
||||
*/
|
||||
QDir l_dir;
|
||||
};
|
||||
|
||||
#endif //WRITER_SQL_H
|
||||
|
@ -29,6 +29,7 @@ WriterFull::WriterFull(QObject* parent) :
|
||||
void WriterFull::flush(const QString f_entry)
|
||||
{
|
||||
l_logfile.setFileName(QString("logs/%1.log").arg(QDate::currentDate().toString("yyyy-MM-dd")));
|
||||
|
||||
if (l_logfile.open(QIODevice::WriteOnly | QIODevice::Append)) {
|
||||
QTextStream file_stream(&l_logfile);
|
||||
file_stream << f_entry;
|
||||
|
@ -20,5 +20,28 @@
|
||||
WriterModcall::WriterModcall(QObject* parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
l_dir.setPath("logs/");
|
||||
if (!l_dir.exists()) {
|
||||
l_dir.mkpath(".");
|
||||
}
|
||||
|
||||
l_dir.setPath("logs/modcall");
|
||||
if (!l_dir.exists()) {
|
||||
l_dir.mkpath(".");
|
||||
}
|
||||
}
|
||||
|
||||
void WriterModcall::flush(const QString f_areaName, QQueue<QString> f_buffer)
|
||||
{
|
||||
l_logfile.setFileName(QString("logs/report_%1_%2.log").arg(f_areaName, (QDateTime::currentDateTime().toString("yyyy-MM-dd_hhmmss"))));
|
||||
|
||||
if (l_logfile.open(QIODevice::WriteOnly | QIODevice::Append)) {
|
||||
QTextStream file_stream(&l_logfile);
|
||||
|
||||
while (!f_buffer.isEmpty())
|
||||
file_stream << f_buffer.dequeue();
|
||||
}
|
||||
|
||||
l_logfile.close();
|
||||
|
||||
};
|
||||
|
@ -18,7 +18,28 @@
|
||||
#include "include/logger/writer_sql.h"
|
||||
|
||||
WriterSQL::WriterSQL(QObject* parent) :
|
||||
QObject(parent)
|
||||
QObject(parent), DRIVER("QSQLITE")
|
||||
{
|
||||
const QString db_filename = "logs/database/log.db";
|
||||
|
||||
};
|
||||
QFileInfo db_info(db_filename);
|
||||
if(!db_info.isReadable() || !db_info.isWritable())
|
||||
qCritical() << tr("Database Error: Missing permissions. Check if \"%1\" is writable.").arg(db_filename);
|
||||
|
||||
log_db = QSqlDatabase::addDatabase(DRIVER);
|
||||
log_db.setDatabaseName("logs/database/log.db");
|
||||
|
||||
if (!log_db.open())
|
||||
qCritical() << "Database Error:" << log_db.lastError();
|
||||
|
||||
QSqlQuery create_chat_events_table("CREATE TABLE IF NOT EXISTS chat_events ('event_time' DATETIME DEFAULT CURRENT_TIMESTAMP, 'ipid' TEXT, 'room_name' TEXT,'event_type' TEXT, 'char_name' TEXT, 'ic_name' TEXT, 'message' TEXT NOT NULL);");
|
||||
create_chat_events_table.exec();
|
||||
|
||||
QSqlQuery create_connection_events_table("CREATE TABLE IF NOT EXISTS users ('event time' DATETIME DEFAULT CURRENT_TIMESTAMP, 'ipid' TEXT, 'ip_address' TEXT, 'hdid' TEXT);");
|
||||
create_connection_events_table.exec();
|
||||
}
|
||||
|
||||
WriterSQL::~WriterSQL()
|
||||
{
|
||||
log_db.close();
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user