LoginSignup
2
1

More than 5 years have passed since last update.

F#でprintfデバッグ

Posted at

sample code

コードはこんな感じで

open System.Diagnostics

/// Debugモードのときだけコンパイルする
#if DEBUG
/// リスナーをインスタンス化する
let listener  = new DefaultTraceListener()
/// logファイルを指定する
let parentDic = System.Reflection.Assembly.GetEntryAssembly().Location |> fun x -> string ( System.IO.Directory.GetParent(x) )
listener.LogFileName <- System.IO.Path.Combine( parentDic, "log.txt" )
/// リスナーに追加する
Debug.Listeners.Add(listener)
#endif

let rec main s (acc:int)=
    match s with
    | "quit" -> () /// quitという文字列で終了する
    | _      -> 
                /// stdoutから出力する
                stdout.WriteLine(s)
                /// log.txtに出力する
                Debug.WriteLine(acc)
                main (stdin.ReadLine()) (acc + 1)

[<EntryPointAttribute>]
let entry arg =
    main arg.[0] 0
    0

デバッグのしかた

まずdebugモードでコンパイル


$ fsharpc -d:DEBUG abc.fsx

で、ログファイルをリッスン

Fを押してForward foreverとする


$ less log.txt

実行してみる


$ mono abc.exe callmekohei

/// 標準出力へ
callmekohei

/// log.txtへ
0
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1