LogSerializer.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*##############################################################################
  2. HPCC SYSTEMS software Copyright (C) 2012 HPCC Systems®.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ############################################################################## */
  13. // LogSerializer.cpp: implementation of the CLogSerializer class.
  14. //
  15. //////////////////////////////////////////////////////////////////////
  16. #pragma warning(disable : 4786)
  17. #include "LogSerializer.hpp"
  18. #include "jexcept.hpp"
  19. #include "jfile.hpp"
  20. #include "jlog.hpp"
  21. #define TRACE_INTERVAL 100
  22. //////////////////////////////////////////////////////////////////////
  23. // Construction/Destruction
  24. //////////////////////////////////////////////////////////////////////
  25. CLogSerializer::CLogSerializer()
  26. {
  27. Init();
  28. }
  29. CLogSerializer::CLogSerializer(const char* fileName)
  30. {
  31. m_FilePath.append(fileName);
  32. Init();
  33. }
  34. void CLogSerializer::Init()
  35. {
  36. m_bytesWritten = 0;
  37. m_ItemCount = 0;
  38. m_fileio = 0;
  39. m_file = 0;
  40. }
  41. CLogSerializer::~CLogSerializer()
  42. {
  43. DBGLOG("CLogSerializer::~CLogSerializer()");
  44. Close();
  45. }
  46. void CLogSerializer::Append(const char* GUID, const char* Data)
  47. {
  48. //DBGLOG("Appening GUID %s with data %s",GUID,Data);
  49. CriticalBlock b(crit);
  50. m_ItemCount++;
  51. StringBuffer toWrite,size;
  52. toWrite.appendf("%s\t%s\r\n",GUID,Data);
  53. size.appendf("%d",toWrite.length());
  54. while (size.length() < 8)
  55. size.insert(0,'0');
  56. size.append("\t");
  57. toWrite.insert(0,size.str());
  58. m_bytesWritten += m_fileio->write(m_bytesWritten, toWrite.length(), toWrite.str());
  59. }
  60. void CLogSerializer::Remove(const char* GUID)
  61. {
  62. }
  63. void CLogSerializer::Open(const char*Directory,const char* NewFileName,const char* Prefix)
  64. {
  65. m_FilePath.clear();
  66. m_FilePath.append(Directory);
  67. if (!EnsureDirectory(m_FilePath))
  68. throw MakeStringException(-1,"Unable to create directory at %s.",m_FilePath.str());
  69. m_FilePath.append("/");
  70. m_FileName.clear();
  71. m_FileName.append(Prefix);
  72. m_FileName.append("_");
  73. m_FileName.append(NewFileName);
  74. m_FilePath.append(m_FileName);
  75. DBGLOG("Creating tank file %s", m_FilePath.str());
  76. m_file = createIFile(m_FilePath.str());
  77. m_fileio = m_file->open(IFOcreate);
  78. if (m_fileio == 0)
  79. throw MakeStringException(-1, "Unable to open logging file %s",m_FilePath.str());
  80. else
  81. DBGLOG("Tank file %s successfully created", m_FilePath.str());
  82. }
  83. bool CLogSerializer::EnsureDirectory(StringBuffer& Dir)
  84. {
  85. try{
  86. Owned<IFile> pDirectory = createIFile(Dir.str());
  87. if(pDirectory->exists() == true)
  88. return true;
  89. return pDirectory->createDirectory();
  90. }
  91. catch(IException *ex)
  92. {
  93. ex->Release();
  94. }
  95. return false;
  96. }
  97. void CLogSerializer::Close()
  98. {
  99. if(m_fileio)
  100. {
  101. m_fileio->Release();
  102. m_fileio = 0;
  103. }
  104. if(m_file)
  105. {
  106. m_file->Release();
  107. m_file = 0;
  108. }
  109. }
  110. void CLogSerializer::Rollover(const char* ClosedPrefix)
  111. {
  112. Close();
  113. Owned<IFile> file = createIFile(m_FilePath.str());
  114. if(file.get() && file->exists() == true)
  115. {
  116. StringBuffer newFileName;
  117. GetRolloverFileName(m_FileName,newFileName,ClosedPrefix);
  118. DBGLOG("Rolling over %s to %s", m_FilePath.str(), newFileName.str());
  119. file->rename(newFileName.str());
  120. }
  121. }
  122. void CLogSerializer::SafeRollover(const char*Directory,const char* NewFileName,const char* Prefix, const char* ClosedPrefix)
  123. {
  124. CriticalBlock b(crit);
  125. Rollover(ClosedPrefix);
  126. Init();
  127. Open(Directory, NewFileName, Prefix);
  128. }
  129. StringBuffer& CLogSerializer::GetRolloverFileName(StringBuffer& oldFile, StringBuffer& newfile, const char* newExtension)
  130. {
  131. newfile.append(oldFile);
  132. newfile.replaceString("log",newExtension);
  133. return newfile;
  134. }
  135. void CLogSerializer::Remove()
  136. {
  137. Close();
  138. Owned<IFile> file = createIFile(m_FilePath.str());
  139. if(file.get() && file->exists() == true)
  140. file->remove();
  141. }
  142. __int64 CLogSerializer::WroteBytes()
  143. {
  144. CriticalBlock b(crit);
  145. return m_bytesWritten;
  146. }
  147. void CSendLogSerializer::LoadDataMap(GuidMap& ACKMap,StringArray& MissedLogs)
  148. {
  149. DBGLOG("Loading missed logs, if any");
  150. try{
  151. m_file = createIFile(m_FilePath.str());
  152. m_fileio = m_file->open(IFOread);
  153. if (m_fileio == 0)
  154. throw MakeStringException(-1, "Unable to open logging file %s",m_FilePath.str());
  155. else
  156. DBGLOG("File %s successfully opened", m_FilePath.str());
  157. long finger,bytesRead;
  158. finger = bytesRead = 0;
  159. bool bOk = true;
  160. MemoryBuffer dataSize,data;
  161. StringBuffer GUID,lostlogStr;
  162. int dataLen;
  163. unsigned int total = 0;
  164. unsigned int total_missed = 0;
  165. while(bOk)
  166. {
  167. bytesRead = m_fileio->read(finger,8,dataSize.reserveTruncate(8));
  168. if(bytesRead==0)
  169. break;
  170. finger+=9;
  171. dataLen = atoi(dataSize.toByteArray());
  172. bytesRead = m_fileio->read(finger,dataLen,data.reserveTruncate(dataLen));
  173. if(bytesRead==0)
  174. break;
  175. LoadMap(data,GUID,lostlogStr);
  176. if(total % TRACE_INTERVAL == 0)
  177. {
  178. DBGLOG("Checking log #%u", total);
  179. DBGLOG("{%s}", GUID.str());
  180. }
  181. total++;
  182. int i = MissedLogs.find(lostlogStr.str());
  183. if (!(*(ACKMap[GUID.str()].c_str())) && MissedLogs.find(lostlogStr.str()) == -1)
  184. {
  185. if(total_missed % TRACE_INTERVAL == 0)
  186. {
  187. DBGLOG("Miss #%u", total_missed);
  188. DBGLOG("<%s>", GUID.str());
  189. }
  190. MissedLogs.append(lostlogStr.str());
  191. total_missed++;
  192. }
  193. finger+=dataLen;
  194. data.clear();
  195. dataSize.clear();
  196. GUID.clear();
  197. lostlogStr.clear();
  198. }
  199. DBGLOG("Total logs checked %u, total missed %u", total, total_missed);
  200. }
  201. catch(IException* ex)
  202. {
  203. StringBuffer errorStr;
  204. ex->errorMessage(errorStr);
  205. ERRLOG("Exception caught within CSendLogSerializer::LoadDataMap: %s",errorStr.str());
  206. ex->Release();
  207. }
  208. catch(...){
  209. DBGLOG("Unknown Exception thrown in CSendLogSerializer::LoadDataMap");
  210. }
  211. Close();
  212. }
  213. void CSendLogSerializer::LoadMap(MemoryBuffer& rawdata,StringBuffer& GUID, StringBuffer& line)
  214. {
  215. //StringBuffer line;
  216. line.append(rawdata.length() -1, rawdata.toByteArray());
  217. const char* strLine = line.str();
  218. while(*strLine && *strLine != '\t' && *strLine != '\0')
  219. {
  220. GUID.append(*strLine);
  221. strLine++;
  222. }
  223. }
  224. void SplitRecord(const char* strLine, StringBuffer& GUID, StringBuffer& Cache)
  225. {
  226. if(strLine==NULL || *strLine=='\0')
  227. return;
  228. while(*strLine && *strLine != '\t' && *strLine != '\0')
  229. {
  230. GUID.append(*strLine);
  231. strLine++;
  232. }
  233. strLine++;
  234. Cache.appendf("%s",strLine);
  235. }
  236. void CRecieveLogSerializer::LoadMap(MemoryBuffer& rawdata,GuidMap& GUIDmap, bool printTrace)
  237. {
  238. //buffer chould be in the form of 000000030\t2515777767.12_11_03_08_25_29\r
  239. //we want to extract the GUID only....
  240. StringBuffer line,GUID;
  241. line.append(rawdata.length() -1, rawdata.toByteArray());
  242. const char* strLine = line.str();
  243. while(*strLine && *strLine != '\t' && *strLine != '\0')
  244. {
  245. GUID.append(*strLine);
  246. strLine++;
  247. }
  248. if(printTrace)
  249. DBGLOG("[%s]", GUID.str());
  250. GUIDmap[GUID.str()] = "1";
  251. }
  252. void CRecieveLogSerializer::LoadDataMap(GuidMap& GUIDmap)
  253. {
  254. DBGLOG("Loading ACKMap");
  255. try{
  256. m_file = createIFile(m_FilePath.str());
  257. m_fileio = m_file->open(IFOread);
  258. if (m_fileio == 0)
  259. throw MakeStringException(-1, "Unable to open logging file %s",m_FilePath.str());
  260. else
  261. DBGLOG("File %s successfully opened", m_FilePath.str());
  262. long finger,bytesRead,dataLen;
  263. finger = bytesRead = dataLen = 0;
  264. MemoryBuffer filecontents,dataSize,data;
  265. bool bOk = true;
  266. unsigned int total = 0;
  267. while(bOk)
  268. {
  269. bytesRead = m_fileio->read(finger,8,dataSize.reserveTruncate(8));
  270. if(bytesRead==0)
  271. break;
  272. finger+=9;
  273. dataLen = atoi(dataSize.toByteArray());
  274. bytesRead = m_fileio->read(finger,dataLen,data.reserveTruncate(dataLen));
  275. if(bytesRead==0)
  276. break;
  277. bool printTrace = false;
  278. if(total % TRACE_INTERVAL == 0)
  279. {
  280. DBGLOG("Loading ack #%u", total);
  281. printTrace = true;
  282. }
  283. LoadMap(data,GUIDmap,printTrace);
  284. total++;
  285. finger+=dataLen;
  286. data.clear();
  287. dataSize.clear();
  288. }
  289. DBGLOG("Total acks loaded %u", total);
  290. }
  291. catch(IException* ex)
  292. {
  293. StringBuffer errorStr;
  294. ex->errorMessage(errorStr);
  295. ERRLOG("Exception caught within CRecieveLogSerializer::LoadDataMap: %s",errorStr.str());
  296. ex->Release();
  297. }
  298. catch(...){
  299. DBGLOG("Unknown Exception thrown in CRecieveLogSerializer::LoadDataMap");
  300. }
  301. Close();
  302. }