1 #Region "Microsoft.VisualBasic::a1fbaec4b8d76d7bd2d01c448e9e1b8c, Microsoft.VisualBasic.Core\ApplicationServices\Debugger\Exception\ExceptionData.vb"
2
3     ' Author:
4     
5     '       asuka (amethyst.asuka@gcmodeller.org)
6     '       xie (genetics@smrucc.org)
7     '       xieguigang (xie.guigang@live.com)
8     
9     ' Copyright (c) 2018 GPL3 Licensed
10     
11     
12     ' GNU GENERAL PUBLIC LICENSE (GPL3)
13     
14     
15     ' This program is free software: you can redistribute it and/or modify
16     ' it under the terms of the GNU General Public License as published by
17     ' the Free Software Foundation, either version 3 of the License, or
18     ' (at your option) any later version.
19     
20     ' This program is distributed in the hope that it will be useful,
21     ' but WITHOUT ANY WARRANTY; without even the implied warranty of
22     ' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23     ' GNU General Public License for more details.
24     
25     ' You should have received a copy of the GNU General Public License
26     ' along with this program. If not, see <http://www.gnu.org/licenses/>.
27
28
29
30     ' /********************************************************************************/
31
32     ' Summaries:
33
34     '     Class ExceptionData
35     
36     '         Properties: Message, StackTrace, TypeFullName
37     
38     '         Function: CreateInstance, GetCurrentStackTrace, ToString
39     
40     '     Class StackFrame
41     
42     '         Properties: File, Line, Method
43     
44     '         Function: Parser, ToString
45     
46     '     Class Method
47     
48     '         Properties: [Module], [Namespace], Method
49     
50     '         Constructor: (+1 OverloadsSub New
51     '         FunctionToString
52     
53     
54     ' /********************************************************************************/
55
56 #End Region
57
58 Namespace ApplicationServices.Debugging.Diagnostics
59
60     Public Class ExceptionData
61
62         Public Property TypeFullName As String
63         Public Property Message As String()
64         Public Property StackTrace As StackFrame()
65
66         Public Overrides Function ToString() As String
67             Return TypeFullName
68         End Function
69
70         Public Shared Function CreateInstance(messages$(), stackTrace$(), type$) As ExceptionData
71             Return New ExceptionData With {
72                 .TypeFullName = type,
73                 .Message = messages,
74                 .StackTrace = stackTrace _
75                     .Select(AddressOf StackFrame.Parser) _
76                     .ToArray
77             }
78         End Function
79
80         ''' <summary>
81         ''' Parsing <see cref="Environment.StackTrace"/>, gets current stack trace information.
82         ''' </summary>
83         ''' <returns></returns>
84         Public Shared Function GetCurrentStackTrace() As StackFrame()
85             Return Environment.StackTrace _
86                 .LineTokens _
87                 .Where(Function(s) Not s.StringEmpty) _
88                 .Skip(3) _
89                 .Select(Function(s)
90                             s = Mid(s, 6).Trim
91                             Return StackFrame.Parser(s)
92                         End Function) _
93                 .ToArray
94         End Function
95     End Class
96
97     Public Class StackFrame
98
99         ''' <summary>
100         ''' Method call
101         ''' </summary>
102         ''' <returns></returns>
103         Public Property Method As Method
104         ''' <summary>
105         ''' The file path of the source file
106         ''' </summary>
107         ''' <returns></returns>
108         Public Property File As String
109         ''' <summary>
110         ''' The line number in current source <see cref="File"/>.
111         ''' </summary>
112         ''' <returns></returns>
113         Public Property Line As String
114
115         Public Overrides Function ToString() As String
116             Return $"{Method} at {File}:line {Line}"
117         End Function
118
119         Public Shared Function Parser(line As StringAs StackFrame
120             With line.Replace("位置""at").Replace("行号""line")
121                 Dim t = .StringSplit(" at ")
122                 Dim method = t(0)
123                 Dim location = t.ElementAtOrDefault(1)
124                 Dim file$, lineNumber$
125
126                 If Not location.StringEmpty Then
127                     t = location.StringSplit("[:]line ")
128                     file = t(0)
129                     lineNumber = t(1)
130                 Else
131                     file = "Unknown"
132                     lineNumber = 0
133                 End If
134
135                 Return New StackFrame With {
136                     .Method = New Method(method),
137                     .File = file,
138                     .Line = lineNumber
139                 }
140             End With
141         End Function
142     End Class
143
144     Public Class Method
145
146         Public Property [Namespace] As String
147         Public Property [Module] As String
148         Public Property Method As String
149
150         Sub New(s As String)
151             Dim t = s.Split("."c).AsList
152
153             Method = t(-1)
154             [Module] = t(-2)
155             [Namespace] = t.Take(t.Count - 2).JoinBy(".")
156         End Sub
157
158         Public Overrides Function ToString() As String
159             Return $"{[Namespace]}.{[Module]}.{Method}"
160         End Function
161     End Class
162 End Namespace