VirtualBox

Changeset 2911 in kBuild


Ignore:
Timestamp:
Sep 10, 2016 11:16:59 AM (9 years ago)
Author:
bird
Message:

err.c: format into string buffer and use fwrite to avoid getting split up error messages.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/kmk/kmkbuiltin/err.c

    r2413 r2911  
    55
    66/*
    7  * Copyright (c) 2005-2010 knut st. osmundsen <[email protected]>
     7 * Copyright (c) 2005-2016 knut st. osmundsen <[email protected]>
    88 *
    99 * This file is part of kBuild.
     
    3434#include "err.h"
    3535
     36#ifdef KBUILD_OS_WINDOWS
     37/* This is a trick to speed up console output on windows. */
     38# undef fwrite
     39# define fwrite maybe_con_fwrite
     40extern size_t maybe_con_fwrite(void const *, size_t, size_t, FILE *);
     41#endif
     42
    3643
    3744/** The current program name. */
     
    4350    va_list args;
    4451    int error = errno;
     52
     53    /* stderr is unbuffered, so try format the whole message and print it in
     54       one go so it won't be split by other output. */
     55    char szMsg[4096];
     56    int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
     57    if (cchMsg < sizeof(szMsg) - 1 && cchMsg > 0)
     58    {
     59        int cchMsg2;
     60        va_start(args, fmt);
     61        cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
     62        va_end(args);
     63
     64        if (   cchMsg < sizeof(szMsg) - 1
     65            && cchMsg2 >= 0)
     66        {
     67            cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
     68            if (   cchMsg < sizeof(szMsg) - 1
     69                && cchMsg2 >= 0)
     70            {
     71                fwrite(szMsg, cchMsg, 1, stderr);
     72                return eval;
     73            }
     74
     75        }
     76
     77    }
     78
     79    /* fallback */
    4580    fprintf(stderr, "%s: ", g_progname);
    4681    va_start(args, fmt);
     
    5691{
    5792    va_list args;
     93
     94    /* stderr is unbuffered, so try format the whole message and print it in
     95       one go so it won't be split by other output. */
     96    char szMsg[4096];
     97    int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
     98    if (cchMsg < sizeof(szMsg) - 1 && cchMsg > 0)
     99    {
     100        int cchMsg2;
     101        va_start(args, fmt);
     102        cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
     103        va_end(args);
     104
     105        if (   cchMsg < sizeof(szMsg) - 1
     106            && cchMsg2 >= 0)
     107        {
     108            szMsg[cchMsg++] = '\n';
     109            fwrite(szMsg, cchMsg, 1, stderr);
     110            return eval;
     111        }
     112
     113    }
     114
     115    /* fallback */
    58116    fprintf(stderr, "%s: ", g_progname);
    59117    va_start(args, fmt);
     
    69127    int error = errno;
    70128    va_list args;
     129
     130    /* stderr is unbuffered, so try format the whole message and print it in
     131       one go so it won't be split by other output. */
     132    char szMsg[4096];
     133    int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
     134    if (cchMsg < sizeof(szMsg) - 1 && cchMsg > 0)
     135    {
     136        int cchMsg2;
     137        va_start(args, fmt);
     138        cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
     139        va_end(args);
     140
     141        if (   cchMsg < sizeof(szMsg) - 1
     142            && cchMsg2 >= 0)
     143        {
     144            cchMsg += cchMsg2 = snprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, ": %s\n", strerror(error));
     145            if (   cchMsg < sizeof(szMsg) - 1
     146                && cchMsg2 >= 0)
     147            {
     148                fwrite(szMsg, cchMsg, 1, stderr);
     149                return;
     150            }
     151
     152        }
     153    }
     154
     155    /* fallback */
    71156    fprintf(stderr, "%s: ", g_progname);
    72157    va_start(args, fmt);
     
    79164{
    80165    va_list args;
     166
     167    /* stderr is unbuffered, so try format the whole message and print it in
     168       one go so it won't be split by other output. */
     169    char szMsg[4096];
     170    int cchMsg = snprintf(szMsg, sizeof(szMsg), "%s: ", g_progname);
     171    if (cchMsg < sizeof(szMsg) - 1 && cchMsg > 0)
     172    {
     173        int cchMsg2;
     174        va_start(args, fmt);
     175        cchMsg += cchMsg2 = vsnprintf(&szMsg[cchMsg], sizeof(szMsg) - cchMsg, fmt, args);
     176        va_end(args);
     177
     178        if (   cchMsg < sizeof(szMsg) - 1
     179            && cchMsg2 >= 0)
     180        {
     181            szMsg[cchMsg++] = '\n';
     182            fwrite(szMsg, cchMsg, 1, stderr);
     183            return;
     184        }
     185
     186    }
     187
     188    /* fallback */
    81189    fprintf(stderr, "%s: ", g_progname);
    82190    va_start(args, fmt);
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