How to flush io buffer in Erlang?

How do you flush io buffer in Erlang?

For instace:

io:format("hello") or io:format(user, "hello")

This post seems to indicate that there is no clean solution.

Is there a better solution than in this post?

+5
source share
3 answers

Unfortunately, besides the correct implementation of the "flush" command in the io / kernel subsystem, and making sure that low-level drivers that implement actual iio support, you really need to rely on system quiesing before closing it. It seems that I did not work.

Look at io.erl / io_lib.erl in stdlib and file_io_server.erl / prim_file.erl in the kernel for gory details.

, file_io_server ( io/io_lib ), :

{put_chars,Chars}
{get_until,...}
{get_chars,...}
{get_line,...}
{setopts, ...}

(.. )!

, , ( ) . , , - , ( gen_server , ):

  case file:open(LogFile, [append]) of
    {ok, IODevice} ->
    io:fwrite(IODevice, "~n~2..0B ~2..0B ~4..0B, ~2..0B:~2..0B:~2..0B: ~-8s : ~-20s : ~12w : ",
          [Day, Month, Year, Hour, Minute, Second, Priority, Module, Pid]),
    io:fwrite(IODevice, Msg, Params),
    io:fwrite(IODevice, "~c", [13]),
    file:close(IODevice);
+5
io:put_chars(<<>>)

script .

0

you can run

flush().

from the shell or try

flush()->
receive
    _ -> flush()
after 0 -> ok
end.

It works more or less like a flash C.

-4
source

All Articles