-
-
Notifications
You must be signed in to change notification settings - Fork 66
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Send output of 'print' statements in notionflux scripts to the invoking shell #134
Conversation
This function is used to resolve subtable-accesses starting at _G in a correctly typed manner.
In order to be able to use print from mod_notionflux, it is temporarily overwritten by a version which collects the output and returns it at the end.
Maybe it's even better to pass stderr, leaving stdout for replies which are formatted as string.format('%q') and can therefore be piped back into lua... the question is, I guess, what kind of tool we want notionflux to be. For me it's an interactive debugging tool first and foremost. |
For me it's both an interactive exploration/debugging tool (and a much better one since your recent changes!), but also a building block for scripts and utilities to call into Notion from 'outside' as needed. |
Do you have any suggestion what we should do about print, then? :) |
This allows to print to notionflux's stdout from notion in realtime and avoids the loss of stdout in case of an error. Example: notionflux -e 'for k=1,100000 do print(k) end'
* Fix warnings reported by mandoc -Tlint * The verbatim text blocks were rendered as faulty HTML by mandoc so I try not to be sophisticated anymore (I assume it can result in faulty rendering elsewhere, as well)
The new toy has a pretty awful API... |
I think I might have screwed up with extl_unref_fn'ing the tostringstr function, because I moved the lookup for the function around. |
Another thing I noticed: |
mod_notionflux.xwrite(idx, table.concat(out)) | ||
end | ||
local oldprint=_G.print | ||
_G.print=newprint |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This does not seem very elegant: this means now suddenly all print
calls will be redirected to this new notionflux connection, also print
calls that don't have anything to do with this notionflux invocation (either from notion itself or from another notionflux invocation).
I suspect the pcall
below will be executed on one thread, and that one thread will not be used for any other processing until pcall
returns. If that is indeed correct, we could use an approach like the one described at https://stackoverflow.com/questions/24356520/thread-locals-in-lua to store a 'print target' per thread, and only forward print calls from this thread to this notionflux instance. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It isn't very elegant, indeed.
Are there multiple threads involved? I thought notion is singlethreaded, and control doesn't pass back out of lua until the function returns, therefore it's a viable option.
A trustworthy source claims that changing _ENV or using setfenv while the code is already loaded here would mean that print() calls in function calls further down the chain are not replaced, and he suggested that I should replace the global print. It would also be possible to load the code snippet using lua_load() in C (or lua's load()), passing a modified environment.
Other than not being very elegant, I don't see a reason, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought notion is singlethreaded, and control doesn't pass back out of lua until the function returns, therefore it's a viable option
Aah, indeed everything is generally done from the mainloop, so indeed this should be safe.
It would also be possible to load the code snippet using lua_load() in C (or lua's load()), passing a modified environment. Other than not being very elegant, I don't see a reason, though.
I agree that's not really a relevant difference.
Also some minor fixes.
In the current version of the PR the 'print' output and the return value are both written to stdout, meaning you can't separate them anymore. I think it would indeed be neat to send the 'print' output to 'stderr' and the 'returned' value to stdout. |
That's what I was thinking, too (it does however change normal expectations about I've just checked with Either way, this is easy enough to change. I'm still undecided. |
Turns out, otherwise libextl will refuse to call it under certain circumstances.
The SIGPIPE message was actually printed by gdb, notion ignores SIGPIPE so this is actually harmless. |
I think with some further testing, this should be ready to merge. @knixeur (or anyone else reading this) any opinion on whether notionflux's stdout or stderr is the right place to print messages to? |
I'd actually prefer stdout, that's what I would expect if I'm scripting something and calling a print method. |
I don't think we should be so strict about this for Notion 4.
I agree if there is a 'print' call in your script it would make sense for its output to go to stdout. However, what about print statements that are in the code of the loaded modules? I think it would be bad if adding such print statements to a module for diagnostics would break scripts that interact with notion through notionflux. One option would be to introduce a |
After some thought I'm favouring stdout.
Good point. I agree it's rather useless for the most part, unless it's a POD type (number, string, boolean or nil), otherwise it gets turned into
Thanks, I completely missed this, errors already go to stderr both in old as well as new notionflux, so they'd be mixed with the
I checked and notion's normal stdout is still reachable by calling
That's cumbersome to use, I think I'd prefer stderr before that and I think it makes sense for a The reason for introducing Now that I think about it, It might actually be counterproductive that |
Right, if we document that you should never use
Sounds reasonable to me (for warnings/errors but not for info/debug) - but let's track that in a separate issue/PR ;) |
Where should this documentation go? |
I'd say for now add them to http://notion.sourceforge.net/notionnotes/node7.html (https://github.com/raboof/notion-doc/blob/master/cstyle.tex), though eventually 'logging' deserves a section of its own (along with notionflux) |
Printing from C (printf/puts/...) is unaffected by my patch, I'm only changing lua's global print function temporarily while lua code is running in mod_notionflux, I don't think the C coding style section would be a good fit? |
You're right - let's perhaps rename it 'coding style' so we can record conventions for both Lua and C together? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good to go! Updating the docs can be done in a separate PR
This patchset implements a print() function for use in mod_notionflux.
This can theoretically be merged, but the question is whether instead of collecting print() output and displaying it at the very end (and losing it in case of an error), passing a file descriptor (e.g. stdout) from notionflux to mod_notionflux over the unix socket is a better solution.