Public Class ArinLog

    Dim file As System.IO.FileInfo
    Dim buffer As System.Text.StringBuilder
    'Dim Datatable As DataTable
    Public Event RaiseMsg(time As String, gubun As ETYPE, msg As String)
    Public Property DivChar As Char
    Public Property Header As String
    Public Property FlushCount As Integer
    Private CurCount As Integer = 0

    Public Enum ETYPE
        STARTUP
        SHUTDOWN
        [ERROR]
        NORMAL
        NETWORK
        KEYBOARD
        MOUSE
        PROCESS
        USERCONTROL
        BARCODE
        VISION
        PRINTER
        MOTION
        SQL
        RS232
        DATABASE
        ETC
        CALC
    End Enum


    Dim setdate As String = ""
    Public FlushTime As Date = Now  '//실기록완료한시간
    Public Property BaseDirectory As String
    Public Property BaseExt As String

    Private LAstTime As Date = Now.AddSeconds(-100)
    Private LAStData As String = ""

    Public Property LogEnable As Boolean

    Public Function GetBuffer() As String
        Return buffer.ToString
    End Function

    Public Sub New(Optional BaseDir As String = "", Optional _BaseExt As String = ".txt")
        If BaseDir = "" Then BaseDir = My.Application.Info.DirectoryPath + "\Log"
        BaseDirectory = BaseDir
        BaseExt = _BaseExt

        setdate = Now.ToString("yyMMdd")
        Me.file = New System.IO.FileInfo(BaseDirectory + "\" & setdate & _BaseExt)
        If file.Directory.Exists = False Then file.Directory.Create()
        buffer = New System.Text.StringBuilder

        DivChar = vbTab
        Header = ""
        FlushCount = 10
        LogEnable = True
        'Datatable = New DataTable
        'Datatable.Columns.Add("시간")
        'Datatable.Columns.Add("형태")
        'Datatable.Columns.Add("메세지")
    End Sub

    Public Sub Add(bodymsg As String, Optional autof As Boolean = False)
        If Not LogEnable Then Return
        Add(ETYPE.NORMAL, bodymsg, autof)
    End Sub

    Public Sub Add(TYPE As ETYPE, bodymsg As String, Optional autof As Boolean = False)
        If Not LogEnable Then Return
        Add(TYPE.ToString(), bodymsg, autof)
    End Sub

    Public Sub Add(TYPE As String, bodymsg As String, Optional autof As Boolean = False)
        If Not LogEnable Then Return

        If setdate <> Now.ToString("yyMMdd") Then   '//날짜가 달라지면 파일을 변경한다.
            Flush()
            setdate = Now.ToString("yyMMdd")
            Me.file = New System.IO.FileInfo(BaseDirectory & "\" + setdate & _BaseExt)
        End If


        Dim nowdate As Date = Now
        Dim m As String = nowdate.ToString("yy-MM-dd HH:mm:ss") & "." & nowdate.Millisecond.ToString("000") & DivChar & TYPE.ToString.PadRight(20) & DivChar & bodymsg

        If m <> LAStData Then
            CurCount += 1
            buffer.AppendLine(m)
            LAStData = m
            If autof OrElse buffer.Length > 100 OrElse TYPE = "ERROR" OrElse CurCount > FlushCount Then    '//에러발생시에는 바로 플러싱" Then
                Flush()
            End If
            '//메세지발생함
            RaiseEvent RaiseMsg(nowdate.ToString("yy-MM-dd HH:mm:ss") & "." & nowdate.Millisecond.ToString("000"), TYPE, bodymsg)
        End If
    End Sub

    Public Sub Add(DataTime As Date, Msg As String, Optional autof As Boolean = False)
        If Not LogEnable Then Return

        If setdate <> DataTime.ToString("yyMMdd") Then   '//날짜가 달라지면 파일을 변경한다.
            Flush()
            setdate = DataTime.ToString("yyMMdd")
            Me.file = New System.IO.FileInfo(BaseDirectory & "\" + setdate & _BaseExt)
        End If

        Dim m As String = DataTime.ToString("yy-MM-dd HH:mm:ss") & DivChar & Msg
        If m <> LAStData Then
            CurCount += 1
            buffer.AppendLine(m)
            LAStData = m
            If autof OrElse buffer.Length > 100 OrElse CurCount > FlushCount Then    '//에러발생시에는 바로 플러싱" Then
                Flush()
            End If
            '//메세지발생함
            RaiseEvent RaiseMsg(DataTime.ToString("yy-MM-dd HH:mm:ss") & "." & DataTime.Millisecond.ToString("000"), ETYPE.NORMAL, Msg)
        Else
            '//동일데이터는 처리하지 않는다.
        End If


        '//메세지발생함
    End Sub


    Public Function Flush() As Boolean
        Try
            If Not (System.IO.File.Exists(file.FullName)) AndAlso Header <> "" Then
                My.Computer.FileSystem.WriteAllText(file.ToString, Header + vbCrLf, True)
            End If

            My.Computer.FileSystem.WriteAllText(file.ToString, buffer.ToString, True)

            buffer = New System.Text.StringBuilder
            FlushTime = Now
            CurCount = 0
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Overrides Function toString() As String
        Try
            Return My.Computer.FileSystem.ReadAllText(file.FullName)
        Catch ex As Exception
            Return ""
        End Try
    End Function
End Class