<div dir="ltr">The suggestion from <a href="https://github.com/rust-lang/rust/issues/24821">https://github.com/rust-lang/rust/issues/24821</a> is to use writeln! instead of println! so you can handle the WOULDBLOCK situation yourself instead of panicing. But, shooting all the messages over to the Erlang side seems to solve your problem.</div><br><div class="gmail_quote"><div dir="ltr">On Sun, Sep 17, 2017 at 2:28 PM Max Lapshin <<a href="mailto:max.lapshin@gmail.com">max.lapshin@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div>Hi.</div><div><br></div><div>I'm playing now with rust nif and can advice nice library: <a href="https://github.com/hansihe/rustler" target="_blank">https://github.com/hansihe/rustler</a></div><div><br></div><div>For those of us, who do not understand what is there about elixir: don't worry, no need to use it, there should be a simple makefile for compiling rust.</div><div><br></div><div><br></div><div>I've met stange situation while using println! mechanism in rust nif (I'm speaking about enterprise real world debugging: as lot of prints as possible). Nif crashes from println.</div><div><br></div><div>if you are lucky to catch debug message, you will see something like:</div><div><br></div><div>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1">stack backtrace:</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>1: std::sys_common::backtrace::_print</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>2: std::panicking::default_hook::{{closure}}</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>3: std::panicking::default_hook</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>4: std::panicking::rust_panic_with_hook</span></p>
<p class="m_-580142622375574122gmail-p1"><span class="m_-580142622375574122gmail-s1"><span class="m_-580142622375574122gmail-Apple-converted-space"> </span>5: std::panicking::begin_panic</span></p>
<p class="m_-580142622375574122gmail-p1"><br></p><p class="m_-580142622375574122gmail-p1">or even info about error code 35.</p><p class="m_-580142622375574122gmail-p1"><br></p><p class="m_-580142622375574122gmail-p1">Problem is that stdout is in non-blocking mode and when rust tries to write output, it gets error that it is not waiting for.</p><p class="m_-580142622375574122gmail-p1">I've decided not to change anything in rust stdlib, but use another mechanism:</p><p class="m_-580142622375574122gmail-p1">1) take pid of output writing process</p><p class="m_-580142622375574122gmail-p1">2) send messages to it from rust.</p><p class="m_-580142622375574122gmail-p1"><br></p><p class="m_-580142622375574122gmail-p1">Here is the way to get whom to send IO request messages:</p><p class="m_-580142622375574122gmail-p1">logger() -></p><p class="m_-580142622375574122gmail-p1"> UserDrv = whereis(user_drv),</p><p class="m_-580142622375574122gmail-p1"> CurGroup = case (is_pid(UserDrv) andalso process_info(UserDrv, dictionary)) of</p><p class="m_-580142622375574122gmail-p1"> {dictionary, UserDrvDict} -></p><p class="m_-580142622375574122gmail-p1"> proplists:get_value(current_group, UserDrvDict);</p><p class="m_-580142622375574122gmail-p1"> _ -></p><p class="m_-580142622375574122gmail-p1"> undefined</p><p class="m_-580142622375574122gmail-p1"> end,</p><p class="m_-580142622375574122gmail-p1"> case is_pid(CurGroup) of</p><p class="m_-580142622375574122gmail-p1"> true -></p><p class="m_-580142622375574122gmail-p1"> CurGroup;</p><p class="m_-580142622375574122gmail-p1"> false -></p><p class="m_-580142622375574122gmail-p1"> {_,GL} = process_info(self(),group_leader),</p><p class="m_-580142622375574122gmail-p1"> GL</p><p class="m_-580142622375574122gmail-p1"> end.</p></div><div><br></div><div><br></div><div>and here is how do I log there from rust:</div><div><br></div><div><br></div><div><div> if let Some(ref logger) = mpegts.logger {</div><div> env.pid();</div><div> env.send(logger, (</div><div> atoms::io_request().encode(env),</div><div> env.pid().encode(env),</div><div> atoms::ok().encode(env),</div><div> (</div><div> atoms::put_chars().encode(env), </div><div> atoms::unicode().encode(env), </div><div> (string+"\n").encode(env)</div><div> ).encode(env)</div><div> ).encode(env))</div><div> }</div></div><div><br></div><div><br></div><div>One simple hack here: I do not send Mref as 3'rd argument, but I send atom ok. Seems to be working.</div><div><br></div><div><br></div><div><br></div></div>
_______________________________________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" rel="noreferrer" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
</blockquote></div>