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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
<#
.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;

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

    process {
        try{
            $LINE = "$LINE "
            $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])")
            }
            elseif ($LINE -match "\d\d\d\d\d\d\d\d/\d\d:\d\d:\d\d.\d\d\d") {
                $orgTimeStr = $matches[0]
                $newTimeStr = "$($orgTimeStr.Substring(0, 4))-$($orgTimeStr.Substring(4, 2))-$($orgTimeStr.Substring(6, 2)) $($orgTimeStr.Substring(9))"
                $curTime = [DateTime]::Parse($newTimeStr)
            }
            else {
                $curTime = $_oldTime
            }

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

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


            $res = $LINE -match "LOGID\s:\s\d+\s\|"

            if (!$res) {
                return
            }

            $res = $matches[0] -match "\d+"
            $logId = $matches[0]

        


            $res = $LINE -match "TID\s:\s[-+]?\d+\s\|"

            if (!$res) {
                return
            }

            $res = $matches[0] -match "[-+]?\d+"
            $tid = $matches[0]
        



            $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
            $idxBar = $LINE.LastIndexOf("|")
            $idxIn = $LINE.LastIndexOf(" ")
            $log = $LINE.Substring($idxBar + 2)

        

            if ($idxIn -gt 0) {
                $funcName = $LINE.Substring($idxBar + 1, $idxIn - $idxBar - 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)) {
                if ($stack.Count -gt 0) {
                    $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;
            $beuatyLog = "[$([string]::Format("{0,6:N0}", $logId))][$([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 ($_funcColorDic.ContainsKey($funcName)) {
                Write-Host $beuatyLog -ForegroundColor $_funcColorDic[$funcName]
            }
            else {
                Write-Host $beuatyLog
            }
        } catch {
            Write-Host $LINE
        }
    }
}