Output logs to AppUser/Roaming/Parasitemia/Logs
[master-thesis.git] / Parasitemia / ParasitemiaUI / Program.fs
1 module ParasitemiaUI.Main
2
3 open System
4 open System.IO
5 open System.Threading
6
7 open Emgu.CV
8 open Emgu.CV.Structure
9
10 open Logger
11
12 open ParasitemiaCore.Utils
13 open ParasitemiaCore.Config
14
15 [<System.Runtime.InteropServices.DllImport "kernel32.dll">]
16 extern bool AttachConsole (int dwProcessId)
17
18 [<EntryPoint>]
19 [<STAThread ()>]
20 let main args =
21 // To redirect stdout to the attached console.
22 AttachConsole -1 |> ignore // -1 to attach to the parent process.
23
24 Log.LogDirectory <- Constants.USER_DIRECTORY_LOG
25
26 #if DEBUG
27 Log.ClearLogFiles ()
28 #endif
29
30 Log.AvoidRepeatingIdenticalMessages <- true
31
32 if Array.exists (fun e -> e = "--help" || e = "-h") args then
33 Args.showArgsHelp ()
34 0
35 else
36 try
37 Log.Info ">>>>> Starting of Parasitemia UI ..."
38 // Log.Info "UI Version: %s" Utils.UIVersion TODO
39
40 let result =
41 match Args.parse args with
42 | mode, debug ->
43 let config = Config defaultParameters
44
45 #if DEBUG
46 Log.DebugLoggingEnabled <- true
47 #else
48 Log.DebugLoggingEnabled <- debug
49 #endif
50
51 Log.Info "Command line arguments: %s" (args |> String.concat " ")
52 Log.Info "Configuration: %O" config
53 // Log.Info "Settings from %s:\n%s" Settings.Instance.Path Settings.Instance.AsJson TODO
54
55 match mode with
56 | Args.CmdLine (input, output) ->
57 if debug then
58 config.Debug <- DebugOn output
59
60 Directory.CreateDirectory output |> ignore
61
62 use logFile = new StreamWriter (new FileStream (Path.Combine(output, "log.txt"), FileMode.Append, FileAccess.Write))
63 let listener = { new IListener with member this.NewEntry severity header mess = logFile.WriteLine (header + " : " + mess) }
64 Log.AddListener listener
65
66 Log.Info "=== New run : %O %s ===" DateTime.Now (if debug then "[DEBUG]" else "[RELEASE]")
67
68 let files =
69 match input with
70 | Args.File file -> [ file ]
71 | Args.Dir dir -> Directory.EnumerateFiles dir |> List.ofSeq
72
73 use resultFile = new StreamWriter (new FileStream (Path.Combine (output, "results.txt"), FileMode.Append, FileAccess.Write))
74
75 let images = [ for file in files -> Path.GetFileNameWithoutExtension (FileInfo(file).Name), config.Copy (), new Image<Bgr, byte> (file) ]
76
77 Log.LogWithTime Types.Severity.INFO (
78 fun () ->
79 match ParasitemiaCore.Analysis.doMultipleAnalysis images None with
80 | Some results ->
81 for id, result in results do
82 let config, img = images |> List.pick (fun (id', config', img') -> if id' = id then Some (config', img') else None)
83 img.Dispose ()
84 let total, infected = countCells result.Cells
85 fprintf resultFile "File: %s %d %d %.2f (diameter: %O)\n" id total infected (100. * (float infected) / (float total)) config.RBCRadius
86 | None ->
87 fprintf resultFile "Analysis aborted"
88 Some ()
89 ) "Whole analyze" |> ignore
90
91 Log.RemoveListener listener
92 0
93
94 | Args.Window fileToOpen ->
95 if debug then config.Debug <- DebugOn "."
96 GUI.run config fileToOpen
97
98 Log.Info "<<<<< Parasitemia UI closed"
99 Log.Close ()
100 result
101
102 with
103 | ex ->
104 Log.Fatal "Error: %A" ex
105 Log.Close ()
106 1