randombits
randombits

Reputation: 48460

Start erlang with user defined variable (using rebar)?

I'd like to be able to use the following macro in my modules:

-ifdef(debug).

My startup script looks something like the following:

#!/bin/sh
PWD="$(pwd)"
#NAME="$(basename $PWD)"
erl -pa "$PWD/ebin" deps/*/ebin -boot start_sasl \
    -name [email protected] \
    -debug 1 \
    -s $NAME \
    +K true \
    +P 65536 

What else would need to be added so that debug is defined in my module? I need this to be dynamic so I don't have to modify source code for deployment into production. Using different startup scripts per dev/qa/prod environments is fine, but modifying source code shouldn't be necessary.

With erlc this can be done with -Ddebug. I use rebar however, and am not sure how to do it with that. I've tried adding the following to my rebar.config:

{erl_opts, [{D, "debug"}]}.

This gives the following error:

{error,
 {1,
  erl_parse,
  "bad term"}}

Upvotes: 1

Views: 2786

Answers (2)

f3r3nc
f3r3nc

Reputation: 620

The define for the compiler in rebar.config should look like this:

{erl_opts, [{d, debug}]}.

Note: the syntax is exactly the same as the compiler module's syntax: http://www.erlang.org/doc/man/compile.html

Current version of rebar (rebar version: 2 date: 20111205_155958 vcs: git 54259c5) does support compiler defines, as well.

rebar -D <defines> compile

See rebar --help for more rebar options.

Upvotes: 4

butter71
butter71

Reputation: 2723

ifdef is a preprocessor macro, it gets evaluated and removed at compile time -- you would have to re-compile your module with something like erlc -Ddebug module.erl to change it. add the "-P" flag if you want to see the output from the preprocessor in module.P.

to get access to the "-debug 1" argument at runtime, you can use init:get_argument(debug).

# erl -debug 1
...
1> init:get_argument(debug).
{ok,[["1"]]}
2> init:get_argument(foo).  
error

Upvotes: 1

Related Questions