From 79d0abaac051924034c7041e90ba5a4e0be651da Mon Sep 17 00:00:00 2001
From: "ankostis@host:STUW025" <ankostis@gmail.com>
Date: Fri, 30 May 2014 03:51:32 +0200
Subject: [PATCH] FIX WorkingDir relative-path error.

* log:
** Move logFile routine along with he others,
** make log routines from funcs -->subs,
** stop unecessary anymore double-logging when sending to dlgbox.
* Always ensure /Header/BodySchema exists, to hint user to use it.
* Move schema-help routine to jsno-utils.
---
 CHANGES.md                               |   1 +
 CSE/Calculation/main_calculation_call.vb |  18 ++--
 CSE/Classes/cJsonFile.vb                 |   2 +
 CSE/Classes/cPreferences.vb              |   2 +-
 CSE/GUI/Export_GUI.vb                    |  63 -----------
 CSE/GUI/F_Main.vb                        |   3 +-
 CSE/GUI/F_Preferences.vb                 |   1 -
 CSE/GUI/minor_routines_GUI.vb            |  48 ---------
 CSE/Minor_routines.vb                    | 128 +++++++++++++++++++++--
 9 files changed, 132 insertions(+), 134 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 31ae967..4c34751 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -12,6 +12,7 @@ JRC contributions:
   * Enhance JSON-files with standard header/body behavior.
   * Link JSON to GUI controls (labels & toolstips)
   * json: Read defaults from schemas.
+  * Rework logging ensure always writing to file-log (even when sent to msg-box).
 
 
 #### 2014-05-23: 2.0.1-pre0 ####
diff --git a/CSE/Calculation/main_calculation_call.vb b/CSE/Calculation/main_calculation_call.vb
index 5dc6528..204f927 100644
--- a/CSE/Calculation/main_calculation_call.vb
+++ b/CSE/Calculation/main_calculation_call.vb
@@ -1,7 +1,7 @@
 Public Module main_calculation_call
 
     ' Main calculation
-    Function calculation(ByVal Cali As Boolean) As Boolean
+    Function calculation(ByVal isCalibrate As Boolean) As Boolean
         ' Declaration
         Dim i As Integer
 
@@ -14,7 +14,7 @@
         Units = Nothing
         UnitsUndef = Nothing
 
-        If Cali Then
+        If isCalibrate Then
             ' Declarations
             Dim MSC As New cMSC
             Dim vMSC As New cVirtMSC
@@ -22,7 +22,7 @@
             ' Read the input data
             fInfWarErrBW(7, False, "Reading Input Files...")
             Dim vehicle As New cVehicle(Vehspez)
-            ReadInputMSC(MSC, MSCCSpez, Cali)
+            ReadInputMSC(MSC, MSCCSpez, isCalibrate)
             ReadDataFile(DataSpez(1), MSC)
 
             ' Exit function if error is detected
@@ -50,8 +50,8 @@
             fInfWarErrBW(7, False, "Writing the output files...")
 
             ' Output
-            fOutDataCalc1Hz(DataSpez(1), Cali)
-            fOutCalcRes(DataSpez, Cali)
+            fOutDataCalc1Hz(DataSpez(1), isCalibrate)
+            fOutCalcRes(DataSpez, isCalibrate)
         Else
             ' Declarations
             Dim MSC As New cMSC
@@ -63,7 +63,7 @@
             ' Read the input files
             fInfWarErrBW(7, False, "Reading Input Files...")
             Dim vehicle As New cVehicle(Vehspez)
-            ReadInputMSC(MSC, MSCTSpez, Cali)
+            ReadInputMSC(MSC, MSCTSpez, isCalibrate)
             ReadWeather(Ambspez)
 
             ' Calculation of the virtual MSC points
@@ -109,7 +109,7 @@
                 fInfWarErrBW(6, False, "Writing the output files...")
 
                 ' Output
-                fOutDataCalc1Hz(DataSpez(i), Cali)
+                fOutDataCalc1Hz(DataSpez(i), isCalibrate)
 
                 ' Save the Result dictionaries
                 fSaveDic(i - 1)
