logsloth.psm1
<#
.SYNOPSIS .EXAMPLE #> function logsloth { [CmdletBinding()] param ( [Parameter(Mandatory=$true, ValueFromPipeline=$true, ValueFromPipelinebyPropertyName=$true)] [System.String] $LINE ) begin { $_oldTime = $null $_stackDic = @{} $_mainTid = 0 $_threadIndentDic = @{} $_funcColorDic = @{} $_COLOR_LIST = @("Green", "Red", "DarkGray", "DarkGreen", "DarkYellow", "Gray", "Magenta", "Cyan", "DarkCyan", "DarkRed", "Yellow") $_COLOR_LIST | Sort-Object { Get-Random } | Set-Variable _COLOR_LIST $_clrIdx = 0; } process { $LINE = "$LINE " $res = $LINE -match "\d\d-\d\d\s\d\d:\d\d:\d\d.\d\d\d" $curTime = $null if($res) { $curTime = [DateTime]::Parse("18-$($matches[0])") if($_oldTime -eq $null) { $_oldTime = $curTime } } else { $curTime = $_oldTime } $period = $curTime - $_oldTime $_oldTime = $curTime $res = $LINE -match "LOGID\s:\s\d+\s\|" $res = $matches[0] -match "\d+" $logId = $matches[0] if(!$res) { return } $res = $LINE -match "TID\s:\s\d+\s\|" $res = $matches[0] -match "\d+" $tid = $matches[0] if(!$res) { return } if($_mainTid -eq 0) { $_mainTid = $tid } $res = $LINE -match "TID\s:\s\d+\s\|\s" $idx = $LINE.IndexOf($matches[0]) $log = $LINE.Substring($idx + $matches[0].Length) $funcName = $null $idx = $LINE.LastIndexOf("|") $idx2 = $LINE.LastIndexOf("in") if($idx2 -gt 0) { $funcName = $LINE.Substring($idx + 1, $idx2 - $idx - 2) $funcName = $funcName.Trim() } $stack = $null $isNewTid = $false if($_stackDic.ContainsKey($tid)) { $stack = $_stackDic[$tid] } else { $stack = New-Object "System.Collections.Generic.Stack[string]" $_stackDic[$tid] = $stack if($tid -ne $_mainTid) { $isNewTid = $true } } if([string]::IsNullOrEmpty($funcName) -and ($stack.Count -gt 0)) { $funcName = $stack.Peek() } if(($funcName -ne $null) -and !$_funcColorDic.ContainsKey($funcName)) { $_funcColorDic[$funcName] = $_COLOR_LIST[$_clrIdx]; $_clrIdx = ($_clrIdx + 1) % $_COLOR_LIST.Length; } if($LINE -match "\sin\s") { $stack.Push($funcName) } $padCount = 0 if($_threadIndentDic.ContainsKey($tid)) { $padCount += $_threadIndentDic[$tid] } else { if($tid -ne $_mainTid) { $padCount += $_stackDic.Count * 20; } $_threadIndentDic[$tid] = $padCount; } $padCount += $stack.Count * 2; if(($LINE -match "\sout\s") -and ($stack.Count -gt 0)) { $res = $stack.Pop() if($stack.Count -eq 0) { $_stackDic.Remove($tid) } } $leftPadding = " " * $padCount; if($isNewTid) { $beuatyLog = "[$([string]::Format("{0,6:N0}", $logId))][$([string]::Format("{0,5:N0}", $period.TotalMilliseconds))ms]$($leftPadding)TID[$tid] $($log)" } else { $beuatyLog = "[$([string]::Format("{0,6:N0}", $logId))][$([string]::Format("{0,5:N0}", $period.TotalMilliseconds))ms]$($leftPadding)$($log)" } if($funcName -eq $null) { Write-Host $beuatyLog } else { Write-Host $beuatyLog -ForegroundColor $_funcColorDic[$funcName] } } } |