1 #Region "Microsoft.VisualBasic::114172961e80001497cd83b56e46bcad, Microsoft.VisualBasic.Core\ApplicationServices\Debugger\Logging\LogFile\LogFile.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 LogFile
35     
36     '         Properties: FileName, NowTimeNormalizedString
37     
38     '         Constructor: (+1 OverloadsSub New
39     
40     '         Function: __getDefaultPath, Read, ReadLine, Save, SaveLog
41     '                   SystemInfo, ToString
42     
43     '         Sub: Dispose, (+2 Overloads) LogException, (+4 Overloads) WriteLine
44     
45     
46     ' /********************************************************************************/
47
48 #End Region
49
50 Imports System.IO
51 Imports System.Runtime.CompilerServices
52 Imports System.Text
53 Imports Microsoft.VisualBasic.ComponentModel
54 Imports Microsoft.VisualBasic.Parallel
55 Imports Microsoft.VisualBasic.Terminal.STDIO__
56
57 Namespace ApplicationServices.Debugging.Logging
58
59     ''' <summary>
60     ''' 日志文件记录模块。因为在Linux平台上面,Windows的日志记录API可能不会正常工作,
61     ''' 所以需要这个日志模块来接替Windows的日志模块的工作
62     ''' </summary>
63     ''' <remarks>
64     ''' 这个类模块将输入的信息格式化保存到文本文件之中,记录的信息包括信息头,消息文本,以及消息等级
65     ''' </remarks>
66     Public Class LogFile : Inherits ITextFile
67         Implements ISaveHandle
68         Implements IDisposable
69         Implements I_ConsoleDeviceHandle
70
71         Dim buffer As TextWriter
72         Dim counts&
73
74         ''' <summary>
75         ''' 没有路径名称和拓展名,仅包含有单独的文件名
76         ''' </summary>
77         ''' <value></value>
78         ''' <returns></returns>
79         ''' <remarks></remarks>
80         Public ReadOnly Property FileName As String
81             Get
82                 Return MyBase.FilePath.BaseName
83             End Get
84         End Property
85
86         ''' <summary>
87         ''' 将时间字符串里面的":"符号去除之后,剩余的字符串可以用于作为路径来使用
88         ''' </summary>
89         ''' <value></value>
90         ''' <returns></returns>
91         ''' <remarks></remarks>
92         Public Shared ReadOnly Property NowTimeNormalizedString As String
93             Get
94                 Return $"{Format(Now.Month, "00")}{ Format(Now.Day, "00")}{Format(Now.Hour, "00")}{Format(Now.Minute, "00")}{Format(Now.Second, "00")}"
95             End Get
96         End Property
97
98         ''' <summary>
99         ''' 
100         ''' </summary>
101         ''' <param name="Path">This logfile will saved to.</param>
102         ''' <remarks></remarks>
103         ''' <param name="bufferSize">当日志的记录数目达到这个数目的时候就会将日志数据写入到文件之中</param>
104         Public Sub New(Path As StringOptional AutoFlush As Boolean = TrueOptional bufferSize As Integer = 1024)
105             Dim file As New FileStream(Path, FileMode.Append)
106
107             buffer = New StreamWriter(file, Encoding.UTF8, bufferSize) With {
108                 .AutoFlush = AutoFlush
109             }
110             buffer.WriteLine($"//{vbTab}[{Now.ToString}]{vbTab}{New String("=", 25)}  START WRITE LOGGING SECTION  {New String("=", 25)}" & vbCrLf)
111             FilePath = FileIO.FileSystem.GetFileInfo(Path).FullName
112         End Sub
113
114         Public Sub LogException(Msg As String, <CallerMemberName> Optional Object$ = Nothing)
115             Call WriteLine(Msg, [Object], Type:=MSG_TYPES.ERR)
116         End Sub
117
118         Public Sub LogException(ex As Exception)
119             Call WriteLine(ex.ToString, "", Type:=MSG_TYPES.ERR)
120         End Sub
121
122         ''' <summary>
123         ''' 向日志文件之中写入数据
124         ''' </summary>
125         ''' <param name="Msg"></param>
126         ''' <param name="[Object]"></param>
127         ''' <param name="Type"></param>
128         Public Sub WriteLine(Msg As String, [Object] As StringOptional Type As MSG_TYPES = MSG_TYPES.INF)
129             Dim LogEntry As New LogEntry With {
130                 .Msg = Msg,
131                 .Object = [Object],
132                 .Time = Now,
133                 .Type = Type
134             }
135
136             buffer.WriteLine(LogEntry.ToString)
137             counts += 1
138         End Sub
139
140         ''' <summary>
141         ''' 会自动拓展已经存在的日志数据
142         ''' </summary>
143         ''' <remarks></remarks>
144         Private Function SaveLog() As Boolean
145             Call buffer.WriteLine(vbCrLf & $"//{vbTab}{New String("=", 25)}  END OF LOG FILE  {New String("=", 25)}")
146             Call buffer.WriteLine(vbCrLf)
147             Call buffer.Flush()
148
149             Return True
150         End Function
151
152         Public Overrides Function ToString() As String
153             Return $"[{counts} records]'{FilePath.ToFileURL}'"
154         End Function
155
156         Public Function ReadLine() As String Implements I_ConsoleDeviceHandle.ReadLine
157             Return ""
158         End Function
159
160         Public Sub WriteLine(Optional s As String = ""Implements I_ConsoleDeviceHandle.WriteLine
161             Call WriteLine(s, Type:=MSG_TYPES.INF, [Object]:="")
162         End Sub
163
164         Public Sub WriteLine(s As String())
165             Dim str As String = String.Join(vbCrLf, s)
166             Call WriteLine(str, Type:=MSG_TYPES.INF, [Object]:="")
167         End Sub
168
169         ''' <summary>
170         ''' 
171         ''' </summary>
172         ''' <param name="s"></param>
173         ''' <param name="args">{[Object] As StringOptional Type As MsgType = MsgType.INF, Optional WriteToScreen As Boolean = True}</param>
174         ''' <remarks></remarks>
175         Public Sub WriteLine(s As StringParamArray args() As StringImplements I_ConsoleDeviceHandle.WriteLine
176             Dim [Object] As String = IIf(String.IsNullOrEmpty(args(0)), "", args(0))
177             Call WriteLine(s, Type:=MSG_TYPES.INF, [Object]:=[Object])
178         End Sub
179
180         Public Overloads Function Read() As Integer Implements I_ConsoleDeviceHandle.Read
181             Return -1
182         End Function
183
184         ''' <summary>
185         ''' 日志文件在保存的时候默认是追加的方式
186         ''' </summary>
187         ''' <param name="FilePath"></param>
188         ''' <param name="Encoding"></param>
189         ''' <returns></returns>
190         ''' <remarks></remarks>
191         Public Overrides Function Save(Optional FilePath As String = ""Optional Encoding As Encoding = NothingAs Boolean
192             FilePath = getPath(FilePath)
193             Return SaveLog()
194         End Function
195
196         ''' <summary>
197         ''' 给出用于调试的系统的信息摘要
198         ''' </summary>
199         ''' <returns></returns>
200         Public Shared Function SystemInfo() As String
201             Dim sBuilder As New StringBuilder(1024)
202
203             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.BuildVersion)}:={OSVersionInfo.BuildVersion}")
204             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.Edition)}:={OSVersionInfo.Edition}")
205             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.MajorVersion)}:={OSVersionInfo.MajorVersion}")
206             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.MinorVersion)}:={OSVersionInfo.MinorVersion}")
207             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.WindowsName)}:={OSVersionInfo.WindowsName}")
208             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.OSBits)}:={OSVersionInfo.OSBits}")
209             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.ProcessorBits)}:={OSVersionInfo.ProcessorBits}")
210             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.ProgramBits)}:={OSVersionInfo.ProgramBits}")
211             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.RevisionVersion)}:={OSVersionInfo.RevisionVersion}")
212             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.ServicePack)}:={OSVersionInfo.ServicePack}")
213             Call sBuilder.AppendLine($"{NameOf(OSVersionInfo.Version)}:={OSVersionInfo.Version.ToString}")
214
215             Return sBuilder.ToString
216         End Function
217
218         Protected Overrides Function __getDefaultPath() As String
219             Return FilePath
220         End Function
221
222         Protected Overrides Sub Dispose(disposing As Boolean)
223             Call SaveLog()
224             MyBase.Dispose(disposing)
225         End Sub
226     End Class
227 End Namespace