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 = @{}
    }

    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 "TID\s:\s\d+\s\|"
        $res = $matches[0] -match "\d+"
        $tid = $matches[0]

        if($_mainTid -eq 0) 
        {
            $_mainTid = $tid
        }

        $idx = $LINE.IndexOf(" | ")
        $log = $LINE.Substring($idx + 3)

        $res = $LINE -match "\s\|\s.*\s"
        $res = $matches[0] -match "\s[\w\d.]*\s"
        $funcName = $matches[0]
        $funcName = $funcName.Trim()

        $stack = $null

        if($_stackDic.ContainsKey($tid)) 
        {
            $stack = $_stackDic[$tid]
        }
        else
        {
            $stack = New-Object "System.Collections.Generic.Stack[string]"
            $_stackDic[$tid] = $stack
        }

        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;
        $beuatyLog = "[$([string]::Format("{0,10:N0}", $period.TotalMilliseconds))ms]$($leftPadding)$($log)"
        Write-Host $beuatyLog
    }
}