ivlogsave.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. #include "ivlogsave.h"
  2. extern int gnlogmin;
  3. iv::Ivlog * givlog;
  4. ivlogsave::ivlogsave()
  5. {
  6. givlog = new iv::Ivlog("ivlog_record");
  7. bool bconnect = QDBusConnection::sessionBus().connect(QString(),"/catarc/adc", "adciv.interface", "ivlog",this,SLOT(onLogMsg(QByteArray)));
  8. QTimer * timer = new QTimer();
  9. connect(timer,SIGNAL(timeout()),this,SLOT(onTimer()));
  10. timer->start(60000);
  11. qDebug("connect is %d",bconnect);
  12. givlog->info("ivlog_record","ivlogsave start");
  13. }
  14. ivlogsave::~ivlogsave()
  15. {
  16. qDebug()<<"ivlog exit";
  17. givlog->info("exit","ivlog_record exit");
  18. }
  19. void ivlogsave::run()
  20. {
  21. char * strhome = getenv("HOME");
  22. QString strsavepath;
  23. strsavepath = strhome;
  24. strsavepath = strsavepath + "/log";
  25. QDir dir(strsavepath);
  26. if(dir.exists())
  27. {
  28. }
  29. else
  30. {
  31. bool ok = dir.mkdir(strsavepath);//只创建一级子目录,即必须保证上级目录存在
  32. if(ok == false)
  33. {
  34. std::cout<< "ivlogsave:run mkdir error. dir path is "<<strsavepath.toLatin1().data()<<std::endl;
  35. return;
  36. }
  37. }
  38. //compress last log file
  39. init_compress(strsavepath);
  40. QDateTime dt = QDateTime::currentDateTime();
  41. // strsavepath = strsavepath + "/" + dt.toString("yyyy-MM-dd-hh-mm-ss-zzz.ilg");
  42. strsavepath = strsavepath + "/" + dt.toString("yyyy-MM-dd-hh-mm-ss-zzz.log");
  43. QFile file;
  44. file.setFileName(strsavepath);
  45. if(!file.open(QIODevice::ReadWrite))
  46. {
  47. std::cout<<"ivlogsave:run Open File Error."<<std::endl;
  48. return;
  49. }
  50. QByteArray ba;
  51. ba.clear();
  52. while(!QThread::isInterruptionRequested())
  53. {
  54. mMutex.lock();
  55. if(mba.size() > 0)
  56. {
  57. ba.append(mba);
  58. mba.clear();
  59. }
  60. mMutex.unlock();
  61. if((ba.size() > 0)&&mbCanSave)
  62. {
  63. file.write(ba);
  64. file.flush();
  65. ba.clear();
  66. }
  67. msleep(100);
  68. }
  69. file.close();
  70. }
  71. void ivlogsave::onLogMsg(QByteArray value)
  72. {
  73. QByteArray bx;
  74. int nsize = value.size();
  75. char * str = new char[nsize];
  76. std::shared_ptr<char> pstr;pstr.reset(str);
  77. memcpy(str,value.data(),nsize);
  78. if(nsize<12)
  79. {
  80. return;
  81. }
  82. char strmark[5];strmark[4] = 0;
  83. memcpy(strmark,str,4);
  84. if(strcmp(strmark,"ilog")!= 0)
  85. {
  86. qDebug("mark error");
  87. }
  88. int ndatasize;
  89. int npos = 4;
  90. memcpy(&ndatasize,str+npos,sizeof(int));npos = npos + sizeof(int);
  91. if(ndatasize != nsize)
  92. {
  93. qDebug("commicate error");
  94. }
  95. int nlognum;
  96. memcpy(&nlognum,str+npos,sizeof(int));npos = npos +sizeof(int);
  97. // qDebug("datasize is %d lognum is %d",ndatasize,nlognum);
  98. if(nlognum < 1)
  99. {
  100. return;
  101. }
  102. int * plogsize = new int[nlognum];
  103. std::shared_ptr<int> pplog;pplog.reset(plogsize);
  104. int i;
  105. for(i=0;i<nlognum;i++)
  106. {
  107. memcpy(&plogsize[i],str + npos,sizeof(int));
  108. npos = npos + sizeof(int);
  109. }
  110. for(i=0;i<nlognum;i++)
  111. {
  112. if((npos + plogsize[i])>ndatasize)
  113. {
  114. std::cout<<"out range."<<std::endl;
  115. return;
  116. }
  117. iv::log::logdata xlog;
  118. if(xlog.ParseFromArray(str+npos,plogsize[i]))
  119. {
  120. if(xlog.logclass()>= gnlogmin)
  121. {
  122. qint64 mstime = xlog.logtime();
  123. QDateTime datetime = QDateTime::fromMSecsSinceEpoch(mstime);
  124. char strout[10000];
  125. if(xlog.has_logtag())
  126. {
  127. snprintf(strout,10000,"[%s %s %d %d (%s) ]: %s\n",xlog.modulename().data(),datetime.toString("yyyy-MM-dd hh:mm:ss:zzz ").toLatin1().data(),
  128. xlog.pid(),(int)xlog.logclass(),xlog.logtag().data(), xlog.logsentence().data());
  129. // std::cout<<strout<<std::endl;
  130. }
  131. else
  132. {
  133. snprintf(strout,10000,"[%s %s %d %d ]: %s\n",xlog.modulename().data(),datetime.toString("yyyy-MM-dd hh:mm:ss:zzz ").toLatin1().data(),
  134. xlog.pid(),(int)xlog.logclass(), xlog.logsentence().data());
  135. // std::cout<<strout<<std::endl;
  136. }
  137. bx.append(strout,strnlen(strout,10000));
  138. }
  139. }
  140. else
  141. {
  142. std::cout<<"parse error."<<std::endl;
  143. }
  144. npos = npos +plogsize[i];
  145. }
  146. mMutex.lock();
  147. if(mba.size() < LOGBUFMAX)mba.append(bx);
  148. mMutex.unlock();
  149. // mMutex.lock();
  150. // if(mba.size() < LOGBUFMAX) mba.append(value);
  151. // mMutex.unlock();
  152. }
  153. #include "sys/statfs.h"
  154. void ivlogsave::onTimer()
  155. {
  156. struct statfs diskInfo;
  157. statfs(getenv("HOME"), &diskInfo);
  158. char * strhome = getenv("HOME");
  159. QString strsavepath;
  160. strsavepath = strhome;
  161. strsavepath = strsavepath + "/log";
  162. QDir dir(strsavepath);
  163. quint64 freesize = diskInfo.f_bavail * diskInfo.f_bsize;
  164. quint64 nfreespace = freesize/MB;
  165. QStringList filters;
  166. filters << "*.qtar";
  167. dir.setNameFilters(filters);
  168. QStringList logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
  169. // for(int i=0; i<logfiles.size(); i++)
  170. // givlog->info("ivlog_record","qtar file name: %s",logfiles.at(i).toStdString().data());
  171. // filters.clear();
  172. // filters << "*.log";
  173. // logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
  174. // for(int i=0; i<logfiles.size(); i++)
  175. // givlog->info("ivlog_record","log file name: %s",logfiles.at(i).toStdString().data());
  176. if(nfreespace < 2000) //less than 2000MB,stop record log
  177. {
  178. mbCanSave = false;
  179. return;
  180. }
  181. if(nfreespace < 3000 && logfiles.size() > 10)
  182. {
  183. givlog->info("ivlog_record","free space is %d", nfreespace);
  184. if(dir.remove(logfiles.at(0)))
  185. givlog->info("ivlog_record","remove file: %s",logfiles.at(0).toStdString().data());
  186. }
  187. }
  188. // compress file
  189. void ivlogsave::fileCompress(QString _filename)
  190. {
  191. QString filename_comp = _filename + ".qtar";//compressed file name
  192. QFile file(_filename);
  193. if(file.exists()){
  194. qDebug() << "brefore compressed file size:" << file.size();
  195. bool ok = file.open(QIODevice::ReadOnly);
  196. if(ok){
  197. QByteArray buffer = file.readAll();
  198. //compress
  199. buffer = qCompress(buffer,-1);
  200. qDebug() << "after compressed buffer size:" << buffer.size();
  201. QFile writeFile(filename_comp);
  202. ok = writeFile.open(QIODevice::WriteOnly);
  203. if(ok){
  204. writeFile.write(buffer);
  205. qDebug() << "after compressed file size:" << writeFile.size();
  206. }
  207. writeFile.close();
  208. file.remove();
  209. }
  210. }
  211. }
  212. void ivlogsave::fileUncompress(QString _filename)
  213. {
  214. QString filename_uncomp = _filename + ".log";//compressed file name
  215. QFile file(_filename);
  216. if(file.exists()){
  217. qDebug() << "brefore compressed file size:" << file.size();
  218. bool ok = file.open(QIODevice::ReadOnly);
  219. if(ok){
  220. QByteArray buffer = file.readAll();
  221. QFile writeFile(filename_uncomp);
  222. ok = writeFile.open(QIODevice::WriteOnly);
  223. if(ok){
  224. //uncompress
  225. buffer = qUncompress(buffer);
  226. qDebug() << "after uncompressed buffer size:" << buffer.size();
  227. writeFile.write(buffer);
  228. qDebug() << "after compressed file size:" << writeFile.size();
  229. }
  230. writeFile.close();
  231. }
  232. }
  233. }
  234. // this function called by ivlog_record::run()
  235. // compress all the *.log file under $HOME/log/ to *.log.qtar
  236. void ivlogsave::init_compress(QString _strpath)
  237. {
  238. QStringList filters;
  239. QString strpath;
  240. QDir dir(_strpath);
  241. filters << "*.log";
  242. dir.setNameFilters(filters);
  243. QStringList logfiles = dir.entryList(filters, QDir::Files, QDir::Name);
  244. for(int i=0; i<logfiles.size(); i++)
  245. {
  246. strpath = _strpath.append("/") + logfiles.at(i);
  247. givlog->info("ivlog_record","log file name: %s",logfiles.at(i).toStdString().data());
  248. fileCompress(strpath);
  249. }
  250. }