#include "xlrulogfile.h"


#include <QDebug>
#include <QFile>

XlruLogClass::XlruLogClass():
    file(0)
{
    file_path = "";
    file_name = "";
}

XlruLogClass::~XlruLogClass()
{
    closeFile();
}

QString XlruLogClass::newNameFile(QString _header)
{
    QDateTime dateTime;
    dateTime = QDateTime::currentDateTime();
    return QString("%1_%2_%3").arg(dateTime.toString("yyyyMMddhhmmss")).arg(_header).arg("xlru_monitor.log");
}

QString XlruLogClass::generateNewFile(QString _header)
{
    file_name = newNameFile(_header);
    //file_path = QString("%1//%2").arg(QFileInfo(".").absolutePath()).arg(file_name);
    file_path = QString("./%1").arg(file_name);

    if (saveString(" "))
    {
        closeFile();
        return file_path;
    }
    else
        return "";
}

bool XlruLogClass::saveString(QString _value)
{
    if (!file.isOpen())
    {
        file.setFileName(file_path);
        if ( !file.open(QFile::WriteOnly | QFile::Text | QIODevice::Append) )
        {
            qDebug() << "File not exists";
            return false;
        }
    }

    // Create a thread to retrieve data from a file
    QTextStream out(&file);
    out << _value;// << "\n";
    //file.close();
    return true;
}


bool XlruLogClass::closeFile()
{
    if (file.isOpen())
    {
        file.close();
        return true;
    }
    return false;
}
