如何进行“检查档案” (还是弦)在长生不老药中?

发布于 2025-01-24 23:14:33 字数 945 浏览 0 评论 0原文

在Elixir中,我们可以io.impect Anystructure获取Anystructure的内部标准打印为输出。是否有类似的方法将其输出到文件(或作为更灵活的解决方案,将其输出到字符串)?

我已经在调试://elixir-lang.org/getting-started/io-and-the-file-system.html“ rel =“ nofollow noreferrer”> io ,但看不到解决方案。我也尝试过

{:ok, file} = File.open("test.log", [:append, {:delayed_write, 100, 20}])
structure = %{ a: 1, b: 2 }
IO.binwrite(file, structure)
File.close file

,但是结果

no function clause matching in IO.binwrite/2 [...]
def binwrite(device, iodata) when is_list(iodata) or is_binary(iodata)

我还谷歌搜索了一些“ elixir序列化”和“ elixir objoct to String”,但还没有找到任何有用的东西(例如:erlang.term_ter_to_binary返回,好吧,二进制)。是否有一种简单的方法可以获得相同的结果,即io.inspect在文件或字符串中打印?

In Elixir, we can IO.inspect anyStructure to get anyStructure's internals printed to output. Is there a similar method to output it to a file (or, as a more flexible solution, to a string)?

I've looked through some articles on debugging and io but don't see a solution. I've also tried

{:ok, file} = File.open("test.log", [:append, {:delayed_write, 100, 20}])
structure = %{ a: 1, b: 2 }
IO.binwrite(file, structure)
File.close file

but that results in

no function clause matching in IO.binwrite/2 [...]
def binwrite(device, iodata) when is_list(iodata) or is_binary(iodata)

I’ve also googled some "elixir serialize" and "elixir object to string", but haven't found anything useful (like :erlang.term_to_binary which returns, well, binary). Is there a simple way to get the same result that IO.inspect prints, into a file or a string?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

薄荷港 2025-01-31 23:14:33

已经有 Inspect/2function(不是与io.Inspect)相同,只需使用它:

#> inspect({1,2,3})
"{1, 2, 3}"

#> h inspect/2
                         def inspect(term, opts \\ [])

  @spec inspect(
          Inspect.t(),
          keyword()
        ) :: String.t()

Inspects the given argument according to the Inspect protocol. The second
argument is a keyword list with options to control inspection.


您可以随后使用字符串做任何想做的事情。

There is already inspect/2 function (not the same as IO.inspect), just go with it:

#> inspect({1,2,3})
"{1, 2, 3}"

#> h inspect/2
                         def inspect(term, opts \\ [])

  @spec inspect(
          Inspect.t(),
          keyword()
        ) :: String.t()

Inspects the given argument according to the Inspect protocol. The second
argument is a keyword list with options to control inspection.


You can do whatever you wish with the string afterwards.

×眷恋的温暖 2025-01-31 23:14:33

您可以给出io.Inspect一个附加参数,可以告诉它在哪里写入:

{:ok, pid} = StringIO.open("")
IO.inspect(pid, %{test: "data"}, label: "IO.inspect options work too \o/")
{:ok, {_in, out}} = StringIO.close(pid)

out # "IO.inspect options work too o/: %{test: \"data\"}\n"

它接受写入的过程的pidStringio提供了这样一个过程,在关闭时返回字符串。

You can give IO.inspect an additional param to tell it where to write to:

{:ok, pid} = StringIO.open("")
IO.inspect(pid, %{test: "data"}, label: "IO.inspect options work too \o/")
{:ok, {_in, out}} = StringIO.close(pid)

out # "IO.inspect options work too o/: %{test: \"data\"}\n"

It accepts a pid of a process to write to. StringIO provides such a process, returning you a string on close.

风吹雨成花 2025-01-31 23:14:33

在长生不老线中,我们可以io。请注意,任何结构都可以将任何结构的内部设置打印为输出。

这不是真的。 io.inspect使用 Inspect 协议。您看到的不是结构的内部,而是编写用于生成检查协议的结构实现。您可以提供不同的选项,以进行检查,以 becess> Inspect.opts ,其中之一是structs:false,它将以地图打印结构。

例如,检查范围结构:

iex> inspect(1..10)
"1..10"
iex> inspect(1..10, structs: false)
"%{__struct__: Range, first: 1, last: 10, step: 1}"

要回答您的问题并添加其他答案,这是一种使用 file.open!/3 重复使用打开文件并将多个检查调用记录到同一文件,然后关闭文件:

File.open!("test.log", [:write], fn file ->
  IO.inspect(file, %{ a: 1, b: 2 }, [])
  IO.inspect(file, "logging a string", [])
  IO.inspect(file, DateTime.utc_now!(), [])
  IO.inspect(file, DateTime.utc_now!(), structs: false)
end)

这将产生以下test.log文件:

%{a: 1, b: 2}
"logging a string"
~U[2022-04-29 09:51:46.467338Z]
%{
  __struct__: DateTime,
  calendar: Calendar.ISO,
  day: 29,
  hour: 9,
  microsecond: {485474, 6},
  minute: 51,
  month: 4,
  second: 46,
  std_offset: 0,
  time_zone: "Etc/UTC",
  utc_offset: 0,
  year: 2022,
  zone_abbr: "UTC"
}

In Elixir, we can IO.inspect anyStructure to get anyStructure's internals printed to output.

This is not quite true; IO.inspect uses the Inspect protocol. What you see is not the internals of the struct, but whatever that struct's implementation of the Inspect protocol is written to produce. There are different options you can give to inspect, defined in Inspect.Opts, one of them is structs: false, which will print structs as maps.

For example, inspecting a range struct:

iex> inspect(1..10)
"1..10"
iex> inspect(1..10, structs: false)
"%{__struct__: Range, first: 1, last: 10, step: 1}"

To answer your question and to add to the other answers, here is a method that uses File.open!/3 to reuse an open file and log multiple inspect calls to the same file, then close the file:

File.open!("test.log", [:write], fn file ->
  IO.inspect(file, %{ a: 1, b: 2 }, [])
  IO.inspect(file, "logging a string", [])
  IO.inspect(file, DateTime.utc_now!(), [])
  IO.inspect(file, DateTime.utc_now!(), structs: false)
end)

This produces the following test.log file:

%{a: 1, b: 2}
"logging a string"
~U[2022-04-29 09:51:46.467338Z]
%{
  __struct__: DateTime,
  calendar: Calendar.ISO,
  day: 29,
  hour: 9,
  microsecond: {485474, 6},
  minute: 51,
  month: 4,
  second: 46,
  std_offset: 0,
  time_zone: "Etc/UTC",
  utc_offset: 0,
  year: 2022,
  zone_abbr: "UTC"
}
淡淡绿茶香 2025-01-31 23:14:33

您只需要组合返回binary 或任何其他函数转储到文件。

File.write("test.log", inspect(%{a: 1, b: 2}, limit: :infinity))

请注意 limit :: infinity 在检查到stdout时,长结构将被截断,以获得更好的可读性。

You simply need to combine inspect/2 which returns a binary and File.write/3 or any other function dumping to a file.

File.write("test.log", inspect(%{a: 1, b: 2}, limit: :infinity))

Note the limit: :infinity option, without it the long structures will be truncated for better readability when inspecting to stdout.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文