I
I
IvanFaQerstein2015-04-21 06:43:53
Qt
IvanFaQerstein, 2015-04-21 06:43:53

How to implement XML logging class on QDomDocument (Qt)?

Hello. I'm trying to implement logging using Qt. I'm using QDomDocument, but the output is an empty file. I'm new to XML and can't find a bug/problem. Thank you in advance.
log.h

#ifndef LOG_H
#define LOG_H

#include <QtXml/QtXml>
#include <QDate>
#include <QtXml/QDomDocument>

class Log
{
private:
    static Log* instance;
public:
    static Log* getInstance();
    ~Log();
    bool appendRecord(QString& _emp_id, QString& _emp_name, QString& _action, QDate _date);
private:
    Log();
    QDomElement makeRecord(QDomDocument& _doc, QString& _emp_id, QString& _emp_name, QString& _action, QDate _date);
    QDomElement makeElement(QDomDocument& _doc, QString& _name, QString& _attribute, QString& _text = QString());
};


#endif // LOG_H

log.cpp
#include "log.h"
#include <QFile>

Log* Log::instance = nullptr;

Log::Log()
{
    qDebug() << Q_FUNC_INFO;
}

Log::~Log()
{
    qDebug() << Q_FUNC_INFO;
    if(instance)
        delete instance;
}

Log* Log::getInstance()
{
    qDebug() << Q_FUNC_INFO;
    if(!instance)
        instance = new Log();
    return instance;
}

bool Log::appendRecord(QString& _emp_id, QString& _emp_name, QString& _action, QDate _date)
{
    qDebug() << Q_FUNC_INFO;
    QDomDocument doc("log");
    QDomElement elem = doc.createElement("log");
    QDomElement record = makeRecord(doc, _emp_id, _emp_name, _action, _date);

    elem.appendChild(record);
    QFile file(QString("log_%1_%2_%3.xml").arg(_date.year()).arg(_date.month()).arg(_date.day()));
    if(file.open(QIODevice::WriteOnly))
    {
        QTextStream(&file) << doc.toString();
        QTextStream(&file) << "END.";
        file.close();
        return true;
    }
    return false;
}

QDomElement Log::makeRecord(QDomDocument& _doc, QString& _emp_id, QString& _emp_name, QString& _action, QDate _date)
{
    qDebug() << Q_FUNC_INFO;
    static int number = 1;

    QDomElement elem = makeElement(_doc, QString("record"), QString::number(number));
    elem.appendChild(makeElement(_doc, QString("ID"), QString(), _emp_id));
    elem.appendChild(makeElement(_doc, QString("name"), QString(), _emp_name));
    elem.appendChild(makeElement(_doc, QString("action"), QString(), _action));
    elem.appendChild(makeElement(_doc, QString("date"), QString(), _date.toString()));


    number++;
    return elem;
}

QDomElement Log::makeElement(QDomDocument& _doc, QString& _name, QString& _attribute, QString& _text)
{
    qDebug() << Q_FUNC_INFO;
    QDomElement elem = _doc.createElement(_name);

    if(!_attribute.isEmpty())
    {
        QDomAttr attr = _doc.createAttribute("number");
        attr.setValue(_attribute);
        elem.setAttributeNode(attr);
    }

    if(!_text.isEmpty())
    {
        QDomText txt = _doc.createTextNode(_text);
        qDebug() << _text;
        elem.appendChild(txt);
    }
    return elem;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Y
Yaroslav, 2015-04-21
@IvanFaQerstein

After elem.appendChild(record); write the line doc.appendChild(elem); Then everything should appear in the file:
Only each time you will overwrite the message in the file because you open it again for writing, destroying the contents. Perhaps that's how it's meant to be. If not, use the QIODevice::Append flag.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question