-
Notifications
You must be signed in to change notification settings - Fork 30
/
log.fs
65 lines (53 loc) · 2.16 KB
/
log.fs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
module FVim.log
open getopt
open System.Diagnostics
open FSharp.Control.Reactive
open System.Collections.Generic
let mutable private _filter = fun _ -> true
let private _logsSource = Event<string*string>()
let private _logsPub = _logsSource.Publish |> Observable.map (fun (a,b) -> $"{a}: {b}")
let private _logsESource = Event<string*string>()
let private _logsEPub = _logsESource.Publish |> Observable.map (fun (a,b) -> $"error: {a}: {b}")
let private _logsSink = Observable.merge _logsPub _logsEPub
|> Observable.filter(fun x -> _filter x)
let mutable private _n_logsSink = 0
let trace cat (fmt: Printf.StringFormat< 'a , unit >) =
Printf.kprintf (fun s -> _logsSource.Trigger(cat, s)) fmt
let error cat fmt =
Printf.kprintf (fun s -> _logsESource.Trigger(cat, s)) fmt
// XXX seriously?
let flush() =
async {
do! Async.Sleep 2000
}
let init { logToStdout = logToStdout; logToFile = logToFile; logPatterns = logPatterns; intent = intent } =
#if DEBUG
let logToStdout = true
#endif
if logToStdout then
_n_logsSink <- _n_logsSink + 1
_logsSink.Add(fun str -> printfn "%s" str)
let time = System.DateTime.Now
let ftime = time.ToString "yyyy-MM-dd-hh-mm-ss"
let fprefix =
match intent with
| Daemon _ -> "fvim-daemon"
| _ -> "fvim"
if logToFile then
_n_logsSink <- _n_logsSink + 1
let logname = $"{fprefix}-{ftime}.log"
let logToFile = System.IO.Path.Combine(config.configdir, logname)
try System.IO.File.Delete logToFile
with _ -> ()
_logsSink
|> Observable.bufferSpan (System.TimeSpan.FromMilliseconds 1000.0)
|> Observable.add (fun strs ->
let strs = strs |> Array.ofSeq
if strs.Length > 0 then
try System.IO.File.AppendAllLines(logToFile, strs)
with _ -> ())
if logPatterns.IsSome then
let patterns = logPatterns.Value.Split(",")
trace "log" "trace patterns: %A" patterns
_filter <- fun s -> Array.exists (fun (x: string) -> s.Contains x) patterns
trace "log" "fvim started. time = %A" time