Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add the code

Signed-off-by: Christopher Hall <hsw@openmoko.com>
  • Loading branch information...
commit 20a1df535c4abaae1c74d2c2a853cac3e8369a99 1 parent 50b635a
Christopher Hall authored
3  .gitignore
View
@@ -1,5 +1,6 @@
.eunit
deps
+ebin
*.o
*.beam
-*.plt
+*.plt
49 README.md
View
@@ -1,4 +1,47 @@
-funcarity.erl
-=============
+# Missing ?FUNCTION and ?ARITY macros
-Missing ?FUNCTION and ?ARITY macros
+## Specifications
+
+This library implements a parse transform to get the function anme and arity
+to make logging output more readable.
+
+This code was written by *Zoltan Lajos Kis* and posted to the Erlang mailing list
+and can be found at:
+
+`http://erlang.2086793.n4.nabble.com/Function-name-macro-the-counterpart-of-MODULE-td2099690.html`
+
+
+Copied here to make it easily accessible using rebar.config
+
+
+## How to add
+
+Just after the module/author directives add a compile directive to call the parse_transform
+function:
+
+`-compile({parse_transform, funcarity}).`
+
+Add an include directive to for the macro definitions:
+
+`-include("funcarity.hrl").`
+
+Ensure the rebar.config has the appropriate additions:
+
+* in erl_opts add: `{i, "apps/funcarity/include"}`
+* in deps add: `{funcarity, ".*", {git, "git://github.com/hxw/funcarity.erl.git", "master"}}`
+
+
+## Use
+
+Just use `?FUNCTION` and `?ARITY` in the same way as existing pre-defined macros like `?MODULE`.
+e.g.
+
+`io:format("~ts:~ts/~b ~p~n", [?MODULE, ?FUNCTION, ?ARITY, Message])`
+
+Or use with some logging system like *lager*
+
+For some example macros use:
+
+`-include("funcarity_lager.hrl").`
+
+Which contains `?DEBUG(Term)`, `?INFO(Term)` and `?ERROR(Term)` macros.
14 include/funcarity.hrl
View
@@ -0,0 +1,14 @@
+%% funcarity.hrl
+%%
+%% From: http://erlang.2086793.n4.nabble.com/Function-name-macro-the-counterpart-of-MODULE-td2099690.html
+%% Author: Zoltan Lajos Kis
+%%
+%% supply the missing ?FUNCTION and ?ARITY macros
+
+-ifndef(_HXW_FUNCARITY_HRL_).
+-define(_HXW_FUNCARITY_HRL_, true).
+
+-define(FUNCTION, '__function_macro__').
+-define(ARITY, '__function_arity__').
+
+-endif.
20 include/funcarity_lager.hrl
View
@@ -0,0 +1,20 @@
+%% funcarity_lager.hrl
+%%
+%% Author: Christopher Hall
+%%
+%% using ?FUNCTION and ?ARITY macros with lager
+
+-ifndef(_HXW_FUNCARITY_LAGER_HRL_).
+-define(_HXW_FUNCARITY_LAGER_HRL_, true).
+
+%% include MODULE, FUNCTION, ARITY macros
+
+-include("funcarity.hrl").
+
+%% logging macros
+
+-define(DEBUG(Term), lager:debug("~ts:~ts/~p ~p", [?MODULE, ?FUNCTION, ?ARITY, Term])).
+-define(ERROR(Term), lager:error("~ts:~ts/~p ~p", [?MODULE, ?FUNCTION, ?ARITY, Term])).
+-define(INFO(Term), lager:log(info, self(), "~ts:~ts/~p ~p", [?MODULE, ?FUNCTION, ?ARITY, Term])).
+
+-endif.
13 src/funcarity.app.src
View
@@ -0,0 +1,13 @@
+%% -*- mode: erlang -*-
+
+{application, funcarity,
+ [
+ {description, ""},
+ {vsn, "1"},
+ {registered, []},
+ {applications, [kernel,
+ stdlib,
+ compiler]},
+ {mod, {teamail_app, []}},
+ {env, []}
+ ]}.
50 src/funcarity.erl
View
@@ -0,0 +1,50 @@
+%% funcarity.hrl
+%%
+%% From: http://erlang.2086793.n4.nabble.com/Function-name-macro-the-counterpart-of-MODULE-td2099690.html
+%% Author: Zoltan Lajos Kis
+%%
+%% supply the missing ?FUNCTION and ?ARITY macros
+
+-module(funcarity).
+-author("Zoltan Lajos Kis").
+
+%% exports
+
+-export([parse_transform/2]).
+
+%% includes
+
+-include("funcarity.hrl").
+
+%% code
+
+-spec parse_transform([any()],_) -> [any()].
+parse_transform(AST, _Options) ->
+ [parse(T) || T <- AST].
+
+
+-spec parse(_) -> any().
+parse({function, _, FName, FArity, _} = T) ->
+ erl_syntax_lib:map(
+ fun(TE) ->
+ parsemacro(FName, FArity, TE)
+ end, T);
+parse(T) ->
+ T.
+
+
+-spec parsemacro(_,_,{'eof',_} | {'error',_} | {'nil',_} | {'warning',_} | {atom(),_,_} | {atom(),_,_,_} | {'bin_element' | 'clause' | 'function' | 'op' | 'receive' | 'record' | 'record_field' | 'rule',_,_,_,_} | {'try',_,_,_,_,_}) -> tuple().
+parsemacro(FName, FArity, T) ->
+ erl_syntax:revert(
+ case erl_syntax:type(T) of
+ atom ->
+ case erl_syntax:atom_value(T) of
+ ?FUNCTION ->
+ erl_syntax:atom(FName);
+ ?ARITY ->
+ erl_syntax:integer(FArity);
+ _ -> T
+ end;
+ _ ->
+ T
+ end).
Please sign in to comment.
Something went wrong with that request. Please try again.