MOOS 0.2375
|
00001 /* 00002 * Zipper.cpp 00003 * MOOS 00004 * 00005 * Created by pnewman on 21/08/2009. 00006 * Copyright 2009 __MyCompanyName__. All rights reserved. 00007 * 00008 */ 00009 00010 #include "Zipper.h" 00011 #include <iostream> 00012 00013 #ifdef ZLIB_FOUND 00014 #define ZIP_FLUSH_SIZE 2048 00015 #include <zlib.h> 00016 #endif 00017 00018 00019 bool _ZipThreadWorker(void * pParam) 00020 { 00021 CZipper* pMe = (CZipper*) pParam; 00022 return pMe->DoZipLogging(); 00023 } 00024 00025 bool CZipper::Start(const std::string sFileBaseName) 00026 { 00027 m_sFileName = sFileBaseName; 00028 m_Thread.Initialise(_ZipThreadWorker, this); 00029 return m_Thread.Start(); 00030 } 00031 00032 bool CZipper::Stop() 00033 { 00034 return m_Thread.Stop(); 00035 } 00036 00037 bool CZipper::IsRunning() 00038 { 00039 return m_Thread.IsThreadRunning(); 00040 } 00041 00042 bool CZipper::Push(const std::string & sStr) 00043 { 00044 if(sStr.size()!=0) 00045 { 00046 m_Lock.Lock(); 00047 m_ZipBuffer.push_back(sStr); 00048 m_Lock.UnLock(); 00049 } 00050 return true; 00051 } 00052 00053 00054 bool CZipper::DoZipLogging() 00055 { 00056 #ifdef ZLIB_FOUND 00057 00058 std::string sZipFile = m_sFileName+".gz"; 00059 gzFile TheZipFile = gzopen(sZipFile.c_str(),"wb"); 00060 00061 00062 00063 int nTotalWritten = 0; 00064 int nSinceLastFlush = 0; 00065 while(!m_Thread.IsQuitRequested()) 00066 { 00067 MOOSPause(1000); 00068 00069 std::list<std::string > Work; 00070 Work.clear(); 00071 00072 m_Lock.Lock(); 00073 { 00074 Work.splice(Work.begin(),m_ZipBuffer); 00075 } 00076 m_Lock.UnLock(); 00077 00078 00079 std::list<std::string >::iterator q; 00080 00081 for(q = Work.begin();q!=Work.end();q++) 00082 { 00083 int nWritten = gzwrite(TheZipFile, q->c_str(), q->size() ); 00084 00085 if(nWritten<=0) 00086 { 00087 int Error; 00088 gzerror(TheZipFile, &Error); 00089 switch(Error) 00090 { 00091 case Z_ERRNO: 00092 std::cerr<<"Z_ERRNO\n"; 00093 break; 00094 case Z_STREAM_ERROR : 00095 std::cerr<<"Z_STREAM_ERROR\n"; 00096 break; 00097 case Z_BUF_ERROR: 00098 std::cerr<<"Z_BUF_ERROR\n"; 00099 break; 00100 case Z_MEM_ERROR: 00101 std::cerr<<"Z_MEM_ERROR\n"; 00102 break; 00103 } 00104 } 00105 else 00106 { 00107 nTotalWritten+=nWritten; 00108 nSinceLastFlush+=nWritten; 00109 00110 if(nSinceLastFlush>ZIP_FLUSH_SIZE) 00111 { 00112 gzflush(TheZipFile, Z_SYNC_FLUSH); 00113 nSinceLastFlush = 0; 00114 } 00115 00116 } 00117 } 00118 Work.clear(); 00119 00120 00121 00122 00123 } 00124 gzflush(TheZipFile, Z_SYNC_FLUSH); 00125 gzflush(TheZipFile, Z_FINISH); 00126 gzclose(TheZipFile); 00127 MOOSTrace("closed compressed file %s \n",sZipFile.c_str()); 00128 00129 #endif 00130 return true; 00131 00132 } 00133