123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- #include "ivlogsave.h"
- extern int gnlogmin;
- iv::Ivlog * givlog;
- ivlogsave::ivlogsave()
- {
- givlog = new iv::Ivlog("ivlog_record");
- bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adciv.interface", "ivlog",this,SLOT(onLogMsg(QByteArray)));
- QTimer * timer = new QTimer();
- connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));
- timer->start(60000);
- qDebug("connect is %d",bconnect);
- givlog->info("ivlog_record","ivlogsave start");
- }
- ivlogsave::~ivlogsave()
- {
- qDebug()<<"ivlog exit";
- givlog->info("exit","ivlog_record exit");
- }
- void ivlogsave::run()
- {
- char * strhome = getenv("HOME");
- QString strsavepath;
- strsavepath = strhome;
- strsavepath = strsavepath + "/log";
- QDir dir(strsavepath);
- if(dir.exists())
- {
- }
- else
- {
- bool ok = dir.mkdir(strsavepath);//只创建一级子目录,即必须保证上级目录存在
- if(ok == false)
- {
- std::cout<< "ivlogsave:run mkdir error. dir path is "<<strsavepath.toLatin1().data()<<std::endl;
- return;
- }
- }
- //compress last log file
- init_compress(strsavepath);
- QDateTime dt = QDateTime::currentDateTime();
- // strsavepath = strsavepath + "/" + dt.toString("yyyy-MM-dd-hh-mm-ss-zzz.ilg");
- strsavepath = strsavepath + "/" + dt.toString("yyyy-MM-dd-hh-mm-ss-zzz.log");
- QFile file;
- file.setFileName(strsavepath);
- if(!file.open(QIODevice::ReadWrite))
- {
- std::cout<<"ivlogsave:run Open File Error."<<std::endl;
- return;
- }
- QByteArray ba;
- ba.clear();
- while(!QThread::isInterruptionRequested())
- {
- mMutex.lock();
- if(mba.size() > 0)
- {
- ba.append(mba);
- mba.clear();
- }
- mMutex.unlock();
- if((ba.size() > 0)&&mbCanSave)
- {
- file.write(ba);
- file.flush();
- ba.clear();
- }
- msleep(100);
- }
- file.close();
- }
- void ivlogsave::onLogMsg(QByteArray value)
- {
- QByteArray bx;
- int nsize = value.size();
- char * str = new char[nsize];
- std::shared_ptr<char> pstr;pstr.reset(str);
- memcpy(str,value.data(),nsize);
- if(nsize<12)
- {
- return;
- }
- char strmark[5];strmark[4] = 0;
- memcpy(strmark,str,4);
- if(strcmp(strmark,"ilog")!= 0)
- {
- qDebug("mark error");
- }
- int ndatasize;
- int npos = 4;
- memcpy(&ndatasize,str+npos,sizeof(int));npos = npos + sizeof(int);
- if(ndatasize != nsize)
- {
- qDebug("commicate error");
- }
- int nlognum;
- memcpy(&nlognum,str+npos,sizeof(int));npos = npos +sizeof(int);
- // qDebug("datasize is %d lognum is %d",ndatasize,nlognum);
- if(nlognum < 1)
- {
- return;
- }
- int * plogsize = new int[nlognum];
- std::shared_ptr<int> pplog;pplog.reset(plogsize);
- int i;
- for(i=0;i<nlognum;i++)
- {
- memcpy(&plogsize[i],str + npos,sizeof(int));
- npos = npos + sizeof(int);
- }
- for(i=0;i<nlognum;i++)
- {
- if((npos + plogsize[i])>ndatasize)
- {
- std::cout<<"out range."<<std::endl;
- return;
- }
- iv::log::logdata xlog;
- if(xlog.ParseFromArray(str+npos,plogsize[i]))
- {
- if(xlog.logclass()>= gnlogmin)
- {
- qint64 mstime = xlog.logtime();
- QDateTime datetime = QDateTime::fromMSecsSinceEpoch(mstime);
- char strout[10000];
- if(xlog.has_logtag())
- {
- snprintf(strout,10000,"[%s %s %d %d (%s) ]: %s\n",xlog.modulename().data(),datetime.toString("yyyy-MM-dd hh:mm:ss:zzz ").toLatin1().data(),
- xlog.pid(),(int)xlog.logclass(),xlog.logtag().data(), xlog.logsentence().data());
- // std::cout<<strout<<std::endl;
- }
- else
- {
- snprintf(strout,10000,"[%s %s %d %d ]: %s\n",xlog.modulename().data(),datetime.toString("yyyy-MM-dd hh:mm:ss:zzz ").toLatin1().data(),
- xlog.pid(),(int)xlog.logclass(), xlog.logsentence().data());
- // std::cout<<strout<<std::endl;
- }
- bx.append(strout,strnlen(strout,10000));
- }
- }
- else
- {
- std::cout<<"parse error."<<std::endl;
- }
- npos = npos +plogsize[i];
- }
- mMutex.lock();
- if(mba.size() < LOGBUFMAX)mba.append(bx);
- mMutex.unlock();
- // mMutex.lock();
- // if(mba.size() < LOGBUFMAX) mba.append(value);
- // mMutex.unlock();
- }
- #include "sys/statfs.h"
- void ivlogsave::onTimer()
- {
- struct statfs diskInfo;
- statfs(getenv("HOME"), &diskInfo);
- char * strhome = getenv("HOME");
- QString strsavepath;
- strsavepath = strhome;
- strsavepath = strsavepath + "/log";
- QDir dir(strsavepath);
- quint64 freesize = diskInfo.f_bavail * diskInfo.f_bsize;
- quint64 nfreespace = freesize/MB;
- QStringList filters;
- filters << "*.qtar";
- dir.setNameFilters(filters);
- QStringList logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
- // for(int i=0; i<logfiles.size(); i++)
- // givlog->info("ivlog_record","qtar file name: %s",logfiles.at(i).toStdString().data());
- // filters.clear();
- // filters << "*.log";
- // logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
- // for(int i=0; i<logfiles.size(); i++)
- // givlog->info("ivlog_record","log file name: %s",logfiles.at(i).toStdString().data());
- if(nfreespace < 2000) //less than 2000MB,stop record log
- {
- mbCanSave = false;
- return;
- }
- if(nfreespace < 3000 && logfiles.size() > 10)
- {
- givlog->info("ivlog_record","free space is %d", nfreespace);
- if(dir.remove(logfiles.at(0)))
- givlog->info("ivlog_record","remove file: %s",logfiles.at(0).toStdString().data());
- }
- }
- // compress file
- void ivlogsave::fileCompress(QString _filename)
- {
- QString filename_comp = _filename + ".qtar";//compressed file name
- QFile file(_filename);
- if(file.exists()){
- qDebug() << "brefore compressed file size:" << file.size();
- bool ok = file.open(QIODevice::ReadOnly);
- if(ok){
- QByteArray buffer = file.readAll();
- //compress
- buffer = qCompress(buffer,-1);
- qDebug() << "after compressed buffer size:" << buffer.size();
- QFile writeFile(filename_comp);
- ok = writeFile.open(QIODevice::WriteOnly);
- if(ok){
- writeFile.write(buffer);
- qDebug() << "after compressed file size:" << writeFile.size();
- }
- writeFile.close();
- file.remove();
- }
- }
- }
- void ivlogsave::fileUncompress(QString _filename)
- {
- QString filename_uncomp = _filename + ".log";//compressed file name
- QFile file(_filename);
- if(file.exists()){
- qDebug() << "brefore compressed file size:" << file.size();
- bool ok = file.open(QIODevice::ReadOnly);
- if(ok){
- QByteArray buffer = file.readAll();
- QFile writeFile(filename_uncomp);
- ok = writeFile.open(QIODevice::WriteOnly);
- if(ok){
- //uncompress
- buffer = qUncompress(buffer);
- qDebug() << "after uncompressed buffer size:" << buffer.size();
- writeFile.write(buffer);
- qDebug() << "after compressed file size:" << writeFile.size();
- }
- writeFile.close();
- }
- }
- }
- // this function called by ivlog_record::run()
- // compress all the *.log file under $HOME/log/ to *.log.qtar
- void ivlogsave::init_compress(QString _strpath)
- {
- QStringList filters;
- QString strpath;
- QDir dir(_strpath);
- filters << "*.log";
- dir.setNameFilters(filters);
- QStringList logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
- for(int i=0; i<logfiles.size(); i++)
- {
- strpath = _strpath.append("/") + logfiles.at(i);
- givlog->info("ivlog_record","log file name: %s",logfiles.at(i).toStdString().data());
- fileCompress(strpath);
- }
- }
|