VirtualBox

vbox的更動 36408 路徑 trunk/src/VBox/Runtime/common


忽略:
時間撮記:
2011-3-24 下午04:25:47 (14 年 以前)
作者:
vboxsync
訊息:

log rotation review and adjustments: Don't delete any excess files if log roation is disabled - we don't know what these files might be. Moved RTLOGGERFILE into log.c. Keep RTLogCreate simple, anyone needing rotation can use RTLogCreateEx[V]. Made RTLogGetDestinations produce the log rotation bits.

檔案:
修改 1 筆資料

圖例:

未更動
新增
刪除
  • trunk/src/VBox/Runtime/common/log/log.cpp

    r36344 r36408  
    7979} RTLOGOUTPUTPREFIXEDARGS, *PRTLOGOUTPUTPREFIXEDARGS;
    8080
     81#ifdef IN_RING3
     82/**
     83 * File logging bits for the logger.
     84 */
     85typedef struct RTLOGGERFILE
     86{
     87    /** Pointer to the function called when starting logging, and when
     88     * ending or starting a new log file as part of history rotation.
     89     * This can be NULL. */
     90    PFNRTLOGPHASE           pfnPhase;
     91    /** Handle to log file (if open). */
     92    RTFILE                  File;
     93    /** Pointer to filename.
     94     * (The memory is allocated in the same block as RTLOGGER.) */
     95    char                   *pszFilename;
     96    /** Log file history settings: number of older files to keep.
     97     * 0 means no history. */
     98    uint32_t                cHistory;
     99    /** Log file history settings: maximum amount of data to put in a file. */
     100    uint64_t                cbHistoryFileMax;
     101    /** Log file history settings: current amount of data in a file. */
     102    uint64_t                cbHistoryFileWritten;
     103    /** Log file history settings: maximum time to use a file (in seconds). */
     104    uint32_t                cSecsHistoryTimeSlot;
     105    /** Log file history settings: in what time slot was the file created. */
     106    uint32_t                uHistoryTimeSlotStart;
     107} RTLOGGERFILE;
     108#endif /* IN_RING3 */
     109
    81110
    82111/*******************************************************************************
     
    96125static DECLCALLBACK(size_t) rtLogOutput(void *pv, const char *pachChars, size_t cbChars);
    97126static DECLCALLBACK(size_t) rtLogOutputPrefixed(void *pv, const char *pachChars, size_t cbChars);
    98 static void rtlogLoggerExV(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args);
     127static void rtlogLoggerExVLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args);
    99128
    100129
     
    128157    PRTLOGGER volatile      pLogger;
    129158} g_aPerThreadLoggers[8] =
    130 {   { NIL_RTNATIVETHREAD, 0, 0},
     159{
     160    { NIL_RTNATIVETHREAD, 0, 0},
    131161    { NIL_RTNATIVETHREAD, 0, 0},
    132162    { NIL_RTNATIVETHREAD, 0, 0},
     
    148178    uint32_t    fFlag;                  /**< The flag value. */
    149179    bool        fInverted;              /**< Inverse meaning? */
    150 }
    151 const s_aLogFlags[] =
     180} const s_aLogFlags[] =
    152181{
    153182    { "disabled",     sizeof("disabled"    ) - 1,   RTLOGFLAGS_DISABLED,            false },
     
    239268}
    240269
    241 
    242270#ifndef IN_RC
    243271# ifdef IN_RING3
     272
    244273/**
    245274 * Logging to file, output callback.
     
    255284    return cbChars;
    256285}
     286
    257287
    258288/**
     
    283313}
    284314
     315
    285316/**
    286317 * Log phase callback function, assumes the lock is already held
     
    298329
    299330    va_start(args, pszFormat);
    300     rtlogLoggerExV(pLogger, 0, ~0, pszFormat, args);
     331    rtlogLoggerExVLocked(pLogger, 0, ~0, pszFormat, args);
    301332    va_end(args);
    302333}
    303334
    304 /**
    305  * Log phase callback function, assumes the lock is not held
     335
     336/**
     337 * Log phase callback function, assumes the lock is not held.
    306338 *
    307339 * @param   pLogger     The logger instance.
     
    320352    va_end(args);
    321353}
     354
    322355# endif /* IN_RING3 */
    323356
    324 /**
    325  * Create a logger instance, comprehensive version.
    326  *
    327  * @returns iprt status code.
    328  *
    329  * @param   ppLogger            Where to store the logger instance.
    330  * @param   fFlags              Logger instance flags, a combination of the RTLOGFLAGS_* values.
    331  * @param   pszGroupSettings    The initial group settings.
    332  * @param   pszEnvVarBase       Base name for the environment variables for this instance.
    333  * @param   cGroups             Number of groups in the array.
    334  * @param   papszGroups         Pointer to array of groups. This must stick around for the life of the
    335  *                              logger instance.
    336  * @param   fDestFlags          The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
    337  * @param   pfnPhase            Callback function for starting logging and for ending or starting a new file for log history rotation.
    338  * @param   cHistory            Number of old log files to keep when performing log history rotation.
    339  * @param   cbHistoryFileMax    Maximum size of log file when performing history rotation. 0=no size limit.
    340  * @param   uHistoryTimeSlotLength     Maximum time interval per log file when performing history rotation, in seconds. 0=no time limit.
    341  * @param   pszErrorMsg         A buffer which is filled with an error message if something fails. May be NULL.
    342  * @param   cchErrorMsg         The size of the error message buffer.
    343  * @param   pszFilenameFmt      Log filename format string. Standard RTStrFormat().
    344  * @param   ...                 Format arguments.
    345  */
    346357RTDECL(int) RTLogCreateExV(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
    347358                           const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
    348359                           uint32_t fDestFlags, PFNRTLOGPHASE pfnPhase, uint32_t cHistory,
    349                            uint64_t cbHistoryFileMax, uint32_t uHistoryTimeSlotLength,
     360                           uint64_t cbHistoryFileMax, uint32_t cSecsHistoryTimeSlot,
    350361                           char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, va_list args)
    351362{
     
    358369     */
    359370    if (    (cGroups && !papszGroups)
    360         ||  !VALID_PTR(ppLogger)
    361        )
     371        ||  !VALID_PTR(ppLogger) )
    362372    {
    363373        AssertMsgFailed(("Invalid parameters!\n"));
     
    368378    if (pszErrorMsg)
    369379        RTStrPrintf(pszErrorMsg, cchErrorMsg, N_("unknown error"));
     380
     381    AssertMsgReturn(cHistory < _1M, ("%#x", cHistory), VERR_OUT_OF_RANGE);
    370382
    371383    /*
     
    397409        else
    398410            pLogger->pFile->cbHistoryFileMax = cbHistoryFileMax;
    399         if (uHistoryTimeSlotLength == 0)
    400             pLogger->pFile->uHistoryTimeSlotLength = UINT32_MAX;
     411        if (cSecsHistoryTimeSlot == 0)
     412            pLogger->pFile->cSecsHistoryTimeSlot = UINT32_MAX;
    401413        else
    402             pLogger->pFile->uHistoryTimeSlotLength = uHistoryTimeSlotLength;
    403 #else /* !IN_RING3 */
     414            pLogger->pFile->cSecsHistoryTimeSlot = cSecsHistoryTimeSlot;
     415#else  /* !IN_RING3 */
    404416        pLogger->pFile       = NULL;
    405417#endif /* !IN_RING3 */
     
    450462            if (pszFilenameFmt)
    451463            {
     464                /** @todo validate the length, fail on overflow. */
    452465                RTStrPrintfV(pLogger->pFile->pszFilename, RTPATH_MAX, pszFilenameFmt, args);
    453466                pLogger->fDestFlags |= RTLOGDEST_FILE;
     
    500513                {
    501514                    rc = rtlogFileOpen(pLogger, pszErrorMsg, cchErrorMsg);
     515
    502516                    /* Rotate in case of appending to a too big log file,
    503                      * otherwise this simply doesn't do anything. */
     517                       otherwise this simply doesn't do anything. */
    504518                    rtlogRotate(pLogger, 0, true /* fFirst */);
    505519                }
     
    509523                    pLogger->pFile->cbHistoryFileWritten = UINT64_MAX;
    510524                    rtlogRotate(pLogger, 0, true /* fFirst */);
     525
    511526                    /* If the file is not open then rotation is not set up. */
    512527                    if (pLogger->pFile->File == NIL_RTFILE)
     
    521536            Assert(VALID_PTR(pLogger->pFile->pfnPhase) || pLogger->pFile->pfnPhase == NULL);
    522537            if (pLogger->pFile->pfnPhase)
    523                 pfnPhase(pLogger, RTLOGPHASE_BEGIN, rtlogPhaseMsgNormal);
     538                pLogger->pFile->pfnPhase(pLogger, RTLOGPHASE_BEGIN, rtlogPhaseMsgNormal);
    524539#endif  /* IN_RING3 */
    525540
     
    570585
    571586
    572 /**
    573  * Create a logger instance.
    574  *
    575  * @returns iprt status code.
    576  *
    577  * @param   ppLogger            Where to store the logger instance.
    578  * @param   fFlags              Logger instance flags, a combination of the RTLOGFLAGS_* values.
    579  * @param   pszGroupSettings    The initial group settings.
    580  * @param   pszEnvVarBase       Base name for the environment variables for this instance.
    581  * @param   cGroups             Number of groups in the array.
    582  * @param   papszGroups         Pointer to array of groups. This must stick around for the life of the
    583  *                              logger instance.
    584  * @param   fDestFlags          The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
    585  * @param   pfnPhase            Callback function for starting logging and for ending or starting a new file for log history rotation.
    586  * @param   cHistory            Number of old log files to keep when performing log history rotation.
    587  * @param   cbHistoryFileMax    Maximum size of log file when performing history rotation. 0=no size limit.
    588  * @param   uHistoryTimeSlotLength     Maximum time interval per log file when performing history rotation, in seconds. 0=no time limit.
    589  * @param   pszFilenameFmt      Log filename format string. Standard RTStrFormat().
    590  * @param   ...                 Format arguments.
    591  */
    592587RTDECL(int) RTLogCreate(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
    593588                        const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
    594                         uint32_t fDestFlags, PFNRTLOGPHASE pfnPhase, uint32_t cHistory,
    595                         uint64_t cbHistoryFileMax, uint32_t uHistoryTimeSlotLength,
    596                         const char *pszFilenameFmt, ...)
     589                        uint32_t fDestFlags, const char *pszFilenameFmt, ...)
    597590{
    598591    va_list args;
     
    600593
    601594    va_start(args, pszFilenameFmt);
    602     rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups, fDestFlags, pfnPhase, cHistory, cbHistoryFileMax, uHistoryTimeSlotLength, NULL, 0, pszFilenameFmt, args);
     595    rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups,
     596                        fDestFlags, NULL /*pfnPhase*/, 0 /*cHistory*/, 0 /*cbHistoryFileMax*/, 0 /*cSecsHistoryTimeSlot*/,
     597                        NULL /*pszErrorMsg*/, 0 /*cchErrorMsg*/, pszFilenameFmt, args);
    603598    va_end(args);
    604599    return rc;
     
    607602
    608603
    609 /**
    610  * Create a logger instance.
    611  *
    612  * @returns iprt status code.
    613  *
    614  * @param   ppLogger            Where to store the logger instance.
    615  * @param   fFlags              Logger instance flags, a combination of the RTLOGFLAGS_* values.
    616  * @param   pszGroupSettings    The initial group settings.
    617  * @param   pszEnvVarBase       Base name for the environment variables for this instance.
    618  * @param   cGroups             Number of groups in the array.
    619  * @param   papszGroups         Pointer to array of groups. This must stick around for the life of the
    620  *                              logger instance.
    621  * @param   fDestFlags          The destination flags. RTLOGDEST_FILE is ORed if pszFilenameFmt specified.
    622  * @param   pfnPhase            Callback function for starting logging and for ending or starting a new file for log history rotation.
    623  * @param   cHistory            Number of old log files to keep when performing log history rotation.
    624  * @param   cbHistoryFileMax    Maximum size of log file when performing history rotation. 0=no size limit.
    625  * @param   uHistoryTimeSlotLength     Maximum time interval per log file when performing history rotation, in seconds. 0=no time limit.
    626  * @param   pszErrorMsg         A buffer which is filled with an error message if something fails. May be NULL.
    627  * @param   cchErrorMsg         The size of the error message buffer.
    628  * @param   pszFilenameFmt      Log filename format string. Standard RTStrFormat().
    629  * @param   ...                 Format arguments.
    630  */
    631604RTDECL(int) RTLogCreateEx(PRTLOGGER *ppLogger, uint32_t fFlags, const char *pszGroupSettings,
    632605                          const char *pszEnvVarBase, unsigned cGroups, const char * const * papszGroups,
    633606                          uint32_t fDestFlags, PFNRTLOGPHASE pfnPhase, uint32_t cHistory,
    634                           uint64_t cbHistoryFileMax, uint32_t uHistoryTimeSlotLength,
     607                          uint64_t cbHistoryFileMax, uint32_t cSecsHistoryTimeSlot,
    635608                          char *pszErrorMsg, size_t cchErrorMsg, const char *pszFilenameFmt, ...)
    636609{
     
    639612
    640613    va_start(args, pszFilenameFmt);
    641     rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups, fDestFlags, pfnPhase, cHistory, cbHistoryFileMax, uHistoryTimeSlotLength, pszErrorMsg, cchErrorMsg, pszFilenameFmt, args);
     614    rc = RTLogCreateExV(ppLogger, fFlags, pszGroupSettings, pszEnvVarBase, cGroups, papszGroups,
     615                        fDestFlags, pfnPhase, cHistory, cbHistoryFileMax, cSecsHistoryTimeSlot,
     616                        pszErrorMsg, cchErrorMsg, pszFilenameFmt, args);
    642617    va_end(args);
    643618    return rc;
     
    688663     * Add end of logging message.
    689664     */
    690     if (pLogger->fDestFlags & RTLOGDEST_FILE && pLogger->pFile->File != NIL_RTFILE)
     665    if (   (pLogger->fDestFlags & RTLOGDEST_FILE)
     666        && pLogger->pFile->File != NIL_RTFILE)
    691667        pLogger->pFile->pfnPhase(pLogger, RTLOGPHASE_END, rtlogPhaseMsgLocked);
    692668
     
    16121588RT_EXPORT_SYMBOL(RTLogSetBuffering);
    16131589
    1614 
    1615 
    16161590#ifndef IN_RC
     1591
    16171592/**
    16181593 * Get the current log flags as a string.
     
    17411716                    if (!pszEnd)
    17421717                        pszEnd = strchr(pszVar, '\0');
    1743 #ifndef IN_RING0
     1718# ifdef IN_RING3
    17441719                    size_t cch = pszEnd - pszVar;
    17451720
     
    17731748                        if (!fNo)
    17741749                        {
    1775                             char szTmp[32];
     1750                            uint32_t    cHistory = 0;
     1751                            char        szTmp[32];
    17761752                            int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszVar, cch);
    17771753                            if (RT_SUCCESS(rc))
    1778                                 rc = RTStrToUInt32Full(szTmp, 0, &pLogger->pFile->cHistory);
    1779                             if (RT_FAILURE(rc))
    1780                                 AssertMsgFailedReturn(("Invalid history value %s (%Rrc)!\n",
    1781                                                        szTmp, rc), rc);
     1754                                rc = RTStrToUInt32Full(szTmp, 0, &cHistory);
     1755                            AssertMsgReturn(RT_SUCCESS(rc) && cHistory < _1M, ("Invalid history value %s (%Rrc)!\n", szTmp, rc), rc);
     1756                            pLogger->pFile->cHistory = cHistory;
    17821757                        }
    17831758                        else
     
    17921767                            if (RT_SUCCESS(rc))
    17931768                                rc = RTStrToUInt64Full(szTmp, 0, &pLogger->pFile->cbHistoryFileMax);
    1794                             if (RT_FAILURE(rc))
    1795                                 AssertMsgFailedReturn(("Invalid history file size value %s (%Rrc)!\n",
    1796                                                        szTmp, rc), rc);
     1769                            AssertMsgRCReturn(rc, ("Invalid history file size value %s (%Rrc)!\n", szTmp, rc), rc);
    17971770                            if (pLogger->pFile->cbHistoryFileMax == 0)
    17981771                                pLogger->pFile->cbHistoryFileMax = UINT64_MAX;
     
    18081781                            int rc = RTStrCopyEx(szTmp, sizeof(szTmp), pszVar, cch);
    18091782                            if (RT_SUCCESS(rc))
    1810                                 rc = RTStrToUInt32Full(szTmp, 0, &pLogger->pFile->uHistoryTimeSlotLength);
    1811                             if (RT_FAILURE(rc))
    1812                                 AssertMsgFailedReturn(("Invalid history timespan value %s (%Rrc)!\n",
    1813                                                        szTmp, rc), rc);
    1814                             if (pLogger->pFile->uHistoryTimeSlotLength == 0)
    1815                                 pLogger->pFile->uHistoryTimeSlotLength = UINT32_MAX;
     1783                                rc = RTStrToUInt32Full(szTmp, 0, &pLogger->pFile->cSecsHistoryTimeSlot);
     1784                            AssertMsgRCReturn(rc, ("Invalid history time slot value %s (%Rrc)!\n", szTmp, rc), rc);
     1785                            if (pLogger->pFile->cSecsHistoryTimeSlot == 0)
     1786                                pLogger->pFile->cSecsHistoryTimeSlot = UINT32_MAX;
    18161787                        }
    18171788                        else
    1818                             pLogger->pFile->uHistoryTimeSlotLength = UINT32_MAX;
     1789                            pLogger->pFile->cSecsHistoryTimeSlot = UINT32_MAX;
    18191790                    }
    18201791                    else
     
    18221793                                               fNo ? "no" : "", s_aLogDst[i].pszInstr),
    18231794                                              VERR_INVALID_PARAMETER);
    1824 #endif
     1795# endif /* IN_RING3 */
    18251796                    pszVar = pszEnd + (*pszEnd != '\0');
    18261797                }
     
    18601831    unsigned    i;
    18611832
    1862     Assert(cchBuf);
     1833    AssertReturn(cchBuf, VERR_INVALID_PARAMETER);
     1834    *pszBuf = '\0';
    18631835
    18641836    /*
     
    18691841        pLogger = RTLogDefaultInstance();
    18701842        if (!pLogger)
    1871         {
    1872             *pszBuf = '\0';
    18731843            return VINF_SUCCESS;
    1874         }
    1875     }
    1876 #define APPEND_PSZ(psz,cch) do { memcpy(pszBuf, (psz), (cch)); pszBuf += (cch); cchBuf -= (cch); } while (0)
    1877 #define APPEND_SZ(sz)       APPEND_PSZ(sz, sizeof(sz) - 1)
    1878 #define APPEND_CH(ch)       do { *pszBuf++ = (ch); cchBuf--; } while (0)
     1844    }
    18791845
    18801846    /*
     
    18851851        if (s_aLogDst[i].fFlag & fDestFlags)
    18861852        {
    1887             size_t cchInstr = s_aLogDst[i].cchInstr;
    1888             if (cchInstr + fNotFirst + 1 > cchBuf)
     1853            if (fNotFirst)
    18891854            {
    1890                 rc = VERR_BUFFER_OVERFLOW;
    1891                 break;
     1855                rc = RTStrCopyP(&pszBuf, &cchBuf, " ");
     1856                if (RT_FAILURE(rc))
     1857                    return rc;
    18921858            }
    1893             if (fNotFirst)
    1894                 APPEND_CH(' ');
     1859            rc = RTStrCopyP(&pszBuf, &cchBuf, s_aLogDst[i].pszInstr);
     1860            if (RT_FAILURE(rc))
     1861                return rc;
    18951862            fNotFirst = true;
    1896             APPEND_PSZ(s_aLogDst[i].pszInstr, cchInstr);
    18971863        }
    18981864
     
    19021868     */
    19031869    if (    (fDestFlags & RTLOGDEST_FILE)
    1904         &&  VALID_PTR(pLogger->pFile->pszFilename)
    1905         &&  RT_SUCCESS(rc))
    1906     {
    1907         size_t cchFilename = strlen(pLogger->pFile->pszFilename);
    1908         if (cchFilename + sizeof("file=") - 1 + fNotFirst + 1 <= cchBuf)
    1909         {
    1910             if (fNotFirst)
    1911                 APPEND_SZ(" file=");
    1912             else
    1913                 APPEND_SZ("file=");
    1914             APPEND_PSZ(pLogger->pFile->pszFilename, cchFilename);
    1915         }
    1916         else
    1917             rc = VERR_BUFFER_OVERFLOW;
    1918     }
    1919 #endif
    1920 
    1921 #undef APPEND_PSZ
    1922 #undef APPEND_SZ
    1923 #undef APPEND_CH
    1924 
    1925     *pszBuf = '\0';
    1926     return rc;
     1870        &&  VALID_PTR(pLogger->pFile->pszFilename))
     1871    {
     1872        rc = RTStrCopyP(&pszBuf, &cchBuf, fNotFirst ? " file=" : "file=");
     1873        if (RT_FAILURE(rc))
     1874            return rc;
     1875        rc = RTStrCopyP(&pszBuf, &cchBuf, pLogger->pFile->pszFilename);
     1876        if (RT_FAILURE(rc))
     1877            return rc;
     1878        fNotFirst = true;
     1879    }
     1880
     1881    if (fDestFlags & RTLOGDEST_FILE)
     1882    {
     1883        char szNum[32];
     1884        if (pLogger->pFile->cHistory)
     1885        {
     1886            RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? "history=%u" : " history=%u", pLogger->pFile->cHistory);
     1887            rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
     1888            if (RT_FAILURE(rc))
     1889                return rc;
     1890        }
     1891        if (pLogger->pFile->cbHistoryFileMax != UINT64_MAX)
     1892        {
     1893            RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? "histsize=%llu" : " histsize=%llu", pLogger->pFile->cbHistoryFileMax);
     1894            rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
     1895            if (RT_FAILURE(rc))
     1896                return rc;
     1897        }
     1898        if (pLogger->pFile->cSecsHistoryTimeSlot != UINT32_MAX)
     1899        {
     1900            RTStrPrintf(szNum, sizeof(szNum), fNotFirst ? "histtime=%llu" : " histtime=%llu", pLogger->pFile->cSecsHistoryTimeSlot);
     1901            rc = RTStrCopyP(&pszBuf, &cchBuf, szNum);
     1902            if (RT_FAILURE(rc))
     1903                return rc;
     1904        }
     1905    }
     1906#endif /* IN_RING3 */
     1907
     1908    return VINF_SUCCESS;
    19271909}
    19281910RT_EXPORT_SYMBOL(RTLogGetDestinations);
     1911
    19291912#endif /* !IN_RC */
    1930 
    19311913
    19321914/**
     
    22292211     * Call worker.
    22302212     */
    2231     rtlogLoggerExV(pLogger, fFlags, iGroup, pszFormat, args);
     2213    rtlogLoggerExVLocked(pLogger, fFlags, iGroup, pszFormat, args);
    22322214
    22332215    /*
     
    23992381RT_EXPORT_SYMBOL(RTLogPrintfV);
    24002382
    2401 
    24022383#ifdef IN_RING3
     2384
    24032385/**
    24042386 * Opens/creates the log file.
    24052387 *
    2406  * @param   pLogger     The logger instance to update. NULL is not allowed!
    2407  * @param   pszErrorMsg A buffer which is filled with an error message if something fails. May be NULL.
    2408  * @param   cchErrorMsg The size of the error message buffer.
     2388 * @param   pLogger         The logger instance to update. NULL is not allowed!
     2389 * @param   pszErrorMsg     A buffer which is filled with an error message if
     2390 *                          something fails.  May be NULL.
     2391 * @param   cchErrorMsg     The size of the error message buffer.
    24092392 */
    24102393static int rtlogFileOpen(PRTLOGGER pLogger, char *pszErrorMsg, size_t cchErrorMsg)
     
    24172400    if (pLogger->fFlags & RTLOGFLAGS_WRITE_THROUGH)
    24182401        fOpen |= RTFILE_O_WRITE_THROUGH;
     2402
    24192403    int rc = RTFileOpen(&pLogger->pFile->File, pLogger->pFile->pszFilename, fOpen);
    24202404    if (RT_FAILURE(rc))
     
    24402424/**
    24412425 * Closes, rotates and opens the log files if necessary.
    2442  * Used by the rtlogFlush() function.
     2426 *
     2427 * Used by the rtlogFlush() function as well as RTLogCreateExV.
    24432428 *
    24442429 * @param   pLogger     The logger instance to update. NULL is not allowed!
    24452430 * @param   uTimeSlit   Current time slot (for tikme based rotation).
    2446  * @param   fFirst      Flag whether this is the beginning of logging.
     2431 * @param   fFirst      Flag whether this is the beginning of logging, i.e.
     2432 *                      called from RTLogCreateExV.  Prevents pfnPhase from
     2433 *                      being called.
    24472434 */
    24482435static void rtlogRotate(PRTLOGGER pLogger, uint32_t uTimeSlot, bool fFirst)
     
    24572444        return;
    24582445
    2459     /* Save "disabled" log flag and make sure logging is disabled.
     2446    /*
     2447     * Save "disabled" log flag and make sure logging is disabled.
    24602448     * The logging in the functions called during log file history
    2461      * rotation would cause severe trouble otherwise. */
    2462     uint32_t fOFlags = pLogger->fFlags;
     2449     * rotation would cause severe trouble otherwise.
     2450     */
     2451    uint32_t const fSavedFlags = pLogger->fFlags;
    24632452    pLogger->fFlags |= RTLOGFLAGS_DISABLED;
    24642453
    2465     /* Disable log rotation temporarily, otherwise with extreme settings and
    2466      * chatty phase logging we could run into endless rotation. */
    2467     uint32_t cOHistory = pLogger->pFile->cHistory;
     2454    /*
     2455     * Disable log rotation temporarily, otherwise with extreme settings and
     2456     * chatty phase logging we could run into endless rotation.
     2457     */
     2458    uint32_t const cSavedHistory = pLogger->pFile->cHistory;
    24682459    pLogger->pFile->cHistory = 0;
    24692460
    2470     /* Close the old log file. */
     2461    /*
     2462     * Close the old log file.
     2463     */
    24712464    if (pLogger->pFile->File != NIL_RTFILE)
    24722465    {
     
    24852478    }
    24862479
    2487     /* Rotate the log files. */
    2488     if (cOHistory)
    2489     {
    2490         for (uint32_t i = cOHistory - 1; i + 1 > 0; i--)
     2480    if (cSavedHistory)
     2481    {
     2482        /*
     2483         * Rotate the log files.
     2484         */
     2485        for (uint32_t i = cSavedHistory - 1; i + 1 > 0; i--)
    24912486        {
    24922487            char szOldName[RTPATH_MAX];
     
    25012496                RTFileDelete(szNewName);
    25022497        }
    2503     }
    2504     /* Delete excess log files. */
    2505     for (uint32_t i = cOHistory + 1; i++; )
    2506     {
    2507         char szExcessName[RTPATH_MAX];
    2508         RTStrPrintf(szExcessName, sizeof(szExcessName), "%s.%u", pLogger->pFile->pszFilename, i);
    2509         int rc = RTFileDelete(szExcessName);
    2510         if (RT_FAILURE(rc))
    2511             break;
    2512     }
    2513 
    2514     /* Update logger state and create new log file. */
     2498
     2499        /*
     2500         * Delete excess log files.
     2501         */
     2502        for (uint32_t i = cSavedHistory + 1; ; i++)
     2503        {
     2504            char szExcessName[RTPATH_MAX];
     2505            RTStrPrintf(szExcessName, sizeof(szExcessName), "%s.%u", pLogger->pFile->pszFilename, i);
     2506            int rc = RTFileDelete(szExcessName);
     2507            if (RT_FAILURE(rc))
     2508                break;
     2509        }
     2510    }
     2511
     2512    /*
     2513     * Update logger state and create new log file.
     2514     */
    25152515    pLogger->pFile->cbHistoryFileWritten = 0;
    25162516    pLogger->pFile->uHistoryTimeSlotStart = uTimeSlot;
    25172517    rtlogFileOpen(pLogger, NULL, 0);
    25182518
    2519     /* Use the callback to generate some initial log contents, but only if this
    2520      * is a rotation with a fully set up logger. Leave the other case to the
    2521      * RTLogCreateExV function. */
     2519    /*
     2520     * Use the callback to generate some initial log contents, but only if this
     2521     * is a rotation with a fully set up logger.  Leave the other case to the
     2522     * RTLogCreateExV function.
     2523     */
    25222524    if (pLogger->pFile->pfnPhase && !fFirst)
    25232525    {
    2524         uint32_t fODestFlags = pLogger->fDestFlags;
     2526        uint32_t const fSavedDestFlags = pLogger->fDestFlags;
    25252527        pLogger->fDestFlags &= RTLOGDEST_FILE;
    25262528        pLogger->pFile->pfnPhase(pLogger, RTLOGPHASE_POSTROTATE, rtlogPhaseMsgLocked);
    2527         pLogger->fDestFlags = fODestFlags;
    2528     }
    2529 
    2530     /* Restore log rotation to the previous value. */
    2531     pLogger->pFile->cHistory = cOHistory;
    2532 
    2533     /* Restore the log flags to the previous value. */
    2534     pLogger->fFlags = fOFlags;
    2535 }
     2529        pLogger->fDestFlags = fSavedDestFlags;
     2530    }
     2531
     2532    /* Restore saved values. */
     2533    pLogger->pFile->cHistory = cSavedHistory;
     2534    pLogger->fFlags          = fSavedFlags;
     2535}
     2536
    25362537#endif /* IN_RING3 */
    2537 
    25382538
    25392539/**
     
    25882588    pLogger->offScratch = 0;
    25892589
    2590 # ifdef IN_RING3
    2591     /* Rotate the log file if configured. Must be done after everything is
     2590#ifdef IN_RING3
     2591    /*
     2592     * Rotate the log file if configured.  Must be done after everything is
    25922593     * flushed, since this will also use logging/flushing to write the header
    2593      * and footer messages. */
    2594     if ((pLogger->fDestFlags & RTLOGDEST_FILE) && pLogger->pFile->cHistory)
    2595     {
     2594     * and footer messages.
     2595     */
     2596    if (   (pLogger->fDestFlags & RTLOGDEST_FILE)
     2597        && pLogger->pFile->cHistory)
    25962598        rtlogRotate(pLogger,
    2597                     RTTimeProgramSecTS() / pLogger->pFile->uHistoryTimeSlotLength,
     2599                    RTTimeProgramSecTS() / pLogger->pFile->cSecsHistoryTimeSlot,
    25982600                    false /* fFirst */);
    2599     }
    26002601#endif
    26012602}
     
    30663067}
    30673068
     3069
    30683070/**
    30693071 * Write to a logger instance (worker function).
     
    30803082 * @param   args        Format arguments.
    30813083 */
    3082 static void rtlogLoggerExV(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
     3084static void rtlogLoggerExVLocked(PRTLOGGER pLogger, unsigned fFlags, unsigned iGroup, const char *pszFormat, va_list args)
    30833085{
    30843086    /*
     
    30893091        RTLOGOUTPUTPREFIXEDARGS OutputArgs;
    30903092        OutputArgs.pLogger = pLogger;
    3091         OutputArgs.iGroup = iGroup;
    3092         OutputArgs.fFlags = fFlags;
     3093        OutputArgs.iGroup  = iGroup;
     3094        OutputArgs.fFlags  = fFlags;
    30933095        RTLogFormatV(rtLogOutputPrefixed, &OutputArgs, pszFormat, args);
    30943096    }
注意: 瀏覽 TracChangeset 來幫助您使用更動檢視器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette