Skip to content

Instantly share code, notes, and snippets.

@ckampfe
Last active September 4, 2025 04:17
Show Gist options
  • Select an option

  • Save ckampfe/3163210fbfd3f9a6bef3ae2aceef3c74 to your computer and use it in GitHub Desktop.

Select an option

Save ckampfe/3163210fbfd3f9a6bef3ae2aceef3c74 to your computer and use it in GitHub Desktop.
defmodule PrintDecorator do
use Decorator.Define, print: 1
def print(trace_name, body, _context) do
quote do
IO.puts("trace_name: #{unquote(trace_name)}")
unquote(body)
end
end
end
defmodule Deftrace do
defmacro __using__(_opts) do
quote do
require Deftrace
import Deftrace
end
end
defmacro deftrace(head, do: body) do
{fun_name, _meta, args} = head
trace_name = "#{inspect(__CALLER__.module)}.#{fun_name}/#{Enum.count(args)}"
quote do
use PrintDecorator
@decorate print(unquote(trace_name))
def unquote(head) do
unquote(body)
end
end
end
end
defmodule M do
use Deftrace
deftrace bar(a, b, c) do
a + b + c
end
end
@ckampfe
Copy link
Author

ckampfe commented Sep 4, 2025

produces this:

iex(2)> M.bar(1,2,3)
trace_name: M.bar/3
6

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment