# Extending println

**URL:** <https://discourse.julialang.org/t/extending-println/3888>\
**Category:** General Usage\
**Created:** [May 23, 2017, 9:57pm UTC](https://discourse.julialang.org/t/extending-println/3888 "2017-05-23T21:57:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![JaredCrean2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaredcrean2/32/3574_2.png) [@JaredCrean2](https://discourse.julialang.org/u/JaredCrean2)\
**Post date:** [May 23, 2017, 9:57pm UTC](https://discourse.julialang.org/t/extending-println/3888/1 "2017-05-23T21:57:37Z")

</div>

I’m trying to define my own IO type to prepend a time stamp to each line. What I have so far is:

```julia
module Mod1

type DebugFileIO <: IO
  fstream::IO
end

write(io::DebugFileIO, x::UInt8) = write(io.fstream, x)
flush(io::DebugFileIO) = flush(io.fstream)
close(io::DebugFileIO) = close(io.fstream)

import Base.println

function println(io::DebugFileIO, args...)
  println("args = ", args)
  println(io.fstream, @sprintf("%16e ", 420.0), args...)
end

end

```

If I run:

```julia
julia> using Mod1

julia> println("hello world = ", 42)

```

It seems like julia gets caught in an infinite loop. When I ^C, [this](https://gist.github.com/JaredCrean2/78db72b464e215000bb6bed89a9eb0bb) output appears.

This shouldn’t call the method of `println` defined above, so I’m not sure why this is happening. Oddly, if I remove `println("args = ", args)`, the code works correctly. Also, the failure occurs on julia 0.4 but not 0.5.2. I’m suspicious that adding a `println` broke it. Is there something wrong with the original code?

Does anyone know a) what might cause this and b) if there are any work-arounds (other than upgrading to 0.5.2, which I can’t do yet because the rest of my code isn’t ready yet).

---

<div class="post-metadata">

**Author:** ![phrb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/phrb/32/13580_2.png) [@phrb](https://discourse.julialang.org/u/phrb)\
**Post date:** [May 23, 2017, 10:52pm UTC](https://discourse.julialang.org/t/extending-println/3888/2 "2017-05-23T22:52:35Z")

</div>

I don’t know how to answer a), but regarding b) I think you could work around this by extending [`Base.show`](https://docs.julialang.org/en/latest/stdlib/io-network.html#Base.show-Tuple%7BAny%7D) instead. I extended it in my code doing something like this:

```julia
import Base.show

mutable struct MyType
    x
end

function Base.show(io::IO, n::MyType)
    @printf io "["
    print_with_color(:blue, io, "timestamp goes here")
    @printf io "] "

    print_with_color(:green, io, "put data here, such as MyType.x")
    @printf io " : "
    print_with_color(:bold, io, "$(n.x)\n")

    return
end

```

When you call `println()` with `MyType` values, `Base.show` will generate the custom output. You can also extend `Base.show` creating a function for all the types you expect to `println()`, or a specific extension for each type.

---

<div class="post-metadata">

**Author:** ![JaredCrean2](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jaredcrean2/32/3574_2.png) [@JaredCrean2](https://discourse.julialang.org/u/JaredCrean2)\
**Post date:** [May 27, 2017, 11:11pm UTC](https://discourse.julialang.org/t/extending-println/3888/3 "2017-05-27T23:11:59Z")

</div>

Interesting approach. I’m not sure it will work for my case because I want to change the printing of all types passed to `println`. My reading of the docs you linked suggests `println` only calls `show` sometimes.
