bool CLogFile::Log(LPCTSTR pszMsg, int iLen) { if (m_fp == NULL) return false; size_t uWritten; if (m_eFileFormat == Unicode) { // don't use 'fputs' + '_filelength' -- gives poor performance size_t uToWrite = ((iLen == -1) ? _tcslen(pszMsg) : (size_t)iLen)*sizeof(TCHAR); uWritten = fwrite(pszMsg, 1, uToWrite, m_fp); } else { TUnicodeToUTF8<2048> utf8(pszMsg, iLen); uWritten = fwrite((LPCSTR)utf8, 1, utf8.GetLength(), m_fp); } bool bResult = !ferror(m_fp); m_uBytesWritten += uWritten; if (m_uBytesWritten >= m_uMaxFileSize) StartNewLogFile(); else fflush(m_fp); return bResult; }
bool CLogFile::Open() { if (m_fp != NULL) return true; m_fp = _tfsopen(m_strFilePath, _T("a+b"), _SH_DENYWR); if (m_fp != NULL) { m_tStarted = time(NULL); m_uBytesWritten = _filelength(_fileno(m_fp)); if (m_uBytesWritten == 0) { if (m_eFileFormat == Unicode) { // write Unicode byte-order mark 0xFEFF fputwc(0xFEFF, m_fp); } else { ASSERT( m_eFileFormat == Utf8 ); ; // could write UTF-8 header.. } } else if (m_uBytesWritten >= sizeof(WORD)) { // check for Unicode byte-order mark 0xFEFF WORD wBOM; if (fread(&wBOM, sizeof(wBOM), 1, m_fp) == 1) { if (wBOM == 0xFEFF && m_eFileFormat == Unicode) { // log file already in Unicode format (void)fseek(m_fp, 0, SEEK_END); // actually not needed because file is opened in 'Append' mode.. } else if (wBOM != 0xFEFF && m_eFileFormat != Unicode) { // log file already in UTF-8 format (void)fseek(m_fp, 0, SEEK_END); // actually not needed because file is opened in 'Append' mode.. } else { // log file does not have the required format, create a new one (with the req. format) ASSERT( (m_eFileFormat==Unicode && wBOM!=0xFEFF) || (m_eFileFormat==Utf8 && wBOM==0xFEFF) ); ASSERT( !m_bInOpenCall ); if (!m_bInOpenCall) // just for safety { m_bInOpenCall = true; StartNewLogFile(); m_bInOpenCall = false; } } } } } return m_fp != NULL; }
bool CLog::Log(LPCTSTR pszMsg, int iLen) { if (m_fp == NULL) return false; // don't use 'fputs' + '_filelength' -- gives poor performance size_t uToWrite = ((iLen == -1) ? _tcslen(pszMsg) : (size_t)iLen)*sizeof(TCHAR); size_t uWritten = fwrite(pszMsg, 1, uToWrite, m_fp); bool bResult = !ferror(m_fp); m_uBytesWritten += uWritten; if (m_uBytesWritten >= m_uMaxFileSize) StartNewLogFile(); else fflush(m_fp); return bResult; }
bool CLog::Open() { if (m_fp != NULL) return true; m_fp = _tfsopen(m_strFilePath, _T("a+b"), _SH_DENYWR); if (m_fp != NULL) { m_tStarted = time(NULL); m_uBytesWritten = _filelength(fileno(m_fp)); #ifdef _UNICODE if (m_uBytesWritten == 0) { // write Unicode byte-order mark 0xFEFF fputwc(0xFEFF, m_fp); } else if (m_uBytesWritten >= sizeof(WORD)) { // check for Unicode byte-order mark 0xFEFF WORD wBOM; if (fread(&wBOM, sizeof(wBOM), 1, m_fp) == 1) { if (wBOM == 0xFEFF) { // log file already in Unicode format fseek(m_fp, 0, SEEK_END); // actually not needed because file is opened in 'Append' mode.. } else { ASSERT( !m_bInOpenCall ); if (!m_bInOpenCall) // just for safety { m_bInOpenCall = true; StartNewLogFile(); m_bInOpenCall = false; } } } } #endif } return m_fp != NULL; }