X-Git-Url: http://git.euphorik.ch/?p=master-thesis.git;a=blobdiff_plain;f=Parasitemia%2FLogger%2FUtils.fs;fp=Parasitemia%2FLogger%2FUtils.fs;h=ba7f191c59b47bd219bfae3482d5ef308ef0bd53;hp=0000000000000000000000000000000000000000;hb=8cf2153bd18919de41745534d3dbf134f085e13c;hpb=6250f10c807301a760b8659f9c00ca6dbbd4c7b7 diff --git a/Parasitemia/Logger/Utils.fs b/Parasitemia/Logger/Utils.fs new file mode 100644 index 0000000..ba7f191 --- /dev/null +++ b/Parasitemia/Logger/Utils.fs @@ -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