Exemplo n.º 1
0
std::string formatTimestamp(long long timestamp)
{
    long long todayTimestamp = time(NULL);

    char buffer[256];
    tm date;
    long long timestamp1000 = timestamp / 1000;
    localtime_s(&date, &timestamp1000);

    const char *formatString = "%Y.%m.%d - %H:%M:%S";

    if (isSameDay(timestamp, todayTimestamp))
    {
        // today!
        formatString = "%H:%M:%S";
    }

    strftime(buffer, 256, formatString, &date);
    return buffer;
}
Exemplo 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;
}
Exemplo 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;
}