@@ -134,7 +134,7 @@
             If BWorker.CancellationPending Then
                 ' Write the summerised output file
                 fInfWarErrBW(7, False, "Writing the summarised output file...")
-                fOutCalcRes(DataSpez, Cali)
+                fOutCalcRes(DataSpez, isCalibrate)
                 Return False
             End If
 
@@ -143,7 +143,7 @@
 
             ' Write the summerised output file
             fInfWarErrBW(7, False, "Writing the summarised output file...")
-            fOutCalcRes(DataSpez, Cali)
+            fOutCalcRes(DataSpez, isCalibrate)
 
             ' Check if all is valid
             For i = 0 To ErgValuesReg(tCompErgReg.SecID).Count - 1
diff --git a/CSE/Classes/cJsonFile.vb b/CSE/Classes/cJsonFile.vb
index 0eeced5..00c402b 100644
--- a/CSE/Classes/cJsonFile.vb
+++ b/CSE/Classes/cJsonFile.vb
@@ -165,6 +165,8 @@ Public MustInherit Class cJsonFile
         End If
         If isIncludeSchema Then
             h("BodySchema") = Me.BodySchema
+        ElseIf bodySchema Is Nothing Then
+            h("BodySchema") = Nothing
         End If
 
         '' Overlay subclass's properties.
diff --git a/CSE/Classes/cPreferences.vb b/CSE/Classes/cPreferences.vb
index 3c0d0b4..54c1d46 100644
--- a/CSE/Classes/cPreferences.vb
+++ b/CSE/Classes/cPreferences.vb
@@ -154,7 +154,7 @@ Each file can override it by setting its '/Header/BodySchema' property to false/
                     value = IO.Path.GetFullPath(value)
                     If value.StartsWith(myPlainPath, StringComparison.OrdinalIgnoreCase) Then
                         value = value.Substring(myPlainPath.Length)
