Upgrade the logger component
[master-thesis.git] / Parasitemia / Logger / Utils.fs
1 module internal Logger.Utils
2
3 open System
4 open System.Diagnostics
5 open System.IO
6 open System.IO.Compression
7
8 open Constants
9 open Logger.Types
10
11 let extractNumberFromLogfilepath (path : string) : int option =
12 if isNull path then
13 None
14 else
15 let filename = path.Substring (path.LastIndexOf Path.DirectorySeparatorChar + 1)
16 let filenameWithoutExtension = filename.Remove (filename.IndexOf '.')
17 match Int32.TryParse filenameWithoutExtension with
18 | (true, n) -> Some n
19 | _ -> None
20
21 let compress (filename : string) =
22 use inputStream = new FileStream (filename, FileMode.Open, FileAccess.Read)
23 let filenameCompressed = filename + COMPRESSED_FILE_POSTFIX
24 use compressedStream = new GZipStream (new FileStream (filenameCompressed, FileMode.Create, FileAccess.Write), CompressionLevel.Optimal)
25 inputStream.CopyTo compressedStream
26
27 let formatHeader (msg : Message) =
28 String.Format (
29 "{0:yyyy-MM-dd HH:mm:ss.fff} [{1}] {{{2}}} ({3})",
30 msg.DateTime,
31 string msg.Severity,
32 msg.ModuleCaller,
33 (if String.IsNullOrEmpty msg.ThreadName then sprintf "%2i" msg.ThreadId else sprintf "%s-%i" msg.ThreadName msg.ThreadId)
34 )
35
36 let formatMessage (formatedHeader : string) (msg : string) =
37 String.Format ("{0} : {1}", formatedHeader, msg)
38
39 let moduleName = (System.Diagnostics.StackFrame 1).GetMethod().Module.Name
40
41 let callerModuleName () =
42 match
43 (
44 StackTrace().GetFrames ()
45 |> Array.tryPick (
46 fun frame ->
47 let name = frame.GetMethod().Module.Name
48 if name <> moduleName then Some name else None
49 )
50 ) with
51 | Some name -> name
52 | _ -> moduleName