为什么PowerShell会在stderr上砍消息? [英] Why does PowerShell chops message on stderr?
问题描述
我正在使用PowerShell脚本控制编译器(ghdl.exe)的不同编译步骤。
I'm using a PowerShell script to control different compilation steps of an compiler (ghdl.exe).
编译器具有3种不同的输出格式:
The compiler has 3 different output formats:
- 没有输出,没有错误=> $ LastExitCode = 0
- 在stderr上输出(警告),但没有错误=> $ LastExitCode = 0
- stderr上的输出(错误),并且可能警告=> $ LastExitCode!= 0
由于处理stderr和stdout接缝非常容易,我使用了此StackOverflow帖子中介绍的方法: PowerShell:使用Invoke-Expression管理错误
Because handling of stderr and stdout seams to be very buggy, I used the method presented in this StackOverflow post: PowerShell: Manage errors with Invoke-Expression
这是我的实现,带有附加消息着色:
Here is my implementation with addition message coloring:
function Format-NativeCommandStreams
{ param([Parameter(ValueFromPipeline=$true)]$InputObject)
begin
{ $ErrorRecordFound = $false }
process
{ if (-not $InputObject)
{ Write-Host "Empty" }
elseif ($InputObject -is [System.Management.Automation.ErrorRecord])
{ $ErrorRecordFound = $true
$text = $InputObject.ToString()
Write-Host $text -ForegroundColor Gray
$stdErr = $InputObject.TargetObject
if ($stdErr)
{ #Write-Host ("err: type=" + $stdErr.GetType() + " " + $stdErr)
if ($stdErr.Contains("warning"))
{ Write-Host "WARNING: " -NoNewline -ForegroundColor Yellow }
else
{ Write-Host "ERROR: " -NoNewline -ForegroundColor Red }
Write-Host $stdErr
}
}
else
{ $stdOut = $InputObject
if ($stdOut.Contains("warning"))
{ Write-Host "WARNING: " -NoNewline -ForegroundColor Yellow }
else
{ Write-Host "ERROR: " -NoNewline -ForegroundColor Red }
Write-Host $stdOut
}
}
end
{ $ErrorRecordFound }
}
用法:
$Options = @(.....)
$Expr = "ghdl.exe -a " + ($Options -join " ") + " " + $File + " 2>&1"
$ret = Invoke-Expression $Expr | Format-NativeCommandStreams
通常,编译器每行发出一条消息(错误或警告)。如下面的屏幕截图所示,某些消息最多被砍成8行。这就是为什么我的输出颜色无法按预期工作的原因。超过几行被检测为错误(误报),因此我无法在日志中找到真正的错误。
Normally, the compiler emits one message (error or warning) per line. As shown in the screenshot below, some messages got chopped in up to 8 lines. That's the reason why my output coloring does not work as expected. More over some lines are detected as errors (false positives), so I can't find the real error in the logs.
示例:
C:\Altera\15.0\quartus\eda\sim_lib\altera_mf.vhd:
39963:
53
:
warning:
universal integer bound must be numeric literal or attribute
C:\Altera\15.0\quartus\eda\sim_lib\altera_mf.vhd
:41794:36:warning: universal integer bound must be numeric literal or attribute
预期结果:
C:\Altera\15.0\quartus\eda\sim_lib\altera_mf.vhd:39963:53:warning: universal integer bound must be numeric literal or attribute
C:\Altera\15.0\quartus\eda\sim_lib\altera_mf.vhd:41794:36:warning: universal integer bound must be numeric literal or attribute
据我所知,编译器(ghdl.exe)确实会以实线发出消息。
As far as I can see, the compiler (ghdl.exe) does emit the messages as full lines.
问题:
- 为什么会这样?
- 谁能解决这个问题?
推荐答案
解决方案
可执行文件的 stderr
的完整输出被简单拆分 System.Management.Automation.ErrorRecord
类型的几个对象。实际的分割似乎不是确定性的(*)。此外,部分字符串存储在属性 Exception
内,而不是 TargetObject
内。只有第一个 ErrorRecord
具有非空的 TargetObject
。也就是说,为什么输出的包含字符串 warning
的后续行未采用黄色和白色格式,如下所示:
Solution
The complete output on stderr
of the executable is simply split across several objects of type System.Management.Automation.ErrorRecord
. The actual splitting seems to be non deterministic (*). Moreover, the partial strings are stored inside the property Exception
instead of TargetObject
. Only the first ErrorRecord
has a non-null TargetObject
. That is, why subsequent lines of your output containing the string "warning"
are not formatted in yellow and white, like this one:
:41794:36:warning: universal integer bound must be numeric literal or attribute
您的灰色输出来自每个 ErrorRecord
返回的 toString()
方法该记录的属性 Exception.Message
的值。
因此,在格式化之前,必须将所有消息连接在一起以获取整个输出。换行符保留在这些消息中。
Your grey output comes from the toString()
method of each ErrorRecord
which returns the value of the property Exception.Message
of this record.
So one must concatenate all messages together to get the whole output before formatting it. Newlines are preserved in these messages.
EDIT :(*)取决于对文件的写入/刷新调用的顺序。与Powershell的读取调用有关的程序。如果在下面的测试程序中的每个 fprintf()
之后添加 fflush(stderr)
,将会有更多 ErrorRecord
对象。除了第一个似乎是确定性的,其中一些包含2条输出行,其中一些包含3条行。
EDIT: (*) It depends on the order of write/flush calls of the program in relation to the read calls of the Powershell. If one adds a fflush(stderr)
after each fprintf()
in my test program below, there will be much more ErrorRecord
objects. Except the first one, which seems deterministic, some of them include 2 output lines and some of them 3.
我没有使用GHDL,而是从一个新的Visual Studio项目开始,并使用以下代码创建了控制台应用程序(HelloWorldEx)。它只是在 stderr
Instead of using GHDL I started with a new Visual Studio project and created a console application (HelloWorldEx) with the following code. It simply prints out a lot of numbered lines on stderr
#include "stdafx.h"
#include <stdio.h>
int _tmain(int argc, _TCHAR* argv[])
{
// Print some warning messages on stderr
for(int i=0; i<70; i++) {
fprintf(stderr, "warning:%070d\n", i); // 80 bytes per line including CR+LF
}
return 0; // exit code is not relevant
}
然后我编译程序并在其中执行Powershell:
(编辑:从我自己的脚本中删除了调试代码)
Then I compiled the program and executed it inside the Powershell with: (EDIT: removed debug code from my own script)
.\HelloWorldEx.exe 2>&1 | set-variable Output
$i = 0
$Output | % {
Write-Host ("--- " + $i + ": " + $_.GetType() + " ------------------------")
Write-Host ($_ | Format-List -Force | Out-String)
$i++
}
这是脚本的输出。如您所见,我的程序的输出被分成3个 ErrorRecords
(实际可能有所不同):
This was the output of the script. As you can see, the output of my program is split accross 3 ErrorRecords
(the actual might differ):
--- 0: System.Management.Automation.ErrorRecord ------------------------
writeErrorStream : True
Exception : System.Management.Automation.RemoteException: warning:00000000000000000000000000000000000000000
00000000000000000000000000000
TargetObject : warning:0000000000000000000000000000000000000000000000000000000000000000000000
CategoryInfo : NotSpecified: (warning:0000000...000000000000000:String) [], RemoteException
FullyQualifiedErrorId : NativeCommandError
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 0, 0}
PSMessageDetails :
--- 1: System.Management.Automation.ErrorRecord ------------------------
writeErrorStream : True
Exception : System.Management.Automation.RemoteException: warning:00000000000000000000000000000000000000000
00000000000000000000000000001
warning:0000000000000000000000000000000000000000000000000000000000000000000002
warning:0000000000000000000000000000000000000000000000000000000000000000000003
warning:0000000000000000000000000000000000000000000000000000000000000000000004
warning:0000000000000000000000000000000000000000000000000000000000000000000005
warning:0000000000000000000000000000000000000000000000000000000000000000000006
warning:0000000000000000000000000000000000000000000000000000000000000000000007
warning:0000000000000000000000000000000000000000000000000000000000000000000008
warning:0000000000000000000000000000000000000000000000000000000000000000000009
warning:0000000000000000000000000000000000000000000000000000000000000000000010
warning:0000000000000000000000000000000000000000000000000000000000000000000011
warning:0000000000000000000000000000000000000000000000000000000000000000000012
warning:0000000000000000000000000000000000000000000000000000000000000000000013
warning:0000000000000000000000000000000000000000000000000000000000000000000014
warning:0000000000000000000000000000000000000000000000000000000000000000000015
warning:0000000000000000000000000000000000000000000000000000000000000000000016
warning:0000000000000000000000000000000000000000000000000000000000000000000017
warning:0000000000000000000000000000000000000000000000000000000000000000000018
warning:0000000000000000000000000000000000000000000000000000000000000000000019
warning:0000000000000000000000000000000000000000000000000000000000000000000020
warning:0000000000000000000000000000000000000000000000000000000000000000000021
warning:0000000000000000000000000000000000000000000000000000000000000000000022
warning:0000000000000000000000000000000000000000000000000000000000000000000023
warning:0000000000000000000000000000000000000000000000000000000000000000000024
warning:0000000000000000000000000000000000000000000000000000000000000000000025
warning:0000000000000000000000000000000000000000000000000000000000000000000026
warning:0000000000000000000000000000000000000000000000000000000000000000000027
warning:0000000000000000000000000000000000000000000000000000000000000000000028
warning:0000000000000000000000000000000000000000000000000000000000000000000029
warning:0000000000000000000000000000000000000000000000000000000000000000000030
warning:0000000000000000000000000000000000000000000000000000000000000000000031
warning:0000000000000000000000000000000000000000000000000000000000000000000032
warning:0000000000000000000000000000000000000000000000000000000000000000000033
warning:0000000000000000000000000000000000000000000000000000000000000000000034
warning:0000000000000000000000000000000000000000000000000000000000000000000035
warning:0000000000000000000000000000000000000000000000000000000000000000000036
warning:0000000000000000000000000000000000000000000000000000000000000000000037
warning:0000000000000000000000000000000000000000000000000000000000000000000038
warning:0000000000000000000000000000000000000000000000000000000000000000000039
warning:0000000000000000000000000000000000000000000000000000000000000000000040
warning:0000000000000000000000000000000000000000000000000000000000000000000041
warning:0000000000000000000000000000000000000000000000000000000000000000000042
warning:0000000000000000000000000000000000000000000000000000000000000000000043
warning:0000000000000000000000000000000000000000000000000000000000000000000044
warning:0000000000000000000000000000000000000000000000000000000000000000000045
warning:0000000000000000000000000000000000000000000000000000000000000000000046
warning:0000000000000000000000000000000000000000000000000000000000000000000047
warning:0000000000000000000000000000000000000000000000000000000000000000000048
warning:0000000000000000000000000000000000000000000000000000000000000000000049
warning:0000000000000000000000000000000000000000000000000000000000000000000050
warning:00000000000000000000000000000000000000000000000000000000000
TargetObject :
CategoryInfo : NotSpecified: (:) [], RemoteException
FullyQualifiedErrorId : NativeCommandErrorMessage
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 0, 1}
PSMessageDetails :
--- 2: System.Management.Automation.ErrorRecord ------------------------
writeErrorStream : True
Exception : System.Management.Automation.RemoteException: 00000000051
warning:0000000000000000000000000000000000000000000000000000000000000000000052
warning:0000000000000000000000000000000000000000000000000000000000000000000053
warning:0000000000000000000000000000000000000000000000000000000000000000000054
warning:0000000000000000000000000000000000000000000000000000000000000000000055
warning:0000000000000000000000000000000000000000000000000000000000000000000056
warning:0000000000000000000000000000000000000000000000000000000000000000000057
warning:0000000000000000000000000000000000000000000000000000000000000000000058
warning:0000000000000000000000000000000000000000000000000000000000000000000059
warning:0000000000000000000000000000000000000000000000000000000000000000000060
warning:0000000000000000000000000000000000000000000000000000000000000000000061
warning:0000000000000000000000000000000000000000000000000000000000000000000062
warning:0000000000000000000000000000000000000000000000000000000000000000000063
warning:0000000000000000000000000000000000000000000000000000000000000000000064
warning:0000000000000000000000000000000000000000000000000000000000000000000065
warning:0000000000000000000000000000000000000000000000000000000000000000000066
warning:0000000000000000000000000000000000000000000000000000000000000000000067
warning:0000000000000000000000000000000000000000000000000000000000000000000068
warning:0000000000000000000000000000000000000000000000000000000000000000000069
TargetObject :
CategoryInfo : NotSpecified: (:) [], RemoteException
FullyQualifiedErrorId : NativeCommandErrorMessage
ErrorDetails :
InvocationInfo : System.Management.Automation.InvocationInfo
PipelineIterationInfo : {0, 0, 2}
PSMessageDetails :
这篇关于为什么PowerShell会在stderr上砍消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!