41 people like it.

Conditional printf

Disable printf based on some condition

 1: 
 2: 
 3: 
 4: 
 5: 
 6: 
 7: 
 8: 
 9: 
10: 
11: 
12: 
13: 
14: 
15: 
open System.IO
let nullOut = new StreamWriter(Stream.Null) :> TextWriter

// this has the same type as printf, but it doesn't print anything
let fakePrintf fmt = fprintf nullOut fmt

// set the verbosity
let mutable verboseLevel = 3

let debug n =
    if n < verboseLevel then printfn
    else fakePrintf

debug 2 "test %d" 42 // is displayed
debug 3 "test %d" 42 // not displayed
namespace System
namespace System.IO
val nullOut : TextWriter

Full name: Script.nullOut
Multiple items
type StreamWriter =
  inherit TextWriter
  new : stream:Stream -> StreamWriter + 6 overloads
  member AutoFlush : bool with get, set
  member BaseStream : Stream
  member Close : unit -> unit
  member Encoding : Encoding
  member Flush : unit -> unit
  member Write : value:char -> unit + 3 overloads
  static val Null : StreamWriter

Full name: System.IO.StreamWriter

--------------------
StreamWriter(stream: Stream) : unit
StreamWriter(path: string) : unit
StreamWriter(stream: Stream, encoding: System.Text.Encoding) : unit
StreamWriter(path: string, append: bool) : unit
StreamWriter(stream: Stream, encoding: System.Text.Encoding, bufferSize: int) : unit
StreamWriter(path: string, append: bool, encoding: System.Text.Encoding) : unit
StreamWriter(path: string, append: bool, encoding: System.Text.Encoding, bufferSize: int) : unit
type Stream =
  inherit MarshalByRefObject
  member BeginRead : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member BeginWrite : buffer:byte[] * offset:int * count:int * callback:AsyncCallback * state:obj -> IAsyncResult
  member CanRead : bool
  member CanSeek : bool
  member CanTimeout : bool
  member CanWrite : bool
  member Close : unit -> unit
  member CopyTo : destination:Stream -> unit + 1 overload
  member Dispose : unit -> unit
  member EndRead : asyncResult:IAsyncResult -> int
  ...

Full name: System.IO.Stream
field Stream.Null
type TextWriter =
  inherit MarshalByRefObject
  member Close : unit -> unit
  member Dispose : unit -> unit
  member Encoding : Encoding
  member Flush : unit -> unit
  member FormatProvider : IFormatProvider
  member NewLine : string with get, set
  member Write : value:char -> unit + 16 overloads
  member WriteLine : unit -> unit + 17 overloads
  static val Null : TextWriter
  static member Synchronized : writer:TextWriter -> TextWriter

Full name: System.IO.TextWriter
val fakePrintf : fmt:Printf.TextWriterFormat<'a> -> 'a

Full name: Script.fakePrintf
val fmt : Printf.TextWriterFormat<'a>
val fprintf : textWriter:TextWriter -> format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.fprintf
val mutable verboseLevel : int

Full name: Script.verboseLevel
val debug : n:int -> (Printf.TextWriterFormat<'a> -> 'a)

Full name: Script.debug
val n : int
val printfn : format:Printf.TextWriterFormat<'T> -> 'T

Full name: Microsoft.FSharp.Core.ExtraTopLevelOperators.printfn
Raw view Test code New version

More information

Link:http://fssnip.net/M
Posted:13 years ago
Author:Laurent
Tags: printf , formatting , debug