#include "logout.h"
#include "iostream"
extern FILE *file=NULL;
FILE *datafile=NULL;
#define _DEBUG
void (*pFunction)(QString message)= nullptr;
QString colorcode = "\033[";
QString lastdatestr="";
int outputlevel = 2;
//
//void logout(QString str)
//{
//#ifdef _DEBUG
//    qDebug()<<str;
//#endif
//}




void Log(QString str, int level)
{


    if(outputlevel>5)
    {
        if (pFunction!= nullptr)
        {
            pFunction(str);
        }

    }
    //return;
    if (level > outputlevel)
    {
        return;
    }
    //QTextCodec *codec = QTextCodec::codecForName("GBK");//�޸�������
    int colorint;
    switch (level)
    {
        case 0:
        {

            // return;
            break;
        }
        case 1:
        {

            qDebug() << "\033[32m";//��ɫ
            break;
        }
        case 2:
        {

            qDebug() << "\033[35m";//��ɫ
            break;
        }
        case 3:
        {

            qDebug() << "\033[31m";//��ɫ
            break;
        }
        case 5:
        {
            qDebug() << "\033[36m";
            break;
        }
        default:
        {
            break;
        }
    }
    //str=codec->toUnicode(str.toStdString().c_str());
    //qDebug() << str;
    //qDebug() << "\033[0m";
    //std::cout<<str.toStdString()<<"<br>";
    std::fflush(stdout);

}

void setoutlevel(int level /*= 4*/)
{
    outputlevel = level;

}

void setfunc(void (*sendmassage1)(QString)) {
    pFunction=sendmassage1;
}

void writelogtofile(QString sender,QString qstr)
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QString datestr=currentTime.toString("yyyy_MM_dd");
    if (lastdatestr!=datestr)
    {
        if (file!=NULL)
        {
            fclose(file);
            file=NULL;
        }
        lastdatestr=datestr;
    }
    if (file==NULL)
    {
        QString path="/home/data/Log/"+currentTime.toString("yyyy_MM_dd_hh_mm_ss")+"_Temptrue.log";
        system("mkdir -p /home/data/Log");
        file=fopen(path.toStdString().c_str(),"a+");
        fprintf(file,"\n\n\n\nstart---------------------------------------------------------------------------------\n");
    }


    fprintf(file,"%s %s: %s\n",currentTime.toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str(),sender.toStdString().c_str(),qstr.toStdString().c_str());
    fflush(file);
}

void saveData(QString sender,QString qstr)
{
    QDateTime currentTime = QDateTime::currentDateTime();
    QString datestr=currentTime.toString("yyyy_MM_dd");
    if (lastdatestr!=datestr)
    {
        if (datafile!=NULL)
        {
            fclose(file);
            datafile=NULL;
        }
        lastdatestr=datestr;
    }
    if (datafile==NULL)
    {
        QString path="/home/data/TempData/"+currentTime.toString("yyyy_MM_dd_hh_mm_ss")+".csv";
        system("mkdir -p /home/data/TempData");
        datafile=fopen(path.toStdString().c_str(),"a+");
        fprintf(datafile,QString::fromLocal8Bit("日期, 数据类型, 日期\n").toStdString().c_str());
    }


    fprintf(datafile,"%s,%s,%s\n",currentTime.toString("yyyy-MM-dd hh:mm:ss.zzz").toStdString().c_str(),sender.toStdString().c_str(),qstr.toStdString().c_str());
    fflush(datafile);
}