<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">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="gmail-p1"><span class="gmail-s1">stack backtrace:</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>0: std::sys::imp::backtrace::tracing::imp::unwind_backtrace</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>1: std::sys_common::backtrace::_print</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>2: std::panicking::default_hook::{{closure}}</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>3: std::panicking::default_hook</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>4: std::panicking::rust_panic_with_hook</span></p>
<p class="gmail-p1"><span class="gmail-s1"><span class="gmail-Apple-converted-space"> </span>5: std::panicking::begin_panic</span></p>
<p class="gmail-p1"><br></p><p class="gmail-p1">or even info about error code 35.</p><p class="gmail-p1"><br></p><p class="gmail-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="gmail-p1">I've decided not to change anything in rust stdlib, but use another mechanism:</p><p class="gmail-p1">1) take pid of output writing process</p><p class="gmail-p1">2) send messages to it from rust.</p><p class="gmail-p1"><br></p><p class="gmail-p1">Here is the way to get whom to send IO request messages:</p><p class="gmail-p1">logger() -></p><p class="gmail-p1"> UserDrv = whereis(user_drv),</p><p class="gmail-p1"> CurGroup = case (is_pid(UserDrv) andalso process_info(UserDrv, dictionary)) of</p><p class="gmail-p1"> {dictionary, UserDrvDict} -></p><p class="gmail-p1"> proplists:get_value(current_group, UserDrvDict);</p><p class="gmail-p1"> _ -></p><p class="gmail-p1"> undefined</p><p class="gmail-p1"> end,</p><p class="gmail-p1"> case is_pid(CurGroup) of</p><p class="gmail-p1"> true -></p><p class="gmail-p1"> CurGroup;</p><p class="gmail-p1"> false -></p><p class="gmail-p1"> {_,GL} = process_info(self(),group_leader),</p><p class="gmail-p1"> GL</p><p class="gmail-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>