VirtualBox

Changeset 2903 in kBuild


Ignore:
Timestamp:
Sep 9, 2016 5:38:50 PM (9 years ago)
Author:
bird
Message:

kmk: faster messages.

Location:
trunk/src/lib
Files:
1 edited
1 copied

Legend:

Unmodified
Added
Removed
  • trunk/src/lib/Makefile.kmk

    r2900 r2903  
    4141        maybe_con_write.c \
    4242        maybe_con_fwrite.c \
     43        maybe_con_printf.c \
    4344        kbuild_version.c
    4445kUtil_SOURCES.win = \
  • trunk/src/lib/maybe_con_printf.c

    r2900 r2903  
    4646
    4747/**
    48  * Drop-in fwrite replacement for optimizing console output on windows.
     48 * Drop-in printf replacement for optimizing console output on windows.
    4949 *
    5050 *
    51  * @returns Units written; 0 & errno on failure.
    52  * @param   pvBuf               What to write.
    53  * @param   cbUnit              How much to write in each unit.
    54  * @param   cUnits              How many units to write.
    55  * @param   pFile               The file to write to.
     51 * @returns Number of bytes written.
     52 * @param   pszFormat   The format string.
     53 * @param   ...         The format arguments.
    5654 */
    57 size_t maybe_con_fwrite(void const *pvBuf, size_t cbUnit, size_t cUnits, FILE *pFile)
     55int maybe_con_printf(const char *pszFormat, ...)
    5856{
     57    va_list va;
     58    int rc;
     59
    5960#ifdef KBUILD_OS_WINDOWS
    6061    /*
    61      * If it's a TTY, do our own conversion to wide char and
    62      * call WriteConsoleW directly.
     62     * Just try format it into a stack buffer.
    6363     */
    64     if (   cbUnit > 0
    65         && cUnits > 0
    66         && (pFile == stdout || pFile == stderr))
     64    extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
     65    char szTmp[16384];
     66    va_start(va, pszFormat);
     67    rc = vsnprintf(szTmp, sizeof(szTmp), pszFormat, va);
     68    va_end(va);
     69    if (rc > 0)
    6770    {
    68         int fd = fileno(pFile);
    69         if (fd >= 0)
    70         {
    71             if (isatty(fd))
    72             {
    73                 HANDLE hCon = (HANDLE)_get_osfhandle(fd);
    74                 if (   hCon != INVALID_HANDLE_VALUE
    75                     && hCon != NULL)
    76                 {
    77                     size_t   cbToWrite = cbUnit * cUnits;
    78                     size_t   cwcTmp    = cbToWrite * 2 + 16;
    79                     wchar_t *pawcTmp   = (wchar_t *)malloc(cwcTmp * sizeof(wchar_t));
    80                     if (pawcTmp)
    81                     {
    82                         int           cwcToWrite;
    83                         static UINT s_uConsoleCp = 0;
    84                         if (s_uConsoleCp == 0)
    85                             s_uConsoleCp = GetConsoleCP();
    86 
    87                         cwcToWrite = MultiByteToWideChar(s_uConsoleCp, 0 /*dwFlags*/, pvBuf, (int)cbToWrite, pawcTmp, (int)(cwcTmp - 1));
    88                         if (cwcToWrite > 0)
    89                         {
    90                             /* Let the CRT do the rest.  At least the Visual C++ 2010 CRT
    91                                sources indicates _cputws will do the right thing we want.  */
    92                             pawcTmp[cwcToWrite] = '\0';
    93                             fflush(pFile);
    94                             if (_cputws(pawcTmp) >= 0)
    95                                 return cUnits;
    96                             return 0;
    97                         }
    98                     }
    99                 }
    100             }
    101         }
     71        size_t cchRet = maybe_con_fwrite(szTmp, 1, rc, stdout);
     72        return cchRet > 0 ? (int)cchRet : -1;
    10273    }
    10374#endif
    10475
    10576    /*
    106      * Semi regular write handling.
     77     * call the 'v' function.
    10778     */
    108     return fwrite(pvBuf, cbUnit, cUnits, pFile);
     79    va_start(va, pszFormat);
     80    rc = vfprintf(stdout, pszFormat, va);
     81    va_end(va);
     82    return rc;
    10983}
     84
Note: See TracChangeset for help on using the changeset viewer.

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