Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

putStrLn doesn't print to console

I am experimenting with wxHaskell. I wasn't able to run the app under ghci, so I have to use application to test it. I wanted to test the program with println debugging. However, it seems that putStrLn doesn't work in GUI:

{-# LANGUAGE Haskell2010 #-}

module Main where

import Graphics.UI.WX

drawUI dc view = do 
  circle dc (point 10 10) 5 [penKind := PenSolid, color := red]
  putStrLn "painted"  

helloGui :: IO ()
helloGui = do
  f <- frame [
    text := "Example", 
    resizeable := False, 
    bgcolor := white,
    layout := space 400 300,
    on paint := drawUI]
    return ()

main :: IO ()
main = do
  putStrLn "Started"
  start helloGui

If I comment out start helloGui, everything is printed well. However, if I return it, nothing is printed but the window is displayed. What's wrong here?

like image 555
Konstantin Solomatov Avatar asked Sep 15 '12 08:09

Konstantin Solomatov


1 Answers

This is probably output buffering; the output is not written until the program exits.

Either flush explicitly:

  putStrLn "Started"
  hFlush stdout

Or turn on line buffering:

  hSetBuffering stdout LineBuffering -- or even NoBuffering
  putStrLn "Started"
like image 118
Thomas Avatar answered Sep 18 '22 16:09

Thomas