logsloth.psm1

<#
.SYNOPSIS
.EXAMPLE
#>

function logsloth {
    [CmdletBinding()]
    param
    (
        [Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
        [System.String]
        $LINE,

        [switch]
        [Parameter(Mandatory = $false, ValueFromPipeline = $true, ValueFromPipelinebyPropertyName = $true)]
        $TASKID_AS_SYMBOL = $false
    )

    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;
        $_oldClr = "White"

        $_taskIdSymDic = @{}
        $_TASK_SYM_LIST = "§ ☆ ★ ● ◎ ◇ ◆ ■ △ ▲ ▽ ▼ ◁ ◀ ▷ ▶ ♤ ♠ ♡ ♥ ♧ ♣ ⊙ ◈ ▣ ◐ ◑ ▒ ▤ ▥ ▨ ▧ ▦ ▩ ♨ ☏ ☎ ☜ ☞ ¶ †‡ ↕ ↗ ↙ ↖ ↘ ♭ ♩ ♪ ♬ ㉿ ㈜ ㉠ ㉡ ㉢ ㉣ ㉤ ㉥ ㉦ ㉧ ㉨ ㉩ ㉪ ㉫ ㉬ ㉭ ㉮ ㉯ ㉰ ㉱ ㉲ ㉳ ㉴ ㉵ ㉶ ㉷ ㉸ ㉹ ㉺ ㉻ ⓐ ⓑ ⓒ ⓓ ⓔ ⓕ ⓖ ⓗ ⓘ ⓙ ⓚ ⓛ ⓜ ⓝ ⓞ ⓟ ⓠ ⓡ ⓢ ⓣ ⓤ ⓥ ⓦ ⓧ ⓨ ⓩ ① ② ③ ④ ⑤ ⑥ ⑦ ⑧ ⑨ ⑩ ⑪ ⑫ ⑬ ⑭ ⑮ ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?"
        $_TASK_SYM_LIST.split(' ', [System.StringSplitOptions]::RemoveEmptyEntries) | Sort-Object { Get-Random } | Set-Variable _TASK_SYM_LIST
        $_taskIdIdx = 0
    }

    process {
        $curTime = $null

        if ($LINE -match "\d\d-\d\d\s\d\d:\d\d:\d\d.\d\d\d") {
            $curTime = [DateTime]::Parse("18-$($matches[0])")
        }
        else {
            Write-Host $LINE -ForegroundColor $_oldClr
            return
        }

        if ($_oldTime -eq $null) {
            $_oldTime = $curTime
        }

        $period = $curTime - $_oldTime
        $_oldTime = $curTime



        $lineTokens = $LINE.Split(" ", [System.StringSplitOptions]::RemoveEmptyEntries)
        $tid = $lineTokens[3]
        $tag = $lineTokens[5]
        $log = [string]::Join( " ", $lineTokens[6..$($lineTokens.Length)] ) 



        $taskId = $null
        $taskSym = $null
        $res = $LINE -match "TASKID\s:\s\d+\s\|"

        if ($res) {
            $res = $matches[0] -match "\d+"
            $taskId = $matches[0]

            if ($_taskIdSymDic.ContainsKey($taskId)) {
                $taskSym = $_taskIdSymDic[$taskId]
            }
            else {
                $taskSym = $_TASK_SYM_LIST[$_taskIdIdx]
                $_taskIdIdx += 1
                $_taskIdSymDic[$taskId] = $taskSym
            }
        }




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

        

        $funcName = $null
        
        for ( $i = 0; $i -lt $lineTokens.Length; $i++ ) {
            $token = $lineTokens[$i]
            if ($token -match "[.cpp|.h|.cc|.c|.cxx|.hpp]:\d+:") {
                $res = $token -split ":"
                $funcName = "$($res[0])::$($res[2])"
                break
            }
        }

        if ($funcName -eq $null) {
            $funcName = "$tag$([string]::Join( " ", $lineTokens[5..6] ))"
            $log = "$tag$log"
        }

        $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 (!$_funcColorDic.ContainsKey($funcName)) {
            $_funcColorDic[$funcName] = $_COLOR_LIST[$_clrIdx];
            $_clrIdx = ($_clrIdx + 1) % $_COLOR_LIST.Length;
        }

        $padCount = 0

        if ($tid -ne $_mainTid) {
            $padCount += ($_stackDic.Count - 1) * 4;
        }

    
        
        $leftPadding = " " * $padCount;
        $beuatyLog = "[$($lineTokens[0]) $($lineTokens[1])][$([string]::Format("{0,5:N0}", $period.TotalMilliseconds))ms]$($leftPadding)";

        if ($isNewTid) {
            $beuatyLog += " TID[$tid]"
        }

        if ($taskId -ne $null) {
            if ($TASKID_AS_SYMBOL) {
                $beuatyLog += $taskSym * 3
            }
            else {
                $beuatyLog += " TASKID[$taskId]"
            }
        }

        $beuatyLog += " $log"

        if (($funcName -ne $null) -and ($_funcColorDic.ContainsKey($funcName))) {
            Write-Host $beuatyLog -ForegroundColor $_funcColorDic[$funcName]
            $_oldClr = $_funcColorDic[$funcName]
        }
        else {
            Write-Host $beuatyLog
        }
    }
}