Skip to content

Commit

Permalink
adding multiple loggers (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
cyberbuff authored Jul 16, 2024
1 parent 96dc214 commit 006bd21
Showing 1 changed file with 39 additions and 22 deletions.
61 changes: 39 additions & 22 deletions Public/Invoke-AtomicTest.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,15 @@ function Invoke-AtomicTest {
)
BEGIN { } # Intentionally left blank and can be removed
PROCESS {
function ConvertTo-LoggerArray {
param (
[Parameter(Mandatory = $true)]
[string]$Loggers
)

return $Loggers -split ',' | ForEach-Object { $_.Trim() }
}

$PathToAtomicsFolder = (Resolve-Path $PathToAtomicsFolder).Path

Write-Verbose -Message 'Attempting to run Atomic Techniques'
Expand Down Expand Up @@ -180,36 +189,38 @@ function Invoke-AtomicTest {
}

if ($isLoggingModuleSet) {
if (Get-Module -name $LoggingModule) {
Write-Verbose "Using Logger: $LoggingModule"
}
else {
Write-Host -Fore Yellow "Logger not found: ", $LoggingModule
}
ConvertTo-LoggerArray $LoggingModule | ForEach-Object {
if (Get-Module -name $_) {
Write-Verbose "Using Logger: $_"
}
else {
Write-Host -Fore Yellow "Logger not found: ", $_
}

# Change the defult logFile extension from csv to json and add a timestamp if using the Attire-ExecutionLogger
if ($LoggingModule -eq "Attire-ExecutionLogger") { $ExecutionLogPath = $ExecutionLogPath.Replace("Invoke-AtomicTest-ExecutionLog.csv", "Invoke-AtomicTest-ExecutionLog-timestamp.json") }
$ExecutionLogPath = $ExecutionLogPath.Replace("timestamp", $(Get-Date -UFormat %s))
# Change the defult logFile extension from csv to json and add a timestamp if using the Attire-ExecutionLogger
if ($_ -eq "Attire-ExecutionLogger") { $ExecutionLogPath = $ExecutionLogPath.Replace("Invoke-AtomicTest-ExecutionLog.csv", "Invoke-AtomicTest-ExecutionLog-timestamp.json") }
$ExecutionLogPath = $ExecutionLogPath.Replace("timestamp", $(Get-Date -UFormat %s))

if (Get-Command "$LoggingModule\Start-ExecutionLog" -erroraction silentlycontinue) {
if (Get-Command "$LoggingModule\Write-ExecutionLog" -erroraction silentlycontinue) {
if (Get-Command "$LoggingModule\Stop-ExecutionLog" -erroraction silentlycontinue) {
Write-Verbose "All logging commands found"
if (Get-Command "$_\Start-ExecutionLog" -erroraction silentlycontinue) {
if (Get-Command "$_\Write-ExecutionLog" -erroraction silentlycontinue) {
if (Get-Command "$_\Stop-ExecutionLog" -erroraction silentlycontinue) {
Write-Verbose "All logging commands found"
}
else {
Write-Host "Stop-ExecutionLog not found or loaded from the wrong module"
return
}
}
else {
Write-Host "Stop-ExecutionLog not found or loaded from the wrong module"
Write-Host "Write-ExecutionLog not found or loaded from the wrong module"
return
}
}
else {
Write-Host "Write-ExecutionLog not found or loaded from the wrong module"
Write-Host "Start-ExecutionLog not found or loaded from the wrong module"
return
}
}
else {
Write-Host "Start-ExecutionLog not found or loaded from the wrong module"
return
}

# Here we're rebuilding an equivalent command line to put in the logs
$commandLine = "Invoke-AtomicTest $AtomicTechnique"
Expand Down Expand Up @@ -290,7 +301,9 @@ function Invoke-AtomicTest {
}

$startTime = Get-Date
&"$LoggingModule\Start-ExecutionLog" $startTime $ExecutionLogPath $executionHostname $executionUser $commandLine (-Not($IsLinux -or $IsMacOS))
ConvertTo-LoggerArray $LoggingModule | ForEach-Object {
&"$_\Start-ExecutionLog" $startTime $ExecutionLogPath $executionHostname $executionUser $commandLine (-Not($IsLinux -or $IsMacOS))
}
}

function Platform-IncludesCloud {
Expand Down Expand Up @@ -489,7 +502,9 @@ function Invoke-AtomicTest {
if (Get-Command 'Invoke-ARTPostAtomicHook' -errorAction SilentlyContinue) { Invoke-ARTPostAtomicHook $test $InputArgs }
$stopTime = Get-Date
if ($isLoggingModuleSet) {
&"$LoggingModule\Write-ExecutionLog" $startTime $stopTime $AT $testCount $test.name $test.auto_generated_guid $test.executor.name $test.description $final_command $ExecutionLogPath $executionHostname $executionUser $res (-Not($IsLinux -or $IsMacOS))
ConvertTo-LoggerArray $LoggingModule | ForEach-Object {
&"$_\Write-ExecutionLog" $startTime $stopTime $AT $testCount $test.name $test.auto_generated_guid $test.executor.name $test.description $final_command $ExecutionLogPath $executionHostname $executionUser $res (-Not($IsLinux -or $IsMacOS))
}
}
Write-KeyValue "Done executing test: " $testId
}
Expand Down Expand Up @@ -521,7 +536,9 @@ function Invoke-AtomicTest {
}

if ($isLoggingModuleSet) {
&"$LoggingModule\Stop-ExecutionLog" $startTime $ExecutionLogPath $executionHostname $executionUser (-Not($IsLinux -or $IsMacOS))
ConvertTo-LoggerArray $LoggingModule | ForEach-Object {
&"$_\Stop-ExecutionLog" $startTime $ExecutionLogPath $executionHostname $executionUser (-Not($IsLinux -or $IsMacOS))
}
}

} # End of PROCESS block
Expand Down

0 comments on commit 006bd21

Please sign in to comment.