How to build a reference from logs?

Background

I have some references in logs and I need to transform them from text to real references so I can compare then with ets references.

Logs and Code

I have some logs that print some errors in the form of:
"Unexpected message: {#Ref<0.427740394.3330539535.243656>,badarg}",

Now, I have some references I got from using table_ref = :ets.whereis :foo. I need to convert the reference I got from the log into something I can compare against table_ref to find out if table_ref is the same as log_ref.

Question

How can I do this?

I suspect you are stuck with a string comparison. Convert table_ref to string and load the ref as a string from the logs and compare.

Already thought about that… didn’t work out as expected:

iex(12)> to_string(:ets.whereis IEx.Pry)
** (Protocol.UndefinedError) protocol String.Chars not implemented for #Reference<0.3188431698.1701707783.210630>
    (elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
    (elixir) lib/string/chars.ex:22: String.Chars.to_string/1

From what I understand, erlang has no way of converting references to strings, which is confusing because since they are written in the logs, there must be a way of doing so.

iex(1)> make_ref
#Reference<0.2681540578.631242754.45080>
iex(2)> ref("0.2681540578.631242754.45080")
#Reference<0.2681540578.631242754.45080>

The ref to string conversion is just inspect, and you can do a string to ref conversion via the iex helper ref/1

5 Likes

This ref function does’t seem to be available in the Kernel module. How do I use it in code ?

It’s an IEX helper, so let’s go see how it works! https://github.com/elixir-lang/elixir/blob/v1.8.2/lib/iex/lib/iex/helpers.ex#L1258

So you can call string = "0.2681540578.631242754.45080"; :erlang.list_to_ref('#Ref<#{string}>')

4 Likes