|
| 1 | +namespace FSharp.Control |
| 2 | + |
| 3 | +open System.Threading.Tasks |
| 4 | +open System |
| 5 | +open System.Diagnostics |
| 6 | +open System.Threading |
| 7 | + |
| 8 | +type Debug = |
| 9 | + |
| 10 | + [<DefaultValue(false)>] |
| 11 | + static val mutable private verbose: bool option |
| 12 | + |
| 13 | + /// Setting from environment variable TASKSEQ_LOG_VERBOSE, which, |
| 14 | + /// when set, enables (very) verbose printing of flow and state |
| 15 | + static member private getVerboseSetting() = |
| 16 | + match Debug.verbose with |
| 17 | + | None -> |
| 18 | + let verboseEnv = |
| 19 | + try |
| 20 | + match Environment.GetEnvironmentVariable "TASKSEQ_LOG_VERBOSE" with |
| 21 | + | null -> false |
| 22 | + | x -> |
| 23 | + match x.ToLowerInvariant().Trim() with |
| 24 | + | "1" |
| 25 | + | "true" |
| 26 | + | "on" |
| 27 | + | "yes" -> true |
| 28 | + | _ -> false |
| 29 | + |
| 30 | + with _ -> |
| 31 | + false |
| 32 | + |
| 33 | + Debug.verbose <- Some verboseEnv |
| 34 | + verboseEnv |
| 35 | + |
| 36 | + | Some setting -> setting |
| 37 | + |
| 38 | + /// Private helper to log to stdout in DEBUG builds only |
| 39 | + [<Conditional("DEBUG")>] |
| 40 | + static member private print value = |
| 41 | + match Debug.getVerboseSetting () with |
| 42 | + | false -> () |
| 43 | + | true -> |
| 44 | + // don't use ksprintf here, because the compiler does not remove all allocations due to |
| 45 | + // the way PrintfFormat types are compiled, even if we set the Conditional attribute. |
| 46 | + let ct = Thread.CurrentThread |
| 47 | + printfn "%i (%b): %s" ct.ManagedThreadId ct.IsThreadPoolThread value |
| 48 | + |
| 49 | + /// Log to stdout in DEBUG builds only |
| 50 | + [<Conditional("DEBUG")>] |
| 51 | + static member logInfo(str) = Debug.print str |
| 52 | + |
| 53 | + /// Log to stdout in DEBUG builds only |
| 54 | + [<Conditional("DEBUG")>] |
| 55 | + static member logInfo(str, data) = Debug.print $"%s{str}{data}" |
0 commit comments