Example #1
0
int CLogFile::WriteLog(const char *sFile, const int iLine, const char *sFunc, int i32Level, const char * pszFormat, va_list ap)
{
	FILE * m_pLogFile;

	if (m_sLogFilePath.empty()) return -2; //not initialize
	m_pLogFile = fopen(m_sLogFilePath.c_str(), "a+");
	if (m_pLogFile == NULL)
	{
		return -1;
	}

	struct tm _tm;
	time_t nowtime = time(NULL);
	localtime_r(&nowtime, &_tm);

	char _pstr[20];
	snprintf(_pstr, sizeof(_pstr), "%04d-%02d-%02d %02d:%02d:%02d",
					_tm.tm_year+1900, _tm.tm_mon+1, _tm.tm_mday,
					_tm.tm_hour, _tm.tm_min, _tm.tm_sec);
	_pstr[20] = '\0';

	fprintf(m_pLogFile, "[%d,%d--%s:%d:%s] <%s> [%s]", getppid(), getpid(), sFile, iLine, sFunc, priority_name(i32Level), _pstr);
	vfprintf(m_pLogFile, pszFormat, ap);
	fprintf(m_pLogFile, "\n");
	fclose(m_pLogFile);

	ShiftFiles(10000000, 5);
	return 0;
}
Example #2
0
long InitLogFile(LogFile* pstLogFile, const char* sLogBaseName, long iShiftType, long iMaxLogNum, long iMAX)
{
    /*char sLogFileName[300] = "";*/

    memset(pstLogFile, 0, sizeof(LogFile));
    strncat(pstLogFile->sLogFileName, sLogBaseName, sizeof(pstLogFile->sLogFileName) - 10);
    strcat(pstLogFile->sLogFileName, ".log");

    // if ((pstLogFile->pLogFile = fopen(sLogFileName, "a+")) == NULL) return -1;
    strncpy(pstLogFile->sBaseFileName, sLogBaseName, sizeof(pstLogFile->sBaseFileName) - 1);
    pstLogFile->iShiftType = iShiftType;
    pstLogFile->iMaxLogNum = iMaxLogNum;
    pstLogFile->lMaxSize = iMAX;
    pstLogFile->lMaxCount = iMAX;
    pstLogFile->lLogCount = iMAX;
    time(&pstLogFile->lLastShiftTime);

    return ShiftFiles(pstLogFile);
}
Example #3
0
int Log(LogFile* pstLogFile, int iLogTime, const char* sFormat, ...)
{
    va_list ap;
    struct timeval stLogTv;

    if ((pstLogFile->pLogFile = fopen(pstLogFile->sLogFileName, "a+")) == NULL) return -1;
    va_start(ap, sFormat);
    if (iLogTime == 1) {
        fprintf(pstLogFile->pLogFile, "[%s] ", CurrDateTimeStr());
    }
    else if (iLogTime == 2) {
        gettimeofday(&stLogTv, NULL);
        fprintf(pstLogFile->pLogFile, "[%s.%.6d] ", DateTimeStr(&(stLogTv.tv_sec)), (int)stLogTv.tv_usec);
    }
    vfprintf(pstLogFile->pLogFile, sFormat, ap);
    fprintf(pstLogFile->pLogFile, "\n");
    va_end(ap);
    pstLogFile->lLogCount++;
    fclose(pstLogFile->pLogFile);
    return ShiftFiles(pstLogFile);
}
Example #4
0
int TLib_Log_VWriteLog(char *sLogBaseName, long lMaxLogSize, int iMaxLogNum,const char *sFormat, va_list ap)
{
	FILE  *pstFile;
	char sLogFileName[300];

   	sprintf(sLogFileName,"%s.log", sLogBaseName);
	if ((pstFile = fopen(sLogFileName, "a+")) == NULL)
	{
		printf("[%s]Fail to open log file %s\n",__FUNCTION__,sLogFileName);
		return -1;
	}

	char szTimeStr[256];
	TLib_Tools_GetCurDateTimeStr(szTimeStr);
	
	fprintf(pstFile, "[%s] ", szTimeStr);
	vfprintf(pstFile, sFormat, ap);
	fclose(pstFile);

	return ShiftFiles(sLogBaseName, lMaxLogSize, iMaxLogNum);
}