VirtualBox

Changeset 95827 in vbox for trunk/src/VBox/Additions


Ignore:
Timestamp:
Jul 26, 2022 9:55:21 AM (3 years ago)
Author:
vboxsync
Message:

Additions/VBoxTray: Added long overdue command line handling to VBoxTray to specify verbosity and log file handling, to make it easier for normal users to enable debugging (also as a runtime setting). When verbosity is increased, this also will enable VBoxTray's popup menu (via right click in system tray). bugref:10267

Location:
trunk/src/VBox/Additions/WINNT/VBoxTray
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.cpp

    r93115 r95827  
    2121#include <iprt/alloca.h>
    2222#include <iprt/system.h>
     23#include <iprt/utf16.h>
    2324#include <VBox/Log.h>
    2425#include <VBox/VBoxGuestLib.h>
     
    331332}
    332333
     334/**
     335 * Shows a message box with a printf() style formatted string.
     336 *
     337 * @param   pszTitle            Title of the message box.
     338 * @param   uStyle              Style of message box to use (see MSDN, MB_ defines).
     339 *                              When 0 is specified, MB_ICONINFORMATION will be used.
     340 * @param   pszFmt              Printf-style format string to show in the message box body.
     341 * @param   ...                 Arguments for format string.
     342 */
     343void hlpShowMessageBox(const char *pszTitle, UINT uStyle, const char *pszFmt, ...)
     344{
     345    if (!uStyle)
     346        uStyle = MB_ICONINFORMATION;
     347
     348    char       *pszMsg;
     349    va_list     va;
     350    va_start(va, pszFmt);
     351    int rc = RTStrAPrintfV(&pszMsg, pszFmt, va);
     352    va_end(va);
     353    if (rc >= 0)
     354    {
     355        PRTUTF16 pwszTitle;
     356        rc = RTStrToUtf16(pszTitle, &pwszTitle);
     357        if (RT_SUCCESS(rc))
     358        {
     359            PRTUTF16 pwszMsg;
     360            rc = RTStrToUtf16(pszMsg, &pwszMsg);
     361            if (RT_SUCCESS(rc))
     362            {
     363                MessageBoxW(GetDesktopWindow(), pwszMsg, pwszTitle, uStyle);
     364                RTUtf16Free(pwszMsg);
     365            }
     366            else
     367                MessageBoxA(GetDesktopWindow(), pszMsg, pszTitle, uStyle);
     368            RTUtf16Free(pwszTitle);
     369        }
     370    }
     371    else /* Should never happen! */
     372        AssertMsgFailed(("Failed to format error text of format string: %s!\n", pszFmt));
     373    RTStrFree(pszMsg);
     374}
     375
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxHelpers.h

    r93115 r95827  
    2424// #define DEBUG_DISPLAY_CHANGE
    2525
     26/** @todo r=andy WTF? Remove this!! */
    2627#ifdef DEBUG_DISPLAY_CHANGE
    2728#   define DDCLOG(a) Log(a)
     
    3031#endif /* !DEBUG_DISPLAY_CHANGE */
    3132
    32 extern int hlpReportStatus(VBoxGuestFacilityStatus statusCurrent);
     33extern int  hlpReportStatus(VBoxGuestFacilityStatus statusCurrent);
    3334extern void hlpReloadCursor(void);
    3435extern void hlpResizeRect(RECTL *paRects, unsigned nRects, unsigned uPrimary, unsigned uResized, int iNewWidth, int iNewHeight, int iNewPosX, int iNewPosY);
    35 extern int hlpShowBalloonTip(HINSTANCE hInst, HWND hWnd, UINT uID, const char *pszMsg, const char *pszTitle, UINT uTimeout, DWORD dwInfoFlags);
     36extern int  hlpShowBalloonTip(HINSTANCE hInst, HWND hWnd, UINT uID, const char *pszMsg, const char *pszTitle, UINT uTimeout, DWORD dwInfoFlags);
     37extern void hlpShowMessageBox(const char *pszTitle, UINT uStyle, const char *pszFmt, ...);
    3638
    3739#endif /* !GA_INCLUDED_SRC_WINNT_VBoxTray_VBoxHelpers_h */
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.cpp

    r94629 r95827  
    4242#include <iprt/asm.h>
    4343#include <iprt/buildconfig.h>
     44#include <iprt/getopt.h>
    4445#include <iprt/ldr.h>
     46#include <iprt/message.h>
    4547#include <iprt/path.h>
    4648#include <iprt/process.h>
    4749#include <iprt/system.h>
    4850#include <iprt/time.h>
     51#include <iprt/utf16.h>
    4952
    5053#include <VBox/log.h>
     
    132135*   Global Variables                                                                                                             *
    133136*********************************************************************************************************************************/
     137int                   g_cVerbosity             = 0;
    134138HANDLE                g_hStopSem;
    135139HANDLE                g_hSeamlessWtNotifyEvent = 0;
     
    559563/**
    560564 * Creates the default release logger outputting to the specified file.
    561  * Pass NULL for disabled logging.
    562565 *
    563566 * @return  IPRT status code.
     567 * @param   pszLogFile          Path to log file to use.
    564568 */
    565 static int vboxTrayLogCreate(void)
     569static int vboxTrayLogCreate(const char *pszLogFile)
    566570{
    567571    /* Create release (or debug) logger (stdout + file). */
    568572    static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
    569 #ifdef DEBUG /* See below, debug logger not release. */
    570     static const char s_szEnvVarPfx[] = "VBOXTRAY_LOG";
    571     static const char s_szGroupSettings[] = "all.e.l.f";
    572 #else
    573573    static const char s_szEnvVarPfx[] = "VBOXTRAY_RELEASE_LOG";
    574     static const char s_szGroupSettings[] = "all";
    575 #endif
     574
    576575    RTERRINFOSTATIC ErrInfo;
    577576    int rc = RTLogCreateEx(&g_pLoggerRelease, s_szEnvVarPfx,
    578577                           RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG | RTLOGFLAGS_USECRLF,
    579                            s_szGroupSettings, RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX,
     578                           "all.e", RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX,
    580579                           0 /*cBufDescs*/, NULL /*paBufDescs*/, RTLOGDEST_STDOUT,
    581580                           vboxTrayLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
    582581                           NULL /*pOutputIf*/, NULL /*pvOutputIfUser*/,
    583                            RTErrInfoInitStatic(&ErrInfo), NULL /*pszFilenameFmt*/);
     582                           RTErrInfoInitStatic(&ErrInfo), "%s", pszLogFile ? pszLogFile : "");
    584583    if (RT_SUCCESS(rc))
    585584    {
    586 #ifdef DEBUG
    587         /* Register this logger as the _debug_ logger.
    588            Note! This means any Log() statement preceeding this may cause a
    589                  20yy-*VBoxTray*.log file to have been created and it will stay
    590                  open till the process quits as we don't destroy it when
    591                  replacing it here. */
    592         RTLogSetDefaultInstance(g_pLoggerRelease);
    593 #else
    594585        /* Register this logger as the release logger. */
    595586        RTLogRelSetDefaultInstance(g_pLoggerRelease);
    596 #endif
     587
     588        /* Register this logger as the _debug_ logger. */
     589        RTLogSetDefaultInstance(g_pLoggerRelease);
     590
     591        switch (g_cVerbosity) /* Not very elegant, but has to do it for now. */
     592        {
     593            case 1:
     594                rc = RTLogGroupSettings(g_pLoggerRelease, "all.e.l");
     595                break;
     596
     597            case 2:
     598                rc = RTLogGroupSettings(g_pLoggerRelease, "all.e.l.l2");
     599                break;
     600
     601            case 3:
     602                rc = RTLogGroupSettings(g_pLoggerRelease, "all.e.l.l2.l3");
     603                break;
     604
     605            case 4:
     606                RT_FALL_THROUGH();
     607            default:
     608                rc = RTLogGroupSettings(g_pLoggerRelease, "all.e.l.l2.l3.f");
     609                break;
     610        }
     611        if (RT_FAILURE(rc))
     612            RTMsgError("Setting debug logging failed, rc=%Rrc\n", rc);
     613
    597614        /* Explicitly flush the log in case of VBOXTRAY_RELEASE_LOG=buffered. */
    598615        RTLogFlush(g_pLoggerRelease);
     
    9851002{
    9861003    RT_NOREF(hPrevInstance, lpCmdLine, nCmdShow);
    987     int rc = RTR3InitExeNoArguments(0);
     1004
     1005    int rc = RTR3InitExe(__argc, &__argv, RTR3INIT_FLAGS_STANDALONE_APP);
    9881006    if (RT_FAILURE(rc))
    989         return RTEXITCODE_INIT;
     1007        return RTMsgInitFailure(rc);
     1008
     1009    LPWSTR pwszCmdLine = GetCommandLineW();
     1010    if (!pwszCmdLine)
     1011        return RTMsgErrorExit(RTEXITCODE_FAILURE, "GetCommandLineW failed");
     1012
     1013    char *pszCmdLine;
     1014    rc = RTUtf16ToUtf8(pwszCmdLine, &pszCmdLine); /* leaked */
     1015    if (RT_FAILURE(rc))
     1016        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to convert the command line: %Rrc", rc);
     1017
     1018    char szLogFile[RTPATH_MAX] = {0};
     1019
     1020    int    cArgs;
     1021    char **papszArgs;
     1022    rc = RTGetOptArgvFromString(&papszArgs, &cArgs, pszCmdLine, RTGETOPTARGV_CNV_QUOTE_MS_CRT, NULL);
     1023    if (RT_SUCCESS(rc))
     1024    {
     1025        /*
     1026         * Parse the top level arguments until we find a command.
     1027         */
     1028        static const RTGETOPTDEF s_aOptions[] =
     1029        {
     1030            { "--help",             'h',                         RTGETOPT_REQ_NOTHING },
     1031            { "-help",              'h',                         RTGETOPT_REQ_NOTHING },
     1032            { "/help",              'h',                         RTGETOPT_REQ_NOTHING },
     1033            { "/?",                 'h',                         RTGETOPT_REQ_NOTHING },
     1034            { "--logfile",          'l',                         RTGETOPT_REQ_STRING  },
     1035            { "-l",                 'l',                         RTGETOPT_REQ_STRING  },
     1036            { "/l",                 'l',                         RTGETOPT_REQ_STRING  },
     1037            { "--verbose",          'v',                         RTGETOPT_REQ_NOTHING },
     1038            { "-v",                 'v',                         RTGETOPT_REQ_NOTHING },
     1039            { "/v",                 'v',                         RTGETOPT_REQ_NOTHING },
     1040            { "--version",          'V',                         RTGETOPT_REQ_NOTHING },
     1041            { "-version",           'V',                         RTGETOPT_REQ_NOTHING },
     1042            { "/version",           'V',                         RTGETOPT_REQ_NOTHING },
     1043            { "/V",                 'V',                         RTGETOPT_REQ_NOTHING }
     1044        };
     1045
     1046        RTGETOPTSTATE GetState;
     1047        rc = RTGetOptInit(&GetState, cArgs, papszArgs, s_aOptions, RT_ELEMENTS(s_aOptions), 1, 0 /*fFlags*/);
     1048        if (RT_FAILURE(rc))
     1049            return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOptInit failed: %Rrc\n", rc);
     1050
     1051        int ch;
     1052        RTGETOPTUNION ValueUnion;
     1053        while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
     1054        {
     1055            switch (ch)
     1056            {
     1057                case 'h':
     1058                    hlpShowMessageBox(VBOX_VBOXTRAY_TITLE, MB_ICONINFORMATION, "-- %s v%u.%u.%ur%u --\n"
     1059                         "\n"
     1060                         "Command Line Parameters:\n\n"
     1061                         "-l, --logfile <file>\n"
     1062                         "    Enables logging to a file\n"
     1063                         "-v, --verbose\n"
     1064                         "    Increases verbosity\n"
     1065                         "-V, --version\n"
     1066                         "   Displays version number and exit\n"
     1067                         "-?, -h, --help\n"
     1068                         "   Displays this help text and exit\n"
     1069                         "\n"
     1070                         "Examples:\n"
     1071                         "  %s -vvv\n",
     1072                         VBOX_VBOXTRAY_TITLE, VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV,
     1073                         papszArgs[0], papszArgs[0]);
     1074                    return RTEXITCODE_SUCCESS;
     1075
     1076                case 'l':
     1077                    if (*ValueUnion.psz == '\0')
     1078                        szLogFile[0] = '\0';
     1079                    else
     1080                    {
     1081                        rc = RTPathAbs(ValueUnion.psz, szLogFile, sizeof(szLogFile));
     1082                        if (RT_FAILURE(rc))
     1083                            return RTMsgErrorExit(RTEXITCODE_FAILURE, "Log file path is too long (%Rrc)", rc);
     1084                    }
     1085                    break;
     1086
     1087                case 'v':
     1088                    g_cVerbosity++;
     1089                    break;
     1090
     1091                case 'V':
     1092                    hlpShowMessageBox(VBOX_VBOXTRAY_TITLE, MB_ICONINFORMATION,
     1093                                      "Version: %u.%u.%ur%u",
     1094                                      VBOX_VERSION_MAJOR, VBOX_VERSION_MINOR, VBOX_VERSION_BUILD, VBOX_SVN_REV);
     1095                    return RTEXITCODE_SUCCESS;
     1096
     1097                default:
     1098                    rc = RTGetOptPrintError(ch, &ValueUnion);
     1099                    break;
     1100            }
     1101        }
     1102
     1103        RTGetOptArgvFree(papszArgs);
     1104    }
     1105    else
     1106        return RTMsgErrorExit(RTEXITCODE_FAILURE, "RTGetOptArgvFromString failed: %Rrc", rc);
    9901107
    9911108    /* Note: Do not use a global namespace ("Global\\") for mutex name here,
    9921109     * will blow up NT4 compatibility! */
    993     HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, "VBoxTray");
     1110    HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, VBOX_VBOXTRAY_TITLE);
    9941111    if (   hMutexAppRunning != NULL
    9951112        && GetLastError() == ERROR_ALREADY_EXISTS)
     
    9981115        CloseHandle (hMutexAppRunning);
    9991116        hMutexAppRunning = NULL;
    1000         return 0;
    1001     }
    1002 
    1003     LogRel(("%s r%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr()));
    1004 
    1005     rc = vboxTrayLogCreate();
     1117        return RTEXITCODE_SUCCESS;
     1118    }
     1119
     1120    rc = vboxTrayLogCreate(szLogFile[0] ? szLogFile : NULL);
    10061121    if (RT_SUCCESS(rc))
    10071122    {
     1123        LogRel(("Verbosity level: %d\n", g_cVerbosity));
     1124
    10081125        rc = VbglR3Init();
    10091126        if (RT_SUCCESS(rc))
     
    11541271                case WM_LBUTTONDBLCLK:
    11551272                    break;
    1156 #ifdef DEBUG
    11571273                case WM_RBUTTONDOWN:
    11581274                {
     1275                    if (!g_cVerbosity) /* Don't show menu when running in non-verbose mode. */
     1276                        break;
     1277
    11591278                    POINT lpCursor;
    11601279                    if (GetCursorPos(&lpCursor))
     
    11921311                    break;
    11931312                }
    1194 #endif
    11951313            }
    11961314            return 0;
  • trunk/src/VBox/Additions/WINNT/VBoxTray/VBoxTray.h

    r93115 r95827  
    3939#include "VBoxDispIf.h"
    4040
     41
     42/*********************************************************************************************************************************
     43*   Defined Constants And Macros                                                                                                 *
     44*********************************************************************************************************************************/
     45/** Title of the program to show.
     46 *  Also shown as part of message boxes. */
     47#define VBOX_VBOXTRAY_TITLE                     "VBoxTray"
     48
    4149/*
    4250 * Windows messsages.
     
    5866#define TIMERID_VBOXTRAY_DT_TIMER               1002
    5967#define TIMERID_VBOXTRAY_ST_DELAYED_INIT_TIMER  1003
     68
     69
     70/*********************************************************************************************************************************
     71*   Common structures                                                                                                            *
     72*********************************************************************************************************************************/
    6073
    6174/**
     
    123136} VBOXSERVICEDESC, *PVBOXSERVICEDESC;
    124137
    125 extern VBOXSERVICEDESC g_SvcDescDisplay;
    126 #ifdef VBOX_WITH_SHARED_CLIPBOARD
    127 extern VBOXSERVICEDESC g_SvcDescClipboard;
    128 #endif
    129 extern VBOXSERVICEDESC g_SvcDescSeamless;
    130 extern VBOXSERVICEDESC g_SvcDescVRDP;
    131 extern VBOXSERVICEDESC g_SvcDescIPC;
    132 extern VBOXSERVICEDESC g_SvcDescLA;
    133 #ifdef VBOX_WITH_DRAG_AND_DROP
    134 extern VBOXSERVICEDESC g_SvcDescDnD;
    135 #endif
    136138
    137139/**
     
    174176} VBOXGLOBALMESSAGE, *PVBOXGLOBALMESSAGE;
    175177
     178
     179/*********************************************************************************************************************************
     180*   Externals                                                                                                                    *
     181*********************************************************************************************************************************/
     182extern VBOXSERVICEDESC g_SvcDescDisplay;
     183#ifdef VBOX_WITH_SHARED_CLIPBOARD
     184extern VBOXSERVICEDESC g_SvcDescClipboard;
     185#endif
     186extern VBOXSERVICEDESC g_SvcDescSeamless;
     187extern VBOXSERVICEDESC g_SvcDescVRDP;
     188extern VBOXSERVICEDESC g_SvcDescIPC;
     189extern VBOXSERVICEDESC g_SvcDescLA;
     190#ifdef VBOX_WITH_DRAG_AND_DROP
     191extern VBOXSERVICEDESC g_SvcDescDnD;
     192#endif
     193
     194extern int          g_cVerbosity;
    176195extern HINSTANCE    g_hInstance;
    177196extern HWND         g_hwndToolWindow;
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