VirtualBox

Changeset 36344 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Mar 22, 2011 2:29:37 PM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
70679
Message:

Runtime/log: implement log rotation, adapt all code creating log files and make use of it in the webservice

Location:
trunk/src/VBox/Main
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/ConsoleImpl.cpp

    r36247 r36344  
    53945394    char szError[RTPATH_MAX + 128] = "";
    53955395    int vrc = RTLogCreateEx(&loggerRelease, fFlags, "all",
    5396                             "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
    5397                             RTLOGDEST_FILE, szError, sizeof(szError), logFile.c_str());
     5396                            "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups, RTLOGDEST_FILE,
     5397                            NULL /* pfnBeginEnd */, 0 /* cHistory */, 0 /* cbHistoryFileMax */, 0 /* uHistoryTimeMax */,
     5398                            szError, sizeof(szError), logFile.c_str());
    53985399    if (RT_SUCCESS(vrc))
    53995400    {
  • trunk/src/VBox/Main/src-server/generic/OpenGLTestApp.cpp

    r33806 r36344  
    55
    66/*
    7  * Copyright (C) 2009-2010 Oracle Corporation
     7 * Copyright (C) 2009-2011 Oracle Corporation
    88 *
    99 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    133133
    134134    int vrc = RTLogCreateEx(&loggerRelease, fFlags, "all",
    135                             "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
    136                             enmLogDest, szError, sizeof(szError), pszFilenameFmt, pszFilename, RTTimeMilliTS());
     135                            "VBOX_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups, enmLogDest,
     136                            NULL /* pfnBeginEnd */, 0 /* cHistory */, 0 /* cbHistoryFileMax */, 0 /* uHistoryTimeMax */,
     137                            szError, sizeof(szError), pszFilenameFmt, pszFilename, RTTimeMilliTS());
    137138    if (RT_SUCCESS(vrc))
    138139    {
  • trunk/src/VBox/Main/webservice/vboxweb.cpp

    r36168 r36344  
    111111unsigned int            g_cMaxKeepAlive = 100;          // maximum number of soap requests in one connection
    112112
     113uint32_t                g_cHistory = 10;                // enable log rotation, 10 files
     114uint32_t                g_uHistoryFileTime = RT_SEC_1WEEK; // max 1 week per file
     115uint64_t                g_uHistoryFileSize = 100 * _1M; // max 100MB per file
    113116bool                    g_fVerbose = false;             // be verbose
    114 bool                    g_fStdOutLogging = true;        // log to stdout
    115 PRTSTREAM               g_pStrmLog = NULL;
    116117
    117118#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
     
    177178        { "--pidfile",          'P', RTGETOPT_REQ_STRING },
    178179        { "--logfile",          'F', RTGETOPT_REQ_STRING },
     180        { "--logrotate",        'R', RTGETOPT_REQ_UINT32 },
     181        { "--logsize",          'S', RTGETOPT_REQ_UINT64 },
     182        { "--loginterval",      'I', RTGETOPT_REQ_UINT32 }
    179183    };
    180184
     
    239243            case 'F':
    240244                pcszDescr = "Name of file to write log to (no file).";
     245                break;
     246
     247            case 'R':
     248                pcszDescr = "Number of log files (0 disables log rotation).";
     249                break;
     250
     251            case 'S':
     252                pcszDescr = "Maximum size of a log file to trigger rotationi (bytes).";
     253                break;
     254
     255            case 'I':
     256                pcszDescr = "Maximum time interval to trigger log rotation (seconds).";
    241257                break;
    242258        }
     
    582598    va_end(args);
    583599
    584     if (g_fStdOutLogging || g_pStrmLog)
    585     {
    586         const char *pcszPrefix = "[   ]";
    587         util::AutoReadLock thrLock(g_pThreadsLockHandle COMMA_LOCKVAL_SRC_POS);
    588         ThreadsMap::iterator it = g_mapThreads.find(RTThreadSelf());
    589         if (it != g_mapThreads.end())
    590             pcszPrefix = it->second.c_str();
    591         thrLock.release();
    592 
    593         // make a timestamp
    594         RTTIMESPEC ts;
    595         RTTimeLocalNow(&ts);
    596         RTTIME t;
    597         RTTimeExplode(&t, &ts);
    598 
    599         com::Utf8StrFmt strPrefix("%04d-%02u-%02u %02u:%02u:%02u %s",
    600                                   t.i32Year, t.u8Month, t.u8MonthDay,
    601                                   t.u8Hour, t.u8Minute, t.u8Second,
    602                                   pcszPrefix);
    603 
    604         // synchronize the actual output
    605         util::AutoWriteLock logLock(g_pWebLogLockHandle COMMA_LOCKVAL_SRC_POS);
    606         // terminal
    607         if (g_fStdOutLogging)
    608             RTPrintf("%s %s", strPrefix.c_str(), psz);
    609 
    610         // log file
    611         if (g_pStrmLog)
    612         {
    613             RTStrmPrintf(g_pStrmLog, "%s %s", strPrefix.c_str(), psz);
    614             RTStrmFlush(g_pStrmLog);
    615         }
    616 
    617 #ifdef DEBUG
    618         // debug logger instance
    619         RTLogLoggerEx(LOG_INSTANCE, RTLOGGRPFLAGS_DJ, LOG_GROUP, "%s %s", pcszPrefix, psz);
    620 #endif
    621 
    622         logLock.release();
    623     }
    624 
    625600    LogRel(("%s", psz));
    626601
     
    645620           pcszFaultString ? pcszFaultString : "[no fault string available]",
    646621           (ppcszDetail && *ppcszDetail) ? *ppcszDetail : "no details available");
     622}
     623
     624static void WebLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
     625{
     626    /* some introductory information */
     627    static RTTIMESPEC timeSpec = {0};
     628    char szTmp[256];
     629    if (enmPhase == RTLOGPHASE_BEGIN)
     630        RTTimeNow(&timeSpec);
     631    RTTimeSpecToString(&timeSpec, szTmp, sizeof(szTmp));
     632
     633    switch (enmPhase)
     634    {
     635        case RTLOGPHASE_BEGIN:
     636        {
     637            pfnLog(pLoggerRelease,
     638                   "VirtualBox web service %s r%u %s (%s %s) release log\n"
     639#ifdef VBOX_BLEEDING_EDGE
     640                   "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
     641#endif
     642                   "Log opened %s\n",
     643                   VBOX_VERSION_STRING, RTBldCfgRevision(), VBOX_BUILD_TARGET,
     644                   __DATE__, __TIME__, szTmp);
     645
     646            int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
     647            if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
     648                pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
     649            vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
     650            if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
     651                pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
     652            vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
     653            if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
     654                pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
     655            if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
     656                pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
     657
     658            /* the package type is interesting for Linux distributions */
     659            char szExecName[RTPATH_MAX];
     660            char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
     661            pfnLog(pLoggerRelease,
     662                   "Executable: %s\n"
     663                   "Process ID: %u\n"
     664                   "Package type: %s"
     665#ifdef VBOX_OSE
     666                   " (OSE)"
     667#endif
     668                   "\n",
     669                   pszExecName ? pszExecName : "unknown",
     670                   RTProcSelf(),
     671                   VBOX_PACKAGE_STRING);
     672            break;
     673        }
     674
     675        case RTLOGPHASE_PREROTATE:
     676            pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
     677            break;
     678
     679        case RTLOGPHASE_POSTROTATE:
     680            pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
     681            break;
     682
     683        case RTLOGPHASE_END:
     684            pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
     685            break;
     686
     687        default:
     688            /* nothing */;
     689    }
    647690}
    648691
     
    692735
    693736            // add the socket to the queue and tell worker threads to
    694             // pick up the jobn
     737            // pick up the job
    695738            size_t cItemsOnQ = g_pSoapQ->add(s);
    696739            WebLog("Request %llu on socket %d queued for processing (%d items on Q)\n", i, s, cItemsOnQ);
     
    748791
    749792    int c;
     793    const char *pszLogFile = NULL;
    750794    const char *pszPidFile = NULL;
    751795    RTGETOPTUNION ValueUnion;
     
    781825            case 'F':
    782826            {
    783                 int rc2 = RTStrmOpen(ValueUnion.psz, "a", &g_pStrmLog);
    784                 if (rc2)
    785                     return RTMsgErrorExit(RTEXITCODE_FAILURE, "Cannot open log file \"%s\" for writing: %Rrc", ValueUnion.psz, rc2);
    786 
    787                 WebLog(VBOX_PRODUCT " Webservice Version %s\n"
    788                        "Opened log file \"%s\"\n", VBOX_VERSION_STRING, ValueUnion.psz);
     827                pszLogFile = ValueUnion.psz;
    789828                break;
    790829            }
     830
     831            case 'R':
     832                g_cHistory = ValueUnion.u32;
     833                break;
     834
     835            case 'S':
     836                g_uHistoryFileSize = ValueUnion.u64;
     837                break;
     838
     839            case 'I':
     840                g_uHistoryFileTime = ValueUnion.u32;
     841                break;
    791842
    792843            case 'P':
     
    824875        }
    825876    }
     877
     878    /* create release logger */
     879    PRTLOGGER pLoggerRelease;
     880    static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
     881    RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
     882#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
     883    fFlags |= RTLOGFLAGS_USECRLF;
     884#endif
     885    char szError[RTPATH_MAX + 128] = "";
     886    int vrc = RTLogCreateEx(&pLoggerRelease, fFlags, "all",
     887                            "VBOXWEBSRV_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups, RTLOGDEST_STDOUT,
     888                            WebLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
     889                            szError, sizeof(szError), pszLogFile);
     890    if (RT_SUCCESS(vrc))
     891    {
     892        /* register this logger as the release logger */
     893        RTLogRelSetDefaultInstance(pLoggerRelease);
     894
     895        /* Explicitly flush the log in case of VBOXWEBSRV_RELEASE_LOG=buffered. */
     896        RTLogFlush(pLoggerRelease);
     897    }
     898    else
     899        return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, vrc);
    826900
    827901#if defined(RT_OS_DARWIN) || defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD)
     
    842916            return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
    843917
    844         /* From now on it's a waste of CPU cycles to send logging to stdout. */
    845         g_fStdOutLogging = false;
    846 
    847918        /* create release logger */
    848         PRTLOGGER loggerRelease;
     919        PRTLOGGER pLoggerRelease;
    849920        static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
    850921        RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG;
     
    853924#endif
    854925        char szError[RTPATH_MAX + 128] = "";
    855         int vrc = RTLogCreateEx(&loggerRelease, fFlags, "all",
    856                                 "VBOXWEBSRV_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups,
    857                                 RTLOGDEST_FILE, szError, sizeof(szError), szLogFile);
     926        int vrc = RTLogCreateEx(&pLoggerRelease, fFlags, "all",
     927                                "VBOXWEBSRV_RELEASE_LOG", RT_ELEMENTS(s_apszGroups), s_apszGroups, RTLOGDEST_FILE,
     928                                WebLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
     929                                szError, sizeof(szError), szLogFile);
    858930        if (RT_SUCCESS(vrc))
    859931        {
    860             /* some introductory information */
    861             RTTIMESPEC timeSpec;
    862             char szTmp[256];
    863             RTTimeSpecToString(RTTimeNow(&timeSpec), szTmp, sizeof(szTmp));
    864             RTLogRelLogger(loggerRelease, 0, ~0U,
    865                            "VirtualBox web service %s r%u %s (%s %s) release log\n"
    866 #ifdef VBOX_BLEEDING_EDGE
    867                            "EXPERIMENTAL build " VBOX_BLEEDING_EDGE "\n"
    868 #endif
    869                            "Log opened %s\n",
    870                            VBOX_VERSION_STRING, RTBldCfgRevision(), VBOX_BUILD_TARGET,
    871                            __DATE__, __TIME__, szTmp);
    872 
    873             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
    874             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    875                 RTLogRelLogger(loggerRelease, 0, ~0U, "OS Product: %s\n", szTmp);
    876             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
    877             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    878                 RTLogRelLogger(loggerRelease, 0, ~0U, "OS Release: %s\n", szTmp);
    879             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
    880             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    881                 RTLogRelLogger(loggerRelease, 0, ~0U, "OS Version: %s\n", szTmp);
    882             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    883                 RTLogRelLogger(loggerRelease, 0, ~0U, "OS Service Pack: %s\n", szTmp);
    884 
    885             /* the package type is interesting for Linux distributions */
    886             char szExecName[RTPATH_MAX];
    887             char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
    888             RTLogRelLogger(loggerRelease, 0, ~0U,
    889                            "Executable: %s\n"
    890                            "Process ID: %u\n"
    891                            "Package type: %s"
    892 #ifdef VBOX_OSE
    893                            " (OSE)"
    894 #endif
    895                            "\n",
    896                            pszExecName ? pszExecName : "unknown",
    897                            RTProcSelf(),
    898                            VBOX_PACKAGE_STRING);
    899 
    900932            /* register this logger as the release logger */
    901             RTLogRelSetDefaultInstance(loggerRelease);
     933            RTLogRelSetDefaultInstance(pLoggerRelease);
    902934
    903935            /* Explicitly flush the log in case of VBOXWEBSRV_RELEASE_LOG=buffered. */
    904             RTLogFlush(loggerRelease);
     936            RTLogFlush(pLoggerRelease);
    905937        }
    906938        else
Note: See TracChangeset for help on using the changeset viewer.

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