logsloth.psm1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

<#
.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]
    }
}