Exemplo n.º 1
0
BOOL KAIManager::LogAIRuntimeStat()
{
    BOOL        bResult         = false;
    time_t      nTime           = g_pSO3World->m_nCurrentTime;
    FILE*       pNpcStatFile    = NULL;
    FILE*       pActionStatFile = NULL;
    char        szFileName[_MAX_PATH];
    struct tm   tmNow; 
    std::map<DWORD, KAI_RUNTIME_STATISTICS_NODE>::iterator  it;
    std::map<DWORD, KAI_RUNTIME_STATISTICS_NODE>::iterator  itEnd;

    localtime_r(&nTime, &tmNow);

    if (!pNpcStatFile)
    {
        snprintf(
            szFileName, sizeof(szFileName), 
            "AIRuntimeStat/%d%2.2d%2.2d-%2.2d%2.2d%2.2d_Npc.tab",
            tmNow.tm_year + 1900,
            tmNow.tm_mon + 1,
            tmNow.tm_mday,
            tmNow.tm_hour,
            tmNow.tm_min,
            tmNow.tm_sec
        );
        szFileName[sizeof(szFileName) - 1] = '\0';

        KG_mkdir("AIRuntimeStat");

        pNpcStatFile = fopen(szFileName, "w");
        KG_PROCESS_ERROR(pNpcStatFile);

        fprintf(pNpcStatFile, "NpcTemp\tTime\tCount\t\n");
    }

    for (it = m_NpcAIRunTimeStatisticsMap.begin(), itEnd = m_NpcAIRunTimeStatisticsMap.end(); it != itEnd; ++it)
        fprintf(pNpcStatFile, "%u\t%llu\t%u\n", it->first, it->second.ullTime, it->second.uCount);

    if (!pActionStatFile)
    {
        snprintf(
            szFileName, sizeof(szFileName), 
            "AIRuntimeStat/%d%2.2d%2.2d-%2.2d%2.2d%2.2d_Action.tab",
            tmNow.tm_year + 1900,
            tmNow.tm_mon + 1,
            tmNow.tm_mday,
            tmNow.tm_hour,
            tmNow.tm_min,
            tmNow.tm_sec
        );
        szFileName[sizeof(szFileName) - 1] = '\0';

        KG_mkdir("AIRuntimeStat");

        pActionStatFile = fopen(szFileName, "w");
        KG_PROCESS_ERROR(pActionStatFile);

        fprintf(pActionStatFile, "Action\tTime\tCount\t\n");
    }

    for (int i = 0; i < sizeof(m_ActionRunTimeStatisticsArray) / sizeof(m_ActionRunTimeStatisticsArray[0]); i++)
        fprintf(pActionStatFile, "%d\t%llu\t%u\n", i, m_ActionRunTimeStatisticsArray[i].ullTime, m_ActionRunTimeStatisticsArray[i].uCount);

    bResult = true;
Exit0:
    if (pNpcStatFile)
    {
        fclose(pNpcStatFile);
        pNpcStatFile = NULL;
    }

    if (pActionStatFile)
    {
        fclose(pActionStatFile);
        pActionStatFile = NULL;
    }

    return bResult;
}
Exemplo n.º 2
0
static int _ChangeLogFile()
{
    int nResult  = false;
    int nRetCode = false;
    time_t tmtNow = 0;

    char szLogFullPath[PATH_MAX];
    char szLogFileName[PATH_MAX];

    nRetCode = KG_mkdir(gs_LogParam.szPath);
    if ((nRetCode == -1) && (errno != EEXIST))
    {
        fprintf(stderr, "KGLog: KG_mkdir(\"%s\") failed = %s\n", gs_LogParam.szPath, strerror(errno));
        goto Exit0;
    }

    nRetCode = KG_mkdir(gs_szLogFileNamePrefix);
    if ((nRetCode == -1) && (errno != EEXIST))
    {
        fprintf(stderr, "KGLog: KG_mkdir(\"%s\") failed = %s\n", gs_szLogFileNamePrefix, strerror(errno));
        goto Exit0;
    }

    if (gs_nLogFile != -1)
    {
        _close(gs_nLogFile);
        gs_nLogFile = -1;
    }

    struct tm tmNow;

    tmtNow = time(NULL);

    localtime_r(&tmtNow, &tmNow);

    nRetCode = _snprintf(
                   szLogFullPath, sizeof(szLogFullPath) - 1,
                   "%s%d_%2.2d_%2.2d/",
                   gs_szLogFileNamePrefix,
                   tmNow.tm_year + 1900,
                   tmNow.tm_mon + 1,
                   tmNow.tm_mday
               );
    ASSERT((nRetCode != -1) && (nRetCode < (sizeof(szLogFullPath) - 1)));
    szLogFullPath[sizeof(szLogFullPath) - 1] = '\0';

    nRetCode = KG_mkdir(szLogFullPath);
    if ((nRetCode == -1) && (errno != EEXIST))
    {
        fprintf(stderr, "KGLog: KG_mkdir(\"%s\") failed = %s\n", szLogFullPath, strerror(errno));
    }

    nRetCode = _snprintf(
                   szLogFileName, sizeof(szLogFileName) - 1,
                   "%s%s_%d_%2.2d_%2.2d_%2.2d_%2.2d_%2.2d.log",
                   szLogFullPath,
                   gs_LogParam.szIdent,
                   tmNow.tm_year + 1900,
                   tmNow.tm_mon + 1,
                   tmNow.tm_mday,
                   tmNow.tm_hour,
                   tmNow.tm_min,
                   tmNow.tm_sec
               );
    ASSERT((nRetCode != -1) && (nRetCode < (sizeof(szLogFileName) - 1)));
    szLogFileName[sizeof(szLogFileName) - 1] = '\0';

    gs_nLogFile = _open(szLogFileName, _KGLOG_OPENFILEFLAG_, _KGLOG_OPENFILEMODE_);
    if (gs_nLogFile == -1)
    {
        fprintf(stderr, "KGLog: open(\"%s\") failed = %s\n", szLogFileName, strerror(errno));
    }
    KGLOG_PROCESS_ERROR(gs_nLogFile != -1);

    nResult = true;
Exit0:
    if (!nResult)
    {
        if (gs_nLogFile != -1)
        {
            _close(gs_nLogFile);
            gs_nLogFile = -1;
        }
    }
    return nResult;
}