You need to sign in or sign up before continuing.
Newer
Older
' Copyright 2017 European Union.
' Licensed under the EUPL (the 'Licence');
'
' * You may not use this work except in compliance with the Licence.
' * You may obtain a copy of the Licence at: http://ec.europa.eu/idabc/eupl
' * Unless required by applicable law or agreed to in writing,
' software distributed under the Licence is distributed on an "AS IS" basis,
' WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
'
' See the LICENSE.txt for the specific language governing permissions and limitations.

Michael KRISPER
committed
Imports System.ComponentModel
Imports System.IO
Imports System.Text
Public Module VECTO_Global

Michael KRISPER
committed
Public COREvers As String = "NOT FOUND"
Public Const LicSigAppCode As String = "VECTO-Release-0093C61E0A2E4BFA9A7ED7E729C56AE4"

Markus Quaritsch
committed
public InstallModeInstalled As Boolean = False
Public MyAppPath As String
Public MyConfPath As String

Markus Quaritsch
committed
public MyLogPath As String
public Const CONFIG_JOBLIST_FILE As string = "joblist.txt"
public const CONFIG_CYCLELIST_FILE As string = "cyclelist.txt"
public Const FILE_HISTORY_DIR_FILE As string = "Directories.txt"

Markus Quaritsch
committed

Michael KRISPER
committed
'to ensure correct format for backgroundworker thread

Michael KRISPER
committed
Public Cfg As Configuration
Public ReadOnly FileFormat As Encoding = Encoding.UTF8
Public ProgBarCtrl As ProgressbarControl
Public Class FileLogger
Private _logStream As StreamWriter

Markus Quaritsch
committed
Const LOG_FILENAME As string = "LOG.txt"
Const BACKUP_LOG_FILENAME As String = "LOG_backup.txt"
Private logPath as string
Private logFile As string
public sub New()
logPath = MyAppPath
If (InstallModeInstalled) Then
logPath = MyLogPath
End If
logFile = path.Combine(logPath, LOG_FILENAME)
End sub
Public Function StartLog() As Boolean
Try

Markus Quaritsch
committed
_logStream = My.Computer.FileSystem.OpenTextFileWriter(logFile, True, FileFormat)
WriteToLog(MessageType.Normal, "Starting Session " & Now)
WriteToLog(MessageType.Normal, "VECTO " & VECTOvers)
Catch ex As Exception
Return False
End Try
'Start new log if file size limit reached

Markus Quaritsch
committed
If File.Exists(logFile) Then

Markus Quaritsch
committed
Dim logfDetail As FileInfo = My.Computer.FileSystem.GetFileInfo(logFile)

Michael KRISPER
committed
If logfDetail.Length / (2 ^ 20) > Cfg.LogSize Then
WriteToLog(MessageType.Normal, "Starting new logfile")

Markus Quaritsch
committed
Dim backUpError As Boolean = False

Markus Quaritsch
committed
If File.Exists(path.Combine(logPath, BACKUP_LOG_FILENAME)) Then File.Delete(path.Combine(logPath, BACKUP_LOG_FILENAME))
File.Move(logFile, path.Combine(logPath, BACKUP_LOG_FILENAME))

Markus Quaritsch
committed
WriteToLog(MessageType.Err, "Failed to backup logfile! (" & Path.Combine(logPath, BACKUP_LOG_FILENAME) & ")")

Markus Quaritsch
committed
WriteToLog(MessageType.Normal, "Logfile restarted. Old log saved to " & BACKUP_LOG_FILENAME)
Public Function CloseLog() As Boolean
Try
WriteToLog(MessageType.Normal, "Closing Session " & Now)
Catch ex As Exception
Return False
End Try
Public Function WriteToLog(msgType As MessageType, msg As String) As Boolean
Dim msgTypeStr As String
Case MessageType.Err
Case MessageType.Warn
_logStream.WriteLine(Now.ToString("yyyy/MM/dd-HH:mm:ss") & vbTab & msgTypeStr & vbTab & msg)
Return True
Catch ex As Exception
Return False
End Try
End Function
End Class
#Region "File path functions"
'When no path is specified, then insert either HomeDir or MainDir Special-folders
Public Function FileRepl(file As String, Optional ByVal mainDir As String = "") As String
'If empty file => Abort
If file = "" Then Return ""
file = Replace(file, DefVehPath & "\", MyAppPath & "Default Vehicles\", 1, -1,

Michael KRISPER
committed
CompareMethod.Text)
file = Replace(file, HomePath & "\", MyAppPath, 1, -1, CompareMethod.Text)
If mainDir = "" Then
replPath = MyAppPath
Do While replPath.Length > 0 AndAlso Left(file, 3) = "..\"
replPath = PathUp(replPath)
Else
Return file
End If
End Function
'Path one-level-up "C:\temp\ordner1\" >> "C:\temp\"
Private Function PathUp(pfad As String) As String
Dim x As Integer
pfad = pfad.Substring(0, pfad.Length - 1)
x = pfad.LastIndexOf("\", StringComparison.Ordinal)

Michael KRISPER
committed
If x = -1 Then Return ""
'File name without the path "C:\temp\TEST.txt" >> "TEST.txt" oder "TEST"

Markus Quaritsch
committed
Public Function GetFilenameWithoutPath(file As String, includeFileExtension As Boolean) As String _
'GetFilenameWithoutPath
Dim x As Integer
x = file.LastIndexOf("\", StringComparison.Ordinal) + 1
file = Right(file, Len(file) - x)
If Not includeFileExtension Then
x = file.LastIndexOf(".", StringComparison.Ordinal)
If x > 0 Then file = Left(file, x)
'Filename without extension "C:\temp\TEST.txt" >> "C:\temp\TEST"
'Filename without path if Path = WorkDir or MainDir

Markus Quaritsch
committed
Public Function GetFilenameWithoutDirectory(file As String, Optional ByVal mainDir As String = "") As String _
'GetFilenameWithoutDirectory
If UCase(GetPath(file)) = UCase(path) Then file = GetFilenameWithoutPath(file, True)
'Path alone "C:\temp\TEST.txt" >> "C:\temp\"
' "TEST.txt" >> ""

Markus Quaritsch
committed
Public Function GetPath(file As String) As String 'GetPath
Dim x As Integer
If file Is Nothing OrElse file.Length < 3 OrElse file.Substring(1, 2) <> ":\" Then Return ""
x = file.LastIndexOf("\", StringComparison.Ordinal)
Return Left(file, x + 1)
'Extension alone "C:\temp\TEST.txt" >> ".txt"

Markus Quaritsch
committed
Public Function GetExtension(file As String) As String 'GetExtension
Dim x As Integer

Michael KRISPER
committed
If x = -1 Then
Module Constants
'Public ReadOnly AUX As AuxiliaryKey
Public Const HomePath As String = "<HOME>"
Public Const DefVehPath As String = "<VEHDIR>"
Public Const NoFile As String = "<NOFILE>"
'Public Sub New()

Markus Quaritsch
committed
' AUX = New AuxiliaryKey
'End Sub
' ReSharper disable once ClassNeverInstantiated.Global
End Module