1 | #Region "Microsoft.VisualBasic::d4b05bb47f5069ec79e736c002643e1d, Microsoft.VisualBasic.Core\My\Log4VB.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 | ' Module Log4VB |
35 | ' |
36 | ' Function: getColor, Print |
37 | ' |
38 | ' Sub: WriteLine |
39 | ' |
40 | ' |
41 | ' /********************************************************************************/ |
42 | |
43 | #End Region |
44 | |
45 | Imports System.Runtime.CompilerServices |
46 | Imports Microsoft.VisualBasic.ApplicationServices.Debugging.Logging |
47 | Imports Microsoft.VisualBasic.Language.C |
48 | Imports Microsoft.VisualBasic.Terminal |
49 | |
50 | Namespace My |
51 | |
52 | ''' <summary> |
53 | ''' VB.NET <see cref="Console"/> log framework. |
54 | ''' </summary> |
55 | Public Module Log4VB |
56 | |
57 | Friend ReadOnly logs As New List(Of LoggingDriver) |
58 | |
59 | ''' <summary> |
60 | ''' ``<see cref="MSG_TYPES"/> -> <see cref="ConsoleColor"/>`` |
61 | ''' </summary> |
62 | ReadOnly DebuggerTagColors As New Dictionary(Of Integer, ConsoleColor) From { |
63 | {MSG_TYPES.DEBUG, ConsoleColor.DarkGreen}, |
64 | {MSG_TYPES.ERR, ConsoleColor.Red}, |
65 | {MSG_TYPES.INF, ConsoleColor.Blue}, |
66 | {MSG_TYPES.WRN, ConsoleColor.Yellow} |
67 | } |
68 | |
69 | <MethodImpl(MethodImplOptions.AggressiveInlining)> |
70 | <Extension> |
71 | Private Function getColor(level As Integer) As ConsoleColor |
72 | Return If(DebuggerTagColors.ContainsKey(level), DebuggerTagColors(level), CType(level, ConsoleColor)) |
73 | End Function |
74 | |
75 | ''' <summary> |
76 | ''' 头部和消息字符串都是放在一个task之中进行输出的,<see cref="xConsole"/>的输出也是和内部的debugger输出使用的同一个消息线程 |
77 | ''' </summary> |
78 | ''' <param name="header"></param> |
79 | ''' <param name="msg"></param> |
80 | ''' <param name="msgColor"></param> |
81 | ''' <param name="level"><see cref="ConsoleColor"/> or <see cref="MSG_TYPES"/></param> |
82 | Public Function Print(header$, msg$, msgColor As ConsoleColor, level As Integer) As Boolean |
83 | My.InnerQueue.AddToQueue( |
84 | Sub() |
85 | ' 2018-12-14 |
86 | ' 替换meta chars可能会导致windows下的路径显示出现bug |
87 | ' msg = msg.ReplaceMetaChars |
88 | |
89 | If ForceSTDError Then |
90 | Call Console.Error.WriteLine($"[{header}]{msg}") |
91 | Else |
92 | Dim cl As ConsoleColor = Console.ForegroundColor |
93 | Dim headColor As ConsoleColor = getColor(level) |
94 | |
95 | If msgColor = headColor Then |
96 | Console.ForegroundColor = headColor |
97 | Console.WriteLine($"[{header}]{msg}") |
98 | Console.ForegroundColor = cl |
99 | Else |
100 | Call Console.Write("[") |
101 | Console.ForegroundColor = headColor |
102 | Call Console.Write(header) |
103 | Console.ForegroundColor = cl |
104 | Call Console.Write("]") |
105 | |
106 | Call WriteLine(msg, msgColor) |
107 | End If |
108 | End If |
109 | End Sub) |
110 | |
111 | For Each driver As LoggingDriver In logs |
112 | Call driver(header, msg, level) |
113 | Next |
114 | |
115 | Return False |
116 | End Function |
117 | |
118 | ''' <summary> |
119 | ''' 输出的终端消息带有指定的终端颜色色彩,当<see cref="UsingxConsole"/>为True的时候, |
120 | ''' <paramref name="msg"/>参数之中的文本字符串兼容<see cref="xConsole"/>语法, |
121 | ''' 而<paramref name="color"/>将会被<see cref="xConsole"/>覆盖而不会起作用 |
122 | ''' </summary> |
123 | ''' <param name="msg">兼容<see cref="xConsole"/>语法</param> |
124 | ''' <param name="color">当<see cref="UsingxConsole"/>参数为True的时候,这个函数参数将不会起作用</param> |
125 | <Extension> |
126 | Public Sub WriteLine(msg$, color As ConsoleColor) |
127 | If Mute Then |
128 | Return |
129 | End If |
130 | |
131 | If ForceSTDError Then |
132 | Console.Error.WriteLine(msg) |
133 | Else |
134 | If UsingxConsole AndAlso App.IsMicrosoftPlatform Then |
135 | Call xConsole.CoolWrite(msg) |
136 | Else |
137 | ' 使用传统的输出输出方法 |
138 | Dim cl As ConsoleColor = Console.ForegroundColor |
139 | |
140 | Console.ForegroundColor = color |
141 | Console.WriteLine(msg) |
142 | Console.ForegroundColor = cl |
143 | End If |
144 | End If |
145 | |
146 | #If DEBUG Then |
147 | Call Debug.WriteLine(msg) |
148 | #End If |
149 | End Sub |
150 | End Module |
151 | End Namespace |