VirtualBox

Changeset 84501 in vbox for trunk/src


Ignore:
Timestamp:
May 25, 2020 1:36:55 PM (5 years ago)
Author:
vboxsync
Message:

bugref:9637. Splitting logging logging related functions from main.cpp

Location:
trunk/src/VBox/Additions/x11/VBoxClient
Files:
1 added
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/x11/VBoxClient/Makefile.kmk

    r84480 r84501  
    4545        seamless.cpp \
    4646        seamless-x11.cpp \
     47        logging.cpp \
    4748        hostversion.cpp
    4849
  • trunk/src/VBox/Additions/x11/VBoxClient/VBoxClient.h

    r83218 r84501  
    3030void VBClLogError(const char *pszFormat, ...);
    3131void VBClLogFatalError(const char *pszFormat, ...);
     32void VBClLogDestroy(void);
     33int VBClLogCreate(const char *pszLogFile);
    3234
    3335/** Call clean-up for the current service and exit. */
  • trunk/src/VBox/Additions/x11/VBoxClient/main.cpp

    r83717 r84501  
    2020*   Header Files                                                                                                                 *
    2121*********************************************************************************************************************************/
    22 #include <sys/types.h>
    2322#include <sys/wait.h>
    2423#include <stdlib.h>       /* For exit */
    25 #include <stdio.h>
    26 #include <string.h>
    27 #include <unistd.h>
    28 #include <errno.h>
    29 #include <poll.h>
    30 #include <signal.h>
    31 
    3224#include <X11/Xlib.h>
    33 #include <X11/Xatom.h>
    34 
    35 #include <package-generated.h>
    3625#include "product-generated.h"
    37 
    3826#include <iprt/buildconfig.h>
    3927#include <iprt/critsect.h>
    40 #include <iprt/env.h>
    41 #include <iprt/file.h>
    4228#include <iprt/getopt.h>
    4329#include <iprt/initterm.h>
    4430#include <iprt/message.h>
    4531#include <iprt/path.h>
    46 #include <iprt/param.h>
    47 #include <iprt/process.h>
    4832#include <iprt/stream.h>
    49 #include <iprt/string.h>
    50 #include <iprt/system.h>
    51 #include <iprt/types.h>
    5233#include <VBox/VBoxGuestLib.h>
    5334#include <VBox/err.h>
    54 #include <VBox/log.h>
    55 
    5635#include "VBoxClient.h"
    5736
     
    9170static RTCRITSECT    g_critSect;
    9271/** Counter of how often our daemon has been respawned. */
    93 static unsigned      g_cRespawn = 0;
     72unsigned      g_cRespawn = 0;
    9473/** Logging verbosity level. */
    95 static unsigned      g_cVerbosity = 0;
     74unsigned      g_cVerbosity = 0;
    9675static char          g_szLogFile[RTPATH_MAX + 128] = "";
    97 /** Logging parameters. */
    98 /** @todo Make this configurable later. */
    99 static PRTLOGGER     g_pLoggerRelease = NULL;
    100 static uint32_t      g_cHistory = 10;                   /* Enable log rotation, 10 files. */
    101 static uint32_t      g_uHistoryFileTime = RT_SEC_1DAY;  /* Max 1 day per file. */
    102 static uint64_t      g_uHistoryFileSize = 100 * _1M;    /* Max 100MB per file. */
    103 
    104 /**
    105  * Notifies the desktop environment with a message.
    106  *
    107  * @param   pszMessage          Message to notify desktop environment with.
    108  */
    109 int vbclLogNotify(const char *pszMessage)
    110 {
    111     AssertPtrReturn(pszMessage, VERR_INVALID_POINTER);
    112 
    113     int rc = VINF_SUCCESS;
    114 
    115     if (g_cRespawn == 0)
    116     {
    117         char *pszCommand = RTStrAPrintf2("notify-send \"VBoxClient: %s\"", pszMessage);
    118         if (pszCommand)
    119         {
    120             int status = system(pszCommand);
    121 
    122             RTStrFree(pszCommand);
    123 
    124             if (WEXITSTATUS(status) != 0)  /* Utility or extension not available. */
    125             {
    126                 pszCommand = RTStrAPrintf2("xmessage -buttons OK:0 -center \"VBoxClient: %s\"",
    127                                            pszMessage);
    128                 if (pszCommand)
    129                 {
    130                     status = system(pszCommand);
    131                     if (WEXITSTATUS(status) != 0)  /* Utility or extension not available. */
    132                     {
    133                         RTPrintf("VBoxClient: %s", pszMessage);
    134                     }
    135 
    136                     RTStrFree(pszCommand);
    137                 }
    138                 else
    139                     rc = VERR_NO_MEMORY;
    140             }
    141         }
    142         else
    143             rc = VERR_NO_MEMORY;
    144     }
    145 
    146     return rc;
    147 }
    148 
    149 /**
    150  * Logs a fatal error, notifies the desktop environment via a message and
    151  * exits the application immediately.
    152  *
    153  * @param   pszFormat           Format string to log.
    154  * @param   ...                 Variable arguments for format string. Optional.
    155  */
    156 void VBClLogFatalError(const char *pszFormat, ...)
    157 {
    158     va_list args;
    159     va_start(args, pszFormat);
    160     char *psz = NULL;
    161     RTStrAPrintfV(&psz, pszFormat, args);
    162     va_end(args);
    163 
    164     AssertPtr(psz);
    165     LogFlowFunc(("%s", psz));
    166     LogRel(("%s", psz));
    167 
    168     vbclLogNotify(psz);
    169 
    170     RTStrFree(psz);
    171 }
    172 
    173 /**
    174  * Logs an error message to the (release) logging instance.
    175  *
    176  * @param   pszFormat               Format string to log.
    177  */
    178 void VBClLogError(const char *pszFormat, ...)
    179 {
    180     va_list args;
    181     va_start(args, pszFormat);
    182     char *psz = NULL;
    183     RTStrAPrintfV(&psz, pszFormat, args);
    184     va_end(args);
    185 
    186     AssertPtr(psz);
    187     LogFlowFunc(("%s", psz));
    188     LogRel(("%s", psz));
    189 
    190     RTStrFree(psz);
    191 }
    192 
    193 /**
    194  * Logs an info message to the (release) logging instance.
    195  *
    196  * @param   pszFormat               Format string to log.
    197  */
    198 void  VBClLogInfo(const char *pszFormat, ...)
    199 {
    200     va_list args;
    201     va_start(args, pszFormat);
    202     char *psz = NULL;
    203     RTStrAPrintfV(&psz, pszFormat, args);
    204     va_end(args);
    205 
    206     AssertPtr(psz);
    207     LogFlowFunc(("%s", psz));
    208     LogRel(("%s", psz));
    209 
    210     RTStrFree(psz);
    211 }
    212 
    213 /**
    214  * @callback_method_impl{FNRTLOGPHASE, Release logger callback}
    215  */
    216 static DECLCALLBACK(void) vbClLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
    217 {
    218     /* Some introductory information. */
    219     static RTTIMESPEC s_TimeSpec;
    220     char szTmp[256];
    221     if (enmPhase == RTLOGPHASE_BEGIN)
    222         RTTimeNow(&s_TimeSpec);
    223     RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
    224 
    225     switch (enmPhase)
    226     {
    227         case RTLOGPHASE_BEGIN:
    228         {
    229             pfnLog(pLoggerRelease,
    230                    "VBoxClient %s r%s (verbosity: %u) %s (%s %s) release log\n"
    231                    "Log opened %s\n",
    232                    RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity, VBOX_BUILD_TARGET,
    233                    __DATE__, __TIME__, szTmp);
    234 
    235             int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
    236             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    237                 pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
    238             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
    239             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    240                 pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
    241             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
    242             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    243                 pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
    244             vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
    245             if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
    246                 pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
    247 
    248             /* the package type is interesting for Linux distributions */
    249             char szExecName[RTPATH_MAX];
    250             char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
    251             pfnLog(pLoggerRelease,
    252                    "Executable: %s\n"
    253                    "Process ID: %u\n"
    254                    "Package type: %s"
    255 #ifdef VBOX_OSE
    256                    " (OSE)"
    257 #endif
    258                    "\n",
    259                    pszExecName ? pszExecName : "unknown",
    260                    RTProcSelf(),
    261                    VBOX_PACKAGE_STRING);
    262             break;
    263         }
    264 
    265         case RTLOGPHASE_PREROTATE:
    266             pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
    267             break;
    268 
    269         case RTLOGPHASE_POSTROTATE:
    270             pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
    271             break;
    272 
    273         case RTLOGPHASE_END:
    274             pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
    275             break;
    276 
    277         default:
    278             /* nothing */
    279             break;
    280     }
    281 }
    282 
    283 /**
    284  * Creates the default release logger outputting to the specified file.
    285  *
    286  * Pass NULL to disabled logging.
    287  *
    288  * @return  IPRT status code.
    289  * @param   pszLogFile      Filename for log output.  NULL disables custom handling.
    290  */
    291 int VBClLogCreate(const char *pszLogFile)
    292 {
    293     if (!pszLogFile)
    294         return VINF_SUCCESS;
    295 
    296     /* Create release logger (stdout + file). */
    297     static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
    298     RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME;
    299 #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
    300     fFlags |= RTLOGFLAGS_USECRLF;
    301 #endif
    302     int rc = RTLogCreateEx(&g_pLoggerRelease, fFlags, "all",
    303 #ifdef DEBUG
    304                            "VBOXCLIENT_LOG",
    305 #else
    306                            "VBOXCLIENT_RELEASE_LOG",
    307 #endif
    308                            RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX /*cMaxEntriesPerGroup*/,
    309                            RTLOGDEST_STDOUT | RTLOGDEST_USER,
    310                            vbClLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
    311                            NULL /*pErrInfo*/, "%s", pszLogFile ? pszLogFile : "");
    312     if (RT_SUCCESS(rc))
    313     {
    314         /* register this logger as the release logger */
    315         RTLogRelSetDefaultInstance(g_pLoggerRelease);
    316 
    317         /* Explicitly flush the log in case of VBOXSERVICE_RELEASE_LOG=buffered. */
    318         RTLogFlush(g_pLoggerRelease);
    319     }
    320 
    321     return rc;
    322 }
    323 
    324 /**
    325  * Destroys the currently active logging instance.
    326  */
    327 void VBClLogDestroy(void)
    328 {
    329     RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
    330 }
     76
    33177
    33278/**
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