From cd9c55f80bcdc4fc6b85eb1b81474aed005e5016 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Wed, 31 Mar 2021 21:40:31 +0200 Subject: [PATCH] Output logs to AppUser/Roaming/Parasitemia/Logs --- Parasitemia/ParasitemiaCore/Config.fs | 7 ++ Parasitemia/ParasitemiaCore/Types.fs | 3 +- Parasitemia/ParasitemiaUI/Args.fs | 33 +++++++++ Parasitemia/ParasitemiaUI/Constants.fs | 20 ++++++ .../ParasitemiaUI/ParasitemiaUI.fsproj | 5 ++ Parasitemia/ParasitemiaUI/Program.fs | 70 ++++++++----------- Parasitemia/ParasitemiaUI/Utils.fs | 13 ++-- 7 files changed, 102 insertions(+), 49 deletions(-) create mode 100644 Parasitemia/ParasitemiaUI/Args.fs diff --git a/Parasitemia/ParasitemiaCore/Config.fs b/Parasitemia/ParasitemiaCore/Config.fs index e000d65..e5b853c 100644 --- a/Parasitemia/ParasitemiaCore/Config.fs +++ b/Parasitemia/ParasitemiaCore/Config.fs @@ -139,3 +139,10 @@ type Config (param : Parameters) = member this.Copy () = this.MemberwiseClone () :?> Config + override this.ToString () = + $"{{{nameof Config}: " + + $"{nameof this.LPFStandardDeviationParasite} = {this.LPFStandardDeviationParasite}, " + + $"{nameof this.LPFStandardDeviationRBC} = {this.LPFStandardDeviationRBC}, " + + $"{nameof this.RBCRadiusByResolution} = {this.RBCRadiusByResolution}, " + + $"{nameof this.RBCRadius} = {this.RBCRadius}, " + + $"{nameof this.Parameters} = {this.Parameters}}}" diff --git a/Parasitemia/ParasitemiaCore/Types.fs b/Parasitemia/ParasitemiaCore/Types.fs index f13043c..f696c27 100644 --- a/Parasitemia/ParasitemiaCore/Types.fs +++ b/Parasitemia/ParasitemiaCore/Types.fs @@ -44,8 +44,7 @@ type Ellipse (cx : float32, cy : float32, a : float32, b : float32, alpha : floa PI * (3.f * (this.A + this.B) - sqrt ((3.f * this.A + this.B) * (this.A + 3.f * this.B))) override this.ToString () = - $"{{Ellipse: cx = %f{this.Cx}, cy = %f{this.Cy}, a = %f{this.A}, b = %f{this.B}, alpha = %f{this.Alpha}}}" - + $"{{{nameof Ellipse}: {nameof this.Cx} = %f{this.Cx}, {nameof this.Cy} = %f{this.Cy}, {nameof this.A} = %f{this.A}, {nameof this.B} = %f{this.B}, {nameof this.Alpha} = %f{this.Alpha}}}" override this.Equals (other : obj) = match other with | :? Ellipse as otherEllipse -> diff --git a/Parasitemia/ParasitemiaUI/Args.fs b/Parasitemia/ParasitemiaUI/Args.fs new file mode 100644 index 0000000..6c74166 --- /dev/null +++ b/Parasitemia/ParasitemiaUI/Args.fs @@ -0,0 +1,33 @@ +module ParasitemiaUI.Args + +open System + +type Input = + | File of string + | Dir of string + +type RunningMode = + | CmdLine of Input * string // A file or a directory to process and the output directory. + | Window of string option // An optional path to a file to open can be given in window mode. + +type Arguments = RunningMode * bool // bool : true if in debug mode. + +let parse (args : string[]) : Arguments = + + let output = Array.tryFindIndex ((=) "--output") args + + let runningMode = + match Array.tryFindIndex ((=) "--folder") args, output with + | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 -> + CmdLine ((Dir args.[i + 1]), args.[i_output + 1]) + | _ -> + match Array.tryFindIndex ((=) "--file") args, output with + | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 -> + CmdLine ((File args.[i + 1]), args.[i_output + 1]) + |_ -> + Window (if args.Length > 0 && not (args.[0].StartsWith ("--")) then Some args.[0] else None) + + runningMode, Array.exists ((=) "--debug") args + +let showArgsHelp () = + Console.WriteLine Utils.argsHelp \ No newline at end of file diff --git a/Parasitemia/ParasitemiaUI/Constants.fs b/Parasitemia/ParasitemiaUI/Constants.fs index 8ad5db7..bef3910 100644 --- a/Parasitemia/ParasitemiaUI/Constants.fs +++ b/Parasitemia/ParasitemiaUI/Constants.fs @@ -1 +1,21 @@ module ParasitemiaUI.Constants + +open System +open System.IO + +let USER_DIRECTORY = + Path.Combine ( + Environment.GetFolderPath Environment.SpecialFolder.ApplicationData, + "Parasitemia" + ) + +let USER_DIRECTORY_LOG = + Path.Combine ( + USER_DIRECTORY, + "Log", +#if DEBUG + "Debug" +#else + "" +#endif + ) \ No newline at end of file diff --git a/Parasitemia/ParasitemiaUI/ParasitemiaUI.fsproj b/Parasitemia/ParasitemiaUI/ParasitemiaUI.fsproj index 2a87c55..70626b0 100644 --- a/Parasitemia/ParasitemiaUI/ParasitemiaUI.fsproj +++ b/Parasitemia/ParasitemiaUI/ParasitemiaUI.fsproj @@ -16,6 +16,10 @@ --win32res:resources.res + + DEBUG;TRACE + + @@ -30,6 +34,7 @@ + diff --git a/Parasitemia/ParasitemiaUI/Program.fs b/Parasitemia/ParasitemiaUI/Program.fs index b3ca81d..5b5edd0 100644 --- a/Parasitemia/ParasitemiaUI/Program.fs +++ b/Parasitemia/ParasitemiaUI/Program.fs @@ -12,36 +12,6 @@ open Logger open ParasitemiaCore.Utils open ParasitemiaCore.Config -type Input = - | File of string - | Dir of string - -type RunningMode = - | CmdLine of Input * string // A file or a directory to process and the output directory. - | Window of string option // An optional path to a file to open can be given in window mode. - -type Arguments = RunningMode * bool // bool : true if in debug mode. - -let parseArgs (args : string[]) : Arguments = - - let output = Array.tryFindIndex ((=) "--output") args - - let runningMode = - match Array.tryFindIndex ((=) "--folder") args, output with - | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 -> - CmdLine ((Dir args.[i + 1]), args.[i_output + 1]) - | _ -> - match Array.tryFindIndex ((=) "--file") args, output with - | Some i, Some i_output when i < args.Length - 2 && i_output < args.Length - 2 -> - CmdLine ((File args.[i + 1]), args.[i_output + 1]) - |_ -> - Window (if args.Length > 0 && not (args.[0].StartsWith ("--")) then Some args.[0] else None) - - runningMode, Array.exists ((=) "--debug") args - -let showArgsHelp () = - Console.WriteLine Utils.argsHelp - [] extern bool AttachConsole (int dwProcessId) @@ -51,20 +21,39 @@ let main args = // To redirect stdout to the attached console. AttachConsole -1 |> ignore // -1 to attach to the parent process. + Log.LogDirectory <- Constants.USER_DIRECTORY_LOG + +#if DEBUG + Log.ClearLogFiles () +#endif + + Log.AvoidRepeatingIdenticalMessages <- true + if Array.exists (fun e -> e = "--help" || e = "-h") args then - showArgsHelp () + Args.showArgsHelp () 0 else try - Log.Info "Starting of Parasitemia UI ..." + Log.Info ">>>>> Starting of Parasitemia UI ..." + // Log.Info "UI Version: %s" Utils.UIVersion TODO let result = - match parseArgs args with + match Args.parse args with | mode, debug -> let config = Config defaultParameters +#if DEBUG + Log.DebugLoggingEnabled <- true +#else + Log.DebugLoggingEnabled <- debug +#endif + + Log.Info "Command line arguments: %s" (args |> String.concat " ") + Log.Info "Configuration: %O" config + // Log.Info "Settings from %s:\n%s" Settings.Instance.Path Settings.Instance.AsJson TODO + match mode with - | CmdLine (input, output) -> + | Args.CmdLine (input, output) -> if debug then config.Debug <- DebugOn output @@ -76,9 +65,10 @@ let main args = Log.Info "=== New run : %O %s ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]") - let files = match input with - | File file -> [ file ] - | Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq + let files = + match input with + | Args.File file -> [ file ] + | Args.Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq use resultFile = new StreamWriter (new FileStream (Path.Combine (output, "results.txt"), FileMode.Append, FileAccess.Write)) @@ -101,14 +91,16 @@ let main args = Log.RemoveListener listener 0 - | Window fileToOpen -> + | Args.Window fileToOpen -> if debug then config.Debug <- DebugOn "." GUI.run config fileToOpen - Log.Info "Parasitemia UI closed" + Log.Info "<<<<< Parasitemia UI closed" + Log.Close () result with | ex -> Log.Fatal "Error: %A" ex + Log.Close () 1 \ No newline at end of file diff --git a/Parasitemia/ParasitemiaUI/Utils.fs b/Parasitemia/ParasitemiaUI/Utils.fs index 9f51436..4dcb7d4 100644 --- a/Parasitemia/ParasitemiaUI/Utils.fs +++ b/Parasitemia/ParasitemiaUI/Utils.fs @@ -16,18 +16,15 @@ let percentText (nbTotal : int, nb : int) : string = let percent = 100. * (float nb) / (float nbTotal) sprintf "%.1f %% (%d / %d)" percent nb nbTotal -let roamingDir = - Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.ApplicationData), "Parasitemia") - let predefinedPPIFilename = "predefined-ppi.json" -let predefinedPPIFilepath = Path.Combine (roamingDir, predefinedPPIFilename) +let predefinedPPIFilepath = Path.Combine (Constants.USER_DIRECTORY, predefinedPPIFilename) let sensorSizesFilename = "sensor-sizes.json" -let sensorSizesFilepath = Path.Combine (roamingDir, sensorSizesFilename) +let sensorSizesFilepath = Path.Combine (Constants.USER_DIRECTORY, sensorSizesFilename) let private savePredefinedPPIToFile (predefinedPPI : PredefinedPPI list) = try - Directory.CreateDirectory roamingDir |> ignore + Directory.CreateDirectory Constants.USER_DIRECTORY |> ignore use file = new StreamWriter (predefinedPPIFilepath) file.Write (JsonConvert.SerializeObject (predefinedPPI, JsonSerializerSettings (Formatting = Formatting.Indented))) with @@ -36,7 +33,7 @@ let private savePredefinedPPIToFile (predefinedPPI : PredefinedPPI list) = let private saveSensorSizesToFile (sensorSizes : SensorSize list) = try - Directory.CreateDirectory roamingDir |> ignore + Directory.CreateDirectory Constants.USER_DIRECTORY |> ignore use file = new StreamWriter (sensorSizesFilepath) file.Write (JsonConvert.SerializeObject (sensorSizes, JsonSerializerSettings (Formatting = Formatting.Indented))) with @@ -101,7 +98,7 @@ let argsHelp = " --folder : an input folder containing images to analyze\n" + " --file : an image file to be analyzed\n" + " --output : a folder to put the results\n" + - " --debug : output more information like intermediate images (it takes more CPU and memory)\n" + + " --debug : output more information like intermediate images (it takes more CPU and memory) and write debug information to the log files\n" + "Interactive mode:\n" + (sprintf " %s [] [--debug]\n" programName) + -- 2.43.0