Last active
June 10, 2023 03:04
-
-
Save fokklz/991f721340e378369d00f89a3c7b18df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Write-Out { | |
param( | |
[Parameter(Mandatory = $true, Position = 0)] | |
[string]$Message, | |
[string]$Type = "INFO", | |
[switch]$LogOnly, | |
[switch]$ForceWriteLog, | |
[Parameter(ValueFromRemainingArguments = $true)] | |
$highlighted | |
) | |
$fullMessage = $Message | |
$printed = $false | |
if ($Message -contains '%s') { | |
$fullMessage = "" | |
$splittedMessage = $Message -split '%s' | |
# print directly if not colored log type and not log only | |
for ($i = 0; $i -lt $splittedMessage.Length; $i++) { | |
if ($Type -notmatch $RGX_COLORED_LOG_TYPES -and -not $LogOnly) { | |
Write-Host $splittedMessage[$i] -NoNewline | |
} | |
$fullMessage += $splittedMessage[$i] | |
if ($i -lt $highlighted.Count) { | |
if ($Type -notmatch $RGX_COLORED_LOG_TYPES -and -not $LogOnly) { | |
Write-Host $highlighted[$i] -ForegroundColor Cyan -NoNewline | |
} | |
$fullMessage += $highlighted[$i] | |
} | |
} | |
# new line because of -NoNewline | |
if ($Type -notmatch $RGX_COLORED_LOG_TYPES -and -not $LogOnly) { | |
Write-Host "`r" | |
$printed = $true | |
} | |
} | |
# print colored if colored log type and not log only | |
if (-not $LogOnly) { | |
switch ($Type) { | |
'SYSTEM' { | |
Write-Host $fullMessage -ForegroundColor Green | |
} | |
'WARN' { | |
Write-Host $fullMessage -ForegroundColor Yellow | |
} | |
'ERROR' { | |
Write-Host $fullMessage -ForegroundColor Red | |
} | |
default { | |
if (-not $printed) { | |
Write-Host $fullMessage | |
} | |
} | |
} | |
} | |
$LOG_TEMP.Add("$(Get-Date -Format "dd-MM-yyyy HH:mm:ss") [$Type] $fullMessage") | Out-Null | |
if ($LOG_TEMP.Count -gt 30 -or $ForceWriteLog) { | |
$LOG_TEMP | Out-File -FilePath $LOG_FILE -Append | |
$LOG_TEMP.Clear() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment