void CLogManager::WriteLog(CLogInfo &oLogInfo, bool bSupportNet) { if(m_bHold) { GetLogDate(oLogInfo.sDate); m_oLogBuffer.Push(oLogInfo); } else { CAfcLogAttr& oLogAttr = GetLogAttr(oLogInfo.sModuleName); if( (oLogAttr.nMode & FOCP_LOG_SCREEN) || (oLogAttr.nMode & FOCP_LOG_FILE) ) { char sLogInfo[FOCP_LOG_MAXMSG]; uint32 nLen = GetLogInfo(oLogAttr.nFilter, oLogInfo, sLogInfo); if(oLogAttr.nMode & FOCP_LOG_SCREEN) PrintScreen(oLogInfo.nLevel, sLogInfo, nLen); if(oLogAttr.nMode & FOCP_LOG_FILE) PrintFile(sLogInfo, nLen); } if(bSupportNet && (oLogAttr.nMode & FOCP_LOG_SERVER)) { char sLogInfo[FOCP_LOG_MAXMSG]; uint32 nLen = CreateLogMsg(oLogInfo, sLogInfo); PrintServer(sLogInfo, nLen); } } }
uint32 CLogManager::CreateLogMsg(CLogInfo& oLogInfo, char sLogInfo[FOCP_LOG_MAXMSG]) { char* pShift = sLogInfo; char* pEnd = pShift+FOCP_LOG_MAXMSG; if(oLogInfo.sDate[0] == '\0') GetLogDate(oLogInfo.sDate); //sHost pShift += snprintf(pShift, pEnd-pShift, "%s", m_sHostIp); if(pEnd-pShift)++pShift; //sDate pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sDate); if(pEnd-pShift)++pShift; //nLevel; pShift += snprintf(pShift, pEnd-pShift, "%u", oLogInfo.nLevel); if(pEnd-pShift)++pShift; //sAppName pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sAppName); if(pEnd-pShift)++pShift; //Dmn pShift += snprintf(pShift, pEnd-pShift, "%u", m_nDmn); if(pEnd-pShift)++pShift; //Ain pShift += snprintf(pShift, pEnd-pShift, "%u", m_nAin); if(pEnd-pShift)++pShift; //sModuleName pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sModuleName); if(pEnd-pShift)++pShift; //sFunction pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sFunction); if(pEnd-pShift)++pShift; //sFileName pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sFileName); if(pEnd-pShift)++pShift; //nLine pShift += snprintf(pShift, pEnd-pShift, "%u", oLogInfo.nLine); if(pEnd-pShift)++pShift; //sLogInfo pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sLogInfo); if(pEnd-pShift)++pShift; return FOCP_LOG_MAXMSG - (pEnd-pShift); }
uint32 CLogManager::GetLogInfo(uint32 nFilter, CLogInfo& oLogInfo, char sLogInfo[FOCP_LOG_MAXMSG]) { char* pShift = sLogInfo, * pEnd = pShift+FOCP_LOG_MAXMSG-1; if(oLogInfo.sDate[0] == '\0') GetLogDate(oLogInfo.sDate); pShift[FOCP_LOG_MAXMSG-1] = '\0'; if(nFilter & FOCP_LOG_HOST) pShift += snprintf(pShift, pEnd-pShift, "%s ", m_sHostIp); if(nFilter & FOCP_LOG_DATE) pShift += snprintf(pShift, pEnd-pShift, "%s ", oLogInfo.sDate); pShift += snprintf(pShift, pEnd-pShift, "[%s] ", GetLogLevelName(oLogInfo.nLevel)); if( (nFilter & FOCP_LOG_APPN) || (nFilter & FOCP_LOG_MODU) ) { pShift += snprintf(pShift, pEnd-pShift, "["); if(nFilter & FOCP_LOG_APPN) { pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sAppName); if(nFilter & FOCP_LOG_MODU) pShift += snprintf(pShift, pEnd-pShift, ":"); } if(nFilter & FOCP_LOG_MODU) pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sModuleName); pShift += snprintf(pShift, pEnd-pShift, "] "); } if(nFilter & FOCP_LOG_TASK) { ulong nTid; #ifdef WINDOWS nTid = GetCurrentThreadId(); #else nTid = pthread_self(); #endif pShift += snprintf(pShift, pEnd-pShift, "[tid=%lu] ", nTid); } if( (nFilter & FOCP_LOG_SRCF) || (nFilter & FOCP_LOG_FUNC) ) { pShift += snprintf(pShift, pEnd-pShift, "["); if(nFilter & FOCP_LOG_FUNC) { pShift += snprintf(pShift, pEnd-pShift, "%s", oLogInfo.sFunction); if(nFilter & FOCP_LOG_SRCF) pShift += snprintf(pShift, pEnd-pShift, "@"); } if(nFilter & FOCP_LOG_SRCF) pShift += snprintf(pShift, pEnd-pShift, "%s:%u", oLogInfo.sFileName, oLogInfo.nLine); pShift += snprintf(pShift, pEnd-pShift, "] ->\n "); } else pShift += snprintf(pShift, pEnd-pShift, "-> "); char* pShift2 = oLogInfo.sLogInfo; while(true) { char* pNewLine = strchr(pShift2, '\n'); if(pNewLine) { pNewLine[0] = '\0'; if(*(pNewLine-1) == '\r') *(pNewLine-1) = '\0'; } pShift += snprintf(pShift, pEnd-pShift, "%s\n", pShift2); if(!pNewLine) break; pShift2 = pNewLine + 1; if(!pShift2[0]) break; pShift += snprintf(pShift, pEnd-pShift, " "); } return FOCP_LOG_MAXMSG - (pEnd-pShift) - 1; }
/** * \fn static inline Flux::string CreateLogName(const Flux::string &file, time_t t = time(NULL)) * \brief Returns a filename for the logs * \param file string containing the filename * \param time time to use on the filename * \return the filename that has been generated */ static inline Flux::string CreateLogName(const Flux::string &file, time_t t = time(NULL)) { return "logs/" + file + "." + GetLogDate(t) + "-" + value_cast<Flux::string>(t); }