开发者

erlang io:format, and a hanging web application

开发者 https://www.devze.com 2022-12-24 14:15 出处:网络
W开发者_运维知识库hile I\'m learning a new language, I\'ll typically put lots of silly println\'s to see what values are where at specific times.It usually suffices because the languages typically hav

W开发者_运维知识库hile I'm learning a new language, I'll typically put lots of silly println's to see what values are where at specific times. It usually suffices because the languages typically have available a tostring equivalent. In trying that same approach with erlang, my webapp just "hangs" when there's a value attempted to be printed that's not a list. This happens when variable being printed is a tuple instead of a list. There's no error, exception, nothing... just doesn't respond. Now, I'm muddling through by being careful about what I'm writing out and as I learn more, things are getting better. But I wonder, is there a way to more reliably to [blindly] print a value to stdout?

Thanks,

--tim


In Erlang, as in other languages, you can print your variables, no matter if they are a list, a tuple or anything else.

My feeling is that, for printing, you're doing something like (just a guess):

io:format("The value is: ~p.", A).

This is wrong, because you're supposed to pass a list of arguments:

io:format("The value is: ~p.", [A]).

Where A can be anything.

I usually find comfortable to use:

erlang:display/1

to print variables.

Also, tracing functions is usually a better way to debug an application, rather than using printouts. Please see:

http://aloiroberto.wordpress.com/2009/02/23/tracing-erlang-functions/


When developing webapps I use the error_logger module I usually define some macros like this

-ifdef(debug).
-define(idbg(FmtStr, Err), 
        error_logger:info_msg("~p (line ~p): " FmtStr "~n", 
                              [?MODULE, ?LINE | Err])).
-define(rdbg(Term), error_logger:info_report(Term)).
-else.
-define(idbg(_FmtStr, _Err), void).
-define(rdbg(_Term), void).
-endif.

You call the macros with something like:

code...
?rdbg(ErlangTerm),
other code...

During development you compile your modules with:

erlc -Ddebug *.erl

and so you get info messages in your erlang console.


Also make sure that there is no terminating process without link which could then cause other process to wait on something and not timeout-ing - hence strange hanging part.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号