Upgrade the logger component
[master-thesis.git] / Parasitemia / Logger / Utils.fs
diff --git a/Parasitemia/Logger/Utils.fs b/Parasitemia/Logger/Utils.fs
new file mode 100644 (file)
index 0000000..ba7f191
--- /dev/null
@@ -0,0 +1,52 @@
+module internal Logger.Utils
+
+open System
+open System.Diagnostics
+open System.IO
+open System.IO.Compression
+
+open Constants
+open Logger.Types
+
+let extractNumberFromLogfilepath (path : string) : int option =
+    if isNull path then
+        None
+    else
+        let filename = path.Substring (path.LastIndexOf Path.DirectorySeparatorChar + 1)
+        let filenameWithoutExtension = filename.Remove (filename.IndexOf '.')
+        match Int32.TryParse filenameWithoutExtension with
+        | (true, n) -> Some n
+        | _ -> None
+
+let compress (filename : string) =
+    use inputStream = new FileStream (filename, FileMode.Open, FileAccess.Read)
+    let filenameCompressed = filename + COMPRESSED_FILE_POSTFIX
+    use compressedStream = new GZipStream (new FileStream (filenameCompressed, FileMode.Create, FileAccess.Write), CompressionLevel.Optimal)
+    inputStream.CopyTo compressedStream
+
+let formatHeader (msg : Message) =
+    String.Format (
+        "{0:yyyy-MM-dd HH:mm:ss.fff} [{1}] {{{2}}} ({3})",
+        msg.DateTime,
+        string msg.Severity,
+        msg.ModuleCaller,
+        (if String.IsNullOrEmpty msg.ThreadName then sprintf "%2i" msg.ThreadId else sprintf "%s-%i" msg.ThreadName msg.ThreadId)
+    )
+
+let formatMessage (formatedHeader : string) (msg : string) =
+    String.Format ("{0} : {1}", formatedHeader, msg)
+
+let moduleName = (System.Diagnostics.StackFrame 1).GetMethod().Module.Name
+
+let callerModuleName () =
+    match
+        (
+            StackTrace().GetFrames ()
+            |> Array.tryPick (
+                fun frame ->
+                    let name = frame.GetMethod().Module.Name
+                    if name <> moduleName then Some name else None
+            )
+        ) with
+    | Some name -> name
+    | _ -> moduleName
\ No newline at end of file