00001 00007 #include "stdafx.h" 00008 #include "reporter.h" 00009 #include <bzscmn/bzscmn.h> 00010 #include "permdesc.h" 00011 00012 StatusReporter::StatusReporter() 00013 : m_pStatus(NULL) 00014 , m_hLogPipe(INVALID_HANDLE_VALUE) 00015 { 00016 TCHAR tszMappingName[MAX_PATH]; 00017 _sntprintf_s(tszMappingName, __countof(tszMappingName), _TRUNCATE, tszMappingNameFormat, GetCurrentProcessId()); 00018 PermissiveSecurityDescriptor desc; 00019 m_hMapping = CreateFileMapping(INVALID_HANDLE_VALUE, desc, PAGE_READWRITE, 0, sizeof(KdClientStatus), tszMappingName); 00020 bool exists = (GetLastError() == ERROR_ALREADY_EXISTS); 00021 m_pStatus = (KdClientStatus *)MapViewOfFile(m_hMapping, FILE_MAP_ALL_ACCESS, 0, 0, 0); 00022 if ((m_hMapping == INVALID_HANDLE_VALUE) || !m_pStatus) 00023 { 00024 if (m_hMapping != INVALID_HANDLE_VALUE) 00025 CloseHandle(m_hMapping); 00026 m_hMapping = INVALID_HANDLE_VALUE; 00027 memset(&m_UnusedStatus, 0, sizeof(m_UnusedStatus)); 00028 m_pStatus = &m_UnusedStatus; 00029 return; 00030 } 00031 if (!exists) 00032 memset(m_pStatus, 0, sizeof(KdClientStatus)); 00033 00034 _sntprintf_s(tszMappingName, __countof(tszMappingName), _TRUNCATE, tszLogPipeNameFormat, GetCurrentProcessId()); 00035 m_hLogPipe = CreateFile(tszMappingName, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); 00036 } 00037 00038 StatusReporter::~StatusReporter() 00039 { 00040 if (m_hMapping != INVALID_HANDLE_VALUE) 00041 { 00042 UnmapViewOfFile(m_pStatus); 00043 CloseHandle(m_hMapping); 00044 } 00045 if (m_hLogPipe != INVALID_HANDLE_VALUE) 00046 CloseHandle(m_hLogPipe); 00047 } 00048 00049 void StatusReporter::DoLogLine(const TCHAR *pszText) 00050 { 00051 DWORD dwOk; 00052 if (!WriteFile(m_hLogPipe, pszText, (DWORD)_tcslen(pszText) * sizeof(TCHAR), &dwOk, NULL)) 00053 { 00054 if (m_hLogPipe != INVALID_HANDLE_VALUE) 00055 CloseHandle(m_hLogPipe); 00056 TCHAR tszMappingName[MAX_PATH]; 00057 _sntprintf_s(tszMappingName, __countof(tszMappingName), _TRUNCATE, tszLogPipeNameFormat, GetCurrentProcessId()); 00058 m_hLogPipe = CreateFile(tszMappingName, GENERIC_WRITE, FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); 00059 WriteFile(m_hLogPipe, pszText, (DWORD)_tcslen(pszText) * sizeof(TCHAR), &dwOk, NULL); 00060 } 00061 }