Writing a term (nested structs) in human-readable form to a text file - is there a nicer way to do this?

Hi,

I want to write a term (nested structs) in human-readable form to a text file. This is what I’ve got, there must be a better way…

 i_am_a_term_now_a_string  = :io_lib.format("~p.", [i_am_a_term]) 
                                          |> List.flatten()   
                                          |> to_string()
File.write!("/tmp/a_term_on_your_disk", i_am_a_term_now_a_string)

Thanks

1 Like

Hey @nhpip did you try inspect/1 ?

i_am_a_term_now_a_string = inspect(term, limit: :infinity)
2 Likes

Sorry, my copy/paste screwed up…it missed the most important line:

{:ok, [term_some_time_later]} = :file.consult('/tmp/a_term_on_your_disk')

@nhpip Can you explain what this does and what you’re trying to do? Are you trying to turn that term back into an Elixir value?

1 Like

In a nutshell yes. I want to be able to write a term to disk as text. Then read it again later, the file needs to be human-readable. I can explain why if you are interested.

A bit of info will be helpful. The way to handle (most) terms in a human readable way is to use inspect to dump the data to disk, and then Code.eval_string to turn it back into a value. However this has a variety of trade offs and won’t handle arbitrary terms because things like pid values for example don’t have an output from inspect that will turn back into a pid when evaled. Whether that matters or not depends on your use case.

3 Likes

If you think quoted expression is human-readable. Try to use:

  • quote
  • Code.eval_quoted

Or, just use :erlang.term_to_binary and :erlang.binary_to_term. When you want to inspect it, use something like following code instead of reading it directly.

File.read!("path/to/file")
|> :erlang.binary_to_term()
|> IO.inspect()

I think it is better to tell us your scenario. It’s an XY problem probably.

4 Likes

Basically we have a deployment that is in a very secure enclave. We don’t have remote shell access and the normal ways you would debug an Elixir application.

What we do have is the ability to view logs. What I had thought is having the ability to write to a log terms that need analyzing and re-load them for later analysis at work (well my desk in the living room). It would allow us to replay the terms locally.

However, if I’m wiling to lose the ability to make whatever is logged human-readable, I think I can do this:

b64term =  term |> :erlang.term_to_binary() |> Base.encode64()
File.write("/tmp/foo.term", b64term) # this is really a log file

Then

term = File.read!("/tmp/foo.term") |> Base.decode64!() |> :erlang.binary_to_term()
1 Like

Gotcha! Yeah so for this purpose I would definitely use term_to_binary/1 because this will be a far more reliable way of getting the exact values loaded into a shell later. You can always make it human readable on your local machine by inspecting the value you loaded via binary_to_term/1

1 Like

I’d use ~tp.~n so there is unicode characters. and a newline between terms. also you don’t need to flatten the iostring prior to write.

Most Erlang config files are written like this and read back by consult. It is very nice and simple. Or you can use other serialization format like json, or toml but they don’t support all erlang types.

2 Likes