Ejemplo n.º 1
0
bool isSameDay(time_t t1, time_t t2)
{
	tm tm1 = timeToTm(t1);
	tm tm2 = timeToTm(t2);
	if ( tm1.tm_year == tm2.tm_year
		&& tm1.tm_yday == tm2.tm_yday)
	{
		return true;
	}
	return false;
}
Ejemplo n.º 2
0
bool LogerManager::openLogger(LogData * pLog)
{
	int id = pLog->_id;
	if (id < 0 || id >_lastId)
	{
		showColorText("log4z: openLogger can not open, invalide logger id! \r\n", LOG_LEVEL_FATAL);
		return false;
	}

	LoggerInfo * pLogger = &_loggers[id];
	if (!pLogger->_enable || !pLogger->_outfile || pLog->_level < pLogger->_level)
	{
		return false;
	}

	bool sameday = isSameDay(pLog->_time, pLogger->_curFileCreateTime);
	bool needChageFile = pLogger->_curWriteLen > pLogger->_limitsize * 1024 * 1024;
	if (!sameday || needChageFile || pLogger->_hotChange)
	{
		if (!sameday || pLogger->_hotChange)
		{
			pLogger->_curFileIndex = 0;
		}
		else
		{
			pLogger->_curFileIndex++;
		}
		if (pLogger->_handle.isOpen())
		{
			pLogger->_handle.close();
		}
	}
	if (!pLogger->_handle.isOpen())
	{
		pLogger->_curFileCreateTime = pLog->_time;
		pLogger->_curWriteLen = 0;

		tm t = timeToTm(pLogger->_curFileCreateTime);
		std::string name;
		std::string path;
		_hotLock.lock();
		name = pLogger->_name;
		path = pLogger->_path;
		pLogger->_hotChange = false;
		_hotLock.unLock();
		
		char buf[100] = { 0 };
		if (pLogger->_monthdir)
		{
			sprintf(buf, "%04d_%02d/", t.tm_year + 1900, t.tm_mon + 1);
			path += buf;
		}

		if (!isDirectory(path))
		{
			createRecursionDir(path);
		}

		sprintf(buf, "%s_%04d%02d%02d%02d%02d_%s_%03d.log",
			name.c_str(), t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
			t.tm_hour, t.tm_min, _pid.c_str(), pLogger->_curFileIndex);
		path += buf;
		pLogger->_handle.open(path.c_str(), "ab");
		if (!pLogger->_handle.isOpen())
		{
			pLogger->_outfile = false;
			return false;
		}
		return true;
	}
	return true;
}
Ejemplo n.º 3
0
bool LogerManager::openLogger(LogData * pLog)
{
    int id = pLog->_id;
    if (id < 0 || id >_lastId)
    {
        showColorText("log4z: openLogger can not open, invalide logger id! \r\n", LOG_LEVEL_FATAL);
        return false;
    }

    LoggerInfo * pLogger = &_loggers[id];
    if (!pLogger->_enable || !pLogger->_outfile || pLog->_level < pLogger->_level)
    {
        return false;
    }

    bool sameday = isSameDay(pLog->_time, pLogger->_curFileCreateTime);
    bool needChageFile = pLogger->_curWriteLen > pLogger->_limitsize * 1024 * 1024;
    if (!sameday || needChageFile)
    {
        if (!sameday)
        {
            pLogger->_curFileIndex = 0;
        }
        else
        {
            pLogger->_curFileIndex++;
        }
        if (pLogger->_handle.isOpen())
        {
            pLogger->_handle.close();
        }
    }
    if (!pLogger->_handle.isOpen())
    {
        pLogger->_curFileCreateTime = pLog->_time;
        pLogger->_curWriteLen = 0;

        tm t = timeToTm(pLogger->_curFileCreateTime);
        std::string name;
        std::string path;

        name = pLogger->_name;
        path = pLogger->_path;

        
        char buf[500] = { 0 };
        if (pLogger->_monthdir)
        {
            sprintf(buf, "%04d_%02d/", t.tm_year + 1900, t.tm_mon + 1);
            path += buf;
        }

        if (!isDirectory(path))
        {
            createRecursionDir(path);
        }

        sprintf(buf, "%s_%s_%04d%02d%02d%02d%02d_%s_%03u.log",
            _proName.c_str(), name.c_str(), t.tm_year + 1900, t.tm_mon + 1, t.tm_mday,
            t.tm_hour, t.tm_min, _pid.c_str(), pLogger->_curFileIndex);
        path += buf;
        pLogger->_handle.open(path.c_str(), "ab");
        if (!pLogger->_handle.isOpen())
        {
			sprintf(buf, "log4z: can not open log file %s. \r\n", path.c_str());
            showColorText("!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n", LOG_LEVEL_FATAL);
            showColorText(buf, LOG_LEVEL_FATAL);
            showColorText("!!!!!!!!!!!!!!!!!!!!!!!!!! \r\n", LOG_LEVEL_FATAL);
            pLogger->_outfile = false;
            return false;
        }
		if (pLogger->_logReserveTime > 0 )
		{
			if (pLogger->_historyLogs.size() > LOG4Z_FORCE_RESERVE_FILE_COUNT)
			{
				while (!pLogger->_historyLogs.empty() && pLogger->_historyLogs.front().first < time(NULL) - pLogger->_logReserveTime)
				{
					pLogger->_handle.removeFile(pLogger->_historyLogs.front().second.c_str());
					pLogger->_historyLogs.pop_front();
				}
			}
			pLogger->_historyLogs.push_back(std::make_pair(time(NULL), path));
		}
        return true;
    }
    return true;
}
Ejemplo n.º 4
0
bool LogerManager::pushLog(LoggerId id, int level, const char * log, const char * file, int line)
{
	// discard log
	if (id < 0 || id > _lastId || !_runing || !_loggers[id]._enable)
	{
		return false;
	}

	//filter log
	if (level < _loggers[id]._level)
	{
		return false;
	}

	//create log data
	LogData * pLog = new LogData;
	pLog->_id =id;
	pLog->_level = level;
	
	//append precise time to log
	{
#ifdef WIN32
		FILETIME ft;
		GetSystemTimeAsFileTime(&ft);
		unsigned long long now = ft.dwHighDateTime;
		now <<= 32;
		now |= ft.dwLowDateTime;
		now /=10;
		now -=11644473600000000ULL;
		now /=1000;
		pLog->_time = now/1000;
		pLog->_precise = (unsigned int)(now%1000);
#else
		struct timeval tm;
		gettimeofday(&tm, NULL);
		pLog->_time = tm.tv_sec;
		pLog->_precise = tm.tv_usec/1000;
#endif
	}

	//format log
	{
		tm tt = timeToTm(pLog->_time);
		if (file == NULL || !_loggers[pLog->_id]._fileLine)
		{
#ifdef WIN32
			int ret = _snprintf_s(pLog->_content, LOG4Z_LOG_BUF_SIZE, _TRUNCATE, "%d-%02d-%02d %02d:%02d:%02d.%03d %s %s \r\n",
				tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, pLog->_precise,
				LOG_STRING[pLog->_level], log);
			if (ret == -1)
			{
				ret = LOG4Z_LOG_BUF_SIZE - 1;
			}
			pLog->_contentLen = ret;
#else
			int ret = snprintf(pLog->_content, LOG4Z_LOG_BUF_SIZE, "%d-%02d-%02d %02d:%02d:%02d.%03d %s %s \r\n",
				tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, pLog->_precise,
				LOG_STRING[pLog->_level], log);
			if (ret == -1)
			{
				ret = 0;
			}
			if (ret >= LOG4Z_LOG_BUF_SIZE)
			{
				ret = LOG4Z_LOG_BUF_SIZE-1;
			}

			pLog->_contentLen = ret;
#endif
		}
		else
		{
			const char * pNameBegin = file+strlen(file);
			do 
			{
				if (*pNameBegin == '\\' || *pNameBegin == '/'){ pNameBegin++; break;}
				if (pNameBegin == file){break;}
				pNameBegin--;
			} while (true);
			
			
#ifdef WIN32
			int ret = _snprintf_s(pLog->_content, LOG4Z_LOG_BUF_SIZE, _TRUNCATE, "%d-%02d-%02d %02d:%02d:%02d.%03d %s %s (%s):%d \r\n",
				tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, pLog->_precise,
				LOG_STRING[pLog->_level], log, pNameBegin, line);
			if (ret == -1)
			{
				ret = LOG4Z_LOG_BUF_SIZE - 1;
			}
			pLog->_contentLen = ret;
#else
			int ret = snprintf(pLog->_content, LOG4Z_LOG_BUF_SIZE, "%d-%02d-%02d %02d:%02d:%02d.%03d %s %s (%s):%d \r\n",
				tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, pLog->_precise,
				LOG_STRING[pLog->_level], log, pNameBegin, line);
			if (ret == -1)
			{
				ret = 0;
			}
			if (ret >= LOG4Z_LOG_BUF_SIZE)
			{
				ret = LOG4Z_LOG_BUF_SIZE-1;
			}

			pLog->_contentLen = ret;
#endif
		}
	
		if (pLog->_contentLen >= 2)
		{
			pLog->_content[pLog->_contentLen - 2] = '\r';
			pLog->_content[pLog->_contentLen - 1] = '\n';
		}
	
	}

	if (_loggers[pLog->_id]._display && LOG4Z_ALL_SYNCHRONOUS_OUTPUT)
	{
		showColorText(pLog->_content, pLog->_level);
	}

	if (LOG4Z_ALL_DEBUGOUTPUT_DISPLAY && LOG4Z_ALL_SYNCHRONOUS_OUTPUT)
	{
#ifdef WIN32
		OutputDebugStringA(pLog->_content);
#endif
	}

	if (_loggers[pLog->_id]._outfile && LOG4Z_ALL_SYNCHRONOUS_OUTPUT)
	{
		AutoLock l(_logLock);
		if (openLogger(pLog))
		{
			_loggers[pLog->_id]._handle.write(pLog->_content, pLog->_contentLen);
			closeLogger(pLog->_id);
			_ullStatusTotalWriteFileCount++;
			_ullStatusTotalWriteFileBytes += pLog->_contentLen;
		}
	}

	if (LOG4Z_ALL_SYNCHRONOUS_OUTPUT)
	{
		delete pLog;
		return true;
	}
	
	AutoLock l(_logLock);
	_logs.push_back(pLog);
	_ullStatusTotalPushLog ++;
	return true;
}
Ejemplo n.º 5
0
LogData * LogerManager::makeLogData(LoggerId id, int level)
{
    LogData * pLog = NULL;
    if (true)
    {
        if (!_freeLogDatas.empty())
        {
            AutoLock l(_logLock);
            if (!_freeLogDatas.empty())
            {
                pLog = _freeLogDatas.back();
                _freeLogDatas.pop_back();
            }
        }
        if (pLog == NULL)
        {
            pLog = new LogData();
        }
    }
    //append precise time to log
    if (true)
    {
        pLog->_id = id;
        pLog->_level = level;
        pLog->_type = LDT_GENERAL;
        pLog->_typeval = 0;
        pLog->_contentLen = 0;
#ifdef WIN32
        FILETIME ft;
        GetSystemTimeAsFileTime(&ft);
        unsigned long long now = ft.dwHighDateTime;
        now <<= 32;
        now |= ft.dwLowDateTime;
        now /= 10;
        now -= 11644473600000000ULL;
        now /= 1000;
        pLog->_time = now / 1000;
        pLog->_precise = (unsigned int)(now % 1000);
#else
        struct timeval tm;
        gettimeofday(&tm, NULL);
        pLog->_time = tm.tv_sec;
        pLog->_precise = tm.tv_usec / 1000;
#endif
    }

    //format log
    if (true)
    {
        tm tt = timeToTm(pLog->_time);

        pLog->_contentLen = sprintf(pLog->_content, "%d-%02d-%02d %02d:%02d:%02d.%03u %s ",
            tt.tm_year + 1900, tt.tm_mon + 1, tt.tm_mday, tt.tm_hour, tt.tm_min, tt.tm_sec, pLog->_precise,
            LOG_STRING[pLog->_level]);
        if (pLog->_contentLen < 0)
        {
            pLog->_contentLen = 0;
        }
    }
    return pLog;
}