# Printf not working in debug=true

**URL:** <https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761>\
**Category:** Challenges\
**Tags:** challenge:redis\
**Created:** [November 3, 2024, 4:11pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761 "2024-11-03T16:11:41Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 3, 2024, 4:11pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/1 "2024-11-03T16:11:41Z")

</div>

I am using Haskell for the redis server  
I am trying to debug, but it doesn’t work

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/c/c76fd34baae958cb2c9253a51923d9bd9c446389.jpeg)

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 3, 2024, 4:15pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/2 "2024-11-03T16:15:53Z")

</div>

@dawkrish Could you [upload your code to GitHub](https://docs.codecrafters.io/features/publish-to-github) and share the link? It will be much easier to debug if I can run it directly.

I’ll try to run your code and get back to you tomorrow.

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 3, 2024, 4:27pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/3 "2024-11-03T16:27:28Z")

</div>

Okay sure, just wait a minute

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 3, 2024, 4:29pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/4 "2024-11-03T16:29:24Z")

</div>

I dont know how I can  
because it already has a remote set

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 3, 2024, 4:30pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/5 "2024-11-03T16:30:30Z")

</div>

![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/d/d759cdd5bb2217335b57aab789cf69602e78156b.png)

> **[Publish to GitHub - CodeCrafters](https://docs.codecrafters.io/features/publish-to-github)**
>
> Learn more about the Publish to GitHub feature

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 3, 2024, 4:32pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/6 "2024-11-03T16:32:59Z")

</div>

> **[GitHub - dawkrish/codecrafters-redis-haskell](https://github.com/dawkrish/codecrafters-redis-haskell)**
>
> Contribute to dawkrish/codecrafters-redis-haskell development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 3, 2024, 11:45pm UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/7 "2024-11-03T23:45:32Z")

</div>

@dawkrish Printing to `stderr` should work:

```auto
import System.IO (stderr, hPutStrLn)

main :: IO ()
main = do
    let port = "6379"
    hPutStrLn stderr "Logs from your program will appear here"
    hPutStrLn stderr $ "Redis server listening on port " ++ port
    TCP.serve TCP.HostAny port $ \(socket, address) -> do
        hPutStrLn stderr $ "Client connected: " ++ show address
        loop socket
        hPutStrLn stderr $ "Client disconnected: " ++ show address

```

 ![image](https://canada1.discourse-cdn.com/flex003/uploads/codecrafters/original/2X/e/ee6f0e3a0e9d09813a733379d3f00684817f714b.png)

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 4:27am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/8 "2024-11-04T04:27:28Z")

</div>

Okay thanks ! But why is not printing to stdout working? Is it due to Haskell or is it same for every language ??!

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 4, 2024, 4:30am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/9 "2024-11-04T04:30:13Z")

</div>

TBH, I’m not quite sure why stdout is not working for Haskell, because it works for other languages.

I need to investigate a bit and get back to you later.

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 4:41am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/11 "2024-11-04T04:41:33Z")

</div>

Thanks a lot ! I will use stderr till then. You have saved me big time

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 4, 2024, 4:45am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/12 "2024-11-04T04:45:38Z")

</div>

@dawkrish Turns out the issue is due to buffering. You can manually flush stdout like this:

```auto
import System.IO (hFlush, stdout)

main :: IO ()
main = do
    let port = "6379"
    putStrLn $ "Redis server listening on port " ++ port
    hFlush stdout

```

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 4:57am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/13 "2024-11-04T04:57:52Z")

</div>

I don’t understand this but I guess you flushing means clearing the previous stdout ?

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 4, 2024, 5:11am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/14 "2024-11-04T05:11:34Z")

</div>

Here’s an explanation from ChatGPT:

> In many programming languages, stdout output is buffered. This buffering can delay the appearance of printed content until:
> 
> • The buffer is full  
> • The program ends  
> • A newline character (\n) is encountered (in some cases)
> 
> When you **flush** stdout, you ensure that everything written to the output buffer is immediately sent to the terminal.

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 5:22am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/15 "2024-11-04T05:22:37Z")

</div>

Okie ! Thanks .  
I hope the Haskell issue fixes

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 10:08am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/16 "2024-11-04T10:08:55Z")

</div>

@andy1li can you raise an issue to get this fix because this way of doing is a bit irritating

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 10:12am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/17 "2024-11-04T10:12:21Z")

</div>

> [@andy1li](#):
>
> ```auto
> import System.IO (stderr, hPutStrLn)
> 
> ```

This is my code right now

```auto
{-# OPTIONS_GHC -Wno-unused-top-binds #-}
{-# LANGUAGE BlockArguments #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import qualified Network.Simple.TCP as TCP
import qualified Data.ByteString as BS
import System.IO (stderr, hPutStrLn, hPrint)
import qualified RESP

main :: IO ()
main = do
    let port = "6379"
    putStrLn $ "Redis server listening on port " ++ port
    TCP.serve TCP.HostAny port $ \(socket, address) -> do
        printToStdOut $ "Client connected: " ++ show address
        loop socket
        printToStdOut $ "Client disconnected: " ++ show address

loop :: TCP.Socket -> IO()
loop sock = do
    msg <- TCP.recv sock 1024
    case msg of
        Nothing -> printToStdOut "Client has exited!"
        (Just bs) -> do
            handleCommand sock bs
            loop sock
            
handleCommand :: TCP.Socket -> BS.ByteString -> IO()
handleCommand sock cmd 
    | cmd == "PING\n" = TCP.send sock "+PONG\r\n"
    | otherwise = do
        printToStdOut cmd
        TCP.send sock cmd

printToStdOut :: Show a => a -> IO()
printToStdOut = hPrint stderr 

```

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 4, 2024, 10:45am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/18 "2024-11-04T10:45:00Z")

</div>

> can you raise an issue to get this fix because this way of doing is a bit irritating

@dawkrish Could you clarify a bit more on what you’d like to get fixed?

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 4, 2024, 11:03am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/19 "2024-11-04T11:03:34Z")

</div>

using “putStrLn” (that goes to stdout) to use in debug mode

---

<div class="post-metadata">

**Author:** ![andy1li](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/andy1li/32/10_2.png) [@andy1li](https://forum.codecrafters.io/u/andy1li)\
**Post date:** [November 5, 2024, 12:11am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/22 "2024-11-05T00:11:56Z")

</div>

@dawkrish You can use `hSetBuffering` to disable buffering like this:

```diff
+ import System.IO (stderr, stdout, hSetBuffering, BufferMode(NoBuffering))

main :: IO ()
main = do
+ hSetBuffering stdout NoBuffering
+ hSetBuffering stderr NoBuffering
    let port = "6379"
    putStrLn $ "Redis server listening on port " ++ port
    TCP.serve TCP.HostAny port $ \(socket, address) -> do
        printToStdOut $ "Client connected: " ++ show address
        loop socket
        printToStdOut $ "Client disconnected: " ++ show address

```

---

<div class="post-metadata">

**Author:** ![dawkrish](https://yyz1.discourse-cdn.com/flex003/user_avatar/forum.codecrafters.io/dawkrish/32/2746_2.png) [@dawkrish](https://forum.codecrafters.io/u/dawkrish)\
**Post date:** [November 5, 2024, 3:56am UTC](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761/24 "2024-11-05T03:56:10Z")

</div>

Hm, for now I am just using my custom function which is there in my above code snippet “printToStdout” . I don’t understand buffering so I am avoiding that solution

By the way for other languages like python, the simple “print”, for C++, the “cout” are working fine ?

[Next page](https://forum.codecrafters.io/t/printf-not-working-in-debug-true/2761.md?page=2)
