logger.cpp 937 Bytes
#include "logger.h"

#include <QDebug>

Logger::Logger(QObject *parent)
    : QObject(parent),
      socket(Q_NULLPTR)
{

}

bool Logger::init(const QString &tag)
{
    this->tag = tag;
    if (socket == Q_NULLPTR)
        socket = new UnixDomainDatagramSocket(this);

    return socket->open();
}

void Logger::print(const QString &string)
{
    QByteArray bytes(string.toLocal8Bit().data());
    bytes.prepend(' ').prepend(tag.toLocal8Bit().data());

    qDebug() << bytes;

    socket->writeDatagram(bytes, "/tmp/logger.uds");
}

void Logger::hexdump(const QString &message, const QByteArray &bytes)
{
    QByteArray line;
    line += tag + " " + message + " [";
    for (int i = 0; i < bytes.length(); i++)
    {
        line += QByteArray::number(uchar(bytes.at(i)), 16).toUpper();
        line += " ";
    }
    line = line.trimmed();
    line += "]\n";

    qDebug() << line;
    socket->writeDatagram(line, "/tmp/logger.uds");
}