4 open System.Diagnostics
13 static let mutable writer : IWriter = new ConsoleWriter () :> IWriter
14 static let monitor = obj
()
15 static let listeners = Listeners ()
18 /// Must be called first before any other action.
20 static member LogDirectory
21 with get
() = lock
monitor (fun () -> writer.LogDir)
26 if String.IsNullOrWhiteSpace value then
27 writer <- new ConsoleWriter ()
29 writer <- new FileWriter (value)
33 /// Close the log. 'LogDirectory' must be set again to reopen it.
35 static member Close () =
39 (writer :> IDisposable).Dispose ()
40 writer <- new ConsoleWriter () :> IWriter
44 /// Return all log files (the current one and the archived) as full paths.
46 static member LogFiles = writer.LogFiles
49 /// Wait that all the previous messages are written.
51 static member Flush () = writer.Flush ()
53 static member DebugLoggingEnabled
54 with get
() = writer.DebugLoggingEnabled
55 and set
value = writer.DebugLoggingEnabled <- value
58 /// Avoid to repeat a message by writting a reference to a previous message instead of the message.
59 /// 'false' by default.
61 static member AvoidRepeatingIdenticalMessages
62 with get
() = writer.AvoidRepeatingIdenticalMessages
63 and set
value = writer.AvoidRepeatingIdenticalMessages <- value
66 /// The maximum size of the current file log. If the file exceed this value it will be zipped and a new file will be created.
67 /// The file size is only tested each time a certain number of messages have been written so the file may exceed this value a bit.
69 /// <param name="size"></param>
70 static member SetLogFilesMaxSize (size
: int64
) =
71 writer.MaxSizeFile <- size
73 static member ClearLogFilesOlderThan (timeOld
: TimeSpan) =
74 writer.ClearLogFilesOlderThan timeOld
77 /// Remove all archived log files and empty the current one.
79 static member ClearLogFiles () =
80 Log.ClearLogFilesOlderThan (TimeSpan 0L)
83 /// Total size in bytes.
85 static member CurrentLogSize () : int64
=
87 |> Seq.map
(fun file -> try (FileInfo file).Length with | _ex -> 0L)
90 static member AddListener (listener
: IListener) =
91 listeners.Add listener
93 static member RemoveListener (listener
: IListener) =
94 listeners.Remove listener
96 static member private
Write (message : string) (severity
: Severity) =
100 ThreadName = Thread.CurrentThread.Name
101 ThreadId = Thread.CurrentThread.ManagedThreadId
102 ModuleCaller = Utils.callerModuleName
()
104 DateTime = TimeZone.CurrentTimeZone.ToLocalTime DateTime.UtcNow
106 listeners.NewEntry msg
110 /// [F#] Execute the given function and measure its time.
112 /// <param name="severity">Severity for writing to log</param>
113 /// <param name="f">Function to test</param>
114 /// <param name="format">Format string for output</param>
115 static member LogWithTime (severity
: Severity) (f
: unit -> 'a) (format : Printf.StringFormat<'b
, 'a>) : 'b
=
116 let sw = Stopwatch ()
120 Printf.kprintf
(fun s
-> Log.Write (s
+ sprintf
" (time: %d ms)" sw.ElapsedMilliseconds) severity
; res) format
123 /// [F#] Write Debug message to log (if DebugLoggingEnabled = true)
125 static member Debug format
=
126 if writer.DebugLoggingEnabled then
127 Printf.kprintf
(fun s
-> Log.Write s Severity.DEBUG) format
129 // [BGR] FIXME: is it possible to simplify a bit here? It's more CPU consuming than the C# couterpart.
130 Printf.kprintf
(fun _
-> ()) format
133 /// [F#] Write Info message to log
135 static member Info format
=
136 Printf.kprintf
(fun s -> Log.Write s Severity.INFO) format
139 /// [F#] Write Warning message to log
141 static member Warning format
=
142 Printf.kprintf
(fun s -> Log.Write s Severity.WARNING) format
145 /// [F#] Write Error message to log
147 static member Error format
=
148 Printf.kprintf
(fun s -> Log.Write s Severity.ERROR) format
151 /// [F#] Write Fatal message to log
153 static member Fatal format =
154 Printf.kprintf
(fun s -> Log.Write s Severity.FATAL) format
157 /// Write DEBUG message to log (if DebugLoggingEnabled = true)
159 static member DEBUG (message : string, [<ParamArray>] args
: obj array
) =
160 if writer.DebugLoggingEnabled then
161 if isNull
args || args.Length = 0 then
162 Log.Write message Severity.DEBUG
164 Log.Write (String.Format (message, args)) Severity.DEBUG
167 /// Write DEBUG message to log (if DebugLoggingEnabled = true)
169 static member DEBUG (message : string) = Log.DEBUG (message, [| |])
172 /// Write INFO message to log
174 static member INFO (message : string, [<ParamArray>] args : obj array
) =
175 if isNull
args || args.Length = 0 then
176 Log.Write message Severity.INFO
178 Log.Write (String.Format (message, args)) Severity.INFO
181 /// Write INFO message to log
183 static member INFO (message : string) = Log.INFO (message, [| |])
186 /// Write WARNING message to log
188 static member WARNING (message : string, [<ParamArray>] args : obj array
) =
189 if isNull
args || args.Length = 0 then
190 Log.Write message Severity.WARNING
192 Log.Write (String.Format (message, args)) Severity.WARNING
195 /// Write WARNING message to log
197 static member WARNING (message : string) = Log.WARNING (message, [| |])
200 /// Write ERROR message to log
202 static member ERROR (message : string, [<ParamArray>] args : obj array
) =
203 if isNull
args || args.Length = 0 then
204 Log.Write message Severity.ERROR
206 Log.Write (String.Format (message, args)) Severity.ERROR
209 /// Write ERROR message to log
211 static member ERROR (message : string) = Log.ERROR (message, [| |])
214 /// Write FATAL message to log
216 static member FATAL (message : string, [<ParamArray>] args : obj array
) =
217 if isNull
args || args.Length = 0 then
218 Log.Write message Severity.FATAL
220 Log.Write (String.Format (message, args)) Severity.FATAL
223 /// Write FATAL message to log
225 static member FATAL (message : string) = Log.FATAL (message, [| |])