int Logger::WriteLog(LogType const type, char const* format, ... ) { if(c_file.is_open()) { try { boost::mutex::scoped_lock lock(c_mutexLog); c_file.exceptions(std::ofstream::eofbit | std::ofstream::failbit | std::ofstream::badbit); char logMessage[256]; va_list args; va_start(args, format); vsprintf(logMessage, format, args); std::string strType = GetLogType(type); std::string strTimestamp = GetTimeStamp(); c_file << strTimestamp << " - " << strType << " - " << __FILE__ << ":" << __LINE__ << " - " << logMessage << std::endl; //std::cout << strTimestamp << " - " << strType << " - " << __FILE__ << ":" << __LINE__ << " - " << logMessage << std::endl; } catch(std::ofstream::failure e) { std::cerr << "Logger::WriteLog Logging to file exception: " << e.what() << std::endl; return FAIL; } catch(...) { std::cout << "Logger::WriteLog Unknown exception" << std::endl; return FAIL; } } else { std::cout << "Logger::WriteLog File is not opened logString = " << format << std::endl; } //std::cout << "Logger::WriteLog Successfully wrote to log file" << std::endl; return SUCCESS; }
/////////////////////////////////////////////////////////////////////////////// // // 函数功能描述:标准日志打印 // 输入:日志级别、类型、源文件名、代码行,格式字串 // 输出:向窗口或文件写日志 // 返回值: // 其它说明: // /////////////////////////////////////////////////////////////////////////////// void CLogTrace::Trace(BYTE cLevel, BYTE cType, const TCHAR * pszSourceFile, DWORD dwLine, LPCTSTR lpszFormat, ...) { if (! IsLogFileEnable()) return; //指定类型级别以下的日志不打印; if ( m_nOutLogLevel < cType ) { return; } //合法性检测 if (!pszSourceFile || !lpszFormat) { OutputDebugString(_T("Error: pszSourceFile or lpszFormat is NULL\r\n")); return; } //读取参数 va_list args; va_start(args, lpszFormat); CLocalLock localLock(&m_lock); //锁 //打入字串 m_szBuffer[0][0] = 0; int nBuf = -1; if(cType == LOGT_COMPRESS) //对打印的日志进行压缩 { TCHAR *tmp = new TCHAR[200000]; memset(tmp,0,200000 * sizeof(TCHAR)); int nBuf = _vsntprintf(tmp,200000-1,lpszFormat,args); CString strSource,strZipData; strSource.Format(_T("%s"),tmp); if (strSource.GetLength() < 100) { strZipData = strSource; } else { ZipEecodeStdString(strSource,strZipData); } _tcsncpy(m_szBuffer[0],strZipData.GetBuffer(strZipData.GetLength()),MAX_LEN_OF_LOG_LINE-128); strZipData.ReleaseBuffer(); delete []tmp; } else { nBuf = _vsntprintf(m_szBuffer[0], MAX_LEN_OF_LOG_LINE - 128, lpszFormat, args); } va_end(args); if (nBuf < 0) //出错 { //有可能是字符串太长 m_szBuffer[0][MAX_LEN_OF_LOG_LINE - 128] = 0; } //取系统日期、时间 TCHAR szData[12], szTime[12]; _tstrdate(szData); _tstrtime(szTime); //类型字串及颜色 CString strType; DWORD dwColor = 0; //获取日志的类型文本及颜色 GetLogType(strType,dwColor,cType); //源文件名缩短处理 TCHAR * pszSourceShort = NULL; TCHAR szSourceShort[MAX_PATH]; WORD wSourceNameLen = lstrlen(pszSourceFile); if (wSourceNameLen > LEN_SHORT_SOURCEFILE_NAME) //太长 { wsprintf(szSourceShort, _T("...%s"), pszSourceFile + (wSourceNameLen - LEN_SHORT_SOURCEFILE_NAME)); pszSourceShort = szSourceShort; } else { pszSourceShort = (TCHAR *)pszSourceFile; } //形成结果字串 nBuf = _sntprintf(m_szBuffer[1], MAX_LEN_OF_LOG_LINE, _T("%s %s [%d]%s: %s at %d in %s\r\n"), szData, szTime, GetCurrentThreadId(), strType.GetBuffer(), m_szBuffer[0], dwLine, pszSourceShort); if (nBuf < 0) //出错 { //有可能是字符串太长 m_szBuffer[1][MAX_LEN_OF_LOG_LINE - 1] = 0; } strType.ReleaseBuffer(); //打印 TraceColor(cLevel, dwColor); }
void CLogDriverFile::set_Type(const string &type) { mType = GetLogType(type); }