Skip to content

Commit

Permalink
Merge pull request #2 from firegiant/silentexecfix
Browse files Browse the repository at this point in the history
Make sure output is read even if it won't be logged.
  • Loading branch information
barnson committed Mar 30, 2015
2 parents 0ca0c21 + ed4faea commit 8557187
Showing 1 changed file with 65 additions and 64 deletions.
129 changes: 65 additions & 64 deletions src/libs/wcautil/qtexec.cpp
Expand Up @@ -108,7 +108,8 @@ static HRESULT CreatePipes(
return hr;
}

static HRESULT LogOutput(
static HRESULT HandleOutput(
__in BOOL fLogOutput,
__in HANDLE hRead
)
{
Expand Down Expand Up @@ -136,76 +137,79 @@ static HRESULT LogOutput(
ExitOnLastError(hr, "Failed to read from handle.");
}

// Check for UNICODE or ANSI output
if (bFirst)
if (fLogOutput)
{
if ((isgraph(pBuffer[0]) && isgraph(pBuffer[1])) ||
(isgraph(pBuffer[0]) && isspace(pBuffer[1])) ||
(isspace(pBuffer[0]) && isgraph(pBuffer[1])) ||
(isspace(pBuffer[0]) && isspace(pBuffer[1])))
// Check for UNICODE or ANSI output
if (bFirst)
{
bUnicode = FALSE;
if ((isgraph(pBuffer[0]) && isgraph(pBuffer[1])) ||
(isgraph(pBuffer[0]) && isspace(pBuffer[1])) ||
(isspace(pBuffer[0]) && isgraph(pBuffer[1])) ||
(isspace(pBuffer[0]) && isspace(pBuffer[1])))
{
bUnicode = FALSE;
}

bFirst = FALSE;
}

bFirst = FALSE;
}

// Keep track of output
if (bUnicode)
{
hr = StrAllocConcat(&szLog, (LPCWSTR)pBuffer, 0);
ExitOnFailure(hr, "failed to concatenate output strings");
}
else
{
hr = StrAllocStringAnsi(&szTemp, (LPCSTR)pBuffer, 0, CP_OEMCP);
ExitOnFailure(hr, "failed to allocate output string");
hr = StrAllocConcat(&szLog, szTemp, 0);
ExitOnFailure(hr, "failed to concatenate output strings");
}

// Log each line of the output
pNext = szLog;
pEnd = wcschr(szLog, L'\r');
if (NULL == pEnd)
{
pEnd = wcschr(szLog, L'\n');
}
while (pEnd && *pEnd)
{
// Find beginning of next line
pEnd[0] = 0;
++pEnd;
if ((pEnd[0] == L'\r') || (pEnd[0] == L'\n'))
// Keep track of output
if (bUnicode)
{
++pEnd;
hr = StrAllocConcat(&szLog, (LPCWSTR)pBuffer, 0);
ExitOnFailure(hr, "failed to concatenate output strings");
}
else
{
hr = StrAllocStringAnsi(&szTemp, (LPCSTR)pBuffer, 0, CP_OEMCP);
ExitOnFailure(hr, "failed to allocate output string");
hr = StrAllocConcat(&szLog, szTemp, 0);
ExitOnFailure(hr, "failed to concatenate output strings");
}

// Log output
hr = StrAllocString(&sczEscaped, pNext, 0);
ExitOnFailure(hr, "Failed to allocate copy of string");

hr = StrReplaceStringAll(&sczEscaped, L"%", L"%%");
ExitOnFailure(hr, "Failed to escape percent signs in string");

hr = StrAnsiAllocString(&szWrite, sczEscaped, 0, CP_OEMCP);
ExitOnFailure(hr, "failed to convert output to ANSI");
WcaLog(LOGMSG_STANDARD, szWrite);

// Next line
pNext = pEnd;
pEnd = wcschr(pNext, L'\r');
// Log each line of the output
pNext = szLog;
pEnd = wcschr(szLog, L'\r');
if (NULL == pEnd)
{
pEnd = wcschr(pNext, L'\n');
pEnd = wcschr(szLog, L'\n');
}
while (pEnd && *pEnd)
{
// Find beginning of next line
pEnd[0] = 0;
++pEnd;
if ((pEnd[0] == L'\r') || (pEnd[0] == L'\n'))
{
++pEnd;
}

// Log output
hr = StrAllocString(&sczEscaped, pNext, 0);
ExitOnFailure(hr, "Failed to allocate copy of string");

hr = StrReplaceStringAll(&sczEscaped, L"%", L"%%");
ExitOnFailure(hr, "Failed to escape percent signs in string");

hr = StrAnsiAllocString(&szWrite, sczEscaped, 0, CP_OEMCP);
ExitOnFailure(hr, "failed to convert output to ANSI");
WcaLog(LOGMSG_STANDARD, szWrite);

// Next line
pNext = pEnd;
pEnd = wcschr(pNext, L'\r');
if (NULL == pEnd)
{
pEnd = wcschr(pNext, L'\n');
}
}
}

hr = StrAllocString(&szTemp, pNext, 0);
ExitOnFailure(hr, "failed to allocate string");
hr = StrAllocString(&szTemp, pNext, 0);
ExitOnFailure(hr, "failed to allocate string");

hr = StrAllocString(&szLog, szTemp, 0);
ExitOnFailure(hr, "failed to allocate string");
hr = StrAllocString(&szLog, szTemp, 0);
ExitOnFailure(hr, "failed to allocate string");
}
}

// Print any text that didn't end with a new line
Expand Down Expand Up @@ -287,11 +291,8 @@ HRESULT WIXAPI QuietExec(
ReleaseFile(hErrWrite);
ReleaseFile(hInRead);

// Log output if we were asked to do so
if (fLogOutput)
{
LogOutput(hOutRead);
}
// Log output if we were asked to do so; otherwise just read the output handle
HandleOutput(fLogOutput, hOutRead);

// Wait for everything to finish
::WaitForSingleObject(oProcInfo.hProcess, dwTimeout);
Expand Down

0 comments on commit 8557187

Please sign in to comment.