-                        If (value.First <> "\"c) Then
+                        If (value.First = "\"c) Then
                             value = value.Substring(1)
                         End If
                         If (value.Last <> "\"c) Then
diff --git a/CSE/GUI/Export_GUI.vb b/CSE/GUI/Export_GUI.vb
index 1d6e1a3..eb80312 100644
--- a/CSE/GUI/Export_GUI.vb
+++ b/CSE/GUI/Export_GUI.vb
@@ -99,67 +99,4 @@
 
         Return True
     End Function
-
-    ' Generation or upgrade from the log file
-    Function fWriteLog(ByVal filePosition As Integer, Optional ByVal logLevel As Integer = 4, Optional ByVal text As String = "", _
-                       Optional ByVal ex As Exception = Nothing) As Boolean
-        ' filePosition:
-        '   Write beginning
-        '   Add
-        '   Write end
-
-        If Not AppPreferences.writeLog Then Return True
-
-        ' Declaration
-        Dim LogFilenam As String = joinPaths(MyPath, "log.txt")
-
-        Try
-            ' Decision where should be write
-            Select Case filePosition
-                Case 1 ' At the beginning of VECTO
-                    Dim fInf As New System.IO.FileInfo(LogFilenam)
-                    If IO.File.Exists(LogFilenam) Then
-                        If fInf.Length > AppPreferences.logSize * Math.Pow(10, 6) Then
-                            fLoeschZeilen(LogFilenam, System.IO.File.ReadAllLines(LogFilenam).Length / 2)
-                        End If
-                        FileOutLog.OpenWrite(LogFilenam, , True)
-                    Else
-                        FileOutLog.OpenWrite(LogFilenam)
-                    End If
-                    FileOutLog.WriteLine("-----")
-
-                    ' Write the start time into the Log
-                    FileOutLog.WriteLine("Starting Session " & CDate(DateAndTime.Now))
-                    FileOutLog.WriteLine(AppName & " " & AppVers)
-
-                Case 2 ' Add a message to the Log
-                    Dim slevel As String
-                    Select Case logLevel
-                        Case 1
-                            slevel = "INFO   | "
-                        Case 2
-                            slevel = "WARNING| "
-                        Case 3
-                            slevel = "ERROR  | "
-                        Case Else
-                            slevel = "DEBUG  | "
-                    End Select
-                    FileOutLog.OpenWrite(LogFilenam, , True)
-                    FileOutLog.WriteLine(slevel & text)
-                    If ex IsNot Nothing Then
-                        FileOutLog.WriteLine(ex.StackTrace)
-                    End If
-
-                Case 3 ' At the end
-                    FileOutLog.OpenWrite(LogFilenam, , True)
-                    ' Write the end to the Log
-                    FileOutLog.WriteLine("Closing Session " & CDate(DateAndTime.Now))
-                    FileOutLog.WriteLine("-----")
-            End Select
-        Finally
-            FileOutLog.Dispose()
-        End Try
-
-        Return True
-    End Function
 End Module
diff --git a/CSE/GUI/F_Main.vb b/CSE/GUI/F_Main.vb
index 8adeadd..59a4322 100644
--- a/CSE/GUI/F_Main.vb
+++ b/CSE/GUI/F_Main.vb
@@ -470,7 +470,6 @@ Public Class F_Main
     ' Create activation file
     Private Sub CreatActivationFileToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CreatActivationFileToolStripMenuItem.Click
         Lic.CreateActFile(MyPath & "ActivationCode.dat")
-        fInfWarErr(7, False, "Activation code created under: " & MyPath & "ActivationCode.dat")
         fInfWarErr(7, True, "Activation code created under: " & MyPath & "ActivationCode.dat")
     End Sub
 
@@ -569,7 +568,7 @@ Public Class F_Main
         Try
             calculation(Cali)
         Catch ex As Exception
-            fInfWarErrBW(9, True, format("Calculation Failed due to: {0}", ex.Message), ex)
+            fInfWarErrBW(9, False, format("Calculation Failed due to: {0}", ex.Message), ex)
         End Try
 
         '#################################
diff --git a/CSE/GUI/F_Preferences.vb b/CSE/GUI/F_Preferences.vb
index 3af4a18..f4616b9 100644
--- a/CSE/GUI/F_Preferences.vb
+++ b/CSE/GUI/F_Preferences.vb
@@ -66,7 +66,6 @@ Public Class F_Preferences
 
             ' Message for the restart of VECTO
             RestartN = True
-            fInfWarErr(7, False, format("Stored Preferences({0}).", PreferencesPath))
             fInfWarErr(7, True, format("Stored Preferences({0}). \n\nDo you want to restart VECTO now?", PreferencesPath))
         Catch ex As Exception
             fInfWarErr(9, False, format("Failed storing Preferences({0}) due to: {1} \n  Preferences left unmodified!", _
diff --git a/CSE/GUI/minor_routines_GUI.vb b/CSE/GUI/minor_routines_GUI.vb
index 8a98e7b..ee4502e 100644
--- a/CSE/GUI/minor_routines_GUI.vb
+++ b/CSE/GUI/minor_routines_GUI.vb
@@ -628,52 +628,4 @@ Module minor_routines_GUI
         End Try
     End Sub
 
-    ''' <summary>Builds a human-readable help-string from any non-null schema-properties.</summary>
-    Function schemaInfos2helpMsg(ByVal ParamArray propSchemaInfos() As JToken) As String
-        Dim titl = propSchemaInfos(0)
-        Dim desc = propSchemaInfos(1)
-        Dim type = propSchemaInfos(2)
-        Dim chce = propSchemaInfos(3)
-        Dim dflt = propSchemaInfos(4)
-        Dim mini = propSchemaInfos(5)
-        Dim miex = propSchemaInfos(6) '' exclusiveMin
-        Dim maxi = propSchemaInfos(7)
-        Dim maex = propSchemaInfos(8) '' exclusiveMax
-
-        Dim sdesc As String = ""
-        Dim stype As String = ""
-        Dim senum As String = ""
-        Dim sdflt As String = ""
-        Dim slimt As String = ""
-
-        If desc IsNot Nothing Then
-            sdesc = format(desc.ToString())
-        ElseIf titl IsNot Nothing Then
-            sdesc = format(titl.ToString())
-        End If
-        If type IsNot Nothing Then stype = type.ToString(Newtonsoft.Json.Formatting.None) & ": "
-        If chce IsNot Nothing Then senum = format("\n- choices: {0}", chce.ToString(Newtonsoft.Json.Formatting.None))
-        If dflt IsNot Nothing Then sdflt = format("\n- default: {0}", dflt)
-        If mini IsNot Nothing OrElse maxi IsNot Nothing Then
-            Dim infinitySymbol = "" + ChrW(&H221E)
-            Dim open = "("c
-            Dim smin = infinitySymbol
-            Dim smax = infinitySymbol
-            Dim clos = ")"c
-
-            If mini IsNot Nothing Then
-                smin = mini
-                If (miex Is Nothing OrElse Not CBool(miex)) Then open = "["c
-            End If
-            If maxi IsNot Nothing Then
-                smax = maxi
-                If (maex Is Nothing OrElse Not CBool(maex)) Then clos = "]"c
-            End If
-            slimt = format("\n- limits : {0}{1}, {2}{3}", _
-                           open, smin, smax, clos)
-        End If
-
-        Return String.Join("", stype, sdesc, senum, sdflt, slimt)
-    End Function
-
 End Module
diff --git a/CSE/Minor_routines.vb b/CSE/Minor_routines.vb
index 65a24b1..aed0805 100644
--- a/CSE/Minor_routines.vb
+++ b/CSE/Minor_routines.vb
@@ -163,8 +163,8 @@ Module Minor_routines
     ' Functions for the information depiction on the GUI with the backgroundworker (Info, Warning, Error)
 #Region "Logging"
     ' Output from Informations\Warnings\Errors on the GUI
-    Function fInfWarErr(ByVal logLevel As Integer, ByVal MsgBoxOut As Boolean, _
-                   ByVal text As String, Optional ByVal ex As Exception = Nothing) As Boolean
+    Sub fInfWarErr(ByVal logLevel As Integer, ByVal MsgBoxOut As Boolean, _
+                   ByVal text As String, Optional ByVal ex As Exception = Nothing)
 
         ' Declaration
         Dim Styletext = "Debug"
@@ -232,13 +232,11 @@ Module Minor_routines
                 MsgBox(text, StyleOut, Styletext)
             End If
         End If
-
-        Return logLevel <> 9
-    End Function
+    End Sub
 
     ''' <summary>Log from Informations\Warnings\Errors from within the Backgoundworker</summary>
-    Function fInfWarErrBW(ByVal logLevel As Integer, ByVal msgBoxOut As Boolean, _
-                     ByVal text As String, Optional ByVal ex As Exception = Nothing) As Boolean
+    Sub fInfWarErrBW(ByVal logLevel As Integer, ByVal msgBoxOut As Boolean, _
+                     ByVal text As String, Optional ByVal ex As Exception = Nothing)
         Dim WorkerMsg As New CMsg
 
         WorkerMsg.LogLevel = logLevel
@@ -248,9 +246,7 @@ Module Minor_routines
 
         ' Output in the Tabcontrols (Call from Backgroundworker_ProgressChanged)
         BWorker.ReportProgress(0, WorkerMsg)
-
-        Return logLevel <> 9
-    End Function
+    End Sub
 
     ' Definition for the Backgroundworker
     Class CMsg
@@ -265,6 +261,70 @@ Module Minor_routines
         End Sub
     End Class
 
+
+    ' Generation or upgrade from the log file
+    Function fWriteLog(ByVal filePosition As Integer, Optional ByVal logLevel As Integer = 4, Optional ByVal text As String = "", _
+                       Optional ByVal ex As Exception = Nothing) As Boolean
+        ' filePosition:
+        '   Write beginning
+        '   Add
+        '   Write end
+
+        If Not AppPreferences.writeLog Then Return True
+
+        ' Declaration
+        Dim LogFilenam As String = joinPaths(MyPath, "log.txt")
+
+        Try
+            ' Decision where should be write
+            Select Case filePosition
+                Case 1 ' At the beginning of VECTO
+                    Dim fInf As New System.IO.FileInfo(LogFilenam)
+                    If IO.File.Exists(LogFilenam) Then
+                        If fInf.Length > AppPreferences.logSize * Math.Pow(10, 6) Then
+                            fLoeschZeilen(LogFilenam, System.IO.File.ReadAllLines(LogFilenam).Length / 2)
+                        End If
+                        FileOutLog.OpenWrite(LogFilenam, , True)
+                    Else
+                        FileOutLog.OpenWrite(LogFilenam)
+                    End If
+                    FileOutLog.WriteLine("-----")
+
+                    ' Write the start time into the Log
+                    FileOutLog.WriteLine("Starting Session " & CDate(DateAndTime.Now))
+                    FileOutLog.WriteLine(AppName & " " & AppVers)
+
+                Case 2 ' Add a message to the Log
+                    Dim slevel As String
+                    Select Case logLevel
+                        Case 1
+                            slevel = "INFO   | "
+                        Case 2
+                            slevel = "WARNING| "
+                        Case 3
+                            slevel = "ERROR  | "
+                        Case Else
+                            slevel = "DEBUG  | "
+                    End Select
+                    FileOutLog.OpenWrite(LogFilenam, , True)
+                    FileOutLog.WriteLine(slevel & text)
+                    If ex IsNot Nothing Then
+                        FileOutLog.WriteLine(ex.StackTrace)
+                    End If
+
+                Case 3 ' At the end
+                    FileOutLog.OpenWrite(LogFilenam, , True)
+                    ' Write the end to the Log
+                    FileOutLog.WriteLine("Closing Session " & CDate(DateAndTime.Now))
+                    FileOutLog.WriteLine("-----")
+            End Select
+        Finally
+            FileOutLog.Dispose()
+        End Try
+
+        Return True
+    End Function
+
 #End Region ' Logging
 
 
@@ -335,6 +395,54 @@ Module Minor_routines
         Return value
     End Function
 
+    ''' <summary>Builds a human-readable help-string from any non-null schema-properties.</summary>
+    Function schemaInfos2helpMsg(ByVal ParamArray propSchemaInfos() As JToken) As String
+        Dim titl = propSchemaInfos(0)
+        Dim desc = propSchemaInfos(1)
+        Dim type = propSchemaInfos(2)
+        Dim chce = propSchemaInfos(3)
+        Dim dflt = propSchemaInfos(4)
+        Dim mini = propSchemaInfos(5)
+        Dim miex = propSchemaInfos(6) '' exclusiveMin
+        Dim maxi = propSchemaInfos(7)
+        Dim maex = propSchemaInfos(8) '' exclusiveMax
+
+        Dim sdesc As String = ""
+        Dim stype As String = ""
+        Dim senum As String = ""
+        Dim sdflt As String = ""
+        Dim slimt As String = ""
+
+        If desc IsNot Nothing Then
+            sdesc = format(desc.ToString())
+        ElseIf titl IsNot Nothing Then
+            sdesc = format(titl.ToString())
+        End If
+        If type IsNot Nothing Then stype = type.ToString(Newtonsoft.Json.Formatting.None) & ": "
+        If chce IsNot Nothing Then senum = format("\n- choices: {0}", chce.ToString(Newtonsoft.Json.Formatting.None))
+        If dflt IsNot Nothing Then sdflt = format("\n- default: {0}", dflt)
+        If mini IsNot Nothing OrElse maxi IsNot Nothing Then
+            Dim infinitySymbol = "" + ChrW(&H221E)
+            Dim open = "("c
+            Dim smin = infinitySymbol
+            Dim smax = infinitySymbol
+            Dim clos = ")"c
+
+            If mini IsNot Nothing Then
+                smin = mini
+                If (miex Is Nothing OrElse Not CBool(miex)) Then open = "["c
+            End If
+            If maxi IsNot Nothing Then
+                smax = maxi
+                If (maex Is Nothing OrElse Not CBool(maex)) Then clos = "]"c
+            End If
+            slimt = format("\n- limits : {0}{1}, {2}{3}", _
+                           open, smin, smax, clos)
+        End If
+
+        Return String.Join("", stype, sdesc, senum, sdflt, slimt)
+    End Function
+
 #End Region ' Json
 
 
-- 
GitLab