logger.cpp
937 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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");
}