Old fashion design for PC-104
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1072 lines
26 KiB

4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
4 years ago
4 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. //----------------------------------------------------------------------------
  2. // Laser Guard Ltd.
  3. // Copyright � 2000-2002. All Rights Reserved.
  4. // Version 1.0 29/03/2000
  5. // Last modification: 08/10/2003
  6. // Project Intruder Position Indicator
  7. // SUBSYSTEM: LogFiles targets
  8. // FILE: LogFiles.cpp
  9. // AUTHOR: Chizhevskiy Sergey
  10. // OVERVIEW
  11. // ~~~~~~~~
  12. // Source file for implemenation of class CLogFiles
  13. //----------------------------------------------------------------------------
  14. #include "stdafx.h"
  15. #include "..\LgLib\Version.h"
  16. #include "..\LgLib\LgDebug.h"
  17. #include "..\LgLib\LgPerformanceCounter.h"
  18. #include "LogFiles.h"
  19. #define SECOND2NANO( a ) ( 10000000i64 * (__int64)( a ) )
  20. #define MINUTE2NANO( a ) ( SECOND2NANO( a ) * 60i64 )
  21. #define HOUR2NANO( a ) ( MINUTE2NANO( a ) * 60i64 )
  22. #define DAY2NANO( a ) ( HOUR2NANO( a ) * 24i64 )
  23. static LPTSTR StickFolderNames( LPTSTR pResult, LPCTSTR name1, LPCTSTR name2 )
  24. {
  25. INT len1 = (name1 == NULL ? 0 : _tcslen(name1));
  26. INT len2 = (name2 == NULL ? 0 : _tcslen(name2));
  27. if(len1)
  28. {
  29. if(*(name1 + len1 - 1) == '\\')
  30. {
  31. _tcsncpy(pResult, name1, len1 - 1);
  32. pResult[--len1] = 0;
  33. }
  34. else
  35. _tcsncpy(pResult, name1, len1 + 1);
  36. }
  37. if(len2)
  38. {
  39. if(*name2 == '\\')
  40. _tcsncpy(pResult + len1, name2, len2 + 1);
  41. else
  42. {
  43. _tcsncpy(pResult + len1, "\\", 1);
  44. _tcsncpy(pResult + len1 + 1, name2, len2 + 1);
  45. }
  46. }
  47. return pResult;
  48. }
  49. void CLogFiles::Reset()
  50. {
  51. static volatile BOOL fInside = FALSE;
  52. _TCHAR pDayFolder[MAX_PATH];
  53. _TCHAR pIncidentFolder[MAX_PATH];
  54. m_bReset = FALSE;
  55. if(!fInside)
  56. {
  57. fInside = TRUE;
  58. TCHAR pFolder[MAX_PATH];
  59. SYSTEMTIME st;
  60. ::GetLocalTime( &st );
  61. StickFolderNames(pFolder, m_pProfile->SettingsReport(), NULL);
  62. ::CreateDirectory(pFolder, NULL);
  63. _stprintf(pDayFolder, _T("%02hu_%02hu_%04hu"),
  64. st.wDay, st.wMonth, st.wYear);
  65. StickFolderNames(pFolder, pFolder, pDayFolder);
  66. ::CreateDirectory(pFolder, NULL);
  67. _stprintf(pIncidentFolder, "%02hu_%02hu_%02hu",
  68. st.wHour, st.wMinute, st.wSecond);
  69. StickFolderNames(pFolder, pFolder, pIncidentFolder);
  70. ::CreateDirectory(pFolder, NULL);
  71. StickFolderNames(m_pPathText, pFolder, m_pProfile->SettingsTargetTxt());
  72. StickFolderNames(m_pPathTextEx, pFolder, "ext.log" );
  73. StickFolderNames(m_pPathImage, pFolder, m_pProfile->SettingsTargetBmp());
  74. StickFolderNames(m_pPathLocator, pFolder, m_pProfile->SettingsLocatorBmp());
  75. StickFolderNames(m_pPathPreview, pFolder, m_pProfile->SettingsPreAlarmBmp());
  76. _stprintf(m_pPathText, _T( "%s.txt" ), m_pPathText);
  77. // _stprintf(m_pPathLocator, _T( "%s.bmp" ), m_pPathLocator);
  78. // _stprintf(m_pPathPreview, _T( "%s.bmp" ), m_pPathPreview);
  79. _stprintf(m_pPathLocator, _T( "%s.jpg" ), m_pPathLocator);
  80. _stprintf(m_pPathPreview, _T( "%s.jpg" ), m_pPathPreview);
  81. m_PreviewNumber = 0;
  82. m_ImageNumber = 0;
  83. fInside = FALSE;
  84. }
  85. }
  86. void CLogFiles::StoreText(TARGET_DESCRIPTION& td)
  87. {
  88. volatile static BOOL fInsige = FALSE;
  89. if(!fInsige)
  90. {
  91. CheckFreeSpace();
  92. fInsige = TRUE;
  93. if(!m_bWriteText)
  94. {
  95. fInsige = FALSE;
  96. return;
  97. }
  98. if (!m_bReset)
  99. Reset();
  100. HANDLE hFile = ::CreateFile(m_pPathText, GENERIC_WRITE,
  101. FILE_SHARE_READ, NULL, OPEN_ALWAYS,
  102. FILE_ATTRIBUTE_NORMAL, NULL);
  103. if(hFile != INVALID_HANDLE_VALUE)
  104. {
  105. ::SetFilePointer(hFile, 0UL, 0UL, FILE_END);
  106. TCHAR pBuffer[0x500];
  107. LPCTSTR master = " ";
  108. if(m_bDebugMode)
  109. {
  110. switch(td.master)
  111. {
  112. case 'a':
  113. master = "M ";
  114. break;
  115. case 'b':
  116. master = "S ";
  117. break;
  118. case 'f':
  119. master = "F ";
  120. break;
  121. case 'w':
  122. master = td.device ? "SW" : "MW";
  123. break;
  124. case 'q':
  125. master = "D ";
  126. break;
  127. }
  128. }
  129. // convert to clockwise
  130. double dAngle = m_pProfile->ConvertToUserCoordinate( td.angle );
  131. if(td.master == 'f')
  132. _stprintf(pBuffer, _T( "%02hu/%02hu/%04hu %02hu:%02hu:%02hu DRD\n"),
  133. td.time.wDay, td.time.wMonth, td.time.wYear,
  134. td.time.wHour, td.time.wMinute, td.time.wSecond);
  135. else
  136. if( td.master == 'q' )
  137. {
  138. _stprintf( pBuffer, _T("%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s %3.0f� (dopler)\r\n"),
  139. td.time.wDay, td.time.wMonth, td.time.wYear,
  140. td.time.wHour, td.time.wMinute, td.time.wSecond,
  141. master, dAngle );
  142. }
  143. else
  144. #ifdef DRAW_TARGET_WIDTH
  145. _stprintf(pBuffer, _T( "%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s %3.0f� %3.1f m. (W %ld)\r\n"),
  146. td.time.wDay, td.time.wMonth, td.time.wYear,
  147. td.time.wHour, td.time.wMinute, td.time.wSecond,
  148. master, dAngle, td.range, td.width );
  149. #else
  150. _stprintf(pBuffer, _T( "%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s %3.0f� %3.1f m.\r\n"),
  151. td.time.wDay, td.time.wMonth, td.time.wYear,
  152. td.time.wHour, td.time.wMinute, td.time.wSecond,
  153. master, dAngle, td.range );
  154. #endif
  155. DWORD dwWrite;
  156. ::WriteFile(hFile, pBuffer, _tcslen(pBuffer), &dwWrite, NULL);
  157. ::CloseHandle(hFile);
  158. m_bReset = TRUE;
  159. }
  160. fInsige = FALSE;
  161. }
  162. }
  163. INT CLogFiles::PrepareImageText( TARGET_DESCRIPTION& td, LPCTSTR pTag, LPTSTR pText, INT nMaxText )
  164. {
  165. // convert to clockwise
  166. double dAngle = m_pProfile->ConvertToUserCoordinate( td.angle );
  167. if(!td.master)
  168. return _sntprintf( pText, nMaxText, "%02hu\\%02hu\\%04hu %02hu:%02hu:%02hu PHOTO",
  169. td.time.wDay, td.time.wMonth, td.time.wYear,
  170. td.time.wHour, td.time.wMinute, td.time.wSecond);
  171. else
  172. if(td.master == 'l')
  173. {
  174. return _sntprintf( pText, nMaxText, "%02hu\\%02hu\\%04hu %02hu:%02hu:%02hu BLIND",
  175. td.time.wDay, td.time.wMonth, td.time.wYear,
  176. td.time.wHour, td.time.wMinute, td.time.wSecond );
  177. }
  178. else
  179. if(td.master == 'q')
  180. {
  181. return _sntprintf( pText, nMaxText, "%02hu\\%02hu\\%04hu %02hu:%02hu:%02hu DOPLER %3.1f�",
  182. td.time.wDay, td.time.wMonth, td.time.wYear,
  183. td.time.wHour, td.time.wMinute, td.time.wSecond,
  184. dAngle );
  185. }
  186. else
  187. if(td.master == 'f')
  188. {
  189. return _sntprintf( pText, nMaxText, "%02hu\\%02hu\\%04hu %02hu:%02hu:%02hu DRD %3.1f�",
  190. td.time.wDay, td.time.wMonth, td.time.wYear,
  191. td.time.wHour, td.time.wMinute, td.time.wSecond,
  192. dAngle );
  193. }
  194. else
  195. return _sntprintf( pText, nMaxText, "%02hu\\%02hu\\%04hu %02hu:%02hu:%02hu %s%3.0f� %3.1fm",
  196. td.time.wDay, td.time.wMonth, td.time.wYear,
  197. td.time.wHour, td.time.wMinute, td.time.wSecond,
  198. pTag, dAngle, td.range);
  199. return 0;
  200. }
  201. void CLogFiles::StoreInformation(LPCTSTR pStr, SYSTEMTIME &time)
  202. {
  203. volatile static BOOL fInsige = FALSE;
  204. if(!fInsige)
  205. {
  206. CheckFreeSpace();
  207. fInsige = TRUE;
  208. if(!m_bWriteText)
  209. {
  210. fInsige = FALSE;
  211. return;
  212. }
  213. if(!m_bReset)
  214. Reset();
  215. HANDLE hFile = ::CreateFile(m_pPathText, GENERIC_WRITE,
  216. FILE_SHARE_READ, NULL, OPEN_ALWAYS,
  217. FILE_ATTRIBUTE_NORMAL, NULL);
  218. if(hFile != INVALID_HANDLE_VALUE)
  219. {
  220. _TCHAR pBuffer[MAX_PATH];
  221. DWORD dwWrite;
  222. ::SetFilePointer(hFile, 0UL, 0UL, FILE_END);
  223. INT nBuffer = _sntprintf( pBuffer, MAX_PATH, _T("%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s\r\n"),
  224. time.wDay, time.wMonth, time.wYear,
  225. time.wHour, time.wMinute, time.wSecond, pStr );
  226. if( nBuffer < 0 )
  227. nBuffer = MAX_PATH;
  228. ::WriteFile(hFile, pBuffer, nBuffer, &dwWrite, NULL);
  229. ::CloseHandle(hFile);
  230. m_bReset = TRUE;
  231. }
  232. fInsige = FALSE;
  233. }
  234. }
  235. void CLogFiles::StoreInformationEx(LPCTSTR pStr, SYSTEMTIME &time)
  236. {
  237. volatile static BOOL fInsige = FALSE;
  238. if(!fInsige)
  239. {
  240. CheckFreeSpace();
  241. fInsige = TRUE;
  242. if(!m_bWriteText)
  243. {
  244. fInsige = FALSE;
  245. return;
  246. }
  247. if(!m_bReset)
  248. Reset();
  249. HANDLE hFile = ::CreateFile(m_pPathTextEx, GENERIC_WRITE,
  250. FILE_SHARE_READ, NULL, OPEN_ALWAYS,
  251. FILE_ATTRIBUTE_NORMAL, NULL);
  252. if(hFile != INVALID_HANDLE_VALUE)
  253. {
  254. _TCHAR pBuffer[0x500];
  255. DWORD dwWrite;
  256. ::SetFilePointer(hFile, 0UL, 0UL, FILE_END);
  257. INT nBuffer = _sntprintf( pBuffer, MAX_PATH, _T("%02hu/%02hu/%04hu %02hu:%02hu:%02hu %s\r\n"),
  258. time.wDay, time.wMonth, time.wYear,
  259. time.wHour, time.wMinute, time.wSecond, pStr );
  260. if( nBuffer < 0 )
  261. nBuffer = MAX_PATH;
  262. ::WriteFile(hFile, pBuffer, nBuffer, &dwWrite, NULL);
  263. ::CloseHandle(hFile);
  264. m_bReset = TRUE;
  265. }
  266. fInsige = FALSE;
  267. }
  268. }
  269. void CLogFiles::StoreLocator()
  270. {
  271. volatile static BOOL fInsige = FALSE;
  272. if(!fInsige)
  273. {
  274. CheckFreeSpace();
  275. fInsige = TRUE;
  276. if(!m_bWriteImage)
  277. {
  278. fInsige = FALSE;
  279. return;
  280. }
  281. if(!m_bReset)
  282. Reset();
  283. m_pLocator->StoreImage( m_pPathLocator );
  284. m_bReset = TRUE;
  285. fInsige = FALSE;
  286. }
  287. }
  288. HBITMAP CLogFiles::StorePreview(TARGET_DESCRIPTION& td)
  289. {
  290. volatile static BOOL fInsige = FALSE;
  291. HBITMAP fSuccess = NULL;
  292. if(!fInsige)
  293. {
  294. CheckFreeSpace();
  295. fInsige = TRUE;
  296. if(!m_bWriteImage)
  297. {
  298. fInsige = FALSE;
  299. return NULL;
  300. }
  301. if (!m_bReset )
  302. Reset();
  303. LPBITMAPINFOHEADER pBIH = m_PreviewDIB;
  304. LPBYTE pBytes = m_PreviewDIB;
  305. if( pBytes && pBIH )
  306. {
  307. INT nSubImage = m_PreviewNumber;
  308. // shift to appropriate location
  309. pBytes += nSubImage * 288 * WIDTHBYTES( pBIH->biWidth * pBIH->biBitCount );
  310. TCHAR pImageText[MAX_PATH];
  311. PrepareImageText( td, "", pImageText, MAX_PATH );
  312. if( fSuccess = m_pVideoWnd->StoreImage( pBytes, pImageText, 384, 288, 24 ) )
  313. {
  314. INT nHeight = pBIH->biHeight;
  315. INT nSizeImage = pBIH->biSizeImage;
  316. pBIH->biHeight = ( nSubImage + 1 ) * 288;
  317. pBIH->biSizeImage = pBIH->biHeight * WIDTHBYTES( pBIH->biWidth * pBIH->biBitCount );
  318. m_PreviewJPEG.Create( m_PreviewDIB, m_PreviewDIB, m_pProfile->ReportQualityImage() );
  319. m_PreviewJPEG.Save( m_pPathPreview );
  320. pBIH->biHeight = nHeight;
  321. pBIH->biSizeImage = nSizeImage;
  322. m_PreviewNumber = (m_PreviewNumber + 1) % 15;
  323. m_bReset = TRUE;
  324. }
  325. }
  326. fInsige = FALSE;
  327. } // if(!fInsige)
  328. return fSuccess;
  329. }
  330. /*
  331. HBITMAP CLogFiles::StorePreview(TARGET_DESCRIPTION& td)
  332. {
  333. volatile static BOOL fInsige = FALSE;
  334. HBITMAP fSuccess = NULL;
  335. TCHAR pImageText[MAX_PATH];
  336. if(!fInsige)
  337. {
  338. CheckFreeSpace();
  339. fInsige = TRUE;
  340. if(!m_bWriteImage)
  341. {
  342. fInsige = FALSE;
  343. return NULL;
  344. }
  345. if (!m_bReset )
  346. Reset();
  347. HANDLE hFile = ::CreateFile( m_pPathPreview, GENERIC_WRITE | GENERIC_READ,
  348. FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL );
  349. if(hFile != INVALID_HANDLE_VALUE)
  350. {
  351. BITMAPFILEHEADER bfh;
  352. BITMAPINFOHEADER bih;
  353. DWORD dwRead, dwWrite;
  354. switch(::GetLastError())
  355. {
  356. case ERROR_SUCCESS:
  357. bfh.bfType = (((WORD)'M') << 8) + 'B';
  358. bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ((m_pProfile->ImageBitCount() == 8) ? sizeof(RGBQUAD)*256 : 0);
  359. bfh.bfReserved1 = 0;
  360. bfh.bfReserved2 = 0;
  361. bfh.bfOffBits = bfh.bfSize;
  362. bih = m_lpBmi->bmiHeader;
  363. bih.biBitCount = m_pProfile->ImageBitCount(); // +++
  364. bih.biHeight = 0;
  365. bih.biSizeImage = 0;
  366. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  367. ::WriteFile(hFile, &bfh, sizeof(bfh), &dwWrite, NULL);
  368. ::WriteFile(hFile, &bih, sizeof(BITMAPINFOHEADER), &dwWrite, NULL);
  369. if( m_pProfile->ImageBitCount() == 8 )
  370. ::WriteFile(hFile, m_lpBmi->bmiColors, sizeof(RGBQUAD) * 256, &dwWrite, NULL); // 256 colors
  371. case ERROR_ALREADY_EXISTS:
  372. if( m_pProfile->ImageBitCount() == 8 )
  373. ::SetFilePointer(hFile, sizeof(RGBQUAD) * 256 + sizeof(BITMAPINFOHEADER) +sizeof(BITMAPFILEHEADER) + 384 * 288 * m_PreviewNumber, 0UL, FILE_BEGIN); // 256 colors
  374. else
  375. ::SetFilePointer(hFile, sizeof(BITMAPINFOHEADER) +sizeof(BITMAPFILEHEADER) + 384 * 288 * 3 * m_PreviewNumber, 0UL, FILE_BEGIN); // true color
  376. PrepareImageText( td, "", pImageText, MAX_PATH );
  377. fSuccess = m_pVideoWnd->StoreImage( hFile, pImageText, 384, 288, m_pProfile->ImageBitCount() );
  378. if(fSuccess)
  379. {
  380. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  381. ::ReadFile(hFile, &bfh, sizeof(bfh), &dwRead, NULL);
  382. ::ReadFile(hFile, &bih, sizeof(bih), &dwRead, NULL);
  383. if(bih.biHeight < 288 * 15)
  384. {
  385. bih.biHeight += 288;
  386. if( m_pProfile->ImageBitCount() == 8 )
  387. {
  388. bfh.bfSize += 384 * 288; // 256 colors
  389. bih.biSizeImage += 384*288; // 256 colors
  390. }
  391. else
  392. {
  393. bfh.bfSize += 384 * 288 * 3; // true color
  394. bih.biSizeImage += 384*288 * 3; // true color
  395. }
  396. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  397. ::WriteFile(hFile, &bfh, sizeof(bfh), &dwWrite, NULL);
  398. ::WriteFile(hFile, &bih, sizeof(bih), &dwWrite, NULL);
  399. }
  400. m_PreviewNumber = (m_PreviewNumber + 1) % 15;
  401. m_bReset = TRUE;
  402. }
  403. break;
  404. } // end of switch(::GetLastError())
  405. ::CloseHandle( hFile );
  406. } // end of if(hFile != INVALID_HANDLE_VALUE)
  407. fInsige = FALSE;
  408. } // if(!fInsige)
  409. return fSuccess;
  410. }
  411. */
  412. void CLogFiles::StoreImage( TARGET_DESCRIPTION& td, BOOL bFixed )
  413. {
  414. volatile static BOOL fInsige = FALSE;
  415. if(!fInsige)
  416. {
  417. CheckFreeSpace();
  418. fInsige = TRUE;
  419. if( !m_bWriteImage )
  420. {
  421. fInsige = FALSE;
  422. return;
  423. }
  424. if( !m_bReset )
  425. Reset();
  426. LPBITMAPINFOHEADER pBIH = m_ImageDIB;
  427. LPBYTE pBytes = m_ImageDIB;
  428. if( pBytes && pBIH )
  429. {
  430. INT nSubImage = m_ImageNumber % REPORT_IMAGES_IN_BLOCK;
  431. // shift to appropriate location
  432. pBytes += nSubImage * 288 * WIDTHBYTES( pBIH->biWidth * pBIH->biBitCount );
  433. HBITMAP bCapture = NULL;
  434. if( bFixed )
  435. {
  436. TCHAR pImageText[MAX_PATH];
  437. PrepareImageText( td, "FIX ", pImageText, MAX_PATH );
  438. bCapture = m_pFixedVideoWnd->StoreImage( pBytes, pImageText, 384, 288, 24 );
  439. }
  440. else
  441. {
  442. TCHAR pImageText[MAX_PATH];
  443. PrepareImageText( td, "", pImageText, MAX_PATH );
  444. bCapture = m_pVideoWnd->StoreImage( pBytes, pImageText, 384, 288, 24 );
  445. }
  446. if( bCapture )
  447. {
  448. INT nHeight = pBIH->biHeight;
  449. INT nSizeImage = pBIH->biSizeImage;
  450. pBIH->biHeight = ( nSubImage + 1 ) * 288;
  451. pBIH->biSizeImage = pBIH->biHeight * WIDTHBYTES( pBIH->biWidth * pBIH->biBitCount );
  452. TCHAR pBuffer[MAX_PATH];
  453. _stprintf( pBuffer, _T( "%s%05d.jpg" ), m_pPathImage, m_ImageNumber / REPORT_IMAGES_IN_BLOCK );
  454. m_ImageJPEG.Create( m_ImageDIB, m_ImageDIB, m_pProfile->ReportQualityImage() );
  455. m_ImageJPEG.Save( pBuffer );
  456. pBIH->biHeight = nHeight;
  457. pBIH->biSizeImage = nSizeImage;
  458. ::DeleteObject( bCapture );
  459. m_ImageNumber++;
  460. m_bReset = TRUE;
  461. }
  462. }
  463. fInsige = FALSE;
  464. } // end of if(!fInsige)
  465. }
  466. /*
  467. void CLogFiles::StoreImage( TARGET_DESCRIPTION& td, BOOL bFixed )
  468. {
  469. volatile static BOOL fInsige = FALSE;
  470. _TCHAR pBuffer[MAX_PATH];
  471. if(!fInsige)
  472. {
  473. CheckFreeSpace();
  474. fInsige = TRUE;
  475. if(!m_bWriteImage)
  476. {
  477. fInsige = FALSE;
  478. return;
  479. }
  480. if(!m_bReset)
  481. Reset();
  482. _stprintf(pBuffer, _T( "%s%05d.bmp" ), m_pPathImage, m_ImageNumber / REPORT_IMAGES_IN_BLOCK );
  483. HANDLE hFile = ::CreateFile(pBuffer, GENERIC_WRITE | GENERIC_READ,
  484. FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  485. if( hFile != INVALID_HANDLE_VALUE )
  486. {
  487. BITMAPFILEHEADER bfh;
  488. BITMAPINFOHEADER bih;
  489. HBITMAP hBitmap = NULL;
  490. DWORD dwRead, dwWrite;
  491. switch(::GetLastError())
  492. {
  493. case ERROR_SUCCESS:
  494. bfh.bfType = (((WORD)'M' ) << 8) + 'B';
  495. bfh.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + ((m_pProfile->ImageBitCount() == 8) ? sizeof(RGBQUAD)*256 : 0);
  496. bfh.bfReserved1 = 0;
  497. bfh.bfReserved2 = 0;
  498. bfh.bfOffBits = bfh.bfSize;
  499. bih = m_lpBmi->bmiHeader;
  500. bih.biBitCount = m_pProfile->ImageBitCount(); // +++
  501. bih.biHeight = 0;
  502. bih.biSizeImage = 0;
  503. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  504. ::WriteFile(hFile, &bfh, sizeof(bfh), &dwWrite, NULL);
  505. ::WriteFile(hFile, &bih, sizeof(BITMAPINFOHEADER), &dwWrite, NULL);
  506. if( m_pProfile->ImageBitCount() == 8 )
  507. ::WriteFile(hFile, m_lpBmi->bmiColors, sizeof(RGBQUAD) * 256, &dwWrite, NULL); // 256 colors
  508. case ERROR_ALREADY_EXISTS:
  509. ::SetFilePointer(hFile, 0UL, 0UL, FILE_END);
  510. if( bFixed )
  511. {
  512. TCHAR pImageText[MAX_PATH];
  513. PrepareImageText( td, "FIX ", pImageText, MAX_PATH );
  514. hBitmap = m_pFixedVideoWnd->StoreImage( hFile, pImageText, 384, 288, m_pProfile->ImageBitCount() );
  515. }
  516. else
  517. {
  518. TCHAR pImageText[MAX_PATH];
  519. PrepareImageText( td, "", pImageText, MAX_PATH );
  520. hBitmap = m_pVideoWnd->StoreImage( hFile, pImageText, 384, 288, m_pProfile->ImageBitCount() );
  521. }
  522. if(hBitmap)
  523. {
  524. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  525. ::ReadFile(hFile, &bfh, sizeof(bfh), &dwRead, NULL);
  526. ::ReadFile(hFile, &bih, sizeof(bih), &dwRead, NULL);
  527. bih.biHeight += 288;
  528. if( m_pProfile->ImageBitCount() == 8 )
  529. {
  530. bfh.bfSize += 384 * 288; // 256 colors
  531. bih.biSizeImage += 384*288; // 256 colors
  532. }
  533. else
  534. {
  535. bfh.bfSize += 384 * 288 * 3; // true color
  536. bih.biSizeImage += 384*288 * 3; // true color
  537. }
  538. ::SetFilePointer(hFile, 0UL, 0UL, FILE_BEGIN);
  539. ::WriteFile(hFile, &bfh, sizeof(bfh), &dwWrite, NULL);
  540. ::WriteFile(hFile, &bih, sizeof(bih), &dwWrite, NULL);
  541. ::DeleteObject(hBitmap);
  542. m_ImageNumber++;
  543. m_bReset = TRUE;
  544. }
  545. } // end of switch(::GetLastError())
  546. ::CloseHandle( hFile );
  547. // --------------------- convert to jpeg ----------------------
  548. // prepare bmp name
  549. PCHAR pBmpFileName = pBuffer;
  550. // prepare jpeg name
  551. TCHAR pJpegFileName[MAX_PATH];
  552. strcpy( pJpegFileName, pBmpFileName );
  553. ::PathRemoveExtension( pJpegFileName );
  554. strcat( pJpegFileName, ".JPG" );
  555. // convert to jpeg
  556. if( m_ImageDIB.Load( pBmpFileName, TRUE ) )
  557. {
  558. LPBITMAPINFOHEADER pBIH = m_ImageDIB;
  559. if( pBIH && pBIH->biWidth > 0 && pBIH->biHeight > 0 )
  560. {
  561. CLgImageJPEG jpeg;
  562. if( jpeg.Create( m_ImageDIB, m_ImageDIB ) )
  563. jpeg.Save( pJpegFileName );
  564. }
  565. }
  566. } // end of if(hFile != INVALID_HANDLE_VALUE)
  567. fInsige = FALSE;
  568. } // end of if(!fInsige)
  569. }
  570. */
  571. CLogFiles::CLogFiles()
  572. {
  573. m_lpBmi = NULL;
  574. m_pVideoWnd = NULL;
  575. m_pLocator = 0;
  576. m_pProfile = 0;
  577. m_hEventQuit = 0;
  578. m_hThread = 0;
  579. m_bDebugMode = 0;
  580. m_PreviewNumber = 0;
  581. m_ImageNumber = 0;
  582. m_bReset = 0;
  583. m_bWriteImage = 0;
  584. m_bWriteText = 0;
  585. m_nDays = 0;
  586. m_bReset = FALSE;
  587. m_pVideoWnd = NULL;
  588. m_pLocator = NULL;
  589. m_hEventQuit = NULL;
  590. m_nDays = 1000;
  591. m_hThread= NULL;
  592. m_lpBmi = (LPBITMAPINFO) new BYTE[sizeof(BITMAPINFOHEADER) + sizeof(RGBQUAD) * 256];
  593. ZeroMemory(m_lpBmi, sizeof(BITMAPINFOHEADER));
  594. m_lpBmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  595. m_lpBmi->bmiHeader.biWidth = 384;
  596. m_lpBmi->bmiHeader.biHeight = 288;
  597. m_lpBmi->bmiHeader.biPlanes = 1;
  598. m_lpBmi->bmiHeader.biBitCount = 8;
  599. m_lpBmi->bmiHeader.biCompression = 0;
  600. m_lpBmi->bmiHeader.biSizeImage = m_lpBmi->bmiHeader.biWidth *
  601. m_lpBmi->bmiHeader.biHeight;
  602. for(int i = 0; i < 255; i++)
  603. {
  604. m_lpBmi->bmiColors[i].rgbRed = i;
  605. m_lpBmi->bmiColors[i].rgbGreen = i;
  606. m_lpBmi->bmiColors[i].rgbBlue = i;
  607. m_lpBmi->bmiColors[i].rgbReserved = 0;
  608. }
  609. m_lpBmi->bmiColors[i].rgbRed = 255;
  610. m_lpBmi->bmiColors[i].rgbGreen = 0;
  611. m_lpBmi->bmiColors[i].rgbBlue = 0;
  612. m_lpBmi->bmiColors[i].rgbReserved = 0;
  613. m_PreviewNumber = 0;
  614. m_ImageNumber = 0;
  615. m_bWriteText = TRUE;
  616. m_bWriteImage = TRUE;
  617. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLogFiles::CLogFiles" );
  618. }
  619. CLogFiles::~CLogFiles()
  620. {
  621. ::SetEvent(m_hEventQuit);
  622. if(::WaitForSingleObject(m_hThread, 1000UL) != WAIT_OBJECT_0)
  623. ::TerminateThread(m_hThread, 0); // !!!
  624. ::CloseHandle(m_hThread);
  625. ::CloseHandle(m_hEventQuit);
  626. delete[] m_lpBmi; // Added delete[] 22/04/2001
  627. LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, "CLogFiles::~CLogFiles" );
  628. }
  629. BOOL CLogFiles::Create(CVideoWnd *pVideoWnd, CVideoWnd *pFixedVideoWnd, CLgsRadarWnd *pLocator, CProfile *pProfile)
  630. {
  631. m_pVideoWnd = pVideoWnd;
  632. m_pFixedVideoWnd = pFixedVideoWnd;
  633. m_pLocator = pLocator;
  634. m_pProfile = pProfile;
  635. m_bClockwise = pProfile->LgsDisplayClockwise();
  636. m_hEventQuit = ::CreateEvent(NULL, FALSE, FALSE, NULL);
  637. m_nDays = m_pProfile->ReportHistory();
  638. DWORD dwThreadId;
  639. m_hThread = ::CreateThread(NULL, 0UL, ThreadProcLogFiles, (LPVOID)this, 0UL, &dwThreadId);
  640. if(!m_hThread)
  641. return FALSE;
  642. if( !m_ImageDIB.Create( 384, 288 * REPORT_IMAGES_IN_BLOCK, 24 ) )
  643. return FALSE;
  644. if( !m_PreviewDIB.Create( 384, 288 * 15, 24 ) )
  645. return FALSE;
  646. return TRUE;
  647. }
  648. static void StickNames(LPTSTR &pResult, INT& nSize, LPCTSTR pName)
  649. {
  650. INT nLen = _tcslen(pName);
  651. if(nSize)
  652. {
  653. INT nNewSize = nSize + nLen + 1;
  654. LPTSTR pNewBuffer = new _TCHAR[nNewSize];
  655. memcpy(pNewBuffer, pResult, nSize);
  656. memcpy(pNewBuffer + nSize - 1, pName, nLen); // rewrite last zero
  657. memcpy(pNewBuffer + nSize - 1 + nLen, _T("\0\0"), 2); // add double zero
  658. delete[] pResult;
  659. pResult = pNewBuffer;
  660. nSize = nNewSize;
  661. }
  662. else
  663. {
  664. nSize = nLen + 2;
  665. pResult = new _TCHAR[nSize];
  666. memcpy(pResult, pName, nLen );
  667. memcpy(pResult + nLen, _T("\0\0"), 2); // add double zero
  668. }
  669. }
  670. void CLogFiles::DeleteOldFiles(SYSTEMTIME st)
  671. {
  672. WIN32_FIND_DATA win32FD;
  673. LARGE_INTEGER ft;
  674. LPTSTR pNames = NULL;
  675. _TCHAR pBuffer[MAX_PATH];
  676. INT nNamesCount = 0;
  677. ::SystemTimeToFileTime( &st, (LPFILETIME)&ft );
  678. ft.QuadPart -= m_nDays * 24i64 * 60i64 * 60i64 * 10000000i64;
  679. StickFolderNames(pBuffer, m_pProfile->SettingsReport(), _T("*.*"));
  680. HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
  681. if(hFF != INVALID_HANDLE_VALUE)
  682. do{
  683. if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  684. {
  685. SYSTEMTIME cst = { 0 };
  686. if(_stscanf(win32FD.cFileName, _T("%02hu_%02hu_%04hu"), &cst.wDay, &cst.wMonth, &cst.wYear) == 3)
  687. {
  688. LARGE_INTEGER cft;
  689. ::SystemTimeToFileTime(&cst, (LPFILETIME)&cft);
  690. if(cft.QuadPart < ft.QuadPart)
  691. {
  692. StickFolderNames(pBuffer, m_pProfile->SettingsReport(), win32FD.cFileName );
  693. StickNames(pNames, nNamesCount, pBuffer);
  694. }
  695. }
  696. }
  697. }while(::FindNextFile(hFF, &win32FD));
  698. if(nNamesCount)
  699. {
  700. SHFILEOPSTRUCT sfs = { 0 };
  701. sfs.wFunc = FO_DELETE;
  702. sfs.pFrom = pNames;
  703. sfs.fFlags = FOF_NOCONFIRMATION | FOF_SIMPLEPROGRESS;
  704. sfs.lpszProgressTitle = _T("Deleting old history...");
  705. ::SHFileOperation(&sfs);
  706. delete pNames;
  707. }
  708. ::FindClose(hFF);
  709. }
  710. BOOL CLogFiles::CheckOldFiles(SYSTEMTIME st)
  711. {
  712. WIN32_FIND_DATA win32FD;
  713. LARGE_INTEGER ft;
  714. _TCHAR pBuffer[MAX_PATH];
  715. BOOL fExist = FALSE;
  716. ::SystemTimeToFileTime(&st, (LPFILETIME)&ft);
  717. ft.QuadPart -= m_nDays * 24i64 * 60i64 * 60i64 * 10000000i64;
  718. StickFolderNames(pBuffer, m_pProfile->SettingsReport(), _T("*.*"));
  719. HANDLE hFF = ::FindFirstFile(pBuffer, &win32FD);
  720. if(hFF != INVALID_HANDLE_VALUE)
  721. do{
  722. if(win32FD.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  723. {
  724. SYSTEMTIME cst = { 0 };
  725. if(_stscanf( win32FD.cFileName, _T("%02hu_%02hu_%04hu"), &cst.wDay, &cst.wMonth, &cst.wYear) == 3)
  726. {
  727. LARGE_INTEGER cft;
  728. ::SystemTimeToFileTime(&cst, (LPFILETIME)&cft);
  729. if(cft.QuadPart < ft.QuadPart)
  730. fExist = TRUE;
  731. }
  732. }
  733. }while(::FindNextFile(hFF, &win32FD));
  734. ::FindClose(hFF);
  735. return fExist;
  736. }
  737. DWORD CALLBACK ThreadProcLogFiles(LPVOID lpvParam)
  738. {
  739. __try
  740. {
  741. TIME_ZONE_INFORMATION tzi;
  742. LARGE_INTEGER lt;
  743. SYSTEMTIME st;
  744. CLogFiles* self = (CLogFiles*) lpvParam;
  745. BOOL fNeedCheck = TRUE;
  746. ::GetTimeZoneInformation(&tzi);
  747. HANDLE hTimer = ::CreateWaitableTimer(NULL, FALSE, NULL);
  748. ::GetLocalTime(&st);
  749. st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
  750. ::SystemTimeToFileTime( &st, (LPFILETIME)&lt );
  751. lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
  752. ::SetWaitableTimer(hTimer, &lt, 0UL, NULL, NULL, FALSE);
  753. HANDLE phEvents[] = { self->m_hEventQuit, hTimer };
  754. if(self->CheckOldFiles(st))
  755. if(CMessageBox::MessageBox( self->m_pLocator->GetSafeHwnd(), "Exists old report files, delete them?", "Report", MB_YESNO | MB_ICONQUESTION) == IDYES )
  756. self->DeleteOldFiles(st);
  757. while(TRUE)
  758. {
  759. switch(::WaitForMultipleObjects(2, phEvents, FALSE, INFINITE))
  760. {
  761. case WAIT_OBJECT_0 + 0:
  762. goto THREAD_EXIT;
  763. case WAIT_OBJECT_0 + 1:
  764. ::CancelWaitableTimer(hTimer);
  765. ::GetLocalTime(&st);
  766. self->DeleteOldFiles(st);
  767. st.wHour = st.wMinute = st.wSecond = st.wMilliseconds = 0;
  768. ::SystemTimeToFileTime( &st, (LPFILETIME)&lt );
  769. lt.QuadPart += DAY2NANO( 1 ) + MINUTE2NANO( tzi.Bias );
  770. ::SetWaitableTimer(hTimer, &lt, 0UL, NULL, NULL, FALSE);
  771. break;
  772. default:
  773. break;
  774. }
  775. } // end of while(TRUE)
  776. THREAD_EXIT:
  777. ::CancelWaitableTimer(hTimer);
  778. ::CloseHandle(hTimer);
  779. }
  780. __except(1)
  781. {
  782. MessageBox( NULL, "Error in CApplication::Run", "Error in CApplication::Run", MB_OK );
  783. }
  784. return 0UL;
  785. }
  786. void CLogFiles::Begin()
  787. {
  788. m_bReset = FALSE;
  789. }
  790. void CLogFiles::SetDebugMode(BOOL fMode)
  791. {
  792. m_bDebugMode = fMode;
  793. }
  794. void CLogFiles::CheckFreeSpace()
  795. {
  796. volatile static BOOL bInside = FALSE;
  797. if( !bInside )
  798. {
  799. bInside = TRUE;
  800. if( m_bWriteText || m_bWriteImage )
  801. {
  802. ULARGE_INTEGER uliFreeBytesAvailableToCaller;
  803. ULARGE_INTEGER uliTotalNumberOfBytes;
  804. ULARGE_INTEGER uliTotalNumberOfFreeBytes;
  805. ::GetDiskFreeSpaceEx(m_pProfile->SettingsReport(),
  806. &uliFreeBytesAvailableToCaller,
  807. &uliTotalNumberOfBytes,
  808. &uliTotalNumberOfFreeBytes);
  809. if(50000000i64 < uliFreeBytesAvailableToCaller.QuadPart &&
  810. uliFreeBytesAvailableToCaller.QuadPart < 100000000i64)
  811. {
  812. if(m_bWriteImage)
  813. {
  814. SYSTEMTIME st;
  815. GetLocalTime(&st);
  816. StoreInformation("Disk space exceed (<100Mb), not enough room for images", st);
  817. m_bWriteImage = FALSE;
  818. }
  819. }
  820. if(50000000i64 > uliFreeBytesAvailableToCaller.QuadPart)
  821. {
  822. if(m_bWriteText)
  823. {
  824. SYSTEMTIME st;
  825. GetLocalTime(&st);
  826. if(m_bWriteImage)
  827. {
  828. StoreInformation("Disk space exceed (<50Mb), not enough room for images and text", st);
  829. m_bWriteImage = FALSE;
  830. }
  831. else
  832. StoreInformation( "Disk space exceed (<50Mb), not enough room for text", st );
  833. m_bWriteText = FALSE;
  834. }
  835. }
  836. }
  837. bInside = FALSE;
  838. }
  839. }