[erlang-questions] variable unsafe just in macros

Lasaro lasaro@REDACTED
Sat Aug 9 23:11:57 CEST 2008


Hi there,

I tried to define a debug macro that would work for both single
parameters and multiple as follows.

-define(debug,true).

-ifdef(debug).

-define(DEBUG(Msg),
case Msg of
	[] ->
	    io:format("{~p@~p:~p}", [?MODULE,?LINE,self()]);
	[[_|_]=Format, [_|_]=Param] ->
	    io:format(lists:append(["{~p@~p:~p} ",Format]), [?MODULE,?
LINE,self()|Param]);
	[_|_] ->
	    io:format("{~p@~p:~p} "++Msg, [?MODULE,?LINE,self()]);
	_ ->
	    io:format("{~p@~p:~p} wrong debug call", [?MODULE,?LINE,self()])
    end.
).
-else.
-define(DEBUG(_M), true).
-endif.



-define(debug,true).

-ifdef(debug).
deb(Msg)->
    case Msg of
	[] ->
	    io:format("{~p@~p:~p}", [?MODULE,?LINE,self()]);
	[[_|_]=Format, [_|_]=Param] ->
	    io:format(lists:append(["{~p@~p:~p} ",Format]), [?MODULE,?
LINE,self()|Param]);
	[_|_] ->
	    io:format("{~p@~p:~p} "++Msg, [?MODULE,?LINE,self()]);
	_ ->
	    io:format("{~p@~p:~p} wrong debug call", [?MODULE,?LINE,self()])
    end.

-define(DEBUG(Msg), deb(Msg)).
-else.
-define(DEBUG(_M), true).
-endif.



More information about the erlang-questions mailing list