<commit_before>#include "logger.h"

#include <QMutex>
#include <QFile>
#include <QTextStream>

#include <iostream>

int Logger::logLevel = 0;
bool Logger::logToFile = true;
Logger::Logger(QObject *parent) : QObject(parent)
{

}

void Logger::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString& message)
{
    static QMutex mutex;
    QMutexLocker lock(&mutex);


    QByteArray localMsg = message.toLocal8Bit();

    if (type == QtDebugMsg && logLevel >= 0)
    {
        QString msg = QString("Debug: %1 (%2:%3, %4)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtInfoMsg && logLevel >= 1)
    {
        QString msg = QString("Info: %s (%s:%u, %s)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtWarningMsg && logLevel >= 2)
    {
        QString msg = QString("Warning: %s (%s:%u, %s)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtCriticalMsg && logLevel >= 3)
    {
        QString msg = QString("Critical: %s (%s:%u, %s)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtFatalMsg && logLevel >= 4)
    {
        QString msg = QString("Fatal: %s (%s:%u, %s)\n").arg(localMsg.constData(), context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();

        if(logToFile) { writeLogToFile(msg); }
        abort();
    }
}
void Logger::writeLogToFile(QString msg)
{
    static QFile file("logfile.log");

    if(!file.isOpen()) { file.open(QIODevice::Append); }
    QTextStream stream(&file);

    stream << msg;// << endl;
    stream.flush();
}<commit_msg>Added timestamp and better looking debug output. Still need to find a way to bring the source(server, client, etc) in.<commit_after>#include "logger.h"

#include <QMutex>
#include <QFile>
#include <QTextStream>
#include <QDateTime>

#include <iostream>

int Logger::logLevel = 0;
bool Logger::logToFile = true;
Logger::Logger(QObject *parent) : QObject(parent)
{

}

void Logger::messageHandler(QtMsgType type, const QMessageLogContext &context, const QString& message)
{
    static QMutex mutex;
    QMutexLocker lock(&mutex);

    if (type == QtDebugMsg && logLevel >= 0)
    {
        QString msg = QString("[ Server ] [ %1 ] [ Debug    ] : %2 (%3:%4, %5)\n").arg(
                            QDateTime::currentDateTime().toString("yyyy-MM-dd : hh:mm:ss.zzz"),
                            message, context.file, QString::number(context.line),context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtInfoMsg && logLevel >= 1)
    {
        QString msg = QString("[ Server ] [ %1 ] [ Info     ] : %2 (%3:%4, %5)\n").arg(
                           QDateTime::currentDateTime().toString("yyyy-MM-dd : hh:mm:ss.zzz"),
                           message, context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtWarningMsg && logLevel >= 2)
    {
        QString msg = QString("[ Server ] [ %1 ] [ Warning  ] : %2 (%3:%4, %5)\n").arg(
                           QDateTime::currentDateTime().toString("yyyy-MM-dd : hh:mm:ss.zzz"),
                           message, context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtCriticalMsg && logLevel >= 3)
    {
        QString msg = QString("[ Server ] [ %1 ] [ Critical ] : %2 (%3:%4, %5)\n").arg(
                            QDateTime::currentDateTime().toString("yyyy-MM-dd : hh:mm:ss.zzz"),
                            message, context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();
        if(logToFile) { writeLogToFile(msg); }
    }
    else if (type == QtFatalMsg && logLevel >= 4)
    {
        QString msg = QString("[ Server ] [ %1 ] [ Fatal    ] : %2 (%3:%4, %5)\n").arg(
                            QDateTime::currentDateTime().toString("yyyy-MM-dd : hh:mm:ss.zzz"),
                            message, context.file, QString::number(context.line), context.function);
        std::cerr << msg.toStdString().c_str();

        if(logToFile) { writeLogToFile(msg); }
        abort();
    }
}
void Logger::writeLogToFile(QString msg)
{
    static QFile file("logfile.log");

    if(!file.isOpen()) { file.open(QIODevice::Append); }
    QTextStream stream(&file);

    stream << msg;// << endl;
    stream.flush();
}
{'fraction_non_alphanumeric': 0.17481709664998074, 'fraction_numerical': 0.008086253369272238, 'mean_word_length': 2.868205510052122, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 22, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3676620', 'n_tokens_mistral': 1701, 'n_tokens_neox': 1682, 'n_words': 337}