如何使PowerShell CMake模块显示完整的错误消息而不进行修剪?

时间:2019-03-01 12:27:04

标签: powershell cmake

在使用失败的Powershell脚本运行CMake时,它总是剪切错误消息并以...后缀(或加前缀),或者仅显示前几个单词,或者最后几个单词,从不显示完整错误。

例如:

Checking paths...     OK
Looking for MSBuild...    OK
Looking for Qt5...    OK
Looking for OpenSSL...    OK
Looking for nsis...    OK
Looking for git...    OK
Looking for cmake...    OK
Configuring the project...
Running cmake
-- The C compiler identification is MSVC 19.0.24215.1
-- The CXX compiler identification is MSVC 19.0.24215.1
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working C compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
-- Check for working CXX compiler: C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
cmake : CMake Error at extensions/extension-mass-delete/CMakeLists.txt:26 (find_package):
At C:\Users\Petr Bena\Documents\huggle3-qt-lx\windows\release.ps1:172 char:5
+     cmake ..\..\src\ -G "$cmake_generator" -DWEB_ENGINE=true -DPYTHON ...
+     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CMake Error at ...(find_package)::String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

这种错误根本无法识别出什么错误,因为它太笼统了。如何展开它,以便看到完整的错误消息?

1 个答案:

答案 0 :(得分:1)

您可以使用try{ } catch { }块来捕获异常,然后访问错误属性。这是我编写的自定义错误编写函数:

Function Write-CustomError()
{
<#
.Synopsis
   Displays error information to the console
.DESCRIPTION
    Writes property information from the current [ErrorRecord] object
    in the pipeline to the console
.EXAMPLE
   Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_
.EXAMPLE
   Write-CustomError -UserMessage "Exception occurred at memory location $x" -ErrorObject $_ -FullDetail
.INPUTS
   $Error[0]
.OUTPUTS
   [String]
.COMPONENT
   adminkitMiscTools   
.FUNCTIONALITY
   General Utility
#>
    [cmdletBinding()]
    param(
        [Parameter(Mandatory=$False)]
        [String]$UserMessage,

        [Parameter(Mandatory=$True)]
        [Object]$ErrorObject,

        [Parameter(Mandatory=$false)]
        [Switch]$FullDetail
    )

    BEGIN
    {}
    PROCESS
    {
        if($UserMessage) {
             Write-Host "`nERROR: $UserMessage" -ForegroundColor Red
        }

        if($FullDetail)
        {
            $ErrorData = $ErrorData + [PSCustomObject]@{AccountUsed=$ENV:USERNAME;
                                            ExceptionMessage=$ErrorObject.ToString();
                                            CategoryInfo=$ErrorObject.CategoryInfo;
                                            ExceptionType=$ErrorObject.Exception.GetType();
                                            ErrorDetails=$ErrorObject.ErrorDetails;
                                            FullyQualifiedErrorId=$ErrorObject.FullyQualifiedErrorId;
                                            InvocationInfo=$ErrorObject.InvocationInfo;
                                            PipelineIterationInfo=$ErrorObject.PipelineIterationInfo;
                                            ScriptStackTrace=$ErrorObject.ScriptStackTrace
                                            TargetObject=$ErrorObject.TargetObject;
                                            }
        }
        return $ErrorData
    }
    END
    {}
}

在您的脚本中:

try { 
      # your cmake command here 
 }
catch {
     Write-CustomError -UserMessage 'There was an error' -ErrorObject $_ -FullDetail
}

这将为您提供有关该错误的更多详细信息,并且不应该剪切消息