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 "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.*\sin"
        $res = $matches[0] -match "\s[\w\d.]*\s"
        $funcName = $null


        if($res)
        {
            $funcName = $matches[0]
            $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))
        {
            $funcName = $stack.Peek()
        }

        if(!$_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,10:N0}", $period.TotalMilliseconds))ms]$($leftPadding)TID[$tid] $($log)"
        }
        else
        {
            $beuatyLog = "[$([string]::Format("{0,10:N0}", $period.TotalMilliseconds))ms]$($leftPadding)$($log)"
        }

        Write-Host $beuatyLog -ForegroundColor $_funcColorDic[$funcName]
    }
}