📄 log.cpp
字号:
/*
Copyright (c) 2008, Intel Corporation.
All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.
* Neither the name of Intel Corporation nor the names of its contributors
may be used to endorse or promote products derived from this software without
specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
OF SUCH DAMAGE.
*/#include "inc/framework/Log.h"//#include "inc/framework/base_LoggerBase.h"#include <stdarg.h>#define NEED_DEBUG 0// Special case where we are supplied a va_list//static CRITICAL_SECTION g_csLog;static CLog *g_pLog = NULL;void LogInit(BOOL bInit){ if(bInit) { // MessageBox(NULL, L"LogInit(TRUE)", NULL, MB_OK); printf("LogInit\n"); //InitializeCriticalSection(&g_csLog); g_pLog = new CLog(); } else { // MessageBox(NULL, L"LogInit(FALSE)", NULL, MB_OK); //DeleteCriticalSection(&g_csLog);// delete g_pLog; }}/*void EnterCriticalSectionLog(){ EnterCriticalSection(&g_csLog);}void LeaveCriticalSectionLog(){ LeaveCriticalSection(&g_csLog);}*/CLog & GetLog(){// return LoggerBase::GetLog(); return *g_pLog; }CLog::CLog(){ /*static int i = 0; TCHAR szMsg[MAX_PATH], szModuleName[MAX_PATH]; memset(szMsg, 0, MAX_PATH * sizeof(TCHAR)); memset(szModuleName, 0, MAX_PATH * sizeof(TCHAR)); GetModuleFileName(NULL, szModuleName, MAX_PATH); wsprintf(szMsg, L"c-tor(%d), %s, this = %x", ++i, szModuleName, this); MessageBox(NULL, szMsg, L"CLog", MB_OK);*/ InitializeCriticalSection(&g_csLog);};CLog::~CLog(){ DeleteCriticalSection(&g_csLog);}void CLog::operator()( const IntelMobileChar* pszFormat, ... ) { /* va_list vaParams; va_start((char*&)vaParams, pszFormat); DoLog( pszFormat, vaParams); va_end((char*&)vaParams); */ va_list vaParams; va_start(vaParams, pszFormat); DoLog( pszFormat, vaParams); va_end(vaParams);};/*void CLog::operator()( const IntelMobileChar* pszFormat, va_list vaParams ){ DoLog( pszFormat, vaParams); };*/void CLog::operator()( const unsigned int logLevel, const IntelMobileChar* pszFormat, ... ){ va_list vaParams; va_start(vaParams, pszFormat); DoLog( pszFormat, vaParams); va_end(vaParams);}; void CLog::operator()( const unsigned int logLevel, const IntelMobileChar* pszFormat, va_list vaParams ){ DoLog( pszFormat, vaParams); }; void CLog::operator()( bool bStderr, const unsigned int logLevel, const IntelMobileChar* pszFormat, ... ){ va_list vaParams; va_start(vaParams, pszFormat); DoLog( pszFormat, vaParams); va_end(vaParams);}; void CLog::operator()(const IntelMobileChar *pszFormat, char *ch, ... ){ va_list vaParams; va_start(vaParams, pszFormat); DoLog( pszFormat, vaParams); va_end(vaParams); if(NEED_DEBUG) cout << pszFormat << endl;}; void CLog::operator()( bool bStderr, const unsigned int logLevel, const IntelMobileChar* pszFormat, va_list vaParams ){ DoLog( pszFormat, vaParams); };//#define INTELMOBILE_DEBUGvoid CLog::DoLog( const IntelMobileChar* pszFormat, va_list vaParams){#ifdef INTELMOBILE_DEBUG //EnterCriticalSectionLog(); EnterCriticalSection(&g_csLog); /* TCHAR szMsg[MAX_PATH]; memset(szMsg, 0, sizeof(TCHAR) * MAX_PATH); _vstprintf(szMsg, pszFormat, vaParams); HANDLE m_hLogFile = CreateFile(L"\\IntelMobileLog.txt", GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if(INVALID_HANDLE_VALUE == m_hLogFile) { // DWORD dwErr = GetLastError(); // wsprintf(szMsg, L"in DoLog() GetLastError() = %d", dwErr); // MessageBox(NULL, szMsg, L"CLog", MB_OK); } else { DWORD dwWritten = 0; SetFilePointer(m_hLogFile, 0, NULL, FILE_END); BOOL bRtn = WriteFile(m_hLogFile, szMsg, wcslen(szMsg) * sizeof(IntelMobileChar), &dwWritten, NULL); SYSTEMTIME st; GetLocalTime(&st); TCHAR szTm[MAX_PATH]; memset(szTm, 0, sizeof(TCHAR) * MAX_PATH); wsprintf(szTm, L" %d:%d:%d\r\n", st.wHour, st.wMinute, st.wSecond); bRtn = WriteFile(m_hLogFile, szTm, wcslen(szTm) * sizeof(IntelMobileChar), &dwWritten, NULL); CloseHandle(m_hLogFile); }*/ time_t tNow = ::time( NULL ); struct tm* ptTimeNow = ::localtime( &tNow ); IntelMobileChar pszTime[1000]; char buf[1000]; IntelMobileChar wbuf[1000]; FILE *m_fp = stdout; // Format the timestamp#ifdef _UNICODE ::wcsftime( pszTime, sizeof(pszTime), L"%m/%d/%y %H:%M:%S", ptTimeNow );#else strftime( pszTime, sizeof(pszTime), "%m/%d/%y %H:%M:%S", ptTimeNow );#endif // Print the timestamp, prefix and level indicator //::fwprintf( m_fp, L"%s %s %s ", pszTime, m_sPrefix.c_str(), GetLevelName( dwLevel ) );#ifdef _UNICODE ::swprintf( wbuf, 1000, L"%ls", pszTime); wcstombs(buf, wbuf, 1000);#else snprintf( buf, 1000, "%s", pszTime);#endif fprintf(m_fp, buf); // Print the variable arguments //::vfwprintf( m_fp, pszMessage, params );#ifdef _UNICODE vswprintf(wbuf, 1000, pszFormat, vaParams); wcstombs(buf, wbuf, 1000);#else vsnprintf(buf, 1000, pszFormat, vaParams);#endif fprintf( m_fp, buf); //vwprintf(pszFormat, vaParams); // Print a trailing newline character if reqired. //::fputwc( L'\n', m_fp ); ::fputc( '\n', m_fp ); // Flush the stream so that the logfile may be monitored ::fflush( m_fp ); //LeaveCriticalSectionLog(); LeaveCriticalSection(&g_csLog);#endif}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -